SHARE
TWEET

Untitled

a guest Feb 24th, 2020 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <omp.h>
  3.  
  4. using namespace std;
  5.  
  6. unsigned __int64 fibRecursive(int n)
  7. {
  8.     return n < 3 ? 1 : fibRecursive(n-1) + fibRecursive(n-2);
  9. }
  10.  
  11. unsigned __int64 fibParallel(int n)
  12. {
  13.     if(n < 3)
  14.     {
  15.         return 1;
  16.     }
  17.     else
  18.     {
  19.         unsigned __int64 firstCall;
  20.         unsigned __int64 secondCall;
  21.        
  22. #pragma omp task shared(firstCall) firstprivate(n)
  23.         {
  24.             if(n <= 40)
  25.             {
  26.                 firstCall = fibRecursive(n - 1);
  27.             }
  28.             else
  29.             {
  30.                 firstCall = fibParallel(n - 1);
  31.             }
  32.         }
  33. #pragma omp task shared(secondCall) firstprivate(n)
  34.         {
  35.             if(n <= 40)
  36.             {
  37.                 secondCall = fibRecursive(n - 2);
  38.             }
  39.             else
  40.             {
  41.                 secondCall = fibParallel(n - 2);
  42.             }
  43.         }
  44. #pragma omp taskwait
  45.         return firstCall + secondCall;
  46.     }
  47. }
  48.  
  49. int main (int argc, char *argv[])
  50. {
  51.     omp_set_dynamic(0);
  52.     omp_set_num_threads(4);
  53.    
  54.     auto start = omp_get_wtime();
  55.    
  56. #pragma omp parallel
  57.     {
  58. #pragma omp single
  59.         {
  60.             cout << fibParallel(50) << endl;
  61.         }
  62.     }
  63.    
  64.     cout << omp_get_wtime() - start << endl;
  65.    
  66.     return 0;
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top