我遇到问题"MPI_Send(99):无效标签,值为-1",而我使用VS2013和MPIEXEC.exe运行以下代码.逻辑很简单,只是线程0将令牌发送给其他工作线程,然后工作线程将令牌再次重置为0.
#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv) {
int myid;
int numprocs;
int token;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
if (numprocs == 1) {
printf("The number of processes for this exercise must be greater than 1!\n");
}
else if (myid == 0) {
/* Master process */
int sdrID;
int activeWorkers;
/* set the number of activeWorkers */
//...
activeWorkers = 3;
/* while there are any active workers */
while (activeWorkers) {
/*receive a token from any worker*/
MPI_Recv(&token, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
/* obtain the sending process ID from status. */
//...;
//;
/* print the message "The Master has received a token from Worker ...". */
printf("%d",status.MPI_SOURCE);
/* reset token to zero and send it back to the same worker */
token = 0;
// //1.?????????????????
// //2.??????????
// //3.?????MPI?????????????????????????
// //4.????????
// //5.??????????????????????if??????
MPI_Send(&token, 1, MPI_INT, status.MPI_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD);
/* reduce the number of activeWorkers */
activeWorkers = activeWorkers - 1;
}
}
else {
/* Worker processes */
/* set token to 1 and send it to the Master.*/
token = 1;
MPI_Send(&token, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD);/////////////////
/* receive token from the Master. */
MPI_Recv(&token, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
/* print a message and return */
printf("Worker %i has received the token from the Master.\n", myid);
}
MPI_Finalize();
return 0;
}
Run Code Online (Sandbox Code Playgroud)