Commit 87a7960d authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

Merge branch 'issue/parallel_output' into 'master'

write only rank 0 in parallel

See merge request !71
parents 721fda7d ee995fd5
...@@ -14,14 +14,11 @@ ...@@ -14,14 +14,11 @@
#include <stdexcept> #include <stdexcept>
#endif #endif
#ifdef HAVE_MPI
#include <mpi.h>
#endif
/// Use the formatting librart fmtlib::fmt /// Use the formatting librart fmtlib::fmt
#include <fmt/core.h> #include <fmt/core.h>
#include <fmt/ostream.h> #include <fmt/ostream.h>
#include <amdis/Environment.hpp>
#include <amdis/common/TypeTraits.hpp> #include <amdis/common/TypeTraits.hpp>
/** /**
...@@ -41,6 +38,11 @@ ...@@ -41,6 +38,11 @@
#endif #endif
#endif #endif
/// Level bound for output of additional information. See \ref info() and \ref info_()
#ifndef AMDIS_INFO_LEVEL
#define AMDIS_INFO_LEVEL 1
#endif
#define AMDIS_UNUSED(var) __attribute__((unused)) var #define AMDIS_UNUSED(var) __attribute__((unused)) var
#define AMDIS_FUNCNAME(nn) AMDIS_UNUSED(const char *funcName); funcName = nn; #define AMDIS_FUNCNAME(nn) AMDIS_UNUSED(const char *funcName); funcName = nn;
...@@ -59,23 +61,27 @@ namespace AMDiS ...@@ -59,23 +61,27 @@ namespace AMDiS
namespace Impl namespace Impl
{ {
template <class OStream, class... Args> template <class OStream, class... Args>
OStream& msg(OStream& out, Args&&... args) OStream& msg_all(OStream& out, std::string const& str, Args&&... args)
{
if (Environment::mpiSize() > 1)
fmt::print(out, "[" + std::to_string(Environment::mpiRank()) + "] " + str, FWD(args)...);
else
fmt::print(out, str, FWD(args)...);
return out;
}
template <class OStream, class... Args>
OStream& msg(OStream& out, std::string const& str, Args&&... args)
{ {
#ifdef AMDIS_HAS_MPI #ifdef AMDIS_MSG_ALL_RANKS
int rank = -1; return msg_all(out, str, FWD(args)...);
int num_ranks = -1;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_ranks);
if (num_ranks > 1 && rank == 0) {
out << "[0] ";
fmt::print(out, FWD(args)...);
} else if (num_ranks == 1) {
fmt::print(out, FWD(args)...);
}
#else #else
fmt::print(out, FWD(args)...); if (Environment::mpiSize() > 1 && Environment::mpiRank() == 0)
#endif fmt::print(out, "[0] " + str, FWD(args)...);
else if (Environment::mpiSize() == 1)
fmt::print(out, str, FWD(args)...);
return out; return out;
#endif
} }
} // end namespace Impl } // end namespace Impl
...@@ -89,9 +95,17 @@ namespace AMDiS ...@@ -89,9 +95,17 @@ namespace AMDiS
* ``` * ```
**/ **/
template <class... Args> template <class... Args>
void msg(Args&&... args) void msg(std::string const& str, Args&&... args)
{
Impl::msg(std::cout, str + "\n", FWD(args)...);
}
/// prints a message, if Environment::infoLevel() >= noInfoLevel
template <class... Args>
void info(int noInfoLevel, std::string const& str, Args&&... args)
{ {
Impl::msg(std::cout, FWD(args)...) << std::endl; if (int(AMDIS_INFO_LEVEL) >= noInfoLevel)
Impl::msg(std::cout, str + "\n", FWD(args)...);
} }
...@@ -103,11 +117,20 @@ namespace AMDiS ...@@ -103,11 +117,20 @@ namespace AMDiS
* ``` * ```
**/ **/
template <class... Args> template <class... Args>
void msg_(Args&&... args) void msg_(std::string const& str, Args&&... args)
{ {
Impl::msg(std::cout, FWD(args)...); Impl::msg(std::cout, str, FWD(args)...);
} }
/// prints a message, if Environment::infoLevel() >= noInfoLevel
template <class... Args>
void info_(int noInfoLevel, std::string const& str, Args&&... args)
{
if (int(AMDIS_INFO_LEVEL) >= noInfoLevel)
Impl::msg(std::cout, str, FWD(args)...);
}
/// \brief print a message and exit /// \brief print a message and exit
/** /**
...@@ -116,17 +139,13 @@ namespace AMDiS ...@@ -116,17 +139,13 @@ namespace AMDiS
* type \ref std::runtime_Error is thrown. * type \ref std::runtime_Error is thrown.
**/ **/
template <class... Args> template <class... Args>
void error_exit(Args&&... args) void error_exit(std::string const& str, Args&&... args)
{ {
#ifdef AMDIS_NO_THROW #ifdef AMDIS_NO_THROW
Impl::msg(std::cerr << "ERROR: ", FWD(args)...) << std::endl; Impl::msg_all(std::cerr, "ERROR: " + str + "\n", FWD(args)...);
#ifndef NDEBUG std::abort();
assert(false);
#else
std::exit(EXIT_FAILURE);
#endif
#else #else
throw std::runtime_error( std::string("ERROR: ") + fmt::format(FWD(args)...)); throw std::runtime_error( std::string("ERROR: ") + fmt::format(str, FWD(args)...));
#endif #endif
} }
...@@ -141,16 +160,16 @@ namespace AMDiS ...@@ -141,16 +160,16 @@ namespace AMDiS
* \p condition argument. * \p condition argument.
**/ **/
template <class... Args> template <class... Args>
void test_exit(bool condition, Args&&... args) void test_exit(bool condition, std::string const& str, Args&&... args)
{ {
if (!condition) { error_exit(FWD(args)...); } if (!condition) { error_exit(str, FWD(args)...); }
} }
template <class... Args> template <class... Args>
void warning(Args&&... args) void warning(std::string const& str, Args&&... args)
{ {
Impl::msg(std::cout << "WARNING: ", FWD(args)...) << std::endl; Impl::msg(std::cout, "WARNING: " + str + "\n", FWD(args)...);
} }
...@@ -161,9 +180,9 @@ namespace AMDiS ...@@ -161,9 +180,9 @@ namespace AMDiS
* string "WARNING". * string "WARNING".
**/ **/
template <class... Args> template <class... Args>
void test_warning(bool condition, Args&&... args) void test_warning(bool condition, std::string const& str, Args&&... args)
{ {
if (!condition) { warning(FWD(args)...); } if (!condition) { warning(str, FWD(args)...); }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment