#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char **argv)
{
int rank;
double t1,t2;
int i, doit,doit2, iter;
double x;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
i = 1;
x = 0;
iter = 0;
doit = 0;
t1 = MPI_Wtime();
while(i>0)
{
// all processes are synced at the same iteration
MPI_Barrier(MPI_COMM_WORLD);
// process 0 checks time
if (rank == 0)
{
t2 = MPI_Wtime();
if (t2-t1 >= 2.)
{
doit = 1;
}
}
// broadcast the flag, this also sync processes
MPI_Bcast(&doit, 1, MPI_INT, 0, MPI_COMM_WORLD);
// all processes are synced so if doit==1
// we write the file
if (doit ==1)
{
printf("I am processor %d and x = %f\n", rank, x);
MPI_Barrier(MPI_COMM_WORLD);
break;
}
i++;
x+=0.0001;
};
printf("%d, %d ,%f\n", rank, i, x);
MPI_Finalize();
return 0;
}