Advertisement
Guest User

Untitled

a guest
Nov 9th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <mpi.h>
  5.  
  6.  
  7. int main(int argc, char **argv)
  8. {
  9.     int rank;
  10.     double t1,t2;
  11.     int i, doit,doit2, iter;
  12.     double x;
  13.  
  14.  
  15.     MPI_Init(&argc, &argv);
  16.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  17.  
  18.     i = 1;
  19.     x = 0;
  20.     iter = 0;
  21.     doit = 0;
  22.  
  23.     t1 = MPI_Wtime();
  24.  
  25.     while(i>0)
  26.     {
  27.         // all processes are synced at the same iteration
  28.         MPI_Barrier(MPI_COMM_WORLD);
  29.  
  30.         // process 0 checks time
  31.         if (rank == 0)
  32.         {
  33.             t2 = MPI_Wtime();
  34.             if (t2-t1 >= 2.)
  35.             {
  36.                 doit = 1;
  37.             }
  38.         }
  39.  
  40.         // broadcast the flag, this also sync processes
  41.         MPI_Bcast(&doit, 1, MPI_INT, 0, MPI_COMM_WORLD);
  42.  
  43.         // all processes are synced so if doit==1
  44.         // we write the file
  45.         if (doit ==1)
  46.         {
  47.         printf("I am processor %d and x = %f\n", rank, x);
  48.         MPI_Barrier(MPI_COMM_WORLD);
  49.         break;
  50.         }
  51.         i++;
  52.         x+=0.0001;
  53.     };
  54.  
  55.     printf("%d, %d ,%f\n", rank, i, x);
  56.  
  57.     MPI_Finalize();
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement