Guest User

Untitled

a guest
Apr 24th, 2012
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. Using of tasks in openmp for recursive calls
  2. #include "stdio.h"
  3.  
  4. int com_fib(int n)
  5. {
  6. int f,fn2,fn1;
  7. // f(n) = f(n-1) + f(n-2)
  8. if(n == 0||n == 1) return n;
  9. if ( n < 20 ) return(com_fib(n-1) + com_fib(n-2));
  10.  
  11. #pragma omp task shared(fn1)
  12. { fn1 = com_fib(n-1); }
  13.  
  14. #pragma omp task shared(fn2)
  15. { fn2 = com_fib(n-2); }
  16.  
  17. #pragma omp taskwait
  18. f = fn1 + fn1;
  19.  
  20. return f;
  21. }
  22.  
  23. void main(int argc, char *argv[])
  24. {
  25. int result;
  26. // printf("arg is %dn", atoi(argv[1]) );
  27. #pragma omp parallel
  28. {
  29. #pragma omp single nowait
  30. {
  31. result = com_fib(atoi(argv[1]));
  32. } // End of single
  33. } // End of parallel region
  34. printf("Fib is %dn", result);
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment