Advertisement
Sawy3R11

MPI_PI

May 15th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5.  # define length 100
  6.  
  7. int main(int argc, char** argv) {
  8.  
  9.     MPI_Init(&argc, &argv);
  10.  
  11.     int world_size;
  12.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  13.     int rank;
  14.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  15.  
  16.  
  17.     //MPI_Bcast(&x, 1, MPI_INT, 0, MPI_COMM_WORLD);
  18.     float x = 0.0f;
  19.     x = 1.0 / (3 + 4*rank);
  20.     float sumMinus = 0.0f;
  21.     MPI_Reduce(&x, &sumMinus, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);
  22.  
  23.     //MPI_Bcast(&x, 1, MPI_INT, 0, MPI_COMM_WORLD);
  24.     float y= 0.0f;
  25.     y = 1.0 / (5 + 4*rank);
  26.     float sumPlus = 0.0f;
  27.     MPI_Reduce(&y, &sumPlus, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);
  28.  
  29.  
  30.     if( rank==0 ){
  31.         printf( "sumMinus: %f, sumPlus: %f", sumMinus, sumPlus );
  32.         float wynik = 1 -sumMinus +  sumPlus;
  33.  
  34.         printf("\nPI/4  = %f", wynik);
  35.         printf("\nPI = %f", wynik*4 );
  36.     }
  37.  
  38.     MPI_Finalize();
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement