I started to learn OpenMP and I can not deal with this code. It gives different results each time it runs.
#include <stdio.h>
#include <omp.h>
int main() {
int numsmp = 10;
double d = 0.0;
double d1 = 0.0;
float trace[10];
#pragma omp parallel for num_threads(2) reduction(+ : d, d1)
for (int i = 0; i < numsmp; i++) {
for (long int k = 0; k < 2; k++) {
printf("\n");
d++;
printf("i = %d k = %d d = %lf", i, k, d);
}
d1 += d;
trace[i] = d;
}
for (int i = 0; i < 10; i++) {
printf("\n%lf", trace[i]);
}
printf("d1=%f\n", d1);
}
I will note that on my machine it seems to give consistent (but obviously wrong) results (60 rather than 110) - but this can be different of different machines, it can also depend on the current system load.
Your issue is that you are using one of the reduction variables d, to calculate the other. An OpenMP reduction will create a local variable for each thread (in this case each thread will have a local d and d1) and sum them together in the end.
In your case, if you run this without sequentially, you will be summing the following values of d to d1: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, but if you run it with 2 threads (who, let's assume, share the load evenly) each of them will sum the following values of their local d to their local d1: 2, 4, 6, 8, 10. After that, the code will sum the local d1 of each thread to give you the final result.
To check our reasoning, we can try the sums ourselves, the single-threaded sum should give us 110 and the code does too. Using 2 threads should then (assuming even load distribution) should give us 2x30=60, which again, it does.
I'm going to assume this is simply a very nice minimal example, so I can not suggest how you should go about solving whatever it is you wish to do. But in this case, you could simply calculate d from i. If such a thing is not possible (in some other case), you can use critical regions, but those are not always a good solution (depends on the problem).
Further reading on reductions can (for instance) be found here: http://pages.tacc.utexas.edu/~eijkhout/pcse/html/omp-reduction.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With