From bec0b4451e640007e69099d70bae48df5151105b Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Wed, 17 Oct 2018 23:45:28 +0200
Subject: [PATCH] Prepare MPI test infrastructure

---
 CMakeLists.txt               |  7 +++++--
 tests/CMakeLists.txt         | 24 ++++++++++++++++++++++++
 tests/mpi_test_Messenger.cpp | 19 +++++++++++++++++++
 tests/mpi_test_main.cpp      | 18 ++++++++++++++++++
 4 files changed, 66 insertions(+), 2 deletions(-)
 create mode 100644 tests/mpi_test_Messenger.cpp
 create mode 100644 tests/mpi_test_main.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e9d392819..73ee21548 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -33,7 +33,6 @@ SET("CMAKE_C_FLAGS_RELWITHDEBINFO"
   FORCE )
 
 # Add new build types
-message("* Adding build types...")
 SET(CMAKE_CXX_FLAGS_COVERAGE
   "-g -O0 --coverage"
   CACHE STRING "Flags used by the C++ compiler during coverage builds."
@@ -204,10 +203,14 @@ if("${CMAKE_BUILD_TYPE}" STREQUAL "Coverage")
 
   set(GCOVR_OPTIONS --object-directory="${PASTIS_BINARY_DIR}" -r "${PASTIS_SOURCE_DIR}/src" ${GCOVR_EXCLUDE} ${GCOVR_EXTRA})
 
+  if(${PASTIS_HAS_MPI})
+    set(MPI_UNIT_TESTS mpi_unit_tests)
+  endif()
+
   add_custom_target(run_unit_tests
     ALL
     COMMAND ${CMAKE_CTEST_COMMAND} -j ${PROCESSOR_COUNT}
-    DEPENDS unit_tests pastis
+    DEPENDS unit_tests ${MPI_UNIT_TESTS} pastis
     COMMENT "Executing unit tests."
     )
 
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 94118a24d..a2370f57d 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -13,6 +13,13 @@ add_executable (unit_tests
   test_TinyVector.cpp
   )
 
+if (${PASTIS_HAS_MPI})
+  add_executable (mpi_unit_tests
+    mpi_test_main.cpp
+    mpi_test_Messenger.cpp
+    )
+endif()
+
 target_include_directories(Catch2 INTERFACE ${CATCH_INCLUDE_DIR})
 
 target_link_libraries (unit_tests
@@ -21,7 +28,24 @@ target_link_libraries (unit_tests
   Catch2
   )
 
+if (${PASTIS_HAS_MPI})
+  target_link_libraries (mpi_unit_tests
+    PastisUtils
+    kokkos
+    ${PARMETIS_LIBRARIES}
+    ${MPI_CXX_LINK_FLAGS} ${MPI_CXX_LIBRARIES}
+    Catch2
+    )
+endif()
+
 enable_testing()
 
 #parse catch tests
 ParseAndAddCatchTests(unit_tests)
+
+if (${PASTIS_HAS_MPI})
+  add_test(mpi_unit_tests ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} 3 "--oversubscribe"
+    ${MPIEXEC_PREFLAGS}
+    ${PASTIS_BINARY_DIR}/mpi_unit_tests
+    ${MPIEXEC_POSTFLAGS})
+endif()
diff --git a/tests/mpi_test_Messenger.cpp b/tests/mpi_test_Messenger.cpp
new file mode 100644
index 000000000..d39c18c19
--- /dev/null
+++ b/tests/mpi_test_Messenger.cpp
@@ -0,0 +1,19 @@
+#include <catch.hpp>
+
+#include <Messenger.hpp>
+#include <mpi.h>
+
+TEST_CASE("Messenger", "[mpi]") {
+
+  SECTION("check for parallel test") {
+    {
+      int rank;
+      MPI_Comm_rank(MPI_COMM_WORLD, &rank);
+      REQUIRE(rank == commRank());
+
+      int size;
+      MPI_Comm_size(MPI_COMM_WORLD, &size);
+      REQUIRE(size == commSize());
+    }
+  }
+}
diff --git a/tests/mpi_test_main.cpp b/tests/mpi_test_main.cpp
new file mode 100644
index 000000000..b6f2a172a
--- /dev/null
+++ b/tests/mpi_test_main.cpp
@@ -0,0 +1,18 @@
+#define CATCH_CONFIG_RUNNER
+#include <catch.hpp>
+
+#include <Kokkos_Core.hpp>
+#include <Messenger.hpp>
+
+int main( int argc, char* argv[] )
+{
+  Messenger::create(argc,  argv);
+  Kokkos::initialize({4,-1,-1,true});
+
+  int result = Catch::Session().run( argc, argv );
+
+  Kokkos::finalize();
+  Messenger::destroy();
+
+  return result;
+}
-- 
GitLab