Skip to content

Commit 15d9690

Browse files
committed
doc: added new example to Type_create_struct
A new example using MPI_UB and an actual struct should make new users more accustomed to its use. Signed-off-by: Nick Papior <nickpapior@gmail.com>
1 parent dad058e commit 15d9690

File tree

1 file changed

+109
-2
lines changed

1 file changed

+109
-2
lines changed

docs/man-openmpi/man3/MPI_Type_create_struct.3.rst

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,115 @@ DESCRIPTION
7373
:ref:`MPI_Type_create_struct` creates a structured data type. This routine
7474
replaces :ref:`MPI_Type_struct`, which is now deprecated.
7575

76-
NOTE - This routine replaces :ref:`MPI_Type_struct`, which is deprecated. See
77-
the man page :ref:`MPI_Type_struct` for information about that routine.
76+
77+
:ref:`MPI_Type_create_struct` is the most general type constructor. It further
78+
generalizes :ref:`MPI_Type_create_hindexed` in that it allows each block to consist of
79+
replications of different datatypes.
80+
81+
**Example 1:**
82+
Let ``type1`` have type map
83+
84+
::
85+
86+
87+
{(double, 0), (char, 8)}
88+
89+
with extent 16. Let ``B = (2, 1, 3)``, ``D = (0, 16, 26)``, and ``T = (MPI_FLOAT, type1, MPI_CHAR)``.
90+
Then a call to ``MPI_Type_create_struct(3, B, D, T, newtype)``
91+
returns a datatype with type map
92+
93+
::
94+
95+
96+
{
97+
(float, 0), (float,4), // 2 float
98+
(double, 16), (char, 24), // 1 type1
99+
(char, 26), (char, 27), (char, 28) // 3 char
100+
}
101+
102+
That is, two copies of ``MPI_FLOAT`` starting at 0, followed by one copy of
103+
``type1`` starting at 16, followed by three copies of ``MPI_CHAR``, starting at
104+
26. (We assume that a float occupies 4 bytes.)
105+
106+
107+
**Example 2:**
108+
109+
An example of a struct with only some components part of the type
110+
111+
.. code-block:: c
112+
113+
struct MyStruct {
114+
double x[2], y;
115+
char a;
116+
int n;
117+
};
118+
119+
// create a new type where we only send x, y and n
120+
int B[] = {
121+
2, // 2 double's
122+
1, // 1 double
123+
1, // 1 int
124+
1 // alignment padding
125+
};
126+
MPI_Aint D[] = {
127+
offsetof(struct MyStruct, x),
128+
offsetof(struct MyStruct, y),
129+
offsetof(struct MyStruct, n),
130+
sizeof(struct MyStruct)
131+
};
132+
MPI_Datatype T[] = {
133+
MPI_DOUBLE,
134+
MPI_DOUBLE,
135+
MPI_INT,
136+
MPI_UB
137+
};
138+
139+
MPI_Datatype mpi_dt_mystruct;
140+
MPI_Type_create_struct(4, B, D, T, &mpi_dt_mystruct);
141+
MPI_Type_commit(&mpi_dt_mystruct);
142+
143+
// We can now send a struct (omitting a)
144+
145+
struct MyStruct values[3];
146+
147+
if ( rank == 0 ) {
148+
// ... initialize values
149+
MPI_Send(values, 3, mpi_dt_mystruct, 1, 0, MPI_COMM_WORLD);
150+
} else if ( rank == 1 )
151+
MPI_Recv(values, 3, mpi_dt_mystruct, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
152+
153+
154+
For more information, see section 3.12.1 of the MPI-1.1 Standard.
155+
156+
157+
NOTES
158+
-----
159+
160+
If an upperbound is set explicitly by using the MPI datatype MPI_UB, the
161+
corresponding index must be positive.
162+
163+
The MPI-1 Standard originally made vague statements about padding and
164+
alignment; this was intended to allow the simple definition of
165+
structures that could be sent with a count greater than one. For
166+
example,
167+
168+
.. code-block:: c
169+
170+
struct {int a; char b;} foo;
171+
172+
may have
173+
174+
.. code-block:: c
175+
176+
sizeof(foo) = sizeof(int) + sizeof(char);
177+
178+
defining the extent of a datatype as including an epsilon, which would
179+
have allowed an implementation to make the extent an MPI datatype for
180+
this structure equal to ``2*sizeof(int)``. However, since different systems
181+
might define different paddings, a clarification to the standard made
182+
epsilon zero. Thus, if you define a structure datatype and wish to send
183+
or receive multiple items, you should explicitly include an MPI_UB entry
184+
as the last member of the structure. See the above example.
78185

79186

80187
FORTRAN 77 NOTES

0 commit comments

Comments
 (0)