Skip to content

Commit f297bdf

Browse files
committed
config: Add get_rank() helper.
1 parent 1bd1e3a commit f297bdf

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

include/pfasst/config.hpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@ namespace pfasst
2626
*/
2727
namespace config
2828
{
29+
/**
30+
* Get MPI rank during initialization.
31+
*
32+
* When running without MPI (ie, without using mpirun/mpiexec), returns 0. When running with
33+
* MPI, returns the MPI rank.
34+
*
35+
* If the user is running with MPI and MPI_Init hasn't been called yet, this will return 0. I
36+
* hope this is rare.
37+
*/
38+
int get_rank()
39+
{
40+
#ifdef WITH_MPI
41+
int initialized = 0, rank = 0;
42+
// if we're not running under "mpirun/mpiexec", just assume rank 0.
43+
MPI_Initialized(&initialized);
44+
if (initialized) {
45+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
46+
}
47+
return rank;
48+
#else
49+
return 0;
50+
#endif
51+
}
52+
2953
/**
3054
* Runtime config options provider.
3155
*
@@ -76,7 +100,7 @@ namespace pfasst
76100
* @param[in] option Name of the command line parameter.
77101
* It is possible to specify a long and optional short option name by comma-separation.
78102
* Short option names are identified by being only a single character.
79-
* They are automatically parsed as '`-[SHORT]`' by `boost::program_options` in contrast
103+
* They are automatically parsed as '`-[SHORT]`' by `boost::program_options` in contrast
80104
* to '`--[LONG]`'.
81105
* @param[in] help help text to be displayed in the help and usage information
82106
*/
@@ -90,7 +114,7 @@ namespace pfasst
90114
* @param[in] option Name of the command line parameter.
91115
* It is possible to specify a long and optional short option name by comma-separation.
92116
* Short option names are identified by being only a single character.
93-
* They are automatically parsed as '`-[SHORT]`' by `boost::program_options` in contrast
117+
* They are automatically parsed as '`-[SHORT]`' by `boost::program_options` in contrast
94118
* to '`--[LONG]`'.
95119
* @param[in] help help text to be displayed in the help and usage information
96120
*
@@ -193,21 +217,11 @@ namespace pfasst
193217
po::store(parsed, options::get_instance().get_variables_map());
194218
po::notify(options::get_instance().get_variables_map());
195219

196-
#ifdef WITH_MPI
197-
int initialized = 0;
198-
MPI_Initialized(&initialized);
199-
assert((bool)initialized);
200-
int rank = 0;
201-
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
202-
#endif
203220
if (options::get_instance().get_variables_map().count("help")) {
204-
#ifdef WITH_MPI
205-
if (rank == 0) {
206-
#endif
221+
if (get_rank() == 0) {
207222
cout << print_help() << endl;
208-
#ifdef WITH_MPI
209223
}
210-
#endif
224+
211225
if (exit_on_help) {
212226
#ifdef WITH_MPI
213227
MPI_Finalize();
@@ -263,7 +277,7 @@ namespace pfasst
263277
options::add_option<double>("Duration", "tend", "final time of simulation");
264278
options::add_option<size_t>("Duration", "num_steps", "number time steps");
265279
options::add_option<size_t>("Duration", "num_iter", "number of iterations");
266-
280+
267281
options::add_option<size_t>("Quadrature", "num_nodes", "number of quadrature nodes");
268282

269283
options::add_option<double>("Tolerances", "abs_res_tol", "absolute residual tolerance");

include/pfasst/logging.hpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ namespace pfasst
166166
* - `VLOG` - the verbose logging levels are used as follows:
167167
* - 0 to 8
168168
* - 9 for function enter and exit messages (cfg. @ref VLOG_FUNC_START and @ref VLOG_FUNC_END)
169-
*
169+
*
170170
* @see [easylogging++](https://github.com/easylogging/easyloggingpp)
171171
*/
172172
namespace log
@@ -213,12 +213,7 @@ namespace pfasst
213213
const string POSITION = "%fbase:%line";
214214
const string MESSAGE = "%msg";
215215
#ifdef WITH_MPI
216-
int initialized = 0;
217-
MPI_Initialized(&initialized);
218-
assert((bool)initialized);
219-
int rank = 0;
220-
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
221-
216+
const int rank = pfasst::config::get_rank();
222217
ostringstream frmter;
223218
frmter << std::setw(3) << rank;
224219
const string MPI_RANK = ", rank " + frmter.str();
@@ -290,11 +285,7 @@ namespace pfasst
290285
defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false");
291286
}
292287
#ifdef WITH_MPI
293-
int initialized = 0;
294-
MPI_Initialized(&initialized);
295-
assert((bool)initialized);
296-
int rank = 0;
297-
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
288+
int rank = pfasst::config::get_rank();
298289
defaultConf.setGlobally(el::ConfigurationType::ToFile, "true");
299290
defaultConf.setGlobally(el::ConfigurationType::Filename,
300291
string("mpi_run_") + to_string(rank) + string(".log"));

0 commit comments

Comments
 (0)