i have an issue with synchronizing Master/slaves processes using MPI.
I wish that the master control the order of the execution of the slaves. Eeach slave have to do : 1- read 2-process.
Here is my code :
int main(int argc, char* argv []){
int rank,numprocess;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &numprocess );
if(rank==0){ //master
MPI_Status s;int Sstate=1;int Rstate;
int p;
for(p=1;p<numprocess;p++){
fflush(stdout);printf("master : order P%d to start reading\n",p);
MPI_Send(&Sstate, sizeof(int), MPI_INT, p, 20, MPI_COMM_WORLD);
MPI_Recv(&Rstate,sizeof(int),MPI_INT,p,21,MPI_COMM_WORLD,&s);
fflush(stdout);printf("master : P%d finished reading\n",p);
}
}
else{ //workers
int state; MPI_Status s;
MPI_Recv(&state,sizeof(int),MPI_INT,0,20,MPI_COMM_WORLD,&s);
//read here
Sleep(1000);
//send to master : finish reading
state=2;
MPI_Send(&state, sizeof(int), MPI_INT, 0, 21, MPI_COMM_WORLD);
//processing
Sleep(3000);
fflush(stdout);printf("worker %d ended processing\n",rank);
}
MPI_Finalize();
return 0;
}
My probleme is with the last process. In fact it didn’t act like the others. here is my output :
mpiexec -n 4 master.exe
master : order P1 to start reading
master : p1 finished reading
master : order P2 to start reading
master : p2 finished reading
master : order P3 to start reading
worker 1 ended processing
worker 2 ended processing
master : p3 finished reading
worker 3 ended processing
Why the third process is not synchronized?
Thank you for precious help!
What you’ve shown is Normal for a multi-threaded or multi-process architecture. Basically, after your processes call
MPI_Sendto the master process, they are released to function on their own as they have no more blocking MPI calls. Thus after they sleep they print their end statement, independently from one another and the master itself!Below I have a diagram which I hope will illustrate what you’re seeing: