Search code examples
cparallel-processingmpigarbage

MPI Asynchoronous Send and Receive not working as expected


I have a question regarding communications using the MPI Library in C. Here's my sample code:

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

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

    // Initialize the MPI Environment
    MPI_Init (&argc, &argv);

    int world_size;
    MPI_Comm_size (MPI_COMM_WORLD, &world_size);

    int world_rank;
    MPI_Comm_rank (MPI_COMM_WORLD, &world_rank);

    int workers = world_size - 1;

    if (world_rank == 0) {
    }
    else if (world_rank == 1) {
        int to_recv;
        int to_send = world_rank;
        MPI_Request request1, request2;
        MPI_Irecv (&to_recv, sizeof (int), MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD, &request1);
        MPI_Isend (&to_send, sizeof (int), MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD, &request2);

        MPI_Wait (&request1, MPI_STATUS_IGNORE);
        MPI_Wait (&request2, MPI_STATUS_IGNORE);

        printf("I am %d\n", world_rank);
        printf("I received rank %d\n", to_recv);
    }
    else if (world_rank == workers) {
        int to_recv;
        int to_send = world_rank;
        MPI_Request request1, request2;
        MPI_Irecv (&to_recv, sizeof (int), MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD, &request1);
        MPI_Isend (&to_send, sizeof (int), MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD, &request2);

        MPI_Wait (&request1, MPI_STATUS_IGNORE);
        MPI_Wait (&request2, MPI_STATUS_IGNORE);

        printf("I am %d\n", world_rank);
        printf("I received rank %d\n", to_recv);
    }
    else {
        int to_recvl, to_recvr;
        int to_send1 = world_rank, to_send2 = world_rank;

        MPI_Request request1, request2, request3, request4;

        MPI_Irecv (&to_recvl, sizeof (int), MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD, &request1);
        MPI_Isend (&to_send1, sizeof (int), MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD, &request2);

        MPI_Irecv (&to_recvr, sizeof (int), MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD, &request3);
        MPI_Isend (&to_send2, sizeof (int), MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD, &request4);

        MPI_Wait (&request1, MPI_STATUS_IGNORE);
        MPI_Wait (&request2, MPI_STATUS_IGNORE);

        MPI_Wait (&request3, MPI_STATUS_IGNORE);
        MPI_Wait (&request4, MPI_STATUS_IGNORE);

        printf("I am %d\n", world_rank);
        printf("I received ranks %d and %d\n", to_recvl, to_recvr);
    }

    MPI_Finalize ();

    return 0;
}

So this is what I basically want to accomplish:

Say I have 11 processes spawned (0 being master and process 1-10 workers). Then,

  • process 1 sends its rank to process 2
  • process 1 receives rank from process 2
  • process 10 sends its rank to process 9
  • process 10 receives rank from process 9
  • process i sends its rank to process i+1 and i-1 (1 < i < 10)
  • process i receives rank from process i+1 and i-1 (1 < i < 10)

While running the program, I get garbage values in some cases like the one added below:

I am 2
I received ranks 1 and 3
I am 3
I received ranks 2 and 4
I am 4
I received ranks 1499161032 and 5
I am 6
I received ranks 1488867784 and 7
I am 9
I received rank 8
I am 32767
I received rank 2
I am 5
I received ranks 1507000776 and 6
I am 7
I received ranks 6 and 8
I am 8
I received ranks 7 and 9

I don't know what is causing the problem. Any help would be greatly appreciated.

Thank you!


Solution

  • A pretty simple problem, the second parameter in your MPI_Isend() and MPI_Irecv() should be the count of elements you wish to send or receive. You are using sizeof(int) which is 4 in most systems, while you only want to send/receive 1 element, hence the count should be 1.