Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef toolx_mpi_world 4 #ifndef toolx_mpi_world 5 #define toolx_mpi_world 5 #define toolx_mpi_world 6 6 7 // code to wrap MPI global things by having an 7 // code to wrap MPI global things by having an interface (toolx::mpi). 8 8 9 #include <tools/impi_world> 9 #include <tools/impi_world> 10 10 11 #include <mpi.h> 11 #include <mpi.h> 12 12 13 #ifdef TOOLS_MEM 13 #ifdef TOOLS_MEM 14 #include <tools/mem> 14 #include <tools/mem> 15 #endif 15 #endif 16 16 17 namespace toolx { 17 namespace toolx { 18 namespace mpi { 18 namespace mpi { 19 19 20 class world : public virtual tools::impi_world 20 class world : public virtual tools::impi_world { 21 typedef tools::impi_world parent; 21 typedef tools::impi_world parent; 22 #ifdef TOOLS_MEM 22 #ifdef TOOLS_MEM 23 protected: 23 protected: 24 static const std::string& s_class() { 24 static const std::string& s_class() { 25 static const std::string s_v("toolx::mpi:: 25 static const std::string s_v("toolx::mpi::world"); 26 return s_v; 26 return s_v; 27 } 27 } 28 #endif 28 #endif 29 public: 29 public: 30 virtual bool init(int* a_argc,char*** a_argv 30 virtual bool init(int* a_argc,char*** a_argv) { 31 if(::MPI_Init(a_argc,a_argv)!=MPI_SUCCESS) 31 if(::MPI_Init(a_argc,a_argv)!=MPI_SUCCESS) return false; 32 return true; 32 return true; 33 } 33 } 34 virtual bool rank(int& a_rank) const { 34 virtual bool rank(int& a_rank) const { 35 if(::MPI_Comm_rank(MPI_COMM_WORLD,&a_rank) 35 if(::MPI_Comm_rank(MPI_COMM_WORLD,&a_rank)!=MPI_SUCCESS) {a_rank=-1;return false;} 36 return true; 36 return true; 37 } 37 } 38 virtual bool size(int& a_size) const { 38 virtual bool size(int& a_size) const { 39 if(::MPI_Comm_size(MPI_COMM_WORLD,&a_size) 39 if(::MPI_Comm_size(MPI_COMM_WORLD,&a_size)!=MPI_SUCCESS) {a_size=0;return false;} 40 return true; 40 return true; 41 } 41 } 42 virtual bool processor_name(std::string& a_s 42 virtual bool processor_name(std::string& a_s) const { 43 char name[MPI_MAX_PROCESSOR_NAME]; 43 char name[MPI_MAX_PROCESSOR_NAME]; 44 int lname; 44 int lname; 45 if(::MPI_Get_processor_name(name,&lname)!= 45 if(::MPI_Get_processor_name(name,&lname)!=MPI_SUCCESS) {a_s.clear();return false;} 46 a_s = std::string(name); 46 a_s = std::string(name); 47 return true; 47 return true; 48 } 48 } 49 public: 49 public: 50 world() { 50 world() { 51 #ifdef TOOLS_MEM 51 #ifdef TOOLS_MEM 52 tools::mem::increment(s_class().c_str()); 52 tools::mem::increment(s_class().c_str()); 53 #endif 53 #endif 54 } 54 } 55 virtual ~world(){ 55 virtual ~world(){ 56 #ifdef TOOLS_MEM 56 #ifdef TOOLS_MEM 57 tools::mem::decrement(s_class().c_str()); 57 tools::mem::decrement(s_class().c_str()); 58 #endif 58 #endif 59 } 59 } 60 protected: 60 protected: 61 world(const world& a_from):parent(a_from) { 61 world(const world& a_from):parent(a_from) { 62 #ifdef TOOLS_MEM 62 #ifdef TOOLS_MEM 63 tools::mem::increment(s_class().c_str()); 63 tools::mem::increment(s_class().c_str()); 64 #endif 64 #endif 65 } 65 } 66 world& operator=(const world&) {return *this 66 world& operator=(const world&) {return *this;} 67 }; 67 }; 68 68 69 }} 69 }} 70 70 71 #endif 71 #endif