- •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
Chapter 16
Language Bindings
16.1 C++
16.1.1 Overview
The C++ language bindings have been deprecated.
There are some issues speci c to C++ that must be considered in the design of an interface that go beyond the simple description of language bindings. In particular, in C++, we must be concerned with the design of objects and their interfaces, rather than just the design of a language-speci c functional interface to MPI. Fortunately, the design of MPI was based on the notion of objects, so a natural set of classes is already part of MPI.
MPI-2 includes C++ bindings as part of its function speci cations. In some cases, MPI-2 provides new names for the C bindings of MPI-1 functions. In this case, the C++ binding matches the new C name | there is no binding for the deprecated name.
16.1.2 Design
The C++ language interface for MPI is designed according to the following criteria:
1.The C++ language interface consists of a small set of classes with a lightweight functional interface to MPI. The classes are based upon the fundamental MPI object types (e.g., communicator, group, etc.).
2.The MPI C++ language bindings provide a semantically correct interface to MPI.
3.To the greatest extent possible, the C++ bindings for MPI functions are member functions of MPI classes.
Rationale. Providing a lightweight set of MPI objects that correspond to the basic MPI types is the best t to MPI's implicit object-based design; methods can be supplied for these objects to realize MPI functionality. The existing C bindings can be used in C++ programs, but much of the expressive power of the C++ language is forfeited. On the other hand, while a comprehensive class library would make user programming more elegant, such a library it is not suitable as a language binding for MPI since a binding must provide a direct and unambiguous mapping to the speci ed functionality of MPI. (End of rationale.)
.
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
467
1
2
3
4
5
6
7
8
9
468 |
CHAPTER 16. LANGUAGE BINDINGS |
16.1.3 C++ Classes for MPI
All MPI classes, constants, and functions are declared within the scope of an MPI namespace. Thus, instead of the MPI_ pre x that is used in C and Fortran, MPI functions essentially have an MPI:: pre x.
The members of the MPI namespace are those classes corresponding to objects implicitly used by MPI. An abbreviated de nition of the MPI namespace and its member classes is as follows:
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
namespace MPI { |
|
|
class Comm |
|
{...}; |
class Intracomm : public Comm |
{...}; |
|
class Graphcomm : public Intracomm |
{...}; |
|
class Distgraphcomm : public Intracomm |
{...}; |
|
class Cartcomm |
: public Intracomm |
{...}; |
class Intercomm : public Comm |
{...}; |
|
class Datatype |
|
{...}; |
class Errhandler |
|
{...}; |
class Exception |
|
{...}; |
class File |
|
{...}; |
class Group |
|
{...}; |
class Info |
|
{...}; |
class Op |
|
{...}; |
class Request |
|
{...}; |
class Prequest |
: public Request |
{...}; |
class Grequest |
: public Request |
{...}; |
class Status |
|
{...}; |
class Win |
|
{...}; |
};
29
30
Note that there are a small number of derived classes, and that virtual inheritance is
31
not used.
32
33
16.1.4 Class Member Functions for MPI
34
35Besides the member functions which constitute the C++ language bindings for MPI, the
36C++ language interface has additional functions (as required by the C++ language). In
37particular, the C++ language interface must provide a constructor and destructor, an
38assignment operator, and comparison operators.
39The complete set of C++ language bindings for MPI is presented in Annex A.4. The
40bindings take advantage of some important C++ features, such as references and const.
41Declarations (which apply to all MPI member classes) for construction, destruction, copying,
42assignment, comparison, and mixed-language operability are also provided.
43Except where indicated, all non-static member functions (except for constructors and
44the assignment operator) of MPI member classes are virtual functions.
45
46Rationale. Providing virtual member functions is an important part of design for
47inheritance. Virtual functions can be bound at run-time, which allows users of libraries
48to re-de ne the behavior of objects already contained in a library. There is a small
16.1. C++ |
469 |
performance penalty that must be paid (the virtual function must be looked up before it can be called). However, users concerned about this performance penalty can force compile-time function binding. (End of rationale.)
Example 16.1 Example showing a derived MPI class.
class foo_comm : public MPI::Intracomm { public:
void Send(const void* buf, int count, const MPI::Datatype& type, int dest, int tag) const
1
2
3
4
5
6
7
8
9
10
11
{
//Class library functionality MPI::Intracomm::Send(buf, count, type, dest, tag);
//More class library functionality
12
13
14
15
}
};
Advice to implementors. Implementors must be careful to avoid unintended side e ects from class libraries that use inheritance, especially in layered implementations. For example, if MPI_BCAST is implemented by repeated calls to MPI_SEND or MPI_RECV, the behavior of MPI_BCAST cannot be changed by derived communicator classes that might rede ne MPI_SEND or MPI_RECV. The implementation of MPI_BCAST must explicitly use the MPI_SEND (or MPI_RECV) of the base
MPI::Comm class. (End of advice to implementors.)
16.1.5 Semantics
The semantics of the member functions constituting the C++ language binding for MPI are speci ed by the MPI function description itself. Here, we specify the semantics for those portions of the C++ language interface that are not part of the language binding. In this subsection, functions are prototyped using the type MPI::hCLASSi rather than listing each function for every MPI class; the word hCLASSi can be replaced with any valid MPI class name (e.g., Group), except as noted.
Construction / Destruction The default constructor and destructor are prototyped as follows:
f MPI::<CLASS>() (binding deprecated, see Section 15.2) g
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
f MPI::<CLASS>() (binding deprecated, see Section 15.2) g |
39 |
|
40 |
In terms of construction and destruction, opaque MPI user level objects behave like
41
handles. Default constructors for all MPI objects except MPI::Status create corresponding
42
MPI::*_NULL handles. That is, when an MPI object is instantiated, comparing it with its
43
corresponding MPI::*_NULL object will return true. The default constructors do not create
44
new MPI opaque objects. Some classes have a member function Create() for this purpose.
45
46
Example 16.2 In the following code fragment, the test will return true and the message
47
will be sent to cout.
48
1
2
3
4
470 |
CHAPTER 16. LANGUAGE BINDINGS |
void foo()
{
MPI::Intracomm bar;
5
6
7
8
if (bar == MPI::COMM_NULL)
cout << "bar is MPI::COMM_NULL" << endl;
}
9The destructor for each MPI user level object does not invoke the corresponding
10 |
MPI_*_FREE function (if it exists). |
|
11
12Rationale. MPI_*_FREE functions are not automatically invoked for the following
13reasons:
14
1. Automatic destruction contradicts the shallow-copy semantics of the MPI classes.
15
162. The model put forth in MPI makes memory allocation and deallocation the re-
17sponsibility of the user, not the implementation.
183. Calling MPI_*_FREE upon destruction could have unintended side e ects, in-
19cluding triggering collective operations (this also a ects the copy, assignment,
20and construction semantics). In the following example, we would want neither
21foo_comm nor bar_comm to automatically invoke MPI_*_FREE upon exit from
22the function.
23
24
25
26
27
28
29
30
31
32
33
34
35
void example_function()
{
MPI::Intracomm foo_comm(MPI::COMM_WORLD), bar_comm; bar_comm = MPI::COMM_WORLD.Dup();
// rest of function
}
(End of rationale.)
Copy / Assignment The copy constructor and assignment operator are prototyped as follows:
f MPI::<CLASS>(const MPI::<CLASS>& data) (binding deprecated, see Section 15.2) g
36 |
f MPI::<CLASS>& MPI::<CLASS>::operator=(const MPI::<CLASS>& data) (binding |
37 |
|
|
deprecated, see Section 15.2) g |
38
39In terms of copying and assignment, opaque MPI user level objects behave like handles.
40Copy constructors perform handle-based (shallow) copies. MPI::Status objects are excep-
41tions to this rule. These objects perform deep copies for assignment and copy construction.
42
43
44
45
46
Advice to implementors. Each MPI user level object is likely to contain, by value or by reference, implementation-dependent state information. The assignment and copying of MPI object handles may simply copy this value (or reference). (End of advice to implementors.)
47
48