'OpenMP reduction on multiple variables (array)

I am trying to do a reduction on multiple variables (an array) using OMP, but wasn't sure how to implement it with OMP. See the code below.

#pramga omp parallel for reduction( ??? )
for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
                [ compute value ... ]

                y[j] += value
        }
}

I thought I could do something like this, with the atomic keyword, but realised this would prevent two threads from updating y at the same time even if they are updating different values.

#pramga omp parallel for
for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
                [ compute value ... ]

                #pragma omp atomic
                y[j] += value
        }
}

Does OMP have any functionality for something like this or otherwise how would I achieve this optimally without OMP's reduction keyword?



Solution 1:[1]

There is an array reduction available in OpenMP since version 4.5:

#pramga omp parallel for reduction(+:y[:m])

where m is the size of the array. The only limitation here is that the local array used in reduction is always reserved on the stack, so it cannot be used in the case of large arrays.

The atomic operation you mentioned should work fine, but it may be less efficient than reduction. Of course, it depends on the actual circumstances (e.g. actual value of n and m, time to compute value, false sharing, etc.).

#pragma omp atomic
  y[j] += value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Laci