From 7fe989d799d4d1d02536a540b7e8b6b6d8462439 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= <oberhuber.tomas@gmail.com>
Date: Sat, 9 Feb 2019 18:20:27 +0100
Subject: [PATCH] Fixed TNL_MPI_PRINT and added TNL_MPI_PRINT_MASTER.

---
 src/TNL/Communicators/MPIPrint.h              | 106 ++++++++++++++++++
 .../DistributedMeshes/BufferEntitiesHelper.h  |   1 +
 2 files changed, 107 insertions(+)
 create mode 100644 src/TNL/Communicators/MPIPrint.h

diff --git a/src/TNL/Communicators/MPIPrint.h b/src/TNL/Communicators/MPIPrint.h
new file mode 100644
index 0000000000..202374677b
--- /dev/null
+++ b/src/TNL/Communicators/MPIPrint.h
@@ -0,0 +1,106 @@
+/***************************************************************************
+                          MPIPrint.h  -  description
+                             -------------------
+    begin                : Feb 7, 2019
+    copyright            : (C) 2019 by Tomas Oberhuber
+    email                : tomas.oberhuber@fjfi.cvut.cz
+ ***************************************************************************/
+
+/* See Copyright Notice in tnl/Copyright */
+
+#pragma once
+
+#include <sstream>
+#include <TNL/Communicators/MpiCommunicator.h>
+
+#ifdef HAVE_MPI
+#define TNL_MPI_PRINT( message )                                                                                                 \
+if( ! TNL::Communicators::MpiCommunicator::IsInitialized() )                                                                     \
+   std::cerr << message << std::endl;                                                                                            \
+else                                                                                                                             \
+{                                                                                                                                \
+   if( TNL::Communicators::MpiCommunicator::GetRank() > 0 )                                                                      \
+   {                                                                                                                             \
+      std::stringstream __tnl_mpi_print_stream_;                                                                                 \
+      __tnl_mpi_print_stream_ << "Node " << TNL::Communicators::MpiCommunicator::GetRank() << " of "                             \
+         << TNL::Communicators::MpiCommunicator::GetSize() << " : " << message << std::endl;                                     \
+      TNL::String __tnl_mpi_print_string_( __tnl_mpi_print_stream_.str().c_str() );                                              \
+      __tnl_mpi_print_string_.send( 0 );                                                                                         \
+   }                                                                                                                             \
+   else                                                                                                                          \
+   {                                                                                                                             \
+      std::cerr << "Node 0 of " << TNL::Communicators::MpiCommunicator::GetSize() << " : " << message << std::endl;              \
+      for( int __tnl_mpi_print_j = 1;                                                                                            \
+           __tnl_mpi_print_j < TNL::Communicators::MpiCommunicator::GetSize();                                                   \
+           __tnl_mpi_print_j++ )                                                                                                 \
+         {                                                                                                                       \
+            TNL::String __tnl_mpi_print_string_;                                                                                 \
+            __tnl_mpi_print_string_.receive( __tnl_mpi_print_j );                                                                \
+            std::cerr << __tnl_mpi_print_string_;                                                                                \
+         }                                                                                                                       \
+   }                                                                                                                             \
+}
+#else
+#define TNL_MPI_PRINT( message )                                                                                                 \
+   std::cerr << message << std::endl;
+#endif
+
+#ifdef HAVE_MPI
+#define TNL_MPI_PRINT_MASTER( message )                                                                                          \
+if( ! TNL::Communicators::MpiCommunicator::IsInitialized() )                                                                     \
+   std::cerr << message << std::endl;                                                                                            \
+else                                                                                                                             \
+{                                                                                                                                \
+   if( TNL::Communicators::MpiCommunicator::GetRank() == 0 )                                                                     \
+   {                                                                                                                             \
+      std::cerr << "Master node : " << message << std::endl;                                                                     \
+   }                                                                                                                             \
+}
+#else
+#define TNL_MPI_PRINT_MASTER( 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                                                                                                                             \
+{                                                                                                                                \
+   if( TNL::Communicators::MpiCommunicator::GetRank() > 0 )                                                                      \
+   {                                                                                                                             \
+      int __tnl_mpi_print_cnd = ( condition );                                                                                   \
+      TNL::Communicators::MpiCommunicator::Send( &__tnl_mpi_print_cnd, 1, 0, 0 );                                                \
+      if( condition ) {                                                                                                          \
+         std::stringstream __tnl_mpi_print_stream_;                                                                              \
+         __tnl_mpi_print_stream_ << "Node " << TNL::Communicators::MpiCommunicator::GetRank() << " of "                          \
+            << TNL::Communicators::MpiCommunicator::GetSize() << " : " << message << std::endl;                                  \
+         TNL::String __tnl_mpi_print_string_( __tnl_mpi_print_stream_.str().c_str() );                                           \
+         __tnl_mpi_print_string_.send( 0 );                                                                                      \
+      }                                                                                                                          \
+   }                                                                                                                             \
+   else                                                                                                                          \
+   {                                                                                                                             \
+      if( condition )                                                                                                            \
+         std::cerr << "Node 0 of " << TNL::Communicators::MpiCommunicator::GetSize() << " : " << message << std::endl;           \
+      for( int __tnl_mpi_print_j = 1;                                                                                            \
+           __tnl_mpi_print_j < TNL::Communicators::MpiCommunicator::GetSize();                                                   \
+           __tnl_mpi_print_j++ )                                                                                                 \
+         {                                                                                                                       \
+            int __tnl_mpi_print_cond;                                                                                            \
+            TNL::Communicators::MpiCommunicator::Recv( &__tnl_mpi_print_cond, 1, __tnl_mpi_print_j, 0 );                         \
+            if( __tnl_mpi_print_cond )                                                                                           \
+            {                                                                                                                    \
+               TNL::String __tnl_mpi_print_string_;                                                                              \
+               __tnl_mpi_print_string_.receive( __tnl_mpi_print_j );                                                             \
+               std::cerr << __tnl_mpi_print_string_;                                                                             \
+            }                                                                                                                    \
+         }                                                                                                                       \
+   }                                                                                                                             \
+}
+#else
+#define TNL_MPI_PRINT_COND( condition, message )                                                                                 \
+   std::cerr << message << std::endl;
+#endif
\ No newline at end of file
diff --git a/src/TNL/Meshes/DistributedMeshes/BufferEntitiesHelper.h b/src/TNL/Meshes/DistributedMeshes/BufferEntitiesHelper.h
index ba2602af21..a20faf0a58 100644
--- a/src/TNL/Meshes/DistributedMeshes/BufferEntitiesHelper.h
+++ b/src/TNL/Meshes/DistributedMeshes/BufferEntitiesHelper.h
@@ -14,6 +14,7 @@
 #include <TNL/Devices/Cuda.h>
 #include <TNL/ParallelFor.h>
 #include <TNL/Containers/StaticVector.h>
+#include <TNL/Communicators/MPIPrint.h>
 
 namespace TNL {
 namespace Meshes {
-- 
GitLab