I have a function like this
void foo(x, *y) {
some calculations
if (x > *y) {
*y = x/2;
}
more calculations
}
My parallel section is
int x[] = some array;
double y=0;
#pragma omp parallel for
for ( int i = 0; i<=n; i++) {
do something
for ( int j = 0; j<=n; j++) {
foo(x[i], &y);
}
If I rewrite the if statement as
*y = x > *y ? x/2 : *y;
I can do
#pragma omp critical
*y = x > *y ? x/2 : *y;
But I don’t want to change the code. How do I create a critical section for the conditional y-update block?
#pragma omp atomic write
? This would avoid the critical section, which would probably need to include the full if block. $\endgroup$