The following code gives multiple outputs
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
As per my understanding only the for loop will be run parallel and only one output should come. Why I am getting same output many times.
#include <iostream>
#include <omp.h>
int main() {
int n = 100000;
long long sum = 0;
#pragma omp parallel for reduction(+: sum)
for (int i = 1; i <= n; ++i) {
sum += i;
}
std::cout << "Sum of first " << n << " natural numbers: " << sum << std::endl;
return 0;
}
mpiexec -n 1 a.out
will output a single line, but this is unlikely what you are trying to achieve.