--sas

apr22-pi2.c

Apr 28th, 2022
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <omp.h>
  5.  
  6. #define CIRCLE(step, i) sqrt(4.0 - (double) (i) * (step) * (double) (i) * (step))
  7.  
  8. int main() {
  9.     omp_set_num_threads(omp_get_num_procs());
  10.  
  11.     int N;
  12.     FILE* datp = fopen("apr22-pi2.dat", "r");
  13.     if (datp == NULL) {
  14.         printf("Error. Couldn't open the data file.");
  15.         exit(1);
  16.     }
  17.     if (fscanf(datp, "%d", &N) != 1) {
  18.         printf("Error. Couldn't read the number from the data file.");
  19.         exit(2);
  20.     }
  21.     fclose(datp);
  22.  
  23.     int i;
  24.     double step, S;
  25.     step = 2.0 / (double) N;
  26.     S = 0.0;
  27.  
  28. #pragma omp parallel shared(N, step) private(i) reduction(+:S)
  29.     {
  30. #pragma omp for
  31.         for (i = 0; i < N; i++) {
  32.             S += (CIRCLE(step, i) + CIRCLE(step, i + 1)) * step / 2.0;
  33.         }
  34.     }
  35.  
  36.     printf("Pi = %f\n", S);
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment