Rheolef  7.1
an efficient C++ finite element environment
environment.cc
Go to the documentation of this file.
1 // boost::mpi::environment-like interface, with thread options for scotch lib
22 
23 #include "rheolef/distributed.h"
24 #ifndef _RHEOLEF_HAVE_MPI
25 
26 namespace rheolef {
27 
28 environment::environment (int& argc, char**& argv, const environment_option_type& opt)
29  : _oldcw(0)
30 {
31 }
32 environment::~environment()
33 {
34 }
35 
36 } // namespace rheolef
37 
38 #else // _RHEOLEF_HAVE_MPI
39 
40 // support MPI-2 (with warnings, but changed to error by the compiler flags)
41 #ifdef MPI_VERSION
42 #if (MPI_VERSION >= 2)
43 #define MPI_Errhandler_set MPI_Comm_set_errhandler
44 #define MPI_Attr_get MPI_Comm_get_attr
45 #endif
46 #endif
47 
48 namespace rheolef {
49 
51  int flag;
52  MPI_Initialized(&flag);
53  return flag != 0;
54 }
56  int flag;
57  MPI_Finalized(&flag);
58  return flag != 0;
59 }
60 environment::environment (int& argc, char**& argv, const environment_option_type& opt)
61  : _rheolef_has_init(false), _oldcw(0)
62 {
63  if (!initialized()) {
64  int status = 0;
66  status = MPI_Init (&argc, &argv);
67  } else {
68  int obtained_thread_level;
69  status = MPI_Init_thread (&argc, &argv, opt.thread_level, &obtained_thread_level);
70  if (obtained_thread_level != opt.thread_level) {
71  warning_macro ("mpi_init: obtained thread level="<<obtained_thread_level <<" while asking for thread level="<<opt.thread_level);
72  }
73  }
74  check_macro (status == MPI_SUCCESS, "mpi init failed");
75  }
76  if (!_rheolef_has_init) {
77 #ifdef _RHEOLEF_HAVE_OBSOLETE_MPI_V1
78  MPI_Errhandler_set (MPI_COMM_WORLD, MPI_ERRORS_RETURN);
79 #else
80  MPI_Comm_set_errhandler (MPI_COMM_WORLD, MPI_ERRORS_RETURN);
81 #endif
82  _rheolef_has_init = true;
83  }
84 }
86 {
87  if (_rheolef_has_init && !finalized()) {
88  boost::mpi::detail::mpi_datatype_cache().clear();
89  MPI_Finalize();
90  }
91 }
92 void environment::abort(int errcode)
93 {
94  MPI_Abort(MPI_COMM_WORLD, errcode);
95 }
97 {
98  int* max_tag_value;
99  int found = 0;
100  MPI_Attr_get(MPI_COMM_WORLD, MPI_TAG_UB, &max_tag_value, &found);
101  assert_macro (found != 0, "MPI_Attr_get: error");
102  return *max_tag_value - _num_reserved_tags;
103 }
105 {
106  return max_tag() + 1;
107 }
108 boost::optional<int> environment::host_rank()
109 {
110  int* host;
111  int found = 0;
112  MPI_Attr_get(MPI_COMM_WORLD, MPI_HOST, &host, &found);
113  if (!found || *host == MPI_PROC_NULL) {
114  return boost::optional<int>();
115  } else {
116  return *host;
117  }
118 }
119 boost::optional<int> environment::io_rank()
120 {
121  int* io;
122  int found = 0;
123  MPI_Attr_get(MPI_COMM_WORLD, MPI_IO, &io, &found);
124  if (!found || *io == MPI_PROC_NULL) {
125  return boost::optional<int>();
126  } else {
127  return *io;
128  }
129 }
131 {
132  char name[MPI_MAX_PROCESSOR_NAME];
133  int len;
134  MPI_Get_processor_name(name, &len);
135  return std::string(name, len);
136 }
137 
138 } // namespace rheolef
139 #endif // _RHEOLEF_HAVE_MPI
rheolef::environment::host_rank
static boost::optional< int > host_rank()
Definition: environment.cc:108
rheolef::environment::processor_name
static std::string processor_name()
Definition: environment.cc:130
warning_macro
#define warning_macro(message)
Definition: dis_macros.h:53
check_macro
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
rheolef::environment::abort
static void abort(int errcode)
Definition: environment.cc:92
rheolef::environment::collectives_tag
static int collectives_tag()
Definition: environment.cc:104
rheolef::environment::environment
environment(int &argc, char **&argv, const environment_option_type &opt=environment_option_type())
Definition: environment.cc:60
assert_macro
#define assert_macro(ok_condition, message)
Definition: dis_macros.h:113
rheolef::environment::io_rank
static boost::optional< int > io_rank()
Definition: environment.cc:119
rheolef::environment_option_type::no_thread
static const int no_thread
Definition: environment.h:86
rheolef
This file is part of Rheolef.
Definition: compiler_eigen.h:37
rheolef::environment_option_type
Definition: environment.h:85
rheolef::environment::~environment
~environment()
Definition: environment.cc:85
rheolef::environment::initialized
static bool initialized()
Definition: environment.cc:50
rheolef::environment::finalized
static bool finalized()
Definition: environment.cc:55
rheolef::environment_option_type::thread_level
int thread_level
Definition: environment.h:97
mkgeo_contraction.status
status
Definition: mkgeo_contraction.sh:290
mkgeo_contraction.name
name
Definition: mkgeo_contraction.sh:133
rheolef::environment::max_tag
static int max_tag()
Definition: environment.cc:96