Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable-length structures between MPI processes

Tags:

c

mpi

I need to MPI_Gatherv() a number of int/string pairs. Let's say each pair looks like this:

struct Pair {
  int x;
  unsigned s_len;
  char s[1]; // variable-length string of s_len chars
};

How to define an appropriate MPI datatype for Pair?

like image 909
Constantin Avatar asked Feb 13 '10 19:02

Constantin


People also ask

How does the MPI_probe function work?

What is different in this example is that process one now calls MPI_Probe to find out how many elements process zero is trying to send (using MPI_Get_count ). Process one then allocates a buffer of the proper size and receives the numbers. Running the code will look similar to this.

How to find the length of the message in a status structure?

The tag of the message. The tag of the message can be accessed by the MPI_TAG element of the structure (similar to MPI_SOURCE ). The length of the message. The length of the message does not have a predefined element in the status structure. Instead, we have to find out the length of the message with MPI_Get_count.

How to access the rank of an MPI_status variable?

That is, if we declare an MPI_Status stat variable, the rank can be accessed with stat.MPI_SOURCE. The tag of the message. The tag of the message can be accessed by the MPI_TAG element of the structure (similar to MPI_SOURCE ). The length of the message. The length of the message does not have a predefined element in the status structure.

How does the MPI_recv function work?

The MPI_Status structure As covered in the previous lesson, the MPI_Recv operation takes the address of an MPI_Status structure as an argument (which can be ignored with MPI_STATUS_IGNORE). If we pass an MPI_Status structure to the MPI_Recv function, it will be populated with additional information about the receive operation after it completes.


3 Answers

In short, it's theoretically impossible to send one message of variable size and receive it into a buffer of the perfect size. You'll either have to send a first message with the sizes of each string and then a second message with the strings themselves, or encode that metainfo into the payload and use a static receiving buffer.

If you must send only one message, then I'd forgo defining a datatype for Pair: instead, I'd create a datatype for the entire payload and dump all the data into one contiguous, untyped package. Then at the receiving end you could iterate over it, allocating the exact amount of space necessary for each string and filling it up. Let me whip up an ASCII diagram to illustrate. This would be your payload:

|..x1..|..s_len1..|....string1....|..x2..|..s_len2..|.string2.|..x3..|..s_len3..|.......string3.......|...

You send the whole thing as one unit (e.g. an array of MPI_BYTE), then the receiver would unpack it something like this:

while (buffer is not empty)
{
    read x;
    read s_len;
    allocate s_len characters;
    move s_len characters from buffer to allocated space;
}

Note however that this solution only works if the data representation of integers and chars is the same on the sending and receiving systems.

like image 100
suszterpatt Avatar answered Oct 24 '22 05:10

suszterpatt


I don't think you can do quite what you want with MPI. I'm a Fortran programmer, so bear with me if my understanding of C is a little shaky. You want, it seems, to pass a data structure consisting of 1 int and 1 string (which you pass by passing the location of the first character in the string) from one process to another ? I think that what you are going to have to do is pass a fixed length string -- which would have, therefore, to be as long as any of the strings you really want to pass. The reception area for the gathering of these strings will have to be large enough to to receive all the strings together with their lengths.

You'll probably want to declare a new MPI datatype for your structs; you can then gather these and, since the gathered data includes the length of the string, recover the useful parts of the string at the receiver.

I'm not certain about this, but I've never come across truly variable message lengths as you seem to want to use and it does sort feel un-MPI-like. But it may be something implemented in the latest version of MPI that I've just never stumbled across, though looking at the documentation on-line it doesn't seem so.

like image 24
High Performance Mark Avatar answered Oct 24 '22 05:10

High Performance Mark


MPI implementations do not inspect or interpret the actual contents of a message. Provided that you know the size of the data structure, you can represent that size in some number of char's or int's. The MPI implementation will not know or care about the actual internal details of the data.

There are a few caveats...both the sender and receiver need to agree on the interpretation of the message contents, and the buffer that you provide on the sending and receiving side needs to fit into some definable number of char's or int's.

like image 26
Stan Graves Avatar answered Oct 24 '22 05:10

Stan Graves