When to use tags when sending and receiving messages in MPI?
Asked Answered
H

3

30

I'm not sure when I have to use different numbers for the tag field in MPI send, receive calls. I've read this, but I can't understand it.

Sometimes there are cases when A might have to send many different types of messages to B. Instead of B having to go through extra measures to differentiate all these messages, MPI allows senders and receivers to also specify message IDs with the message (known as tags). When process B only requests a message with a certain tag number, messages with different tags will be buffered by the network until B is ready for them.

Do I have to use tags, for example, when I have multiple calls "isend" (with different tags) from process A and only 1 call to "ireceive" in process B?

Hornbill answered 13/7, 2015 at 14:52 Comment(1)
In this case you use MPI_TAG_ANY on the receive side, look at the Status structure after receive to determine which tag was used, and process accordingly.Luzluzader
S
24

Message tags are optional. You can use arbitrary integer values for them and use whichever semantics you like and seem useful to you.

Like you suggested, tags can be used to differentiate between messages that consist of different types (MPI_INTEGER, MPI_REAL, MPI_BYTE, etc.). You could also use tags to add some information about what the data actually represents (if you have an nxn matrix, a message to send a row of this matrix will consist of n values, as will a message to send a column of that matrix; nevertheless, you may want to treat row and column data differently).

Note that the receive operation has to match the tag of a message it wants to receive. This, however, does not mean that you have to specify the same tag, you can also use the wildcard MPI_ANY_TAG as message tag; the receive operation will then match arbitrary message tags. You can find out which tag the sender used with the help of MPI_Probe.

Sumbawa answered 17/7, 2015 at 8:37 Comment(0)
P
14

In general, I tend to avoid them. There is no requirement that you use tags. If you need to get the message size before parsing the message, you can use MPI_Probe. That way you can send different messages rather than specifying Tags. I typically use tags because MPI_Recv requires that you know the message size before fetching the data. If you have different sizes and types, tags can help you differentiate between them by having multiple threads or processes listening over a different subset. Tag 1 can mean messages of type X and tag 2 will be messages of type Y. Also, it enables you to have multiple "channels" of communication without having to do the work of creating unique communicators and groups.

#include <mpi.h>
#include <iostream>

using namespace std;

int main( int argc, char* argv[] )
{
    // Init MPI
    MPI_Init( &argc, &argv);

    // Get the rank and size
    int rank, size;
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    MPI_Comm_size( MPI_COMM_WORLD, &size );

    // If Master
    if( rank == 0 ){

        char* message_r1 = "Hello Rank 1";
        char* message_r2 = "Hello Rank 2";

        // Send a message over tag 0
        MPI_Send( message_r1, 13, MPI_CHAR, 1, 0, MPI_COMM_WORLD );

        // Send a message over tag 1
        MPI_Send( message_r2, 13, MPI_CHAR, 2, 1, MPI_COMM_WORLD );


    }
    else{

        // Buffer
        char buffer[256];
        MPI_Status status;

        // Wait for your own message
        MPI_Recv( buffer, 13, MPI_CHAR, 0, rank-1, MPI_COMM_WORLD, &status );

        cout << "Rank: " << rank << ", Message: " << buffer << std::endl;

    }

    // Finalize MPI
    MPI_Finalize();

}
Percolate answered 13/7, 2015 at 15:21 Comment(4)
so if i have to send multiple messages,in sequence (1 send = 1 message), from process A to process B there isn't any need to change the tag, is it right?Hornbill
Yeah. You only need different tags if you plan on sending different types of messages. Otherwise, 0 is good.Percolate
different types only mean MPI_INTEGER and MPI_REAL for example, right?Hornbill
That is one good example. It is not that specific though. You could send integers over both, where tag1 is the ID of something, and the second tag could be the value. The key is that tags are completely optional, where the user differentiates them on their own terms.Percolate
U
5

Tags can be useful in distributed computing algorithms where there can be multiple types of messages. Consider the leader election problem, where a process (election candidate) sends a message of type requestVote and the other processes respond with a message of type voteGrant.

There are many such algorithms that distinguish between the types of messages and the tag can be useful to categorize among such messages.

Uuge answered 11/5, 2017 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.