mpihelper.hh

Go to the documentation of this file.
00001 // $Id: $
00002 #ifndef DUNE_MPIHELPER
00003 #define DUNE_MPIHELPER
00004 
00005 #include <cassert>
00006 #if HAVE_MPI
00007 #include"mpi.h"
00008 #endif
00009 
00010 #include "stdstreams.hh"
00011 
00012 namespace Dune
00013 {
00062   class FakeMPIHelper
00063   {
00064   public:
00065     enum{ 
00070       isFake = true
00071     };
00072   
00076     typedef int MPICommunicator;
00077 
00083     static MPICommunicator getCommunicator()
00084     {
00085       return -1;
00086     }
00087     
00103     static FakeMPIHelper& instance(int argc, char** argv)
00104     {
00105       // create singleton instance 
00106       static FakeMPIHelper singleton;
00107       return singleton;
00108     }
00109 
00113     int rank () const { return 0; }
00117     int size () const { return 1; }
00118     
00119   private:  
00120     FakeMPIHelper() {}
00121     FakeMPIHelper(const FakeMPIHelper&);
00122     FakeMPIHelper& operator=(const FakeMPIHelper);
00123   };
00124 
00125 #if HAVE_MPI
00126 
00132   class MPIHelper
00133   {
00134   public:
00135     enum{ 
00140       isFake = false
00141     };
00142   
00146     typedef MPI_Comm MPICommunicator;
00147 
00153     static MPICommunicator getCommunicator(){
00154       return MPI_COMM_WORLD;
00155     }
00156     
00172     static MPIHelper& instance(int& argc, char**& argv)
00173     {
00174       // create singleton instance 
00175       static MPIHelper singleton (argc, argv);
00176       return singleton; 
00177     }
00178 
00182     int rank () const { return rank_; }
00186     int size () const { return size_; }
00187     
00188   private:
00189     int rank_;
00190     int size_;
00191     
00193     MPIHelper(int& argc, char**& argv)  
00194     {
00195       rank_ = -1;
00196       size_ = -1;
00197       MPI_Init(&argc, &argv);
00198       MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
00199       MPI_Comm_size(MPI_COMM_WORLD,&size_);
00200       
00201       assert( rank_ >= 0 );
00202       assert( size_ >= 1 );
00203       
00204       dvverb << "Called  MPI_Init on p=" << rank_ << "!" << std::endl; 
00205     }
00207     ~MPIHelper()
00208     {
00209       MPI_Finalize();
00210       dvverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl; 
00211     }
00212     MPIHelper(const MPIHelper&);
00213     MPIHelper& operator=(const MPIHelper);
00214   };
00215 #else
00216   // We do not have MPI therefore FakeMPIHelper
00217   // is the MPIHelper
00222   typedef FakeMPIHelper MPIHelper;
00223 
00224 #endif
00225  
00226 } // end namespace Dune
00227 #endif

Generated on 12 Dec 2007 with Doxygen (ver 1.5.1)