我的 MPI C 程序有问题。这是代码:
void wantEat(int p, int rank, char *state, char* stateLeft, char* stateRight){
char *s;
MPI_Status status ;
/* if left or right neighbor is eating */
if(compare(stateLeft, "eat") || compare (stateRight, "eat")){
state = "want_Eat";
printf("%s : I wait for eating\n", nomPhilosophe(rank));
/* the process have to send his new state to his neighbors */
MPI_Send(state, strlen(state)+1, MPI_CHAR,
(rank - 1 + p) % p, 0, MPI_COMM_WORLD);
MPI_Send(state, strlen(state)+1, MPI_CHAR,
(rank + 1) % p, 0, MPI_COMM_WORLD);
/* if only left neighbor is eating */
if(compare(stateLeft,"eat") && !compare(stateRight,"eat")){
/* Wait for left neighbor finishes eating */
MPI_Recv(stateLeft, 6, MPI_CHAR, (rank - 1 + p) % p, 0,
MPI_COMM_WORLD, &status);
/* and eat */
state = "eat";
}
/* if only right neighbor is eating */
if(compare(stateRight,"eat") && !compare(stateLeft,"eat")){
/* wait for right neighbor message */
MPI_Recv(stateRight, 6, MPI_CHAR, (rank + 1) % p, 0,
MPI_COMM_WORLD, &status);
/* and eat */
state = "eat";
}
/* if both neighboors are eating */
if(compare(stateRight,"eat") && compare(stateLeft,"eat")){
/* wait for messages of the 2 neighbors */
MPI_Recv(stateLeft, strlen("think")+1, MPI_CHAR, MPI_ANY_SOURCE, 0,
MPI_COMM_WORLD, &status);
MPI_Recv(stateRight, strlen("think")+1, MPI_CHAR, MPI_ANY_SOURCE, 0,
MPI_COMM_WORLD, &status);
/* and eat */
state = "eat";
}
}
/* if neighbors are not eating */
else{
/* eat */
state= "eat";
}
/* send the new state to neighbors */
MPI_Send(state, strlen(state)+1, MPI_CHAR,
(rank - 1 + p) % p, 0, MPI_COMM_WORLD);
MPI_Send(state, strlen(state)+1, MPI_CHAR,
(rank + 1) % p, 0, MPI_COMM_WORLD);
}
int main(int argc, char* argv[]){
int my_rank; /* rank of process */
int p; /* number of processes */
char* state = "think"; /* state of process (think, eat, or want_eat) */
char* stateLeft = "think"; /* state of the left neighbor of the process */
char* etatD = "think"; /* state of the right neighbor of the process */
/* start up MPI */
MPI_Init(&argc, &argv);
/* find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* find out number of processes */
MPI_Comm_size(MPI_COMM_WORLD, &p);
think(my_rank); /* process "my_rank" is thinking */
wantEat(p, my_rank, state, stateLeft, stateRight); /* process "my_rank" wants to eat */
eat(my_rank); /* process "my_rank" is eating */
/* shut down MPI */
MPI_Finalize();
return 0;
}
问题是所有进程同时进食而不等待邻居。我认为邻居不会收到 MPI 发送的新状态。你有解决这个问题的想法吗?