LAMA
/home/brandes/workspace/LAMA/src/lama/mpi/MPICommunicator.hpp
Go to the documentation of this file.
00001 
00034 #ifndef LAMA_MPI_COMMUNICATOR_HPP_
00035 #define LAMA_MPI_COMMUNICATOR_HPP_
00036 
00037 #include <mpi.h> //Intel MPI need mpi.h to be included before stdio.h so this header comes first
00038 
00039 // for dll_import
00040 #include <lama/config.hpp>
00041 
00042 // base classes
00043 #include <lama/Communicator.hpp>
00044 
00045 // others
00046 #include <lama/LAMATypes.hpp>
00047 #include <lama/SyncToken.hpp>
00048 
00049 // logging
00050 #include <logging/logging.hpp>
00051 
00052 // boost
00053 #include <boost/thread/thread.hpp>
00054 
00055 #include <vector>
00056 
00057 namespace lama
00058 {
00059 
00065 class LAMA_DLL_IMPORTEXPORT MPICommunicator: public Communicator
00066 {
00067 
00068 // Only MPICommunicatorManager is allowed to create MPI communicator
00069 
00070     friend class MPICommunicatorManager;
00071 
00072 public:
00073     virtual ~MPICommunicator();
00074 
00075     virtual bool isEqual( const Communicator& other ) const;
00076 
00077     virtual ThreadSafetyLevel getThreadSafetyLevel() const;
00078 
00079     virtual PartitionId getSize() const;
00080 
00081     virtual PartitionId getRank() const;
00082 
00083     MPI_Comm getMPIComm() const;
00084 
00093     virtual void all2all( int* recvValues, const int* sendValues ) const;
00094 
00103     virtual void exchangeByPlan(
00104         int* const recvData,
00105         const CommunicationPlan& recvPlan,
00106         const int* const sendData,
00107         const CommunicationPlan& sendPlan ) const;
00108 
00109     virtual void exchangeByPlan(
00110         float* const recvData,
00111         const CommunicationPlan& recvPlan,
00112         const float* const sendData,
00113         const CommunicationPlan& sendPlan ) const;
00114 
00115     virtual void exchangeByPlan(
00116         double* const recvData,
00117         const CommunicationPlan& recvPlan,
00118         const double* const sendData,
00119         const CommunicationPlan& sendPlan ) const;
00120 
00121     virtual std::auto_ptr<SyncToken> exchangeByPlanAsync(
00122         int* const recvData,
00123         const CommunicationPlan& recvPlan,
00124         const int* const sendData,
00125         const CommunicationPlan& sendPlan ) const;
00126 
00127     virtual std::auto_ptr<SyncToken> exchangeByPlanAsync(
00128         float* const recvData,
00129         const CommunicationPlan& recvPlan,
00130         const float* const sendData,
00131         const CommunicationPlan& sendPlan ) const;
00132 
00133     virtual std::auto_ptr<SyncToken> exchangeByPlanAsync(
00134         double* const recvData,
00135         const CommunicationPlan& recvPlan,
00136         const double* const sendData,
00137         const CommunicationPlan& sendPlan ) const;
00138 
00139     template<typename T>
00140     MPI_Request startrecv( T* buffer, int count, int source ) const;
00141 
00142     template<typename T>
00143     MPI_Request startsend( const T* buffer, int count, int target ) const;
00144 
00145     virtual float sum( const float value ) const;
00146 
00147     virtual double sum( const double value ) const;
00148 
00149     virtual int sum( const int value ) const;
00150 
00151     virtual float min( const float value ) const;
00152 
00153     virtual size_t sum( const size_t value ) const;
00154 
00155     virtual float max( const float value ) const;
00156 
00157     virtual double min( const double value ) const;
00158 
00159     virtual double max( const double value ) const;
00160 
00161     virtual int min( const int value ) const;
00162 
00163     virtual int max( const int value ) const;
00164 
00165     virtual void gather( std::vector<float>& values, float value ) const;
00166 
00167     virtual void synchronize() const;
00168 
00169     virtual IndexType shift( double newVals[], const IndexType newSize,
00170                              const double oldVals[], const IndexType oldSize,
00171                              const int direction ) const;
00172 
00173     virtual IndexType shift( float newVals[], const IndexType newSize,
00174                              const float oldVals[], const IndexType oldSize,
00175                              const int direction ) const;
00176 
00177     virtual IndexType shift( int newVals[], const IndexType newSize,
00178                              const int oldVals[], const IndexType oldSize,
00179                              const int direction ) const;
00180 
00181     virtual std::auto_ptr<SyncToken> shiftAsync( double newVals[], const double oldVals[],
00182                                                  const IndexType size, const int direction ) const;
00183 
00184     virtual std::auto_ptr<SyncToken> shiftAsync( float newVals[], const float oldVals[],
00185                                                  const IndexType size, const int direction ) const;
00186 
00187     virtual std::auto_ptr<SyncToken> shiftAsync( int newVals[], const int oldVals[],
00188                                                  const IndexType size, const int direction ) const;
00189 
00192     virtual void bcast ( double val[], const IndexType n, const PartitionId root ) const;
00193 
00196     virtual void bcast ( int val[], const IndexType n, const PartitionId root ) const;
00197 
00200     virtual void bcast ( float val[], const IndexType n, const PartitionId root ) const;
00201 
00204     virtual void scatter ( double myvals[], const IndexType n, const PartitionId root, const double allvals[] ) const;
00205     virtual void scatter ( float myvals[], const IndexType n, const PartitionId root, const float allvals[] ) const;
00206     virtual void scatter ( int myvals[], const IndexType n, const PartitionId root, const int allvals[] ) const;
00207 
00208     virtual void scatter ( double myvals[], const IndexType n, const PartitionId root,
00209                            const double allvals[], const IndexType sizes[] ) const;
00210     virtual void scatter ( float myvals[], const IndexType n, const PartitionId root,
00211                            const float allvals[], const IndexType sizes[] ) const;
00212     virtual void scatter ( int myvals[], const IndexType n, const PartitionId root,
00213                            const int allvals[], const IndexType sizes[] ) const;
00214 
00217     virtual void gather ( double allvals[], const IndexType n, const PartitionId root, const double myvals[] ) const;
00218     virtual void gather ( float allvals[], const IndexType n, const PartitionId root, const float myvals[] ) const;
00219     virtual void gather ( int allvals[], const IndexType n, const PartitionId root, const int myvals[] ) const;
00220 
00221     virtual void gather ( double allvals[], const IndexType n, const PartitionId root,
00222                           const double myvals[], const IndexType sizes[] ) const;
00223     virtual void gather ( float allvals[], const IndexType n, const PartitionId root,
00224                           const float myvals[], const IndexType sizes[] ) const;
00225     virtual void gather ( int allvals[], const IndexType n, const PartitionId root,
00226                           const int myvals[], const IndexType sizes[] ) const;
00227 
00236     virtual void maxloc( double& val, int& location, const PartitionId root ) const;
00237 
00238     virtual void maxloc( float& val, int& location, const PartitionId root ) const;
00239 
00240     virtual void maxloc( int& val, int& location, const PartitionId root ) const;
00241 
00244     virtual void swap( double val[], const IndexType n, const PartitionId partner ) const;
00245 
00246     virtual void swap( float val[], const IndexType n, const PartitionId partner ) const;
00247 
00248     virtual void swap( int val[], const IndexType n, const PartitionId partner ) const;
00249 
00250     virtual void writeAt( std::ostream& stream ) const;
00251 
00252 private:
00253 
00254     MPICommunicator( int& argc, char** & argv );
00255 
00256     template<typename T>
00257     inline static MPI_Datatype getMPIType();
00258 
00259     template<typename T1, typename T2>
00260     inline static MPI_Datatype getMPI2Type();
00261 
00262     template<typename T>
00263     T sumImpl( T myVal ) const;
00264 
00265     template<typename T>
00266     T maxval( T myVal ) const;
00267 
00268     template<typename T>
00269     T minval( T myVal ) const;
00270 
00271     template<typename T>
00272     inline void send( const T* buffer, int count, int target ) const;
00273 
00274     template<typename T>
00275     inline int getCount( MPI_Status& status ) const;
00276 
00277     template<typename T>
00278     void scatterImpl ( T myvals[], const IndexType n, const PartitionId root,
00279                        const T allvals[] ) const;
00280 
00281     template<typename T>
00282     void scatterImpl ( T myvals[], const IndexType n, const PartitionId root,
00283                        const T allvals[], const IndexType sizes[] ) const;
00284 
00285     template<typename T>
00286     void gatherImpl( T allvals[], const IndexType n, const PartitionId root,
00287                      const T myvals[] ) const;
00288 
00289     template<typename T>
00290     void gatherImpl( T allvals[], const IndexType n, const PartitionId root,
00291                      const T myvals[], const IndexType sizes[] ) const;
00292 
00293     template <typename T>
00294     IndexType shiftImpl(
00295         T newvals[], const IndexType newSize, const PartitionId source,
00296         const T oldVals[], const IndexType oldSize, const PartitionId dest ) const;
00297 
00298     template <typename T>
00299     std::auto_ptr<SyncToken> shiftAsyncImpl(
00300         T newvals[], const PartitionId source,
00301         const T oldVals[], const PartitionId dest, const IndexType size ) const;
00302 
00303     template<typename T>
00304     void swapImpl( T val[], const IndexType n, PartitionId partner ) const;
00305 
00306     template<typename T>
00307     void maxlocImpl( T& val, int& location, PartitionId root ) const;
00308 
00309     template<typename T>
00310     void exchangeByPlanImpl(
00311         T* const recvData,
00312         const CommunicationPlan& recvPlan,
00313         const T* const sendData,
00314         const CommunicationPlan& sendPlan ) const;
00315 
00316     template<typename T>
00317     std::auto_ptr<SyncToken> exchangeByPlanAsyncImpl(
00318         T* const recvData,
00319         const CommunicationPlan& recvPlan,
00320         const T* const sendData,
00321         const CommunicationPlan& sendPlan ) const;
00322 
00323     MPI_Comm mCommWorld;
00324     MPI_Comm mComm;
00325     MPI_Comm mCommTask;
00326 
00327     const boost::thread mMainThread;
00328 
00329     inline MPI_Comm selectMPIComm() const;
00330 
00331     Communicator::ThreadSafetyLevel mThreadSafetyLevel;
00332 
00333     int      mRank;   // rank of this processor
00334     int      mSize;   // size of communicator
00335 
00336     bool mExternInitialization;
00337 
00338     LAMA_LOG_DECL_STATIC_LOGGER( logger );
00339 
00340     static const int defaultTag;
00341 protected:
00342     virtual ContextPtr getCommunicationContext() const;
00343 
00344 };
00345 
00346 }
00347 
00348 #endif // LAMA_MPI_COMMUNICATOR_HPP_