I tried to write a small C program to figure out how does OpenMP works. This example is supposed to compute the sum of 1 to 1000; however, it printed out 0 in the terminal. I can only get the desired result after commenting the #pragma stuff out. Can someone possibly tell me the reason?
This guide says that #pragma omp for divides the work of the for-loop among the threads of the current team. It does not create threads, it only divides the work amongst the threads of the currently executing team. So we should only have one main thread throughout the execution, correct?
#include <stdio.h> int main() { int n, sum = 0; #pragma omp for for (n = 0; n <1000; n++) { sum += n; } printf("%d\n"); return 0; }