1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <math.h> 4 #include <omp.h> 5 #include <unistd.h> 6 #define MAX 33 //41 7 int Fibonacci(int n) 8 { int x, y; 9 if (n < 2) 10 return n; 11 else { 12 x = Fibonacci(n - 1); 13 y = Fibonacci(n - 2); 14 return (x + y); 15 } } 16 int FibonacciTask(int n) 17 { int x, y; 18 if (n < 2) 19 return n; 20 else { 21 #pragma omp task shared(x) 22 x = Fibonacci(n - 1); 23 #pragma omp task shared(y) 24 y = Fibonacci(n - 2); 25 #pragma omp taskwait 26 return (x + y); 27 } } 28 29 int main(int argc, char * argv[]) 30 {int FibNumber[MAX] = {0}; 31 struct timeval time_start, time_end; 32 int i = 0; 33 // openmp related print message 34 printf("CPU_ONLN= %d\n", sysconf(_SC_NPROCESSORS_ONLN)); 35 printf("Number of CPUs=%d\n", omp_get_num_procs()); 36 printf("Number of max threads=%d\n", omp_get_max_threads()); 37 printf("Number of executing thread=%d\n", omp_get_thread_num()); 38 printf("Number of threads=%d\n", omp_get_num_threads()); 39 omp_set_num_threads( omp_get_num_procs() ); 40 gettimeofday(&time_start, NULL); 41 #pragma omp parallel 42 { 43 #pragma omp single private(i) 44 for(i = 1; i < MAX; i++) { 45 FibNumber[i] = FibonacciTask(i); 46 } } 47 gettimeofday(&time_end, NULL); 48 time_end.tv_usec = time_end.tv_usec-time_start.tv_usec; 49 time_end.tv_sec = time_end.tv_sec-time_start.tv_sec; 50 time_end.tv_usec += (time_end.tv_sec*1000000); 51 printf("Execution time of The Fibonacci Numbers with OpenMP : %lf sec\n", time_end.tv_usec / 1000000.0); 52 for(i = 0; i < MAX; i++) 53 printf("%d ", FibNumber[i]); 54 printf("\n-------------------------------\n"); 55 return 0; 56 } 57