i have following program in open mp (c).
it gives 0 or 3 fibonnaci number or crashes giving segmentation fault.
#include <stdlib.h> #include <stdio.h> #include <omp.h> static int fib(int); int main(){ int nthreads, tid; int n =8; #pragma omp parallel num_threads(4) private(tid) { #pragma omp single { tid = omp_get_thread_num(); printf("hello world (%d)\n", tid); printf("fib(%d) = %d %d\n", n, fib(n), tid); } } // threads join master thread , terminates } static int fib(int n){ int i, j, id; if(n < 2) return n; #pragma omp task shared (i) private (id) { = fib(n-1); } #pragma omp task shared (j) private (id) { j = fib(n-2); } return (i+j); }
what wrong program ?
the output like:
hello world (3)
fib(8) = 3 3
you need have taskwait in #pragma omp taskwait
right before returning (i+j)
. otherwise return before numbers computed, without waiting other tasks.
Comments
Post a Comment