--sas

mar11-pi2.c

Mar 11th, 2022
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <mpi.h>
  4. #include "l17.h"
  5.  
  6. #define CIRCLE(step, i) sqrt(4.0 - (double) (i) * (step) * (double) (i) * (step))
  7.  
  8. int main(int argc, char** argv) {
  9.     int rc;
  10.     rc = MPI_Init(&argc, &argv);
  11.     L17_CHECK_ERROR(MPI_Init, rc);
  12.  
  13.     int size;
  14.     rc = MPI_Comm_size(MPI_COMM_WORLD, &size);
  15.     L17_CHECK_ERROR(MPI_Comm_size, rc);
  16.  
  17.     int rank;
  18.     rc = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  19.     L17_CHECK_ERROR(MPI_Comm_rank, rc);
  20.  
  21.     if (rank == 0) {
  22.         int N;
  23.         FILE* datp = fopen("mar11-pi2.dat", "r");
  24.         if (datp == NULL) {
  25.             printf("Error. Can't open the data file.\n");
  26.             MPI_Abort(MPI_COMM_WORLD, 1);
  27.         }
  28.         rc = fscanf(datp, "%d", &N);
  29.         if (rc != 1) {
  30.             printf("Error. Can't read the N from the data file.\n");
  31.             MPI_Abort(MPI_COMM_WORLD, 2);
  32.         }
  33.         fclose(datp);
  34.  
  35.         int i;
  36.         for (i = 1; i < size; ++i) {
  37.             rc = MPI_Send(&N, 1, MPI_INT, i, 1, MPI_COMM_WORLD);
  38.             L17_CHECK_ERROR(MPI_Send, rc);
  39.         }
  40.  
  41.         double step = 2.0 / (double) N;
  42.         double S_no_coef, FS_no_coef = 0;
  43.         int k = size - 1;
  44.         int j;
  45.         for (j = 0; j < k; ++j) {
  46.             rc = MPI_Recv(&S_no_coef, 1, MPI_DOUBLE, j + 1, 1, MPI_COMM_WORLD, NULL);
  47.             L17_CHECK_ERROR(MPI_Recv, rc);
  48.  
  49.             FS_no_coef += S_no_coef;
  50.         }
  51.  
  52.         double coef = step / 2.0;
  53.         double FS = FS_no_coef * coef;
  54.  
  55.         printf("Pi = %f\n", FS);
  56.     }
  57.     else {
  58.         int j = rank - 1, k = size - 1, ibeg, iend;
  59.  
  60.         int N;
  61.         rc = MPI_Recv(&N, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, NULL);
  62.         L17_CHECK_ERROR(MPI_Recv, rc);
  63.  
  64.         ibeg = (N / k) * j;
  65.  
  66.         iend = (j == k - 1) ? (N - 1) : ((N / k) * (j + 1) - 1);
  67.  
  68.         double step = 2.0 / (double) N;
  69.         double f_old = CIRCLE(step, ibeg);
  70.         double f_new;
  71.         double S_no_coef = 0;
  72.         int i;
  73.         for (i = ibeg; i <= iend; ++i) {
  74.             f_new = CIRCLE(step, i + 1);
  75.             S_no_coef += f_old;
  76.             S_no_coef += f_new;
  77.             f_old = f_new;
  78.         }
  79.  
  80.         rc = MPI_Send(&S_no_coef, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD);
  81.         L17_CHECK_ERROR(MPI_Send, rc);
  82.     }
  83.  
  84.     MPI_Finalize();
  85.  
  86.     return 0;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment