Guest User

Untitled

a guest
Dec 16th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <omp.h>
  4.  
  5. long long num_steps = 100000000;
  6. double step, table[40];
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10. clock_t start, stop;
  11. double x, pi, sum = 0.0;
  12. int i,j;
  13. omp_set_num_threads(2);
  14. step = 1. / (double)num_steps;
  15.  
  16. for (j = 0; j < num_steps; j++){
  17.  
  18. start = clock();
  19. double sum = 0;
  20. #pragma omp parallel
  21. {
  22. int id = omp_get_thread_num();
  23. table[j + id] = 0;
  24. #pragma omp for
  25. for (i = 0; i < num_steps; i++)
  26. {
  27. double x = (i + .5)*step;
  28. //unieważnienie całej linii danych
  29. table[j + id] += 4.0 / (1. + x*x);
  30. }
  31. #pragma omp atomic
  32. sum += table[j + id];
  33. }
  34.  
  35. pi = sum*step;
  36. stop = clock();
  37.  
  38. printf("Wartosc liczby PI wynosi %15.12f\n", pi);
  39. printf("Czas przetwarzania wynosi %f sekund\n", ((double)(stop - start) / 1000.0));
  40. }
  41. return 0;
  42. }
Add Comment
Please, Sign In to add comment