'MPI communication problem, passing wrong values

I have just installed OpenMPI-4.1.3 on my Ubuntu 22.04 64 bit OS. I tested the MPI communication with a sample. The idea is that all ranks sends its rank ID to 2 other ranks. And I have the following code:

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

int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);
    // Get the number of processes and check if even number of processes are used
    int size;   
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    if(size%2!=0)
    {
    printf("The number of ranks must be even.\n");
    exit(-1);
    }
    // Get my rank
    int my_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

   
    int Send[1], Recv[2];
    Send[0]=my_rank;
    printf("MPI process %d sends the value %d.\n", my_rank, Send[0]);

    MPI_Barrier(MPI_COMM_WORLD);

    MPI_Request requestsSend[2];
    MPI_Request requestsRecv[2];

    if(my_rank <size/2)
    {
    // The MPI process sends the message.             
        MPI_Isend(&Send[0], 1, MPI_INT, my_rank+size/2, 0, MPI_COMM_WORLD, &requestsSend[0]);
    MPI_Isend(&Send[0], 1, MPI_INT, size-1-my_rank, 0, MPI_COMM_WORLD, &requestsSend[1]);

    // The MPI processes receive the message.           
        MPI_Irecv(&Recv[0], 1, MPI_INT, my_rank+size/2, 0, MPI_COMM_WORLD, &requestsRecv[0]);
    MPI_Irecv(&Recv[1], 1, MPI_INT, size-1-my_rank, 0, MPI_COMM_WORLD, &requestsRecv[1]);   

        printf("Process %d received value %d from %d and value %d from %d.\n", my_rank, Recv[0],my_rank+size/2,Recv[1],size-1-my_rank);    
    }
    else
    {
    // The MPI process sends the message.
        MPI_Isend(&Send[0], 1, MPI_INT, my_rank-size/2, 0, MPI_COMM_WORLD, &requestsSend[0]);
    MPI_Isend(&Send[0], 1, MPI_INT, size-1-my_rank, 0, MPI_COMM_WORLD, &requestsSend[1]);

        // The MPI processes receive the message.           
        MPI_Irecv(&Recv[0], 1, MPI_INT, my_rank-size/2, 0, MPI_COMM_WORLD, &requestsRecv[0]);
    MPI_Irecv(&Recv[1], 1, MPI_INT, size-1-my_rank, 0, MPI_COMM_WORLD, &requestsRecv[1]);

        printf("Process %d received value %d from %d and value %d from %d.\n", my_rank, Recv[0],my_rank-size/2,Recv[1],size-1-my_rank);
    }
    
    MPI_Barrier(MPI_COMM_WORLD);

    // Wait for both routines to complete
    MPI_Waitall(2, requestsSend, MPI_STATUSES_IGNORE);
    printf("Process %d: both messages have been sent.\n", my_rank);

    MPI_Waitall(2, requestsRecv, MPI_STATUSES_IGNORE);
    printf("Process %d: both messages have been received.\n", my_rank);

    MPI_Finalize();
    return EXIT_SUCCESS;
}

This compiles with no errors, but the running result is

$ mpirun -n 4 ./mpitest
MPI process 1 sends the value 1.
Process 1 received value 1190733916 from 3 and value 32738 from 2.
Process 1: both messages have been sent.
Process 1: both messages have been received.
MPI process 2 sends the value 2.
Process 2 received value 738584668 from 0 and value 32722 from 1.
Process 2: both messages have been sent.
Process 2: both messages have been received.
MPI process 3 sends the value 3.
Process 3 received value 1924225116 from 1 and value 32664 from 0.
Process 3: both messages have been sent.
Process 3: both messages have been received.
MPI process 0 sends the value 0.
Process 0 received value -1700759460 from 2 and value 32595 from 3.
Process 0: both messages have been sent.
Process 0: both messages have been received.

It seems to me that both the communication and Barriers are not working correctly. Is this something wrong with my compiler or system? How can I fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source