What do I need to do to find out when the number of send messages is unknown in MPI?

I program in MPI. I want to send something to another processor and get it there, but I do not know how many messages I will send. In fact, the number of messages sent to another processor depends on the file that I read during the program, so I don’t know how much I receive, I have to write on the other side. What method and function should I use?

+4
source share
1 answer

You can still use sending and receiving, but you must also add a new kind of message that tells the receiving process that there will be no new messages. This is usually handled by sending with a different tag. Thus, the program will look something like this:

if (sender) {
    while (data_to_send == true) {
        MPI_Send(data, size, datatype, receiving_rank, 0, MPI_COMM_WORLD);
    }
    for (i = 0; i < size; i++) {
        MPI_Send(NULL, 0, MPI_INT, i, 1, MPI_COMM_WORLD);
    }
} else {
    while (1) {
        MPI_Recv(data, size, datatype, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
        if (status.MPI_TAG == 1) break;

        /* Do processing */
    }
}

There is a better way that works if you have non-blocking teams (from MPI-3). Before you begin to receive data, you publish a non-blocking barrier. Then you start posting non-blocking messages. Instead of waiting only on receipt, you use waitany for both requests, and when the barrier is completed, you know that there will no longer be data. On the sender side, you just keep sending data until there is no more data, then make a non-blocking barrier to finish the job.

+4

Source: https://habr.com/ru/post/1524278/


All Articles