Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 9th, 2012  |  syntax: C  |  size: 1.12 KB  |  views: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }