I have the following program in OPen MP (C).
It sometimes gives 0 or 3 as the 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 from (%d)\n", tid);
printf("Fib(%d) = %d by %d\n", n, fib(n), tid);
}
} // all threads join master thread and terminates
}
static int fib(int n){
int i, j, id;
if(n < 2)
return n;
#pragma omp task shared (i) private (id)
{
i = fib(n-1);
}
#pragma omp task shared (j) private (id)
{
j = fib(n-2);
}
return (i+j);
}
What is wrong with the program ?
The output is like:
Hello world from (3)
Fib(8) = 3 by 3