From 86e477f80fa81779980a3f974ea4cc7b37638d46 Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Tue, 26 Feb 2019 18:39:38 +0100
Subject: [PATCH] Prepare parallel synchronization

---
 src/mesh/ItemValueSynchronizer.hpp | 32 ++++++++++++++++++++++++++++++
 src/mesh/ItemValueUtils.hpp        | 14 +++++++++++++
 src/scheme/AcousticSolver.hpp      |  9 ++++++---
 3 files changed, 52 insertions(+), 3 deletions(-)
 create mode 100644 src/mesh/ItemValueSynchronizer.hpp

diff --git a/src/mesh/ItemValueSynchronizer.hpp b/src/mesh/ItemValueSynchronizer.hpp
new file mode 100644
index 000000000..e4189014f
--- /dev/null
+++ b/src/mesh/ItemValueSynchronizer.hpp
@@ -0,0 +1,32 @@
+#ifndef ITEM_VALUE_SYNCHRONIZER_HPP
+#define ITEM_VALUE_SYNCHRONIZER_HPP
+
+#include <ItemValue.hpp>
+#include <Connectivity.hpp>
+
+class ItemValueSynchronizer
+{
+ public:
+  template <typename DataType,
+            ItemType item_type,
+            typename ConnectivityPtr>
+  PASTIS_INLINE
+  void synchronize(ItemValue<DataType, item_type, ConnectivityPtr>& item_value)
+  {
+    pout() << "Calling synchronize...\n";
+    auto connectivity_ptr = item_value.connectivity_ptr();
+    Assert(connectivity_ptr.use_count()>0, "No connectivity is associated to this ItemValue");
+    parallel::barrier();
+    parallel::Messenger::destroy();
+    pout() << __FILE__ << ':' << __LINE__ << ": NIY!\n";
+    std::exit(0);
+  }
+
+  PASTIS_INLINE
+  ItemValueSynchronizer()
+  {
+    ;
+  }
+};
+
+#endif // ITEM_VALUE_SYNCHRONIZER_HPP
diff --git a/src/mesh/ItemValueUtils.hpp b/src/mesh/ItemValueUtils.hpp
index 26d05a37b..cd1110fe7 100644
--- a/src/mesh/ItemValueUtils.hpp
+++ b/src/mesh/ItemValueUtils.hpp
@@ -6,6 +6,8 @@
 
 #include <Connectivity.hpp>
 
+#include <ItemValueSynchronizer.hpp>
+
 template <typename DataType,
           ItemType item_type>
 std::remove_const_t<DataType>
@@ -282,4 +284,16 @@ sum(const ItemValue<DataType, item_type>& item_value)
   return parallel::allReduceSum(local_sum);
 }
 
+template <typename DataType,
+          ItemType item_type,
+          typename ConnectivityPtr>
+void synchronize(ItemValue<DataType, item_type, ConnectivityPtr>& item_value)
+{
+  static_assert(not std::is_const_v<DataType>, "cannot synchronize ItemValue of const data");
+  if (parallel::size() > 1) {
+    ItemValueSynchronizer synchronizer;
+    synchronizer.synchronize(item_value);
+  }
+}
+
 #endif // ITEM_VALUE_UTILS_HPP
diff --git a/src/scheme/AcousticSolver.hpp b/src/scheme/AcousticSolver.hpp
index 142b6f03b..cb83a583b 100644
--- a/src/scheme/AcousticSolver.hpp
+++ b/src/scheme/AcousticSolver.hpp
@@ -210,13 +210,16 @@ class AcousticSolver
     computeAjr(rhocj, Cjr, ljr, njr);
 
     NodeValuePerCell<const Rdd> Ajr = m_Ajr;
-    const NodeValue<const Rdd> Ar = computeAr(Ajr);
-    const NodeValue<const Rd> br = computeBr(m_Ajr, Cjr, uj, pj);
+    this->computeAr(Ajr);
+    this->computeBr(m_Ajr, Cjr, uj, pj);
 
     this->applyBoundaryConditions();
 
+    synchronize(m_Ar);
+    synchronize(m_br);
+
     NodeValue<Rd>& ur = m_ur;
-    ur = computeUr(Ar, br);
+    ur = computeUr(m_Ar, m_br);
     computeFjr(m_Ajr, ur, Cjr, uj, pj);
   }
 
-- 
GitLab