Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

c - Do MPI Datatypes of Send/Receive Have to Match?

I'm trying to determine what parameters of a sending operation and receiving operation must match for a message to be passed.

I understand that a receiving operation must match (or encompass) the source of the message, the tag and the communicator. However, is it required for the datatype to be matching?

E.g. could I match a send from rank 0:

MPI_Send (send_buffer, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);

with the receiving operation on rank 1:

MPI_Recv (recv_buffer, 4, MPI_BYTE, 0, 0, MPI_COMM_WORLD, status);

Particularly as the sending 'count' is less than the receiving count, which I am aware is valid for MPI.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The short answer is yes, datatypes must match, but there are some caveats.

When using primitive MPI datatypes such as MPI_INT and MPI_FLOAT, the types must be identical. The only exception is MPI_PACKED, which can match any other type.

When using derived datatypes (a.k.a. user-defined datatypes), things get slightly more complicated. A general MPI datatype consists of two things: a type signature, which defines the basic language datatypes that make up the derived datatype, and a series of byte displacements, which define where those basic elements reside in memory. The combination of both sequences is called the type map of the datatype. The primitive MPI datatypes are particular cases of such general datatypes having a single basic datatype in the type signature with a displacement of 0, e.g. MPI_INT has the following type map: { (int, 0) }. For a Send and Receive operation to match, the type signature must be the same on both sides, but the displacements can vary. E.g. the sender might send a single MPI_Type_contiguous object consisting of 3 MPI_FLOATs, but the receiver can receive this as an MPI_Type_indexed type of 3 MPI_FLOATs with arbitrary displacements. The sender might also supply an array of 3 MPI_FLOAT elements, e.g. MPI_Send(buf, 3, MPI_FLOAT, ...) and this would still result in the same type signature.

I should emphasize that the type signature consists of basic datatypes only. Whether the user-defined datatype was created directly from primitive MPI types or other user-defined datatypes (or a mixture of thereof) is irrelevant.

Example: the sender defines an MPI_Type_struct consisting of 3 MPI_INTs and 2 MPI_FLOATs. The receiver defines an MPI_Type_contiguous consisting of 3 MPI_INTs (let's call this type A), another MPI_Type_contiguous of 2 MPI_FLOATs (type B), and then an MPI_Type_struct consisting of 1 A and 1 B. These two datatypes will match, since ultimately they're both 3 ints followed by 2 floats.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...