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