PlayaMPIComm.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 
00003 // @HEADER
00004 
00005 #ifndef PLAYA_MPICOMM_H
00006 #define PLAYA_MPICOMM_H
00007 
00008 /*! \file PlayaMPIComm.hpp
00009   \brief Object representation of a MPI communicator
00010 */
00011 
00012 #include "PlayaDefs.hpp"
00013 #include "PlayaMPIOp.hpp"
00014 #include "PlayaMPIDataType.hpp"
00015 #include "Teuchos_ConfigDefs.hpp"
00016 #include "Teuchos_Array.hpp"
00017 #include "Teuchos_RCP.hpp"
00018 
00019 #ifdef HAVE_MPI
00020 #include "mpi.h"
00021 #endif
00022 
00023 
00024 namespace Playa
00025 {
00026 
00027 
00028 /**
00029  * \brief Object representation of an MPI communicator.
00030  *
00031  * At present, groups are not implemented so the only communicators
00032  * are MPI_COMM_WORLD and MPI_COMM_SELF
00033  */
00034 class MPIComm
00035 {
00036 public:
00037 
00038   //! Empty constructor builds an object for MPI_COMM_WORLD
00039   MPIComm();
00040 
00041 #ifdef HAVE_MPI
00042   //! Construct a MPIComm for a given MPI communicator
00043   MPIComm(MPI_Comm comm);
00044 #endif
00045 
00046   //! Get an object representing MPI_COMM_WORLD 
00047   static MPIComm& world();
00048   //! Get an object representing MPI_COMM_SELF
00049   static MPIComm& self();
00050 
00051   //! Return process rank
00052   int getRank() const {return myRank_;}
00053 
00054   //! Return number of processors in the communicator
00055   int getNProc() const {return nProc_;}
00056 
00057   //! Synchronize all the processors in the communicator
00058   void synchronize() const ;
00059 
00060   //! @name Collective communications 
00061   //@{
00062 
00063   //! All-to-all gather-scatter
00064   void allToAll(void* sendBuf, int sendCount, const MPIDataType& sendType,
00065     void* recvBuf, int recvCount, 
00066     const MPIDataType& recvType) const ;
00067 
00068   //! Variable-length gather-scatter
00069   void allToAllv(void* sendBuf, int* sendCount, int* sendDisplacements,
00070     const MPIDataType& sendType,
00071     void* recvBuf, int* recvCount,
00072     int* recvDisplacements,
00073     const MPIDataType& recvType) const ;
00074 
00075   //! Do a collective operation, scattering the results to all processors
00076   void allReduce(void* input, void* result, int inputCount, 
00077     const MPIDataType& type,
00078     const MPIOp& op) const ;
00079 
00080 
00081   //! Gather to root 
00082   void gather(void* sendBuf, int sendCount, const MPIDataType& sendType,
00083     void* recvBuf, int recvCount, const MPIDataType& recvType,
00084     int root) const ;
00085 
00086   //! Gather variable-sized arrays to root 
00087   void gatherv(void* sendBuf, int sendCount, const MPIDataType& sendType,
00088     void* recvBuf, int* recvCount, int* displacements, 
00089     const MPIDataType& recvType, int root) const ;
00090 
00091   //! Gather to all processors
00092   void allGather(void* sendBuf, int sendCount, 
00093     const MPIDataType& sendType,
00094     void* recvBuf, int recvCount, 
00095     const MPIDataType& recvType) const ;
00096 
00097   //! Variable-length gather to all processors
00098   void allGatherv(void* sendBuf, int sendCount, 
00099     const MPIDataType& sendType,
00100     void* recvBuf, int* recvCount, int* recvDisplacements,
00101     const MPIDataType& recvType) const ;
00102 
00103   //! Broadcast 
00104   void bcast(void* msg, int length, 
00105     const MPIDataType& type, int src) const ;
00106 
00107   //@}
00108 
00109 #ifdef HAVE_MPI
00110   //! Get the MPI_Comm communicator handle 
00111   MPI_Comm getComm() const {return comm_;}
00112 #endif
00113 
00114   
00115 
00116   // errCheck() checks the return value of an MPI call and throws
00117   // a ParallelException upon failure.
00118   static void errCheck(int errCode, const std::string& methodName);
00119 
00120 private:
00121 #ifdef HAVE_MPI
00122   MPI_Comm comm_;
00123 #endif
00124 
00125   int nProc_;
00126   int myRank_;
00127 
00128   /** common initialization function, called by all ctors */
00129   void init();
00130 
00131   /** Indicate whether MPI is currently running */
00132   int mpiIsRunning() const ;
00133 };
00134 }
00135 #endif
00136 

Site Contact