我在使用 MPI 同步主/从进程时遇到问题。我希望主人控制奴隶的执行顺序。每个奴隶必须做:1-读取2-进程。这是我的代码:
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;
}
我的问题是最后一个过程。事实上,它并没有像其他人那样行事。这是我的输出:
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
为什么第三个进程不同步?感谢您的宝贵帮助!