From 088af030fcf070c92542dfac4043f072b0b8cf9b Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Fri, 21 Sep 2018 14:34:20 +0200
Subject: [PATCH] Define PASTIS_HAS_MPI when MPI is found and used

Use the macro in a crappy way to allow non MPI builds to compile
---
 CMakeLists.txt                 |  2 ++
 src/utils/PastisUtils.cpp      | 10 ++++++++++
 src/utils/pastis_config.hpp.in |  2 ++
 3 files changed, 14 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1cbf56fa7..01b131228 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -94,6 +94,8 @@ elseif(PASTIS_ENABLE_MPI STREQUAL "ON")
   message(FATAL_ERROR "Could not find MPI library while requested")
 endif()
 
+set(PASTIS_HAS_MPI ${MPI_FOUND})
+
 #------------------------------------------------------
 
 # setting Kokkos defaults to OpenMP when available
diff --git a/src/utils/PastisUtils.cpp b/src/utils/PastisUtils.cpp
index 72764650f..28c130e52 100644
--- a/src/utils/PastisUtils.cpp
+++ b/src/utils/PastisUtils.cpp
@@ -13,10 +13,14 @@
 #include <ConsoleManager.hpp>
 
 #include <CLI/CLI.hpp>
+
+#ifdef PASTIS_HAS_MPI
 #include <mpi.h>
+#endif // PASTIS_HAS_MPI
 
 std::string initialize(int& argc, char* argv[])
 {
+#ifdef PASTIS_HAS_MPI
   MPI_Init(&argc, &argv);
 
   {
@@ -30,6 +34,7 @@ std::string initialize(int& argc, char* argv[])
       pout.setOutput(null_stream);
     }
   }
+#endif // PASTIS_HAS_MPI
 
   long unsigned number = 10;
   std::string filename;
@@ -115,6 +120,7 @@ std::string initialize(int& argc, char* argv[])
 
 void finalize()
 {
+#ifdef PASTIS_HAS_MPI
   MPI_Barrier(MPI_COMM_WORLD);
   const int mpi_rank
       =[](){
@@ -133,7 +139,11 @@ void finalize()
   pout() << rang::fgB::green << "Terminating process " << rang::fg::reset
          << rang::fgB::yellow << mpi_rank << rang::fg::reset << " of "
          << rang::style::bold << mpi_size << rang::style::reset << '\n';;
+#endif // PASTIS_HAS_MPI
 
   Kokkos::finalize();
+
+#ifdef PASTIS_HAS_MPI
   MPI_Finalize();
+#endif // PASTIS_HAS_MPI
 }
diff --git a/src/utils/pastis_config.hpp.in b/src/utils/pastis_config.hpp.in
index b27b12f70..9cd9e145b 100644
--- a/src/utils/pastis_config.hpp.in
+++ b/src/utils/pastis_config.hpp.in
@@ -2,6 +2,8 @@
 #define PASTIS_CONFIG_HPP
 
 #cmakedefine PASTIS_HAS_FENV_H
+#cmakedefine PASTIS_HAS_MPI
+
 #cmakedefine SYSTEM_IS_LINUX
 #cmakedefine SYSTEM_IS_DARWIN
 #cmakedefine SYSTEM_IS_WINDOWS
-- 
GitLab