PSCF v1.1
Namespaces | Functions
MpiSendRecv.h File Reference

This file contains templates for global functions send<T>, recv<T> and bcast<T>. More...

#include <util/global.h>
#include <util/mpi/MpiTraits.h>
#include <util/containers/DArray.h>
#include <util/containers/DMatrix.h>

Go to the source code of this file.

Namespaces

namespace  Util
 Utility classes for scientific computation.
 

Functions

template<typename T >
void Util::send (MPI::Comm &comm, T &data, int dest, int tag)
 Send a single T value. More...
 
template<typename T >
void Util::recv (MPI::Comm &comm, T &data, int source, int tag)
 Receive a single T value. More...
 
template<typename T >
void Util::bcast (MPI::Intracomm &comm, T &data, int root)
 Broadcast a single T value. More...
 
template<typename T >
void Util::send (MPI::Comm &comm, T *array, int count, int dest, int tag)
 Send a C-array of T values. More...
 
template<typename T >
void Util::recv (MPI::Comm &comm, T *array, int count, int source, int tag)
 Receive a C-array of T objects. More...
 
template<typename T >
void Util::bcast (MPI::Intracomm &comm, T *array, int count, int root)
 Broadcast a C-array of T objects. More...
 
template<typename T >
void Util::send (MPI::Comm &comm, DArray< T > &array, int count, int dest, int tag)
 Send a DArray<T> container. More...
 
template<typename T >
void Util::recv (MPI::Comm &comm, DArray< T > &array, int count, int source, int tag)
 Receive a DArray<T> container. More...
 
template<typename T >
void Util::bcast (MPI::Intracomm &comm, DArray< T > &array, int count, int root)
 Broadcast a DArray<T> container. More...
 
template<typename T >
void Util::send (MPI::Comm &comm, DMatrix< T > &matrix, int m, int n, int dest, int tag)
 Send a DMatrix<T> container. More...
 
template<typename T >
void Util::recv (MPI::Comm &comm, DMatrix< T > &matrix, int m, int n, int source, int tag)
 Receive a DMatrix<T> container. More...
 
template<typename T >
void Util::bcast (MPI::Intracomm &comm, DMatrix< T > &matrix, int m, int n, int root)
 Broadcast a DMatrix<T> container. More...
 
template<>
void Util::send< bool > (MPI::Comm &comm, bool &data, int dest, int tag)
 Explicit specialization of send for bool data. More...
 
template<>
void Util::recv< bool > (MPI::Comm &comm, bool &data, int source, int tag)
 Explicit specialization of recv for bool data. More...
 
template<>
void Util::bcast< bool > (MPI::Intracomm &comm, bool &data, int root)
 Explicit specialization of bcast for bool data. More...
 
template<>
void Util::send< std::string > (MPI::Comm &comm, std::string &data, int dest, int tag)
 Explicit specialization of send for std::string data. More...
 
template<>
void Util::recv< std::string > (MPI::Comm &comm, std::string &data, int source, int tag)
 Explicit specialization of recv for std::string data. More...
 
template<>
void Util::bcast< std::string > (MPI::Intracomm &comm, std::string &data, int root)
 Explicit specialization of bcast for std::string data. More...
 

Detailed Description

This file contains templates for global functions send<T>, recv<T> and bcast<T>.

These are wrappers for the MPI send, recv (receive), and bcast (broadcast) functions. Overloaded forms of these functions are provided to transmit single values and arrays. The main difference between the wrappers and the underlying MPI functions is that the wrapper functions do not require an MPI type handle as a parameter. Instead, the MPI type associated with C++ type T (the template parameter) is inferred by the function implementations, by methods that are described below. The most important advantage of this is that it allows the wrapper functions to be used within other templates that take type T as a template parameter. The corresponding MPI methods cannot be used in generic templates because they require MPI type handle parameters that have different values for different date types.

The implementation of the templates send<T>, recv<T>, bcast<T> for single values of type T rely on the existence of an associated explicit specialization of the class template MpiTraits<typename T>. If it exists, the class MpiTraits<T> maps C++ type T onto an associated MPI type. Each specialization MpiTraits<T> has a static member MpiTraits<T>::type that contains an opaque handle for the MPI type associated with C++ type T. Explicit specializations for the most common built-in C++ types are defined in MpiTraits.h.

The send<T>, recv<T>, and bcast<T> templates can also be used to transmit instances of a user defined class T if an appropriate MPI type exists. To make this work, the user must define and commit an associated user-defined MPI data type, and also define an explicit specialization MpiTraits<T> to associate this MPI type with C++ type T. Specialized MPI data types and MpiTraits classes for Util::Vector and Util::Vector are defined in the header and implementation files for these classes. User defined MPI types must be committed before they can be used.

Explicit specializations of send<T>, recv<T> and bcast<T> may also be provided for some types for which the algorithm based on MpiTraits is awkward or unworkable. Explicit specializations are declared in this file for bool and std::string. The implementations of send<T> recv<T>, and bcast<T> for T=bool transmit boolean values as integers. The implementations for T = std::string transmit strings as character arrays. No MpiTraits classes are needed or provided for bool or std::string, because the compiler will always use these explicit specializations, which do not rely on MpiTraits classes, rather than the main function templates. It may also be more convenient for some user-defined classes to provide explicit specializations of these three functions, rather than defining an associated MPI type and MpiTraits specialization. The templates defined here can be used to transmit instances of type T either if: (i) Explicit specializations are defined for these three functions, or (ii) an associated MpiTraits class and MPI data type are defined.

Overloaded forms of send<T>, recv<T>, and bcast<T> are provided to transmit 1D and 2D C arrays of data and DArray<T> and DMatrix<T> containers. These functions send the data in one transmission, as a contiguous buffer, if an MPI type is available, but send each element in a separate transmission if no MpiType exists but an explicit specialization exists for the required scalar form of send, recv, or bcast.

Definition in file MpiSendRecv.h.