Skip to content
Snippets Groups Projects

write only rank 0 in parallel

Merged Praetorius, Simon requested to merge issue/parallel_output into master
+ 55
36
@@ -14,14 +14,11 @@
#include <stdexcept>
#endif
#ifdef HAVE_MPI
#include <mpi.h>
#endif
/// Use the formatting librart fmtlib::fmt
#include <fmt/core.h>
#include <fmt/ostream.h>
#include <amdis/Environment.hpp>
#include <amdis/common/TypeTraits.hpp>
/**
@@ -41,6 +38,11 @@
#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_FUNCNAME(nn) AMDIS_UNUSED(const char *funcName); funcName = nn;
@@ -59,23 +61,27 @@ namespace AMDiS
namespace Impl
{
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
int rank = -1;
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)...);
}
#ifdef AMDIS_MSG_ALL_RANKS
return msg_all(out, str, FWD(args)...);
#else
fmt::print(out, FWD(args)...);
#endif
if (Environment::mpiSize() > 1 && Environment::mpiRank() == 0)
fmt::print(out, "[0] " + str, FWD(args)...);
else if (Environment::mpiSize() == 1)
fmt::print(out, str, FWD(args)...);
return out;
#endif
}
} // end namespace Impl
@@ -89,9 +95,17 @@ namespace AMDiS
* ```
**/
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
* ```
**/
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
/**
@@ -116,17 +139,13 @@ namespace AMDiS
* type \ref std::runtime_Error is thrown.
**/
template <class... Args>
void error_exit(Args&&... args)
void error_exit(std::string const& str, Args&&... args)
{
#ifdef AMDIS_NO_THROW
Impl::msg(std::cerr << "ERROR: ", FWD(args)...) << std::endl;
#ifndef NDEBUG
assert(false);
#else
std::exit(EXIT_FAILURE);
#endif
Impl::msg_all(std::cerr, "ERROR: " + str + "\n", FWD(args)...);
std::abort();
#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
}
@@ -141,16 +160,16 @@ namespace AMDiS
* \p condition argument.
**/
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>
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
* string "WARNING".
**/
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)...); }
}
Loading