Facebook
From Walloping Bison, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 414
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <omp.h>
  4.  
  5. long long num_steps = 1000000000;
  6. double step;
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10.         clock_t start, stop;
  11.         volatile double x;
  12.         double pi, sum = 0.0, tab[40];
  13.         int i;
  14.         step = 1. / (double)num_steps;
  15.         start = clock();
  16. #pragma omp parallel
  17.         { int id = omp_get_thread_num();
  18.         tab[id] = 0;
  19. #pragma omp for reduction(+:sum)
  20.         for (i = 0; i < num_steps; i++)
  21.         {
  22.                 x = (i + .5)*step;
  23.                 tab[id] += 4.0 / (1. + x*x);
  24.         }
  25. #pragma atomic
  26.         sum += tab[id];
  27.         }
  28.  
  29.         pi = sum*step;
  30.         stop = clock();
  31.  
  32.         printf("Wartosc liczby PI wynosi %15.12fn", pi);
  33.         printf("Czas przetwarzania wynosi %f sekundn", ((double)(stop - start) / 1000.0));
  34.         return 0;
  35. }

Replies to Untitled rss

Title Name Language When
Re: Untitled Aqua Mockingjay text 6 Years ago.