Advertisement
pexea12

Fibonacci without memoization

Feb 4th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <omp.h>
  4.  
  5. #define N 47
  6. #define NUM_THREADS 4
  7.  
  8.  
  9. int fib(int k) {
  10.   if (k <= 1) return k;
  11.   return fib(k - 1) + fib(k - 2);
  12. }
  13.  
  14. int main()
  15. {
  16.   long a[N];
  17.   int i;
  18.   clock_t cpu_start, cpu_end;
  19.   double start, end;
  20.  
  21.   omp_set_num_threads(NUM_THREADS);
  22.  
  23.   cpu_start = clock();
  24.   start = omp_get_wtime();
  25.  
  26.   #pragma omp parallel for schedule(dynamic)
  27.   for (i = 0; i < N; ++i) {
  28.     a[i] = fib(i);
  29.   }
  30.  
  31.   long long sum = 0;
  32.   #pragma omp parallel for reduction (+:sum)
  33.   for (i = 0; i < N; ++i) {
  34.     sum += a[i];
  35.   }
  36.  
  37.   cpu_end = clock();
  38.   end = omp_get_wtime();
  39.  
  40.   printf("Sum: %lld\n", sum);
  41.  
  42.   double cpu_time = (double) (cpu_end - cpu_start) / CLOCKS_PER_SEC;
  43.   double wall_time = end - start;
  44.   printf("CPU Time: %f\n", cpu_time);
  45.   printf("Wall-clock Time: %f\n", wall_time);
  46.  
  47.   return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement