Skip to content

Commit c0f8c79

Browse files
committed
logging: make logging on MPI runs only go to per-rank-files
this required to improve the init function of pfasst to be also used for MPI initialization
1 parent 264cda3 commit c0f8c79

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

include/pfasst.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
#ifndef _PFASST_HPP_
22
#define _PFASST_HPP_
33

4+
#include <iostream>
5+
using namespace std;
6+
47
#include "pfasst/config.hpp"
58
#include "pfasst/logging.hpp"
69

10+
#ifdef WITH_MPI
11+
#include <mpi.h>
12+
#endif
13+
714

815
namespace pfasst
916
{
1017
inline static void init(int argc, char** argv,
1118
std::function<void()> opts = nullptr,
12-
std::function<void()> logs = nullptr)
19+
std::function<void()> logs = nullptr,
20+
bool with_mpi = false)
1321
{
1422
if (opts) {
1523
opts();
1624
}
1725
config::init();
26+
if (with_mpi) {
27+
#ifdef WITH_MPI
28+
MPI_Init(&argc, &argv);
29+
#else
30+
cerr << "PFASST::init() : 'with_mpi' flag used without enabling MPI" << endl;
31+
#endif
32+
}
1833
log::start_log(argc, argv);
1934
if (logs) {
2035
logs();

include/pfasst/logging.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include <string>
77
using namespace std;
88

9+
#ifdef WITH_MPI
10+
#include <mpi.h>
11+
#endif
12+
913
#include <boost/algorithm/string.hpp>
1014

1115
struct OUT
@@ -168,6 +172,20 @@ namespace pfasst
168172
el::Configurations* conf = logger->configurations();
169173
conf->setGlobally(el::ConfigurationType::MillisecondsWidth,
170174
PFASST_LOGGER_DEFAULT_GLOBAL_MILLISECOND_WIDTH);
175+
#ifdef WITH_MPI
176+
int initialized = 0;
177+
MPI_Initialized(&initialized);
178+
assert((bool)initialized);
179+
int rank = 0;
180+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
181+
conf->setGlobally(el::ConfigurationType::ToFile, "true");
182+
conf->setGlobally(el::ConfigurationType::ToStandardOutput, "false");
183+
conf->setGlobally(el::ConfigurationType::Filename,
184+
string("mpi_run_") + to_string(rank) + string(".log"));
185+
#else
186+
conf->setGlobally(el::ConfigurationType::ToFile, "false");
187+
conf->setGlobally(el::ConfigurationType::ToStandardOutput, "true");
188+
#endif
171189
conf->set(el::Level::Info, el::ConfigurationType::Format,
172190
TIMESTAMP + INFO_COLOR + "[" + id2print + ", " + LEVEL + " " + MESSAGE + OUT::reset);
173191
conf->set(el::Level::Debug, el::ConfigurationType::Format,
@@ -191,8 +209,20 @@ namespace pfasst
191209
el::Configurations defaultConf;
192210
defaultConf.setToDefault();
193211

212+
#ifdef WITH_MPI
213+
int initialized = 0;
214+
MPI_Initialized(&initialized);
215+
assert((bool)initialized);
216+
int rank = 0;
217+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
218+
defaultConf.setGlobally(el::ConfigurationType::ToFile, "true");
219+
defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false");
220+
defaultConf.setGlobally(el::ConfigurationType::Filename,
221+
string("mpi_run_") + to_string(rank) + string(".log"));
222+
#else
194223
defaultConf.setGlobally(el::ConfigurationType::ToFile, "false");
195224
defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
225+
#endif
196226
defaultConf.setGlobally(el::ConfigurationType::MillisecondsWidth, PFASST_LOGGER_DEFAULT_GLOBAL_MILLISECOND_WIDTH);
197227
el::Loggers::reconfigureAllLoggers(defaultConf);
198228

0 commit comments

Comments
 (0)