Facebook
From Aqua Mockingjay, 6 Years ago, written in Plain Text.
This paste is a reply to Untitled from Walloping Bison - view diff
Embed
Download Paste or View Raw
Hits: 434
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <omp.h>
  4.  
  5. long long num_steps = 10000000;
  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[50];
  13.         int i, j;
  14.         step = 1. / (double)num_steps;
  15.         omp_set_num_threads(4);
  16.         for (j = 0; j < 39; j++)                //do mierzenia czasu dostępu do pamięci i długości lini
  17.         {
  18.                 sum = 0;
  19.                 start = clock();
  20.         #pragma omp parallel
  21.                 { int id = omp_get_thread_num();
  22.                 tab[j+id] = 0;
  23. #pragma omp for schedule(dynamic, 10000)
  24.                 for (i = 0; i < num_steps; i++)
  25.                 {
  26.                         x = (i + .5)*step;
  27.                         tab[j+id] += 4.0 / (1. + x*x);
  28.                 }
  29.         #pragma atomic
  30.                 sum += tab[j+id];
  31.                 }
  32.  
  33.                 pi = sum*step;
  34.                 stop = clock();
  35.  
  36.                 printf("Wartosc liczby PI wynosi %15.12f\n", pi);
  37.                 printf("Czas przetwarzania iteraccji %d wynosi %f sekund\n", j, ((double)(stop - start) / 1000.0));
  38.         }
  39.         return 0;
  40. }