tags:

views:

188

answers:

2
+2  Q: 

MPI and C structs

I have to admit, I was quite shocked to see how many lines of code are required to transfer one C struct with MPI.

Under what circumstances will it work to simply transmit a struct using the predefined dataype MPI_CHAR? Consider the following example:

struct particle {
   double x;
   double y;
   long   i;
};

struct particle p;
MPI_Isend(&p, sizeof(particle), MPI_CHAR, tag, MPI_COMM_WORLD, &sendr);

In my case, all processes run on the same architecture. Is padding the only issue?

+1  A: 

Personally I'd be more concerned about comprehensibility and maintainability, even portability, than padding. If I'm sending a structure I like my code to show that I am sending a structure, not a sequence of bytes or chars. And I expect my codes to run on multiple architectures, across multiple generations of language standards and compilers.

I guess all I'm saying is that, if it's worth defining a structure (and you obviously think it is) then it's worth defining a structure. Saving a few lines of (near-)boilerplate isn't much of an argument against that.

High Performance Mark
I don't understand the argument about maintainability. In the above code segment, I can change the struct without having to do anything else. Whereas if I do it the proper way using `MPI_Type_create_struct`, I have to change at least 4 lines of code...
hanno
@hanno, see @suszterpatt's answer for the argument about maintainability, better explained than I managed.
High Performance Mark
+2  A: 

MPI_BYTE is the datatype to use when sending untyped data, not MPI_CHAR. If the two machines have the same architecture but use different character encoding, using MPI_CHAR may corrupt your data. Sending your data as MPI_BYTE will leave the binary representation as it is and perform no representation conversion whatsoever.

That said, yes, technically it is correct to send a struct that way, if (and only if) you can guarantee that the data representation will be the same on the sending and receiving ends. However, it is poor programming practice, as it obfuscates the purpose of your code, and introduces platform dependency.

Keep in mind that you only have to define and commit a datatype once, while you will generally need to write code to send it several times. Reducing the clarity of all your sends just to save a couple lines on the single definition is not a trade up.

suszterpatt
Thanks. Just one more question: Is there a performance benefit from using `MPI_BYTE`? Or, is there any overhead when I transmit a struct with my own MPI datatype? I'm thinking of something like MPI copying the data to another buffer in small chunks, etc...
hanno
This is probably implementation dependent, and I would imagine that whatever performance gain you'd get from not performing representation checks/conversion is massively overshadowed by the overall cost of passing a message in the first place. Still, it might be an interesting experiment to compare how long it takes to send a large payload (thousands) of structs typed vs untyped.
suszterpatt