为什么进程 0 不接收来自自身的数据?

Why doesn't process 0 receive data from itself?

刚开始学MPI,遇到问题的原因不是很清楚

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


int main(int argc, char *argv[])
{
    int myRank, size;
    double myLeftBound, myRightBound;
    MPI_Status status1;
    MPI_Status status2;
    MPI_Request request1;
    MPI_Request request2;   

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);   


    if (myRank == 0)
    {
        int counter;

        for (counter = size - 1; counter >= 0; counter--)
        {
            myLeftBound = 0;
            myRightBound = 1;
            MPI_Isend(&myLeftBound, 1, MPI_DOUBLE, counter, 2 * counter, MPI_COMM_WORLD, &request1);
            MPI_Isend(&myRightBound, 1, MPI_DOUBLE, counter, 2 * counter + 1, MPI_COMM_WORLD, &request2);   
        }
    }

    MPI_Recv(&myLeftBound, 1, MPI_DOUBLE, 0, myRank * 2, MPI_COMM_WORLD, &status1);
    MPI_Recv(&myRightBound, 1, MPI_DOUBLE, 0, myRank * 2 + 1, MPI_COMM_WORLD, &status2);
    printf ("I received my left boundary %f\n, I'm %d\n", myLeftBound, myRank);
    MPI_Finalize();
    return 0;
}

其中一个输出如下(进程数为 4)

I received my left boundary 0.000000

, I'm 1

I received my left boundary 0.000000

, I'm 2

I received my left boundary 0.000000

, I'm 3

Assertion failed in file src/mpid/ch3/src/ch3u_buffer.c at line 77: FALSE memcpy argument memory ranges overlap, dst_=0x7ffcc26963d0 src_=0x7ffcc26963d0 len_=8

internal ABORT - process 0

进程 0 出了什么问题?

您不能在发送时接收到同一个缓冲区。从技术上讲,在完成非阻塞发送操作之前,您不得修改发送缓冲区的任何部分。您还应该完成发送操作,这意味着您应该将其存储在多个请求对象中。

无论如何,只需使用 MPI_Bcast 并在以后对 MPI 有更多经验时留下非阻塞操作。由于某种原因,它们经常被初学者过度使用和错误使用。