Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 由于某些原因,必须发送两次MPI消息_C++_C_Multiprocessing_Mpi - Fatal编程技术网

C++ 由于某些原因,必须发送两次MPI消息

C++ 由于某些原因,必须发送两次MPI消息,c++,c,multiprocessing,mpi,C++,C,Multiprocessing,Mpi,我一直在从事一个MPI项目,在这个项目中,所有从机都将数据发送回主机。由于某些原因,只有在我连续发送两次数据时,主机才会接收数据。这是非常奇怪的,我认为这是造成一些其他奇怪的问题,我得到。知道这是什么原因吗?我认为第一次发送是发送一些垃圾数据之类的东西。发送的代码完全相同 编辑:下面的代码 if (numProcs > 0) MPI_Barrier( MPI_COMM_WORLD ) ; //only wait if there are other processes to wai

我一直在从事一个MPI项目,在这个项目中,所有从机都将数据发送回主机。由于某些原因,只有在我连续发送两次数据时,主机才会接收数据。这是非常奇怪的,我认为这是造成一些其他奇怪的问题,我得到。知道这是什么原因吗?我认为第一次发送是发送一些垃圾数据之类的东西。发送的代码完全相同

编辑:下面的代码

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);
}
}
void getHandsFromSlaves(int&activeCount,double handTotals[10]){
静态MPI_请求;
静态int-msgBuff,recvFlag;
静态双手buff[10];
MPI_状态;
而(活动计数>0)
{
如果(请求)
{
//已经在听消息了吗
//测试以查看是否已收到消息
MPI_测试(请求、记录和状态);

//cout嗯,您可能试图处理过多的消息,因为在输入
getHandsFromSlaves()时,
请求
变量未定义
routine。由于在输入时,
request
几乎肯定是非零的,因此即使您没有发布
Irecv
,您也会立即尝试对消息进行
MPI\u测试

事实上,这里发布的代码摘录有很多非常奇怪的地方。为什么局部变量
是静态的
?为什么要在
MPI\u Test()上实现自己的busywait()
而不是使用
MPI\u Wait()
?如果在接收之间没有做任何有用的事情,为什么要使用非阻塞接收?事实上,如果只是对所有数组求和,为什么要进行单独的点到点接收,而不是执行
MPI_Reduce()

以下简短得多的代码似乎可以完成您在上面尝试执行的操作:

#include <stdio.h>
#include <mpi.h>


int main (int argc, char **argv) {

    int rank, numProcs;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
    double handArray[10];
    double handTotal[10];

    for (int i=0; i<10; i++)
        handArray[i] = rank + i;

    if (rank == 0)  // Since apparently rank 0 doesn't do anything
    {
        for (int i=0; i<10; i++)
            handArray[i] = 0;
    }

    MPI_Reduce(handArray, handTotal, 10, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

    if (rank == 0) {
        printf("Hand Totals= \n");
        for (int i=0; i<10; i++)
            printf(" %lf ", handTotal[i]);
        printf("\n");
    }

    MPI_Finalize();
}
#包括
#包括
int main(int argc,字符**argv){
整数级,numProcs;
MPI_Init(&argc,&argv);
MPI通信等级(MPI通信世界和等级);
MPI通信大小(MPI通信世界和numProcs);
双手阵列[10];
双手共[10];

对于(int i=0;iadded代码。如果您还需要什么,请告诉我。有人能提供帮助吗?非常感谢您的帮助!包括进程0在内的所有进程都将添加到各自的handArray中。最后,进程0汇总所有总数并打印数据。我只是没有发布所有进程运行时发生的代码s正在做他们的工作。handArray的每个元素都在存储它找到的每种类型的扑克手的数量。感谢您的帮助,我将查看MPI_Reduce,看看它是否能正常工作。