dune-common 2.1.1
mpihelper.hh
Go to the documentation of this file.
00001 // $Id: $
00002 #ifndef DUNE_MPIHELPER
00003 #define DUNE_MPIHELPER
00004 
00005 #include <cassert>
00006 #include"collectivecommunication.hh"
00007 #if HAVE_MPI
00008 #include"mpi.h"
00009 #include"mpicollectivecommunication.hh"
00010 #endif
00011 
00012 #include "stdstreams.hh"
00013 
00014 namespace Dune
00015 {
00065   class FakeMPIHelper
00066   {
00067   public:
00068     enum{ 
00073       isFake = true
00074     };
00075   
00079     typedef No_Comm MPICommunicator;
00080 
00087     static MPICommunicator getCommunicator ()
00088     {
00089       static MPICommunicator comm;
00090       return comm;
00091     }
00092 
00099     static MPICommunicator getLocalCommunicator ()
00100     {
00101       return getCommunicator();
00102     }
00103 
00104 
00105 
00106     static CollectiveCommunication<MPICommunicator>
00107     getCollectiveCommunication()
00108     {
00109       return CollectiveCommunication<MPICommunicator>(getCommunicator());
00110     }
00111     
00127     static FakeMPIHelper& instance(int argc, char** argv)
00128     {
00129       // create singleton instance 
00130       static FakeMPIHelper singleton;
00131       return singleton;
00132     }
00133 
00137     int rank () const { return 0; }
00141     int size () const { return 1; }
00142     
00143   private:  
00144     FakeMPIHelper() {}
00145     FakeMPIHelper(const FakeMPIHelper&);
00146     FakeMPIHelper& operator=(const FakeMPIHelper);
00147   };
00148 
00149 #if HAVE_MPI
00150 
00156   class MPIHelper
00157   {
00158   public:
00159     enum{ 
00164       isFake = false
00165     };
00166   
00170     typedef MPI_Comm MPICommunicator;
00171 
00178     static MPICommunicator getCommunicator ()
00179     {
00180       return MPI_COMM_WORLD;
00181     }
00182 
00189     static MPICommunicator getLocalCommunicator ()
00190     {
00191       return MPI_COMM_SELF;
00192     }
00193 
00194     static CollectiveCommunication<MPICommunicator>
00195     getCollectiveCommunication()
00196     {
00197       return CollectiveCommunication<MPICommunicator>(getCommunicator());
00198     }
00214     static MPIHelper& instance(int& argc, char**& argv)
00215     {
00216       // create singleton instance 
00217       static MPIHelper singleton (argc, argv);
00218       return singleton; 
00219     }
00220 
00224     int rank () const { return rank_; }
00228     int size () const { return size_; }
00229 
00230   private:
00231     int rank_;
00232     int size_;
00233     void prevent_warning(int){}
00234 
00236     MPIHelper(int& argc, char**& argv)  
00237     {
00238 #if MPI_2
00239       int wasInitialized = -1;
00240       MPI_Initialized( &wasInitialized );
00241       if(!wasInitialized)
00242 #endif
00243       {
00244         rank_ = -1;
00245         size_ = -1;
00246         static int is_initialized = MPI_Init(&argc, &argv);
00247         prevent_warning(is_initialized);
00248       }
00249 
00250       MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
00251       MPI_Comm_size(MPI_COMM_WORLD,&size_);
00252       
00253       assert( rank_ >= 0 );
00254       assert( size_ >= 1 );
00255 
00256       dverb << "Called  MPI_Init on p=" << rank_ << "!" << std::endl; 
00257     }
00259     ~MPIHelper()
00260     {
00261 #ifdef MPI_2
00262       int wasFinalized = -1;
00263       MPI_Finalized( &wasFinalized );
00264       if(!wasFinalized){
00265 #endif
00266       MPI_Finalize();
00267       dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl; 
00268 #ifdef MPI_2
00269       }
00270       
00271 #endif
00272     }
00273     MPIHelper(const MPIHelper&);
00274     MPIHelper& operator=(const MPIHelper);
00275   };
00276 #else
00277   // We do not have MPI therefore FakeMPIHelper
00278   // is the MPIHelper
00283   typedef FakeMPIHelper MPIHelper;
00284 
00285 #endif
00286  
00287 } // end namespace Dune
00288 #endif