Skip to content

Commit

Permalink
Merge pull request #146 from torbjoernk/feature/rewamp-logging
Browse files Browse the repository at this point in the history
LGTM
  • Loading branch information
memmett committed Jan 21, 2015
2 parents 8792d7b + 5c772d9 commit 549019c
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 77 deletions.
6 changes: 3 additions & 3 deletions examples/advection_diffusion/advection_diffusion_sweeper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace pfasst

virtual ~AdvectionDiffusionSweeper()
{
LOG(INFO) << "number of f1 evals: " << this->nf1evals;
CLOG(INFO, "Advec") << "number of f1 evals: " << this->nf1evals;
}
//! @}

Expand Down Expand Up @@ -141,7 +141,7 @@ namespace pfasst

auto n = this->get_controller()->get_step();
auto k = this->get_controller()->get_iteration();
LOG(INFO) << "err: " << n << " " << k << " " << max << " (" << qend.size() << "," << predict << ")";
CLOG(INFO, "Advec") << "err: " << n << " " << k << " " << max << " (" << qend.size() << "," << predict << ")";

this->errors.insert(vtype(ktype(n, k), max));
}
Expand All @@ -163,7 +163,7 @@ namespace pfasst

auto n = this->get_controller()->get_step();
auto k = this->get_controller()->get_iteration();
LOG(INFO) << "res: " << n << " " << k << " " << rmax << " (" << residuals.size() << ")";
CLOG(INFO, "Advec") << "res: " << n << " " << k << " " << rmax << " (" << residuals.size() << ")";

this->residuals[ktype(n, k)] = rmax;
}
Expand Down
18 changes: 12 additions & 6 deletions examples/advection_diffusion/serial_mlsdc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ using namespace std;
#include <fftw3.h>

#include <pfasst.hpp>
#include <pfasst/logging.hpp>
#include <pfasst/config.hpp>
#include <pfasst/mlsdc.hpp>
#include <pfasst/encap/vector.hpp>
using namespace pfasst::encap;
Expand All @@ -28,14 +30,14 @@ namespace pfasst
{
MLSDC<> mlsdc;

const size_t nsteps = 4;
const double dt = 0.01;
const size_t niters = 8;
const size_t nsteps = config::get_value<size_t>("num_steps", 4);
const double dt = config::get_value<double>("delta_step", 0.01);
const size_t niters = config::get_value<size_t>("num_iter", 8);
const int xrat = 2;
const int trat = 2;

size_t nnodes = 5;
size_t ndofs = 128;
size_t nnodes = config::get_value<size_t>("num_nodes", 5);
size_t ndofs = config::get_value<size_t>("spatial_dofs", 128);

/*
* build space/time discretisation levels and add them to mlsdc
Expand Down Expand Up @@ -93,8 +95,12 @@ namespace pfasst
} // ::pfasst

#ifndef PFASST_UNIT_TESTING
int main(int /*argc*/, char** /*argv*/)
int main(int argc, char** argv)
{
pfasst::examples::advection_diffusion::AdvectionDiffusionSweeper<>::enable_config_options();
pfasst::init(argc, argv);
pfasst::log::add_custom_logger("Advec");

pfasst::examples::advection_diffusion::run_serial_mlsdc(3);
}
#endif
117 changes: 73 additions & 44 deletions examples/advection_diffusion/serial_mlsdc_autobuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ using namespace std;
#include <fftw3.h>

#include <pfasst.hpp>
#include <pfasst/logging.hpp>
#include <pfasst/config.hpp>
#include <pfasst/mlsdc.hpp>
#include <pfasst/encap/automagic.hpp>
#include <pfasst/encap/vector.hpp>
Expand All @@ -29,49 +31,76 @@ using pfasst::examples::advection_diffusion::AdvectionDiffusionSweeper;
using pfasst::examples::advection_diffusion::SpectralTransfer1D;


int main(int /*argc*/, char** /*argv*/)
namespace pfasst
{
MLSDC<> mlsdc;

const size_t nsteps = 4;
const double dt = 0.01;
const size_t niters = 4;

vector<pair<size_t, pfasst::quadrature::QuadratureType>> nodes = {
{ 3, pfasst::quadrature::QuadratureType::GaussLobatto },
{ 5, pfasst::quadrature::QuadratureType::GaussLobatto }
};

vector<size_t> ndofs = { 64, 128 };

/*
* the 'build' function is called once for each level, and returns a
* tuple containing a sweeper, encapsulation factory, and transfer
* routines. in this case our builder is a lambda function that
* captures the 'ndofs' variable from above.
*/
auto build_level = [ndofs](size_t level) {
auto factory = make_shared<VectorFactory<double>>(ndofs[level]);
auto sweeper = make_shared<AdvectionDiffusionSweeper<>>(ndofs[level]);
auto transfer = make_shared<SpectralTransfer1D<>>();

return AutoBuildTuple<>(sweeper, transfer, factory);
};

/*
* the 'initial' function is called once for each level to set the
* intial conditions.
*/
auto initial = [](shared_ptr<EncapSweeper<>> sweeper, shared_ptr<Encapsulation<>> q0) {
auto ad = dynamic_pointer_cast<AdvectionDiffusionSweeper<>>(sweeper);
assert(ad);
ad->exact(q0, 0.0);
};

auto_build(mlsdc, nodes, build_level);
auto_setup(mlsdc, initial);
mlsdc.set_duration(0.0, nsteps*dt, dt, niters);
mlsdc.run();

fftw_cleanup();
namespace examples
{
namespace advection_diffusion
{
tuple<error_map, residual_map> run_serial_mlsdc_autobuild()
{
MLSDC<> mlsdc;

const size_t nsteps = config::get_value<size_t>("num_steps", 4);
const double dt = config::get_value<double>("delta_step", 0.01);
const size_t niters = config::get_value<size_t>("num_iter", 4);

vector<pair<size_t, pfasst::quadrature::QuadratureType>> nodes = {
{ 3, pfasst::quadrature::QuadratureType::GaussLobatto },
{ 5, pfasst::quadrature::QuadratureType::GaussLobatto }
};

vector<size_t> ndofs = { 64, 128 };

/*
* the 'build' function is called once for each level, and returns a
* tuple containing a sweeper, encapsulation factory, and transfer
* routines. in this case our builder is a lambda function that
* captures the 'ndofs' variable from above.
*/
auto build_level = [ndofs](size_t level) {
auto factory = make_shared<VectorFactory<double>>(ndofs[level]);
auto sweeper = make_shared<AdvectionDiffusionSweeper<>>(ndofs[level]);
auto transfer = make_shared<SpectralTransfer1D<>>();

return AutoBuildTuple<>(sweeper, transfer, factory);
};

/*
* the 'initial' function is called once for each level to set the
* intial conditions.
*/
auto initial = [](shared_ptr<EncapSweeper<>> sweeper, shared_ptr<Encapsulation<>> q0) {
auto ad = dynamic_pointer_cast<AdvectionDiffusionSweeper<>>(sweeper);
assert(ad);
ad->exact(q0, 0.0);
};

auto_build(mlsdc, nodes, build_level);
auto_setup(mlsdc, initial);
mlsdc.set_duration(0.0, nsteps*dt, dt, niters);
mlsdc.run();

fftw_cleanup();

tuple<error_map, residual_map> rinfo;
get<0>(rinfo) = mlsdc.get_finest<AdvectionDiffusionSweeper<>>()->get_errors();
for (auto l = mlsdc.coarsest(); l <= mlsdc.finest(); ++l) {
get<1>(rinfo).insert(pair<size_t, error_map>(l.level, l.current<AdvectionDiffusionSweeper<>>()->get_residuals()));
}
return rinfo;
}
} // ::pfasst::examples::advection_diffusion
} // ::pfasst::examples
} // ::pfasst

#ifndef PFASST_UNIT_TESTING
int main(int argc, char** argv)
{
pfasst::examples::advection_diffusion::AdvectionDiffusionSweeper<>::enable_config_options();
pfasst::init(argc, argv);
pfasst::log::add_custom_logger("Advec");

pfasst::examples::advection_diffusion::run_serial_mlsdc_autobuild();
}
#endif
1 change: 1 addition & 0 deletions examples/advection_diffusion/vanilla_sdc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ int main(int argc, char** argv)
{
pfasst::examples::advection_diffusion::AdvectionDiffusionSweeper<>::enable_config_options();
pfasst::init(argc, argv);
pfasst::log::add_custom_logger("Advec");

pfasst::examples::advection_diffusion::run_vanilla_sdc(0.0);
}
Expand Down
9 changes: 9 additions & 0 deletions include/pfasst/encap/imex_sweeper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>

#include "../globals.hpp"
#include "../logging.hpp"
#include "../quadrature.hpp"
#include "encapsulation.hpp"
#include "encap_sweeper.hpp"
Expand Down Expand Up @@ -292,6 +293,8 @@ namespace pfasst
{
time dt = this->get_controller()->get_time_step();
time t = this->get_controller()->get_time();
CLOG(INFO, "Sweeper") << "predicting step " << this->get_controller()->get_step() + 1
<< " (t=" << t << ", dt=" << dt << ")";

if (initial) {
this->state[0]->copy(this->start_state);
Expand Down Expand Up @@ -319,6 +322,8 @@ namespace pfasst
UNUSED(initial);
time dt = this->get_controller()->get_time_step();
time t = this->get_controller()->get_time();
CLOG(INFO, "Sweeper") << "predicting step " << this->get_controller()->get_step() + 1
<< " (t=" << t << ", dt=" << dt << ")";
time ds;

shared_ptr<Encapsulation<time>> rhs = this->get_factory()->create(pfasst::encap::solution);
Expand Down Expand Up @@ -349,6 +354,8 @@ namespace pfasst
auto const nodes = this->quadrature->get_nodes();
auto const dt = this->get_controller()->get_time_step();
auto const s_mat = this->quadrature->get_s_mat().block(1, 0, nodes.size()-1, nodes.size());
CLOG(INFO, "Sweeper") << "sweeping on step " << this->get_controller()->get_step() + 1
<< " in iteration " << this->get_controller()->get_iteration() << " (dt=" << dt << ")";
time ds;

this->s_integrals[0]->mat_apply(this->s_integrals, dt, s_mat, this->fs_expl, true);
Expand Down Expand Up @@ -384,6 +391,8 @@ namespace pfasst
auto const nodes = this->quadrature->get_nodes();
auto const dt = this->get_controller()->get_time_step();
auto const s_mat = this->quadrature->get_s_mat();
CLOG(INFO, "Sweeper") << "sweeping on step " << this->get_controller()->get_step() + 1
<< " in iteration " << this->get_controller()->get_iteration() << " (dt=" << dt << ")";
time ds;

this->s_integrals[0]->mat_apply(this->s_integrals, dt, s_mat, this->fs_expl, true);
Expand Down
91 changes: 67 additions & 24 deletions include/pfasst/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string>
using namespace std;

#include <boost/algorithm/string.hpp>

struct OUT
{
public:
Expand Down Expand Up @@ -97,6 +99,8 @@ const string OUT::reset = "\033[0m";

#define LOG_INDENT string(pfasst::log::stack_position * 2, ' ')

//! length of logger ID to print
#define LOGGER_ID_LENGTH 8

namespace pfasst
{
Expand All @@ -120,43 +124,82 @@ namespace pfasst
static size_t stack_position;

/**
* sets default configuration for default loggers
* \brief provides convenient way of adding additional named loggers
* \details With this function one can easily create additional named loggers distinctable by the `id`.
* The first \ref LOGGER_ID_LENGTH characters of the ID (as uppercase) will be included in every line of the log.
* The ID is used in the actual logging calls.
*
* \code{.cpp}
* add_custom_logger("MyCustomLogger")
* // somewhere else in the code
* CLOG(INFO, "MyCustomLogger") << "a logging message";
* \endcode
*
* This results in the log line (for the default value of \ref LOGGER_ID_LENGTH):
*
* <TIME> [MYCUSTOM, INFO ] a logging message
* \note Please make sure to use `CLOG` (and `CVLOG` for verbose logging) to be able to specify a specific logger.
* Otherwise the default logger will be used.
* \param[in] id The ID of the logger. This is used in logging calls.
*/
inline static void load_default_config()
inline static void add_custom_logger(const string& id)
{
const string TIMESTAMP = OUT::white + "%datetime{%H:%m:%s,%g}" + OUT::reset + " ";
const string LEVEL = "[%level]";
const string VLEVEL = "[VERB%vlevel]";
const string LEVEL = "%level]";
const string VLEVEL = "VERB%vlevel]";
const string POSITION = "%fbase:%line";
const string MESSAGE = "%msg";

const string INFO_COLOR = OUT::blue;
const string DEBG_COLOR = "";
const string WARN_COLOR = OUT::magenta;
const string ERRO_COLOR = OUT::red;
const string FATA_COLOR = OUT::red + OUT::bold;
const string VERB_COLOR = OUT::white;

const size_t id_length = id.size();
string id2print = id.substr(0, LOGGER_ID_LENGTH);
boost::to_upper(id2print);
if (id_length < LOGGER_ID_LENGTH) {
id2print.append(LOGGER_ID_LENGTH - id_length, ' ');
}

el::Logger* logger = el::Loggers::getLogger(id);
el::Configurations* conf = logger->configurations();
conf->set(el::Level::Info, el::ConfigurationType::Format,
TIMESTAMP + INFO_COLOR + "[" + id2print + ", " + LEVEL + " " + MESSAGE + OUT::reset);
conf->set(el::Level::Debug, el::ConfigurationType::Format,
TIMESTAMP + DEBG_COLOR + "[" + id2print + ", " + LEVEL + " " + POSITION + " " + MESSAGE + OUT::reset);
conf->set(el::Level::Warning, el::ConfigurationType::Format,
TIMESTAMP + WARN_COLOR + "[" + id2print + ", " + LEVEL + " " + MESSAGE + OUT::reset);
conf->set(el::Level::Error, el::ConfigurationType::Format,
TIMESTAMP + ERRO_COLOR + "[" + id2print + ", " + LEVEL + " " + MESSAGE + OUT::reset);
conf->set(el::Level::Fatal, el::ConfigurationType::Format,
TIMESTAMP + FATA_COLOR + "[" + id2print + ", " + LEVEL + " " + POSITION + " " + MESSAGE + OUT::reset);
conf->set(el::Level::Verbose, el::ConfigurationType::Format,
TIMESTAMP + VERB_COLOR + "[" + id2print + ", " + VLEVEL + " " + MESSAGE + OUT::reset);
el::Loggers::reconfigureLogger(logger, *conf);
}

/**
* sets default configuration for default loggers
*/
inline static void load_default_config()
{
el::Configurations defaultConf;
defaultConf.setToDefault();

defaultConf.setGlobally(el::ConfigurationType::Format, "%msg");
defaultConf.setGlobally(el::ConfigurationType::ToFile, "false");
defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
defaultConf.setGlobally(el::ConfigurationType::MillisecondsWidth, PFASST_LOGGER_DEFAULT_GLOBAL_MILLISECOND_WIDTH);

defaultConf.set(el::Level::Info, el::ConfigurationType::Format,
TIMESTAMP + OUT::blue + LEVEL + " " + MESSAGE + OUT::reset);

defaultConf.set(el::Level::Debug, el::ConfigurationType::Format,
TIMESTAMP + LEVEL + " " + POSITION + " " + MESSAGE + OUT::reset);

defaultConf.set(el::Level::Warning, el::ConfigurationType::Format,
TIMESTAMP + OUT::magenta + LEVEL + " " + MESSAGE + OUT::reset);

defaultConf.set(el::Level::Error, el::ConfigurationType::Format,
TIMESTAMP + OUT::red + LEVEL + " " + MESSAGE + OUT::reset);

defaultConf.set(el::Level::Fatal, el::ConfigurationType::Format,
TIMESTAMP + OUT::red + OUT::bold + LEVEL + " " + POSITION + " " + MESSAGE + OUT::reset);

defaultConf.set(el::Level::Verbose, el::ConfigurationType::Format,
TIMESTAMP + OUT::white + VLEVEL + " " + MESSAGE + OUT::reset);

el::Loggers::reconfigureAllLoggers(defaultConf);

add_custom_logger("default");
add_custom_logger("Controller");
add_custom_logger("Sweeper");
add_custom_logger("Encap");
add_custom_logger("Quadrature");
add_custom_logger("User");
}

/**
Expand Down
Loading

0 comments on commit 549019c

Please sign in to comment.