【问题标题】:Can anybody help me to identify the runtime MPI error in this code sample?任何人都可以帮助我识别此代码示例中的运行时 MPI 错误吗?
【发布时间】:2011-06-03 17:24:59
【问题描述】:

此代码采样器用于学习 MPI 编程。我使用的 MPI 包是 MPICH2 1.3.1。下面的代码是我学习MPI_Isend()MPI_Irecv()MPI_Wait()的第一步。该代码有一个主人和几个工人。 Master从worker接收数据,而worker向master发送数据。像往常一样,数据量非常大,工作人员将数据拆分为中继并按顺序发送中继。在发送中继时,我使用了一些技巧来重叠计算和通信。方法很简单,只需要保留两个buffer,每个发送周期保存两个trunk即可。

int test_mpi_wait_2(int argc, char* argv[])
{
    int rank;
    int numprocs; 

    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);

    int trunk_num = 6;// assume there are six trunks
    int trunk_size = 10000;// assume each trunk has 10,000 data points
    if(rank == 0)
    {
        //allocate receiving buffer for all workers
        int** recv_buf = new int* [numprocs];
        for(int i=0;i<numprocs;i++)
            recv_buf[i] = new int [trunk_size];

        //collecting first trunk from all workers
        MPI_Request* requests = new MPI_Request[numprocs];
        for(int i=1;i<numprocs;i++)
            MPI_Irecv(recv_buf[i], trunk_size, MPI_INT, i, 0, MPI_COMM_WORLD, &requests[i]);

        //define send_buf counter used to record how many trunks have been collected
        vector<int> counter(numprocs);

        MPI_Status status;
        //assume therer are N-1 workers, then the total trunks will be collected is (N-1)*trunk_num
        for(int i=0;i<(numprocs-1)*trunk_num;i++)
        {         
            //wait until receive one trunk from any worker
            int active_index;
            MPI_Waitany(numprocs-1, requests+1, &active_index, &status);      

            int request_index = active_index + 1;
            int procs_index = active_index + 1;

            //check wheather all trunks from this worker have been collected
            if(++counter[procs_index] != trunk_num)
            {    
                //receive next trunk from this worker
                MPI_Irecv(recv_buf[procs_index], trunk_size, MPI_INT, procs_index, 0, MPI_COMM_WORLD, &requests[request_index]);
            }   
        }

        for(int i=0;i<numprocs;i++)
            delete [] recv_buf[i];
        delete [] recv_buf;
        delete [] requests;

        cout<<rank<<" done"<<endl;
    }
    else
    {  
        //for each worker, the worker first fill one trunk and send it to master
        //for efficiency, the computation of trunk and communication to master is overlapped.
        //two buffers are allocated to implement the overlapped computation

        int* send_buf[2];  
        send_buf[0] = new int [trunk_size];//Buffer A
        send_buf[1] = new int [trunk_size];//Buffer B

        MPI_Request requests[2];

        //file first trunk
        for(int i=0;i<trunk_size;i++)
            send_buf[0][i] = 0;
        //send this trunk
        MPI_Isend(send_buf[0], trunk_size, MPI_INT, 0, 0, MPI_COMM_WORLD, &requests[0]);

        if(trunk_num > 1)
        {
            //file second trunk
            for(int i=0;i<trunk_size;i++)
            send_buf[1][i] = i;
            //send this trunk
            MPI_Isend(send_buf[1], trunk_size, MPI_INT, 0, 0, MPI_COMM_WORLD, &requests[1]);
        }

        //for remained trunks, keep cycle until all trunks are sent
        for(int i=2;i<trunk_num;i+=2)
        {      
            //wait till trunk data at buffer A is sent
            MPI_Wait(&requests[0], MPI_STATUS_IGNORE);

            //fill buffer A with next trunk data
            for(int j=0;j<trunk_size;j++)
                send_buf[0][j] = j * i;

            //send buffer A
            MPI_Isend(send_buf[0], trunk_size, MPI_INT, 0, 0, MPI_COMM_WORLD, &requests[0]);

            //if more trunks are remained, fill buffer B and sent it
            if(i+ 1 < trunk_num)
            {
                MPI_Wait(&requests[1], MPI_STATUS_IGNORE);
                for(int j=0;j<trunk_size;j++)
                    send_buf[1][j] = j * (i + 1);
                MPI_Isend(send_buf[1], trunk_size, MPI_INT, 0, 0, MPI_COMM_WORLD, &requests[1]);
            }
        }

        //wait until last two trunks have been sent
        if(trunk_num == 1)
        {
            MPI_Wait(&requests[0], MPI_STATUS_IGNORE);
        }
        else
        {   
            MPI_Wait(&requests[0], MPI_STATUS_IGNORE);
            MPI_Wait(&requests[1], MPI_STATUS_IGNORE);       
        }

        delete [] send_buf[0];
        delete [] send_buf[1];

        cout<<rank<<" done"<<endl;
    }

    MPI_Finalize();

    return 0;
}

【问题讨论】:

  • 格式化代码(使用编辑框中的 0101 按钮)并添加一些上下文,例如正在使用的 API、环境以及您认为可能存在的问题将帮助您获得更快地得到更好的答案。
  • 看起来更好 - 那么错误是什么?
  • 错误是PMPI_Wait错误和MPI_Finalize中的致命错误

标签: c++ mpi


【解决方案1】:

答案不多,但它可以在我的 MPI 版本上编译和运行,最多有 4 个处理器。代码似乎有点复杂,但我也看不出它不应该工作的任何原因。

【讨论】:

    【解决方案2】:

    我看到几个明显的:一些 for 循环没有终止,一些 cout 语句没有终止,等等。我相信代码的格式不正确。 ..

    【讨论】:

    • 这只是一个关于 MPI_Isend、MPI_Irecv 和 MPI_Wait 的测试代码。代码在 Visual Studio 2008 上可以正确编译,但运行时总是停止。
    猜你喜欢
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多