- •Contents
- •List of Figures
- •List of Tables
- •Acknowledgments
- •Introduction to MPI
- •Overview and Goals
- •Background of MPI-1.0
- •Background of MPI-1.1, MPI-1.2, and MPI-2.0
- •Background of MPI-1.3 and MPI-2.1
- •Background of MPI-2.2
- •Who Should Use This Standard?
- •What Platforms Are Targets For Implementation?
- •What Is Included In The Standard?
- •What Is Not Included In The Standard?
- •Organization of this Document
- •MPI Terms and Conventions
- •Document Notation
- •Naming Conventions
- •Semantic Terms
- •Data Types
- •Opaque Objects
- •Array Arguments
- •State
- •Named Constants
- •Choice
- •Addresses
- •Language Binding
- •Deprecated Names and Functions
- •Fortran Binding Issues
- •C Binding Issues
- •C++ Binding Issues
- •Functions and Macros
- •Processes
- •Error Handling
- •Implementation Issues
- •Independence of Basic Runtime Routines
- •Interaction with Signals
- •Examples
- •Point-to-Point Communication
- •Introduction
- •Blocking Send and Receive Operations
- •Blocking Send
- •Message Data
- •Message Envelope
- •Blocking Receive
- •Return Status
- •Passing MPI_STATUS_IGNORE for Status
- •Data Type Matching and Data Conversion
- •Type Matching Rules
- •Type MPI_CHARACTER
- •Data Conversion
- •Communication Modes
- •Semantics of Point-to-Point Communication
- •Buffer Allocation and Usage
- •Nonblocking Communication
- •Communication Request Objects
- •Communication Initiation
- •Communication Completion
- •Semantics of Nonblocking Communications
- •Multiple Completions
- •Non-destructive Test of status
- •Probe and Cancel
- •Persistent Communication Requests
- •Send-Receive
- •Null Processes
- •Datatypes
- •Derived Datatypes
- •Type Constructors with Explicit Addresses
- •Datatype Constructors
- •Subarray Datatype Constructor
- •Distributed Array Datatype Constructor
- •Address and Size Functions
- •Lower-Bound and Upper-Bound Markers
- •Extent and Bounds of Datatypes
- •True Extent of Datatypes
- •Commit and Free
- •Duplicating a Datatype
- •Use of General Datatypes in Communication
- •Correct Use of Addresses
- •Decoding a Datatype
- •Examples
- •Pack and Unpack
- •Canonical MPI_PACK and MPI_UNPACK
- •Collective Communication
- •Introduction and Overview
- •Communicator Argument
- •Applying Collective Operations to Intercommunicators
- •Barrier Synchronization
- •Broadcast
- •Example using MPI_BCAST
- •Gather
- •Examples using MPI_GATHER, MPI_GATHERV
- •Scatter
- •Examples using MPI_SCATTER, MPI_SCATTERV
- •Example using MPI_ALLGATHER
- •All-to-All Scatter/Gather
- •Global Reduction Operations
- •Reduce
- •Signed Characters and Reductions
- •MINLOC and MAXLOC
- •All-Reduce
- •Process-local reduction
- •Reduce-Scatter
- •MPI_REDUCE_SCATTER_BLOCK
- •MPI_REDUCE_SCATTER
- •Scan
- •Inclusive Scan
- •Exclusive Scan
- •Example using MPI_SCAN
- •Correctness
- •Introduction
- •Features Needed to Support Libraries
- •MPI's Support for Libraries
- •Basic Concepts
- •Groups
- •Contexts
- •Intra-Communicators
- •Group Management
- •Group Accessors
- •Group Constructors
- •Group Destructors
- •Communicator Management
- •Communicator Accessors
- •Communicator Constructors
- •Communicator Destructors
- •Motivating Examples
- •Current Practice #1
- •Current Practice #2
- •(Approximate) Current Practice #3
- •Example #4
- •Library Example #1
- •Library Example #2
- •Inter-Communication
- •Inter-communicator Accessors
- •Inter-communicator Operations
- •Inter-Communication Examples
- •Caching
- •Functionality
- •Communicators
- •Windows
- •Datatypes
- •Error Class for Invalid Keyval
- •Attributes Example
- •Naming Objects
- •Formalizing the Loosely Synchronous Model
- •Basic Statements
- •Models of Execution
- •Static communicator allocation
- •Dynamic communicator allocation
- •The General case
- •Process Topologies
- •Introduction
- •Virtual Topologies
- •Embedding in MPI
- •Overview of the Functions
- •Topology Constructors
- •Cartesian Constructor
- •Cartesian Convenience Function: MPI_DIMS_CREATE
- •General (Graph) Constructor
- •Distributed (Graph) Constructor
- •Topology Inquiry Functions
- •Cartesian Shift Coordinates
- •Partitioning of Cartesian structures
- •Low-Level Topology Functions
- •An Application Example
- •MPI Environmental Management
- •Implementation Information
- •Version Inquiries
- •Environmental Inquiries
- •Tag Values
- •Host Rank
- •IO Rank
- •Clock Synchronization
- •Memory Allocation
- •Error Handling
- •Error Handlers for Communicators
- •Error Handlers for Windows
- •Error Handlers for Files
- •Freeing Errorhandlers and Retrieving Error Strings
- •Error Codes and Classes
- •Error Classes, Error Codes, and Error Handlers
- •Timers and Synchronization
- •Startup
- •Allowing User Functions at Process Termination
- •Determining Whether MPI Has Finished
- •Portable MPI Process Startup
- •The Info Object
- •Process Creation and Management
- •Introduction
- •The Dynamic Process Model
- •Starting Processes
- •The Runtime Environment
- •Process Manager Interface
- •Processes in MPI
- •Starting Processes and Establishing Communication
- •Reserved Keys
- •Spawn Example
- •Manager-worker Example, Using MPI_COMM_SPAWN.
- •Establishing Communication
- •Names, Addresses, Ports, and All That
- •Server Routines
- •Client Routines
- •Name Publishing
- •Reserved Key Values
- •Client/Server Examples
- •Ocean/Atmosphere - Relies on Name Publishing
- •Simple Client-Server Example.
- •Other Functionality
- •Universe Size
- •Singleton MPI_INIT
- •MPI_APPNUM
- •Releasing Connections
- •Another Way to Establish MPI Communication
- •One-Sided Communications
- •Introduction
- •Initialization
- •Window Creation
- •Window Attributes
- •Communication Calls
- •Examples
- •Accumulate Functions
- •Synchronization Calls
- •Fence
- •General Active Target Synchronization
- •Lock
- •Assertions
- •Examples
- •Error Handling
- •Error Handlers
- •Error Classes
- •Semantics and Correctness
- •Atomicity
- •Progress
- •Registers and Compiler Optimizations
- •External Interfaces
- •Introduction
- •Generalized Requests
- •Examples
- •Associating Information with Status
- •MPI and Threads
- •General
- •Initialization
- •Introduction
- •File Manipulation
- •Opening a File
- •Closing a File
- •Deleting a File
- •Resizing a File
- •Preallocating Space for a File
- •Querying the Size of a File
- •Querying File Parameters
- •File Info
- •Reserved File Hints
- •File Views
- •Data Access
- •Data Access Routines
- •Positioning
- •Synchronism
- •Coordination
- •Data Access Conventions
- •Data Access with Individual File Pointers
- •Data Access with Shared File Pointers
- •Noncollective Operations
- •Collective Operations
- •Seek
- •Split Collective Data Access Routines
- •File Interoperability
- •Datatypes for File Interoperability
- •Extent Callback
- •Datarep Conversion Functions
- •Matching Data Representations
- •Consistency and Semantics
- •File Consistency
- •Random Access vs. Sequential Files
- •Progress
- •Collective File Operations
- •Type Matching
- •Logical vs. Physical File Layout
- •File Size
- •Examples
- •Asynchronous I/O
- •I/O Error Handling
- •I/O Error Classes
- •Examples
- •Subarray Filetype Constructor
- •Requirements
- •Discussion
- •Logic of the Design
- •Examples
- •MPI Library Implementation
- •Systems with Weak Symbols
- •Systems Without Weak Symbols
- •Complications
- •Multiple Counting
- •Linker Oddities
- •Multiple Levels of Interception
- •Deprecated Functions
- •Deprecated since MPI-2.0
- •Deprecated since MPI-2.2
- •Language Bindings
- •Overview
- •Design
- •C++ Classes for MPI
- •Class Member Functions for MPI
- •Semantics
- •C++ Datatypes
- •Communicators
- •Exceptions
- •Mixed-Language Operability
- •Problems With Fortran Bindings for MPI
- •Problems Due to Strong Typing
- •Problems Due to Data Copying and Sequence Association
- •Special Constants
- •Fortran 90 Derived Types
- •A Problem with Register Optimization
- •Basic Fortran Support
- •Extended Fortran Support
- •The mpi Module
- •No Type Mismatch Problems for Subroutines with Choice Arguments
- •Additional Support for Fortran Numeric Intrinsic Types
- •Language Interoperability
- •Introduction
- •Assumptions
- •Initialization
- •Transfer of Handles
- •Status
- •MPI Opaque Objects
- •Datatypes
- •Callback Functions
- •Error Handlers
- •Reduce Operations
- •Addresses
- •Attributes
- •Extra State
- •Constants
- •Interlanguage Communication
- •Language Bindings Summary
- •Groups, Contexts, Communicators, and Caching Fortran Bindings
- •External Interfaces C++ Bindings
- •Change-Log
- •Bibliography
- •Examples Index
- •MPI Declarations Index
- •MPI Function Index
112 |
CHAPTER 4. DATATYPES |
1
2
3
4
5
6
7
8
9
10
Constructor argument |
C & C++ location |
Fortran location |
lb |
a[0] |
A(1) |
extent |
a[1] |
A(2) |
oldtype |
d[0] |
D(1) |
|
|
|
and ni = 0, na = 2, nd = 1.
4.1.14 Examples
The following examples illustrate the use of derived datatypes.
11 Example 4.13 Send and receive a section of a 3D array.
12
13REAL a(100,100,100), e(9,9,9)
14INTEGER oneslice, twoslice, threeslice, sizeofreal, myrank, ierr
15INTEGER status(MPI_STATUS_SIZE)
16 |
|
|
17 |
C |
extract the section a(1:17:2, 3:11, 2:10) |
|
|
|
18 |
C |
and store it in e(:,:,:). |
|
|
|
19 |
|
|
20 |
CALL MPI_COMM_RANK(MPI_COMM_WORLD, myrank, ierr) |
|
|
21 |
|
22
23
CALL MPI_TYPE_EXTENT( MPI_REAL, sizeofreal, ierr)
24C create datatype for a 1D section
25CALL MPI_TYPE_VECTOR( 9, 1, 2, MPI_REAL, oneslice, ierr)
26
27C create datatype for a 2D section
28CALL MPI_TYPE_HVECTOR(9, 1, 100*sizeofreal, oneslice, twoslice, ierr)
29
30C create datatype for the entire section
31CALL MPI_TYPE_HVECTOR( 9, 1, 100*100*sizeofreal, twoslice,
32 |
threeslice, ierr) |
33
34CALL MPI_TYPE_COMMIT( threeslice, ierr)
35CALL MPI_SENDRECV(a(1,3,2), 1, threeslice, myrank, 0, e, 9*9*9,
36
37
38
MPI_REAL, myrank, 0, MPI_COMM_WORLD, status, ierr)
Example 4.14 Copy the (strictly) lower triangular part of a matrix.
39REAL a(100,100), b(100,100)
40INTEGER disp(100), blocklen(100), ltype, myrank, ierr
41INTEGER status(MPI_STATUS_SIZE)
42
43C copy lower triangular part of array a
44C onto lower triangular part of array b
45
46 |
CALL MPI_COMM_RANK(MPI_COMM_WORLD, myrank, ierr) |
|
|
47 |
|
48 |
C |
compute start and size of each column |
|
4.1. DERIVED DATATYPES |
113 |
DO i=1, 100
disp(i) = 100*(i-1) + i blocklen(i) = 100-i
END DO
Ccreate datatype for lower triangular part
CALL MPI_TYPE_INDEXED( 100, blocklen, disp, MPI_REAL, ltype, ierr)
CALL MPI_TYPE_COMMIT(ltype, ierr)
CALL MPI_SENDRECV( a, 1, ltype, myrank, 0, b, 1,
ltype, myrank, 0, MPI_COMM_WORLD, status, ierr)
Example 4.15 Transpose a matrix.
REAL a(100,100), b(100,100)
INTEGER row, xpose, sizeofreal, myrank, ierr
INTEGER status(MPI_STATUS_SIZE)
Ctranspose matrix a onto b
CALL MPI_COMM_RANK(MPI_COMM_WORLD, myrank, ierr)
CALL MPI_TYPE_EXTENT( MPI_REAL, sizeofreal, ierr)
Ccreate datatype for one row
CALL MPI_TYPE_VECTOR( 100, 1, 100, MPI_REAL, row, ierr)
Ccreate datatype for matrix in row-major order
CALL MPI_TYPE_HVECTOR( 100, 1, sizeofreal, row, xpose, ierr)
CALL MPI_TYPE_COMMIT( xpose, ierr)
Csend matrix in row-major order and receive in column major order CALL MPI_SENDRECV( a, 1, xpose, myrank, 0, b, 100*100,
MPI_REAL, myrank, 0, MPI_COMM_WORLD, status, ierr)
Example 4.16 Another approach to the transpose problem:
REAL a(100,100), b(100,100)
INTEGER disp(2), blocklen(2), type(2), row, row1, sizeofreal INTEGER myrank, ierr
INTEGER status(MPI_STATUS_SIZE)
CALL MPI_COMM_RANK(MPI_COMM_WORLD, myrank, ierr)
Ctranspose matrix a onto b
CALL MPI_TYPE_EXTENT( MPI_REAL, sizeofreal, ierr)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
114 |
CHAPTER 4. DATATYPES |
1C create datatype for one row
2CALL MPI_TYPE_VECTOR( 100, 1, 100, MPI_REAL, row, ierr)
3
4C create datatype for one row, with the extent of one real number
5
6
7
8
9
disp(1) = 0
disp(2) = sizeofreal type(1) = row type(2) = MPI_UB blocklen(1) = 1
10blocklen(2) = 1
11CALL MPI_TYPE_STRUCT( 2, blocklen, disp, type, row1, ierr)
12
13
14
CALL MPI_TYPE_COMMIT( row1, ierr)
15C send 100 rows and receive in column major order
16CALL MPI_SENDRECV( a, 100, row1, myrank, 0, b, 100*100,
17
18
MPI_REAL, myrank, 0, MPI_COMM_WORLD, status, ierr)
19 |
Example 4.17 We manipulate an array of structures. |
|
20
21struct Partstruct
22{
23
24
25
26
int |
class; |
/* particle |
class */ |
double |
d[6]; |
/* particle |
coordinates */ |
char |
b[7]; |
/* some additional information */ |
};
27 |
|
|
|
28 |
struct Partstruct |
particle[1000]; |
|
29 |
|
|
|
30 |
int |
i, dest, rank, tag; |
|
31 |
MPI_Comm |
comm; |
|
32 |
|
|
|
33 |
|
|
|
34 |
/* build datatype describing structure */ |
||
35 |
|
|
|
36MPI_Datatype Particletype;
37MPI_Datatype type[3] = {MPI_INT, MPI_DOUBLE, MPI_CHAR};
38 |
int |
blocklen[3] = {1, 6, 7}; |
39 |
MPI_Aint |
disp[3]; |
40 |
MPI_Aint |
base; |
41 |
|
|
42 |
|
|
43 |
/* compute displacements of structure components */ |
|
44 |
|
|
45MPI_Address( particle, disp);
46MPI_Address( particle[0].d, disp+1);
47MPI_Address( particle[0].b, disp+2);
48base = disp[0];
4.1. DERIVED DATATYPES |
115 |
for (i=0; i < 3; i++) disp[i] -= base;
MPI_Type_struct( 3, blocklen, disp, type, &Particletype);
/* If compiler does padding in mysterious ways, the following may be safer */
MPI_Datatype |
type1[4] = {MPI_INT, MPI_DOUBLE, MPI_CHAR, MPI_UB}; |
|
int |
blocklen1[4] = {1, 6, 7, |
1}; |
MPI_Aint |
disp1[4]; |
|
/* compute displacements of structure |
components */ |
MPI_Address( particle, disp1); MPI_Address( particle[0].d, disp1+1); MPI_Address( particle[0].b, disp1+2); MPI_Address( particle+1, disp1+3); base = disp1[0];
for (i=0; i < 4; i++) disp1[i] -= base;
/* build datatype describing structure */
MPI_Type_struct( 4, blocklen1, disp1, type1, &Particletype);
/* 4.1:
send the entire array */
MPI_Type_commit( &Particletype);
MPI_Send( particle, 1000, Particletype, dest, tag, comm);
|
/* 4.2: |
|
|
|
send only the entries |
of class zero particles, |
|
|
preceded by the number of such entries */ |
||
MPI_Datatype Zparticles; |
/* |
datatype describing all particles |
|
|
|
|
with class zero (needs to be recomputed |
|
|
|
if classes change) */ |
MPI_Datatype Ztype; |
|
|
|
MPI_Aint |
zdisp[1000]; |
|
|
int |
zblock[1000], j, |
k; |
|
int |
zzblock[2] = {1,1}; |
||
MPI_Aint |
zzdisp[2]; |
|
|
MPI_Datatype zztype[2];
/* compute displacements of class zero particles */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
1
2
3
4
5
6
7
8
9
116 |
CHAPTER 4. DATATYPES |
j = 0; |
|
for(i=0; i < 1000; i++) |
|
if (particle[i].class == 0) |
|
{ |
|
zdisp[j] = i; |
|
zblock[j] = 1; |
|
j++; |
|
} |
|
10/* create datatype for class zero particles */
11MPI_Type_indexed( j, zblock, zdisp, Particletype, &Zparticles);
12
13/* prepend particle count */
14MPI_Address(&j, zzdisp);
15MPI_Address(particle, zzdisp+1);
16zztype[0] = MPI_INT;
17zztype[1] = Zparticles;
18MPI_Type_struct(2, zzblock, zzdisp, zztype, &Ztype);
19
20MPI_Type_commit( &Ztype);
21MPI_Send( MPI_BOTTOM, 1, Ztype, dest, tag, comm);
22
23
24
25
/* A probably more efficient way of defining Zparticles */
26/* consecutive particles with index zero are handled as one block */
27j=0;
28for (i=0; i < 1000; i++)
29if (particle[i].index == 0)
30{
31for (k=i+1; (k < 1000)&&(particle[k].index == 0) ; k++);
32zdisp[j] = i;
33zblock[j] = k-i;
34j++;
35i = k;
36}
37MPI_Type_indexed( j, zblock, zdisp, Particletype, &Zparticles);
38
39
40
41
42
/* 4.3:
send the first two coordinates of all entries */
43 |
MPI_Datatype Allpairs; |
/* datatype for all pairs of coordinates */ |
|
44 |
|
45 |
MPI_Aint sizeofentry; |
|
|
46 |
|
47 |
MPI_Type_extent( Particletype, &sizeofentry); |
|
48
4.1. DERIVED DATATYPES |
117 |
/* sizeofentry can also be computed by subtracting the address of particle[0] from the address of particle[1] */
MPI_Type_hvector( 1000, 2, sizeofentry, MPI_DOUBLE, &Allpairs); MPI_Type_commit( &Allpairs);
MPI_Send( particle[0].d, 1, Allpairs, dest, tag, comm);
/* an alternative solution to 4.3 */
MPI_Datatype Onepair; /* datatype for one pair of coordinates, with the extent of one particle entry */
MPI_Aint disp2[3];
MPI_Datatype type2[3] = {MPI_LB, MPI_DOUBLE, MPI_UB}; int blocklen2[3] = {1, 2, 1};
MPI_Address( particle, disp2); MPI_Address( particle[0].d, disp2+1); MPI_Address( particle+1, disp2+2); base = disp2[0];
for (i=0; i<2; i++) disp2[i] -= base;
MPI_Type_struct( 3, blocklen2, disp2, type2, &Onepair);
MPI_Type_commit( &Onepair);
MPI_Send( particle[0].d, 1000, Onepair, dest, tag, comm);
Example 4.18 The same manipulations as in the previous example, but use absolute addresses in datatypes.
struct Partstruct
{
int class; double d[6]; char b[7];
};
struct Partstruct particle[1000];
/* build datatype describing first array entry */
MPI_Datatype |
Particletype; |
MPI_Datatype |
type[3] = {MPI_INT, MPI_DOUBLE, MPI_CHAR}; |
int |
block[3] = {1, 6, 7}; |
MPI_Aint |
disp[3]; |
MPI_Address( |
particle, disp); |
MPI_Address( |
particle[0].d, disp+1); |
MPI_Address( |
particle[0].b, disp+2); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
118 |
CHAPTER 4. DATATYPES |
1
2
MPI_Type_struct( 3, block, disp, type, &Particletype);
3/* Particletype describes first array entry -- using absolute
4addresses */
5 |
|
6 |
/* 5.1: |
|
|
7 |
send the entire array */ |
|
|
8 |
|
9MPI_Type_commit( &Particletype);
10 |
MPI_Send( MPI_BOTTOM, 1000, Particletype, dest, tag, comm); |
|
|
||
11 |
|
|
12 |
|
|
13 |
|
/* 5.2: |
|
|
|
14 |
|
send the entries of class zero, |
|
|
|
15 |
|
preceded by the number of such entries */ |
|
|
|
16 |
|
|
17 |
MPI_Datatype Zparticles, Ztype; |
|
|
||
18 |
|
|
19 |
MPI_Aint |
zdisp[1000]; |
|
||
20 |
int |
zblock[1000], i, j, k; |
|
||
21 |
int |
zzblock[2] = {1,1}; |
|
||
22 |
MPI_Datatype zztype[2]; |
|
|
||
23 |
MPI_Aint |
zzdisp[2]; |
|
||
24 |
|
|
25j=0;
26for (i=0; i < 1000; i++)
27if (particle[i].index == 0)
28{
29for (k=i+1; (k < 1000)&&(particle[k].index == 0) ; k++);
30zdisp[j] = i;
31zblock[j] = k-i;
32j++;
33i = k;
34}
35MPI_Type_indexed( j, zblock, zdisp, Particletype, &Zparticles);
36/* Zparticles describe particles with class zero, using
37their absolute addresses*/
38
39/* prepend particle count */
40MPI_Address(&j, zzdisp);
41zzdisp[1] = MPI_BOTTOM;
42zztype[0] = MPI_INT;
43zztype[1] = Zparticles;
44MPI_Type_struct(2, zzblock, zzdisp, zztype, &Ztype);
45
46MPI_Type_commit( &Ztype);
47MPI_Send( MPI_BOTTOM, 1, Ztype, dest, tag, comm);
48
4.1. DERIVED DATATYPES |
119 |
Example 4.19 Handling of unions.
union {
int ival; float fval; } u[1000];
int utype;
/* All entries of u have identical type; variable utype keeps track of their current type */
MPI_Datatype |
type[2]; |
int |
blocklen[2] = {1,1}; |
MPI_Aint |
disp[2]; |
MPI_Datatype |
mpi_utype[2]; |
MPI_Aint |
i,j; |
/* compute an MPI datatype for each possible union type; assume values are left-aligned in union storage. */
MPI_Address( u, &i); MPI_Address( u+1, &j); disp[0] = 0; disp[1] = j-i; type[1] = MPI_UB;
type[0] = MPI_INT;
MPI_Type_struct(2, blocklen, disp, type, &mpi_utype[0]);
type[0] = MPI_FLOAT;
MPI_Type_struct(2, blocklen, disp, type, &mpi_utype[1]);
for(i=0; i<2; i++) MPI_Type_commit(&mpi_utype[i]);
/* actual communication */
MPI_Send(u, 1000, mpi_utype[utype], dest, tag, comm);
Example 4.20 This example shows how a datatype can be decoded. The routine printdatatype prints out the elements of the datatype. Note the use of MPI_Type_free for datatypes that are not prede ned.
/*
Example of decoding a datatype.
Returns 0 if the datatype is predefined, 1 otherwise */
#include <stdio.h> #include <stdlib.h>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
1
2
3
4
5
6
7
8
9
10
11
120 |
CHAPTER 4. DATATYPES |
#include "mpi.h" |
|
int printdatatype( MPI_Datatype datatype ) |
|
{ |
|
int *array_of_ints; |
|
MPI_Aint *array_of_adds; |
|
MPI_Datatype *array_of_dtypes; |
|
int num_ints, num_adds, num_dtypes, combiner; |
|
int i; |
|
MPI_Type_get_envelope( datatype,
&num_ints, &num_adds, &num_dtypes, &combiner );
12switch (combiner) {
13case MPI_COMBINER_NAMED:
14printf( "Datatype is named:" );
15/* To print the specific type, we can match against the
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
predefined forms. We can NOT use a switch statement here We could also use MPI_TYPE_GET_NAME if we prefered to use names that the user may have changed.
*/ |
|
|
|
if |
(datatype |
== MPI_INT) |
printf( "MPI_INT\n" ); |
else if (datatype |
== MPI_DOUBLE) printf( "MPI_DOUBLE\n" ); |
||
... else test for |
other types ... |
|
|
return 0; |
|
|
|
break; |
|
|
|
case MPI_COMBINER_STRUCT: |
|
||
case MPI_COMBINER_STRUCT_INTEGER: |
|
||
printf( "Datatype |
is struct containing" ); |
||
array_of_ints = |
(int *)malloc( num_ints * sizeof(int) ); |
||
array_of_adds = |
|
|
|
|
(MPI_Aint *) malloc( num_adds * sizeof(MPI_Aint) ); |
||
array_of_dtypes = |
(MPI_Datatype *) |
malloc( num_dtypes * sizeof(MPI_Datatype) ); MPI_Type_get_contents( datatype, num_ints, num_adds, num_dtypes,
array_of_ints, array_of_adds, array_of_dtypes ); printf( " %d datatypes:\n", array_of_ints[0] );
for (i=0; i<array_of_ints[0]; i++) {
printf( "blocklength %d, displacement %ld, type:\n", array_of_ints[i+1], array_of_adds[i] );
if (printdatatype( array_of_dtypes[i] )) { /* Note that we free the type ONLY if it
is not predefined */ MPI_Type_free( &array_of_dtypes[i] );
}
}
free( array_of_ints ); free( array_of_adds ); free( array_of_dtypes ); break;