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 { 00064 class FakeMPIHelper 00065 { 00066 public: 00067 enum{ 00072 isFake = true 00073 }; 00074 00078 typedef int MPICommunicator; 00079 00085 static MPICommunicator getCommunicator() 00086 { 00087 return -1; 00088 } 00089 00090 static CollectiveCommunication<MPICommunicator> 00091 getCollectiveCommunication() 00092 { 00093 return CollectiveCommunication<MPICommunicator>(getCommunicator()); 00094 } 00095 00111 static FakeMPIHelper& instance(int argc, char** argv) 00112 { 00113 // create singleton instance 00114 static FakeMPIHelper singleton; 00115 return singleton; 00116 } 00117 00121 int rank () const { return 0; } 00125 int size () const { return 1; } 00126 00127 private: 00128 FakeMPIHelper() {} 00129 FakeMPIHelper(const FakeMPIHelper&); 00130 FakeMPIHelper& operator=(const FakeMPIHelper); 00131 }; 00132 00133 #if HAVE_MPI 00134 00140 class MPIHelper 00141 { 00142 public: 00143 enum{ 00148 isFake = false 00149 }; 00150 00154 typedef MPI_Comm MPICommunicator; 00155 00161 static MPICommunicator getCommunicator(){ 00162 return MPI_COMM_WORLD; 00163 } 00164 static CollectiveCommunication<MPICommunicator> 00165 getCollectiveCommunication() 00166 { 00167 return CollectiveCommunication<MPICommunicator>(getCommunicator()); 00168 } 00184 static MPIHelper& instance(int& argc, char**& argv) 00185 { 00186 // create singleton instance 00187 static MPIHelper singleton (argc, argv); 00188 return singleton; 00189 } 00190 00194 int rank () const { return rank_; } 00198 int size () const { return size_; } 00199 00200 private: 00201 int rank_; 00202 int size_; 00203 00205 MPIHelper(int& argc, char**& argv) 00206 { 00207 rank_ = -1; 00208 size_ = -1; 00209 MPI_Init(&argc, &argv); 00210 MPI_Comm_rank(MPI_COMM_WORLD,&rank_); 00211 MPI_Comm_size(MPI_COMM_WORLD,&size_); 00212 00213 assert( rank_ >= 0 ); 00214 assert( size_ >= 1 ); 00215 00216 dverb << "Called MPI_Init on p=" << rank_ << "!" << std::endl; 00217 } 00219 ~MPIHelper() 00220 { 00221 MPI_Finalize(); 00222 dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl; 00223 } 00224 MPIHelper(const MPIHelper&); 00225 MPIHelper& operator=(const MPIHelper); 00226 }; 00227 #else 00228 // We do not have MPI therefore FakeMPIHelper 00229 // is the MPIHelper 00234 typedef FakeMPIHelper MPIHelper; 00235 00236 #endif 00237 00238 } // end namespace Dune 00239 #endif