$treeview $search $mathjax
Palabos  Version 1.1
$projectbrief
$projectbrief
$searchbox

mpiManager.h

Go to the documentation of this file.
00001 /* This file is part of the Palabos library.
00002  *
00003  * Copyright (C) 2011 FlowKit Sarl
00004  * Avenue de Chailly 23
00005  * 1012 Lausanne, Switzerland
00006  * E-mail contact: contact@flowkit.com
00007  *
00008  * The most recent release of Palabos can be downloaded at 
00009  * <http://www.palabos.org/>
00010  *
00011  * The library Palabos is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Affero General Public License as
00013  * published by the Free Software Foundation, either version 3 of the
00014  * License, or (at your option) any later version.
00015  *
00016  * The library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Affero General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Affero General Public License
00022  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00029 #ifndef MPI_MANAGER_H
00030 #define MPI_MANAGER_H
00031 
00032 #include "core/globalDefs.h"
00033 
00034 #ifdef PLB_MPI_PARALLEL
00035 #include "mpi.h"
00036 #include <vector>
00037 #include <string>
00038 #endif
00039 
00040 
00041 namespace plb {
00042 
00043 namespace global {
00044 
00045 #ifdef PLB_MPI_PARALLEL
00046 
00048 class MpiManager {
00049 public:
00051     void init(int *argc, char ***argv, bool verbous=false);
00054     void init(MPI_Comm globalCommunicator_);
00057     void init();
00059     int getSize() const;
00061     int getRank() const;
00063     int bossId() const;
00065     bool isMainProcessor() const;
00067     double getTime() const;
00069     MPI_Comm getGlobalCommunicator() const;
00070 
00072     void barrier();
00073 
00075     template <typename T>
00076     void send( T *buf, int count, int dest, int tag = 0 );
00077 
00079     template <typename T>
00080     void iSend( T *buf, int count, int dest, MPI_Request* request, int tag = 0 );
00081 
00083     template <typename T>
00084     void rSend( T *buf, int count, int dest, int tag = 0 );
00085 
00087     template <typename T>
00088     void iSendRequestFree( T *buf, int count, int dest, int tag = 0 );
00089 
00091     template <typename T>
00092     void receive( T *buf, int count, int source, int tag = 0 );
00093 
00095     template <typename T>
00096     void iRecv( T *buf, int count, int source, MPI_Request* request, int tag = 0 );
00097 
00099     template <typename T>
00100     void sendRecv( T *sendBuf, T *recvBuf, int count, int dest,
00101                    int source, int tag = 0 );
00102 
00104     template <typename T>
00105     void sendToMaster( T* sendBuf, int sendCount, bool iAmRoot );
00106 
00108     template <typename T>
00109     void scatterV( T *sendBuf, T *recvBuf, int* sendCounts, int root = 0 );
00110 
00112     template <typename T>
00113     void gatherV( T* sendBuf, T* recvBuf, int *recvCounts, int root = 0 );
00114 
00115 
00117     template <typename T>
00118     void bCast( T* sendBuf, int sendCount, int root = 0 );
00119     
00121     void bCast( std::string& message, int root = 0 );
00122 
00124     template <typename T>
00125     void bCastThroughMaster( T* sendBuf, int sendCount, bool iAmRoot );
00126 
00128     template <typename T>
00129     void reduce( T sendVal, T& recvVal, MPI_Op op, int root = 0 );
00130 
00132     template <typename T>
00133     void reduceVect( std::vector<T>& sendVal, std::vector<T>& recvVal,
00134                      MPI_Op op, int root = 0 );
00135 
00138     template <typename T>
00139     void allReduceVect( std::vector<T>& sendRecvVal, MPI_Op op );
00140 
00142     template <typename T>
00143     void reduceAndBcast(T& reductVal, MPI_Op op, int root = 0 );
00144 
00146     void wait(MPI_Request* request, MPI_Status* status);
00147 
00148 private:
00150     template <typename T>
00151     void scatterv_impl(T *sendBuf, int* sendCounts, int* displs,
00152                        T* recvBuf, int recvCount, int root);
00153 
00155     template <typename T>
00156     void gatherv_impl(T* sendBuf, int sendCount, T* recvBuf, int* recvCounts,
00157                       int* displs, int root);
00158 private:
00159     MpiManager();
00160     ~MpiManager();
00161 private:
00162     int numTasks, taskId;
00163     bool ok;
00164     bool responsibleForMpiMachine;
00165     MPI_Comm globalCommunicator;
00166 
00167 friend MpiManager& mpi();
00168 };
00169 
00170 #else  // #ifdef PLB_MPI_PARALLEL
00171 
00172 class MpiManager {
00173 public:
00175     void init(int *argc, char ***argv, bool verbous=false) { }
00178     void init() { }
00180     int getSize() const { return 1; }
00182     int getRank() const { return 0; }
00184     int bossId() const { return 0; }
00186     bool isMainProcessor() const { return true; }
00188     template <typename T>
00189     void bCast(T* sendBuf, int sendCount, int root = 0) { }
00191     void bCast(std::string& message, int root = 0) { }
00193     void barrier() { }
00194 
00195 friend MpiManager& mpi();
00196 };
00197 
00198 #endif  // PLB_MPI_PARALLEL
00199 
00200 inline MpiManager& mpi() {
00201     static MpiManager instance;
00202     return instance;
00203 }
00204 
00205 }  // namespace global
00206 
00207 }  // namespace plb
00208 
00209 
00210 #endif  // MPI_MANAGER_H