Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members
Teuchos::GlobalMPISession Class Reference

Initialize, finalize, and query the global MPI session. More...

#include <Teuchos_GlobalMPISession.hpp>

Public Member Functions

Public constructor and destructor
 GlobalMPISession (int *argc, char ***argv, std::ostream *out=&std::cout)
 Calls MPI_Init() if MPI is enabled. More...
 
 ~GlobalMPISession ()
 Call MPI_Finalize() if MPI is enabled. More...
 

Static Public Member Functions

Static functions
static bool mpiIsInitialized ()
 Return whether MPI was initialized. More...
 
static bool mpiIsFinalized ()
 Return whether MPI was already finalized. More...
 
static int getRank ()
 Returns the process rank relative to MPI_COMM_WORLD More...
 
static int getNProc ()
 Returns the number of processors relative to MPI_COMM_WORLD More...
 
static void barrier ()
 Perform a barrier so that all processes line up. More...
 
static int sum (int localVal)
 Sum a set of integers across processes. More...
 
static void allGather (int localVal, const ArrayView< int > &allVals)
 Global all-to-all of a set of integers across processes. More...
 

Detailed Description

Initialize, finalize, and query the global MPI session.

This class insulates basic main() program type of code from having to know if MPI is enabled or not. The typical use case is to replace an explicit call to MPI_Init() in your main() routine with creation of a GlobalMPISession instance. The instance's destructor (which in this case will be called at the end of main()) then calls MPI_Finalize(). So, instead of writing:

* int main () {
* (void) MPI_Init (&argc, &argv);
* // Your code goes here ...
* (void) MPI_Finalize ();
* return 0;
* }
*

you would write:

* #include <Teuchos_GlobalMPISession.hpp>
*
* int main () {
* Teuchos::GlobalMPISession (&argc, &argv, NULL);
* // Your code goes here ...
* return 0;
* }
*

This saves you from needing to remember to call MPI_Init() or MPI_Finalize(). GlobalMPISession cleverly checks whether MPI has been initialized already before calling MPI_Init(), so you can use it in your libraries without needing to know whether users have called MPI_Init() yet.

This class even works if you have not built Trilinos with MPI support. In that case, it behaves as if MPI_COMM_WORLD had one process, which is always the calling process. Thus, you can use this class to insulate your code from needing to know about MPI. You don't even have to include mpi.h, as long as your code doesn't directly use MPI routines or types. Teuchos implements wrappers for MPI communicators (see the Comm class and its subclasses in the TeuchosComm subpackage) which allow you to use a very very small subset of MPI functionality without needing to include mpi.h or depend on MPI in any way.

This class also contains the the most minimal of other members that are needed for only the most simplistic of tasks needed by other TeuchosCore software. For example, you can do a barrier or sum an int across processes. These are needed by the most basic operations involving output or determiing success or failure across processes for unit tests.

Examples:
ArrayRCP_test.cpp, CommandLineProcessor/cxx_main.cpp, FancyOutputting_test.cpp, and test/MemoryManagement/RCP_test.cpp.

Definition at line 102 of file Teuchos_GlobalMPISession.hpp.

Constructor & Destructor Documentation

Teuchos::GlobalMPISession::GlobalMPISession ( int *  argc,
char ***  argv,
std::ostream *  out = &std::cout 
)

Calls MPI_Init() if MPI is enabled.

Parameters
argc[in] Address of the argument passed into main(argc,argv). Same as the first argument of MPI_Init().
argv[in] Address of the argument passed into main(argc,argv). Same as the second argument of MPI_Init().
out[in] If out!=NULL, then a small message on each processor will be printed to this stream. The default is &std::cout.

If the option –teuchos-suppress-startup-banner is found, the this option will be removed from argv[] before being passed to MPI_Init(...) and the startup output message to *out will be suppressed.

If Teuchos was not built with MPI support, the constructor just prints a startup banner (unless the banner was suppressed – see previous paragraph).

Warning
This constructor can only be called once per executable. Otherwise, an error is printed to *out and an std::exception will be thrown!

Definition at line 60 of file Teuchos_GlobalMPISession.cpp.

Teuchos::GlobalMPISession::~GlobalMPISession ( )

Call MPI_Finalize() if MPI is enabled.

Definition at line 121 of file Teuchos_GlobalMPISession.cpp.

Member Function Documentation

bool Teuchos::GlobalMPISession::mpiIsInitialized ( )
static

Return whether MPI was initialized.

Definition at line 136 of file Teuchos_GlobalMPISession.cpp.

bool Teuchos::GlobalMPISession::mpiIsFinalized ( )
static

Return whether MPI was already finalized.

Definition at line 142 of file Teuchos_GlobalMPISession.cpp.

int Teuchos::GlobalMPISession::getRank ( )
static

Returns the process rank relative to MPI_COMM_WORLD

Returns 0 if MPI is not enabled.

Note, this function can be called even if the above constructor was never called so it is safe to use no matter how MPI_Init() got called (but it must have been called somewhere).

Examples:
CommandLineProcessor/cxx_main.cpp, and test/MemoryManagement/RCP_test.cpp.

Definition at line 148 of file Teuchos_GlobalMPISession.cpp.

int Teuchos::GlobalMPISession::getNProc ( )
static

Returns the number of processors relative to MPI_COMM_WORLD

Returns 1 if MPI is not enabled.

Note, this function can be called even if the above constructor was never called so it is safe to use no matter how MPI_Init() got called (but it must have been called somewhere).

Examples:
FancyOutputting_test.cpp.

Definition at line 155 of file Teuchos_GlobalMPISession.cpp.

void Teuchos::GlobalMPISession::barrier ( )
static

Perform a barrier so that all processes line up.

Definition at line 161 of file Teuchos_GlobalMPISession.cpp.

int Teuchos::GlobalMPISession::sum ( int  localVal)
static

Sum a set of integers across processes.

Parameters
localVal[in] Value on local process to sum across processes.

Definition at line 170 of file Teuchos_GlobalMPISession.cpp.

void Teuchos::GlobalMPISession::allGather ( int  localVal,
const ArrayView< int > &  allVals 
)
static

Global all-to-all of a set of integers across processes.

Parameters
localVal[in] Value on local process to pass to all processes.
allVals[out] Array (length getNProc()) that gives the value of localVal for each process. On output, allVals[k] is localVal for process k.

Definition at line 183 of file Teuchos_GlobalMPISession.cpp.


The documentation for this class was generated from the following files: