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