Message Passing Interface: Difference between revisions

Content deleted Content added
Line 79:
 
===Derived data types===
Many MPI functions require that you specifyspecifing the type of data which is sent between processes. This is because MPI aims to support heterogeneous environments where types might be represented differently on the different nodes<ref name="node37">{{cite web|url=http://mpi-forum.org/docs/mpi-1.1/mpi-11-html/node37.html|title=Type matching rules|website=mpi-forum.org}}</ref> (for example they might be running different CPU architectures that have different [[endianness]]), in which case MPI implementations can perform ''data conversion''.<ref name="node37" /> Since the C language does not allow a type itself to be passed as a parameter, MPI predefines the constants <code>MPI_INT</code>, <code>MPI_CHAR</code>, <code>MPI_DOUBLE</code> to correspond with <code>int</code>, <code>char</code>, <code>double</code>, etc.
 
Here is an example in C that passes arrays of <code>int</code>s from all processes to one. The one receiving process is called the "root" process, and it can be any designated process but normally it will be process 0. All the processes ask to send their arrays to the root with <code>MPI_Gather</code>, which is equivalent to having each process (including the root itself) call <code>MPI_Send</code> and the root make the corresponding number of ordered <code>MPI_Recv</code> calls to assemble all of these arrays into a larger one:<ref>{{cite web|url=https://www.open-mpi.org/doc/v1.8/man3/MPI_Gather.3.php|title=MPI_Gather(3) man page (version 1.8.8)|website=www.open-mpi.org}}</ref>
Line 93:
</syntaxhighlight>
 
However, youit may be instead wishdesirable to send data as one block as opposed to 100 <code>int</code>s. To do this define a "contiguous block" derived data type:
<syntaxhighlight lang="c">
MPI_Datatype newtype;