multithreading - How does OpenMP works internally -
i tried write small c program figure out how openmp works. example supposed compute sum of 1 1000; however, printed out 0 in terminal. can desired result after commenting #pragma
stuff out. can possibly tell me reason?
this guide says #pragma omp for
divides work of for-loop among threads of current team. not create threads, divides work amongst threads of executing team. should have 1 main thread throughout execution, correct?
#include <stdio.h> int main() { int n, sum = 0; #pragma omp (n = 0; n <1000; n++) { sum += n; } printf("%d\n"); return 0; }
you have several issues such simple example...
1) not starting parallel region. this, use omp parallel for
instead of omp for
.
2) variables not being made private each thread working on different loops. each thread over-writing each other thread's version of variables. n
needs made private.
3) attempting sum 1 shared variable across multiple threads. must done reduction
clause.
4) you're not printing out. printf()
syntax as-is not print proper result ever.
so example should like:
int n, sum = 0; #pragma omp parallel private(n) reduction(+:sum) (n = 0; n < 1000; n++) { sum += n; } printf("%d\n", sum);
i'd suggest basic openmp tutorial (either online or in book). first 3 problems have been obvious little bit of research.
Comments
Post a Comment