pborawski

obliczanie pi z mpi bcast i redce

Jan 14th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <mpi.h>
  3. #include <time.h>
  4.  
  5. double calka(double x)
  6. {
  7.         return 4.0/ (1 + x * x );
  8. }
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.         int id, numproc;
  13.         int i,n, root = 0;
  14.         double pi,h,suma,x;
  15.        
  16.         MPI_Init(&argc, &argv);
  17.         MPI_Comm_rank(MPI_COMM_WORLD, &id);
  18.         MPI_Comm_size(MPI_COMM_WORLD, &numproc);
  19.        
  20.         if(id == root)
  21.         {
  22.             printf("Okresl liczba przedzialow: ");
  23.         }
  24.         scanf("%d", &n);
  25.        
  26.         MPI_Bcast( &n, 1, MPI_INT, root, MPI_COMM_WORLD);
  27.         h = 1.0/(double)n;
  28.        
  29.         suma = 0.0;
  30.         for ( i = id; i < n; i+=numproc)
  31.         {
  32.             x = h * ((double)i + 0.5);
  33.             suma += 4.0/(1 + x * x);
  34.         }
  35.        
  36.         MPI_Reduce( &suma, &pi, 1, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);
  37.        
  38.         if ( id == root )
  39.         {
  40.             pi = h * pi;
  41.             printf("pi = %.20f\n", pi);
  42.         }
  43.        
  44.         MPI_Finalize();
  45.         return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment