我一直在研究一个 MPI 项目,其中从站都将数据发送回主站。出于某种原因,如果我连续进行 2 次连续发送,主机只会收到数据。这很奇怪,我认为这会导致我遇到其他一些奇怪的问题。知道什么会导致这种情况吗?我认为第一次发送是发送某种垃圾数据或其他东西。但是,发送是完全相同的代码行。
编辑:下面的代码...
if (numProcs > 0)
MPI_Barrier( MPI_COMM_WORLD ) ; //only wait if there are other processes to wait for
if (rank != 0)
{
MPI_Send(handArray, 10, MPI_DOUBLE, 0, TAG_HAND, MPI_COMM_WORLD);
MPI_Send(handArray, 10, MPI_DOUBLE, 0, TAG_HAND, MPI_COMM_WORLD);
}
//8. After the main loop the master process receives and sums together the hand counts array
// from each slave process.
else
{
int activeProcs = numProcs - 1;
getHandsFromSlaves( activeProcs, handArray );
然后主人继续打印一些数据......
这是 getHands FromSlaves 方法。请注意,我也尝试过使用阻塞调用来解决同样的问题。
void getHandsFromSlaves( int& activeCount, double handTotals[10] ){
static MPI_Request request;
static int msgBuff, recvFlag;
static double handBuff[10];
MPI_Status status;
while (activeCount > 0)
{
if( request )
{
// Already listening for a message
// Test to see if message has been received
MPI_Test( &request, &recvFlag, &status );
//cout << "TAG: " << status.MPI_TAG << " SOURCE: "<< status.MPI_SOURCE << " ERROR: " << status.MPI_ERROR << endl;
if( recvFlag )
{
// Message received
if( status.MPI_TAG == TAG_HAND )
{
cout << "Hand Received!" << endl;
for(int m = 0; m < 10; ++m)
{
handTotals[m] += handBuff[m];
}
activeCount--;
}
else
{
//error report... what happened?
cout << "TAG: " << status.MPI_TAG << " SOURCE: "<< status.MPI_SOURCE << " ERROR: " << status.MPI_ERROR << endl;
}
// Reset the request handle
request = 0;
}
}
if( !request && activeCount > 0 )
// Start listening again
MPI_Irecv(&handBuff, 10, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &request);
}
}