I have array a
of size N with random numbers. Using OpenMP I want to increment elements 0 to 9 of array b
of size 10 for every number in A. The language is C.
#pragma omp parallel for
for(i = 0; i < N; i++)
b[a[i]]++;
Unfortunately there are apparently simultanous writes in some elements of b and the result is not as expected. I tried it with setting b to firstprivate and lastprivate but this didn't help either.
The task seems simple but I don't know how to do it as there is no atomic
for arrays in OpenMP. I could create a new array for the number of threads and then add them together in the end but that doesn't seem optimal.
Which would be the fastest way to count the occurences of the numbers in a
in the elements of array b
?