3
votes

i am learning mpi communication in c. I encountered a problem when trying to send a message from one node with MPI_Isend and receive it on the other node with MPI_Irecv. Here is the code:

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

#define TAG 3
#define ROOT 0

int main (int argc, char ** argv){
    int number_of_proc = 0;
    int rank = 0;
    // initialize mpi environment
    MPI_Init(NULL, NULL);
    // get the number of processes
    MPI_Comm_size(MPI_COMM_WORLD, &number_of_proc);
    // get the rank of this process
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    if (rank == 1){
        // the message i want to send to the other node
        int message = 123;
        MPI_Request success_send = MPI_REQUEST_NULL;
        // send message to the root node
        MPI_Isend(&message, 1, MPI_INT, ROOT, TAG, MPI_COMM_WORLD, &success_send);
        // to check if the node keeps running after sending the message
        printf("Node 1 still working\n");
    } else if (rank == ROOT){
        printf("Node 0:%10d Nodes total\n", number_of_proc);
        int message = 0;
        MPI_Request success_received = MPI_REQUEST_NULL;
        // receive the message from node 1
        MPI_Irecv(&message, 1, MPI_INT, MPI_ANY_SOURCE, TAG, MPI_COMM_WORLD, &success_received);
        printf("Node 0: greetings from node 1: %10d\n", message);
    }
    MPI_Finalize();
    return EXIT_SUCCESS;
} 

The file is called mpitest.c. I compile it with mpicc -pedantic -Wall -std=c99 -o mpitest mpitest.c and start it with mpirun -c 2 ./mpitest.

The expected output is

Node 1 still working
Node 0:         2 Nodes total
Node 0: greetings from node 1:          123

but i am getting

Node 1 still working
Node 0:         2 Nodes total
Node 0: greetings from node 1:            0

what am i doing wrong?

2
Since you are now learning MPI, note that you are not required to match MPI_Isend with MPI_Irecv and vice versa. You can freely mix blocking and non-blocking operations on both sides.Hristo Iliev

2 Answers

2
votes

Try this. You are using non-blocking communication, so you have to wait for the end of the receiving operation.

MPI_Irecv(&message, 1, MPI_INT, MPI_ANY_SOURCE, TAG, MPI_COMM_WORLD, &success_received);
MPI_Status status;
MPI_Wait(&success_received,  &status);
1
votes

Its non blocking !

You do not know when you will receive the data on MPI_Irec(..) will return immediately as its non blocking. You need to wait till data may have been written.

You need to test and check the status.(in a loop )

See for MPI_Test and MPI_Wait online.

Happy parallel programming.