You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the Yargen tool and found a bug in the tool.
wrong code:
void test(int *acc, int *inc) {
#pragma omp simd
for (int i = 0; i < 16; i++)
*acc += *inc;
}
OK code:
void test(int *acc, int *inc) {
#pragma omp simd reduction(+:acc[0])
for (int i = 0; i < 16; i++)
*acc += *inc;
}
The absence of the "reduction" keyword leads to undefined behavior in this test case.
#pragma omp for simd reduction(+:local_sum) is applied to the loop. This tells the compiler to vectorize the loop and also to perform a reduction operation on local_sum across different iterations assigned to different threads.
The reduction is done with the addition operator +, which combines local_sum from all threads into the final result.
Activity
dbabokin commentedon Oct 22, 2024
Could you please elaborate why is this UB? I'm not an expert in OpenMP, so pointers to standard would help.
huangcj123456 commentedon Oct 22, 2024
SIMD Reduction:
#pragma omp for simd reduction(+:local_sum) is applied to the loop. This tells the compiler to vectorize the loop and also to perform a reduction operation on local_sum across different iterations assigned to different threads.
The reduction is done with the addition operator +, which combines local_sum from all threads into the final result.
[-]yaprgen support pragma omp simd[/-][+]yarpgen support pragma omp simd[/+]dbabokin commentedon Oct 22, 2024
But why is it UB without
reduction?