From c8e724ac311611bc224cba51d049769d3f227a23 Mon Sep 17 00:00:00 2001
From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz>
Date: Mon, 4 Feb 2019 16:45:30 +0100
Subject: [PATCH] Improved macro TNL_MPI_RINT to work even wihout MPI. Added
 macro TNL_MPI_PRINT_COND for conditional print outs.

---
 src/TNL/Communicators/MpiCommunicator.h | 60 +++++++++++++++++++------
 1 file changed, 47 insertions(+), 13 deletions(-)

diff --git a/src/TNL/Communicators/MpiCommunicator.h b/src/TNL/Communicators/MpiCommunicator.h
index cc5a5cb57f..52dec3b95d 100644
--- a/src/TNL/Communicators/MpiCommunicator.h
+++ b/src/TNL/Communicators/MpiCommunicator.h
@@ -536,17 +536,51 @@ bool MpiCommunicator::redirect = true;
 } // namespace Communicators
 } // namespace TNL
 
-#define TNL_MPI_PRINT( message )                                                                                              \
-for( int __tnl_mpi_print_j = 0;                                                                                               \
-     __tnl_mpi_print_j < TNL::Communicators::MpiCommunicator::GetSize( TNL::Communicators::MpiCommunicator::AllGroup );       \
-     __tnl_mpi_print_j++ )                                                                                                    \
-{                                                                                                                             \
-   if( __tnl_mpi_print_j == TNL::Communicators::MpiCommunicator::GetRank( TNL::Communicators::MpiCommunicator::AllGroup ) )   \
-   {                                                                                                                          \
-      std::cerr << "Node " << __tnl_mpi_print_j << " of "                                                                     \
-                << TNL::Communicators::MpiCommunicator::GetSize( TNL::Communicators::MpiCommunicator::AllGroup )              \
-                << " : " << message << std::endl;                                                                             \
-   }                                                                                                                          \
-   TNL::Communicators::MpiCommunicator::Barrier( TNL::Communicators::MpiCommunicator::AllGroup );                             \
-}
+#ifdef HAVE_MPI
+#define TNL_MPI_PRINT( message )                                                                                                 \
+if( ! TNL::Communicators::MpiCommunicator::IsInitialized() )                                                                     \
+   std::cerr << message << std::endl;                                                                                            \
+else                                                                                                                             \
+   for( int __tnl_mpi_print_j = 0;                                                                                               \
+        __tnl_mpi_print_j < TNL::Communicators::MpiCommunicator::GetSize( TNL::Communicators::MpiCommunicator::AllGroup );       \
+        __tnl_mpi_print_j++ )                                                                                                    \
+   {                                                                                                                             \
+      if( __tnl_mpi_print_j == TNL::Communicators::MpiCommunicator::GetRank( TNL::Communicators::MpiCommunicator::AllGroup ) )   \
+      {                                                                                                                          \
+         std::cerr << "Node " << __tnl_mpi_print_j << " of "                                                                     \
+                   << TNL::Communicators::MpiCommunicator::GetSize( TNL::Communicators::MpiCommunicator::AllGroup )              \
+                   << " : " << message << std::endl;                                                                             \
+      }                                                                                                                          \
+      TNL::Communicators::MpiCommunicator::Barrier( TNL::Communicators::MpiCommunicator::AllGroup );                             \
+   }
+#else
+#define TNL_MPI_PRINT( message )                                                                                                 \
+   std::cerr << message << std::endl;
+#endif
 
+#ifdef HAVE_MPI
+#define TNL_MPI_PRINT_COND( condition, message )                                                                                 \
+if( ! TNL::Communicators::MpiCommunicator::IsInitialized() )                                                                     \
+{                                                                                                                                \
+   if( condition ) std::cerr << message << std::endl;                                                                            \
+}                                                                                                                                \
+else                                                                                                                             \
+{                                                                                                                                \
+   for( int __tnl_mpi_print_j = 0;                                                                                               \
+        __tnl_mpi_print_j < TNL::Communicators::MpiCommunicator::GetSize( TNL::Communicators::MpiCommunicator::AllGroup );       \
+        __tnl_mpi_print_j++ )                                                                                                    \
+   {                                                                                                                             \
+      if( __tnl_mpi_print_j == TNL::Communicators::MpiCommunicator::GetRank( TNL::Communicators::MpiCommunicator::AllGroup ) )   \
+      {                                                                                                                          \
+         if( condition )                                                                                                         \
+            std::cerr << "Node " << __tnl_mpi_print_j << " of "                                                                  \
+                      << TNL::Communicators::MpiCommunicator::GetSize( TNL::Communicators::MpiCommunicator::AllGroup )           \
+                      << " : " << message << std::endl;                                                                          \
+      }                                                                                                                          \
+      TNL::Communicators::MpiCommunicator::Barrier( TNL::Communicators::MpiCommunicator::AllGroup );                             \
+   }                                                                                                                             \
+}
+#else
+#define TNL_MPI_PRINT_COND( condition, message )                                                                                 \
+   if( condition ) std::cerr << message << std::endl;
+#endif
-- 
GitLab