From 23b7370e17811d1d26efc00233511c2a867d72fb Mon Sep 17 00:00:00 2001 From: Sergio Mauricio Vanegas Arias Date: Wed, 2 Jul 2025 16:36:53 +0300 Subject: [PATCH] In its current state, the operation `a[i] += b[i]` expands to `a[i] += a[i+1]`, which is fully parallelizable in incremental order of iteration. With this change, it instead expands to `a[i] += a[i-1]`, which requires sequential execution. --- content/english/hpc/compilation/contracts.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/english/hpc/compilation/contracts.md b/content/english/hpc/compilation/contracts.md index 56a50d6b..49a98cee 100644 --- a/content/english/hpc/compilation/contracts.md +++ b/content/english/hpc/compilation/contracts.md @@ -157,7 +157,7 @@ void add(int *a, int *b, int n) { Since each iteration of this loop is independent, it can be executed in parallel and [vectorized](/hpc/simd). But is it, technically? -There may be a problem if the arrays `a` and `b` intersect. Consider the case when `b == a + 1`, that is, if `b` is just a memory view of `a` starting from its second element. In this case, the next iteration depends on the previous one, and the only correct solution is to execute the loop sequentially. The compiler has to check for such possibilities even if the programmer knows they can't happen. +There may be a problem if the arrays `a` and `b` intersect. Consider the case when `a == b + sizeof(int)`, that is, if `a` is just a memory view of `b` starting from its second element. In this case, the next iteration depends on the previous one, and the only correct solution is to execute the loop sequentially. The compiler has to check for such possibilities even if the programmer knows they can't happen. This is why we have `const` and `restrict` keywords. The first one enforces that we won't modify memory with the pointer variable, and the second is a way to tell the compiler that the memory is guaranteed to not be aliased.