This is my first time using OpenMP and I feel I have a core misunderstanding in the implementation of the following:
#include <omp.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int i, n;
float a[100], b[100], result;
/* Some initializations */
n = 100;
result = 0.0;
for (i=0; i < n; i++) {
a[i] = i * 1.0;
b[i] = i * 2.0;
}
//pragma statement for omp here
for (int i=0; i < n; i++)
result = result + (a[i] * b[i]);
printf("Final result= %f\n",result);
}
The program is designed to calculate a dot product which involves a summation.
In this question the person answering suggests using reduction to implement the parallel summation in the for loop using #pragma omp parallel for reduction(+:results)
however when experimenting I get the same answer as if I just used #pragma omp parallel for
, which naively I assumed to be correct but left me with a feeling of unease as I could not find any documentation saying this isn't correct. An explanation of why I am probably wrong would be helpful.