Search code examples
cmpiblockingnonblocking

Why doesn't process 0 receive data from itself?


I've just started to learn MPI, and the reason of problem I got is not clear to me.

#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;
}

One of outputs is following (number of processes is 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

What's wrong with process 0?


Solution

  • You cannot be receiving into the same buffer you are sending from. Technically you must not modify any part of the send buffer before completing the nonblocking send operation. You should also complete the send operation, which means you should store it in multiple request objects.

    In anyway, just use MPI_Bcast and leave nonblocking operations for later when you are more experienced with MPI. For some reason, they are often overused and wrongly used by beginners.