From be2c0161e6f162e724627101896f75753f5ec32c Mon Sep 17 00:00:00 2001
From: Simon Praetorius <simon.praetorius@tu-dresden.de>
Date: Thu, 13 Jun 2019 19:33:57 +0200
Subject: [PATCH] write only rank 0 in parallel

---
 src/amdis/Output.hpp | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/src/amdis/Output.hpp b/src/amdis/Output.hpp
index 91264e57..38c92edc 100644
--- a/src/amdis/Output.hpp
+++ b/src/amdis/Output.hpp
@@ -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>
 
 /**
@@ -59,21 +56,20 @@ namespace AMDiS
   namespace Impl
   {
     template <class OStream, class... Args>
-    OStream& msg(OStream& out, Args&&... args)
+    OStream& msg(OStream& out, bool lineBreak, 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) {
+#if HAVE_MPI
+      if (Environment::mpiSize() > 1 && Environment::mpiRank() == 0) {
         out << "[0] ";
         fmt::print(out, FWD(args)...);
-      } else if (num_ranks == 1) {
+        if (lineBreak) { out << std::endl; }
+      } else if (Environment::mpiSize() == 1) {
         fmt::print(out, FWD(args)...);
+        if (lineBreak) { out << std::endl; }
       }
 #else
       fmt::print(out, FWD(args)...);
+      if (lineBreak) { out << std::endl; }
 #endif
       return out;
     }
@@ -91,7 +87,7 @@ namespace AMDiS
   template <class... Args>
   void msg(Args&&... args)
   {
-    Impl::msg(std::cout, FWD(args)...) << std::endl;
+    Impl::msg(std::cout, true, FWD(args)...);
   }
 
 
@@ -105,7 +101,7 @@ namespace AMDiS
   template <class... Args>
   void msg_(Args&&... args)
   {
-    Impl::msg(std::cout, FWD(args)...);
+    Impl::msg(std::cout, false, FWD(args)...);
   }
 
 
@@ -119,7 +115,7 @@ namespace AMDiS
   void error_exit(Args&&... args)
   {
 #ifdef AMDIS_NO_THROW
-    Impl::msg(std::cerr << "ERROR: ", FWD(args)...) << std::endl;
+    Impl::msg(std::cerr << "ERROR: ", true, FWD(args)...);
     #ifndef NDEBUG
       assert(false);
     #else
@@ -150,7 +146,7 @@ namespace AMDiS
   template <class... Args>
   void warning(Args&&... args)
   {
-    Impl::msg(std::cout << "WARNING: ", FWD(args)...) << std::endl;
+    Impl::msg(std::cout << "WARNING: ", true, FWD(args)...);
   }
 
 
-- 
GitLab