From 538a38f72031fd53860096a33487e8916518f931 Mon Sep 17 00:00:00 2001
From: Anna Wellmann <a.wellmann@tu-bs.de>
Date: Thu, 8 Dec 2022 11:20:58 +0100
Subject: [PATCH] Partially revert "Refactor  exchangeIndicesForCommAfterFtoC"

This reverts commit cc7bb07a08d2c8c53150a1cc52cea2ecb6751553.

The refactored version using std::find instead of std::unique does not work for vectors containing a zero as a receiving index.  For example this is the case in the app BoundaryLayer --> revert to the old version.
---
 .../IndexRearrangementForStreams.cpp          | 47 ++++++++++++++-----
 1 file changed, 34 insertions(+), 13 deletions(-)

diff --git a/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreams.cpp b/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreams.cpp
index f191c628d..63a518f73 100644
--- a/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreams.cpp
+++ b/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreams.cpp
@@ -103,10 +103,10 @@ std::vector<uint> IndexRearrangementForStreams::initSendIndicesForCommAfterFToCZ
 std::vector<uint> IndexRearrangementForStreams::exchangeIndicesForCommAfterFtoCX(
     uint level, int indexOfProcessNeighbor, std::vector<uint> &sendIndicesForCommAfterFtoCPositions) const
 {
-    // fill the receive vector with zeros as placeholders (0 is never a valid fluid node)
+    // fill the receive vector with zeros as placeholders (0 is not a valid fluid node)
     // give vector an arbitrary size (larger than needed) // TODO: Find a better way
     std::vector<uint> recvIndicesForCommAfterFtoCPositions(
-        (size_t)para->getParH(level)->sendProcessNeighborsAfterFtoCX[indexOfProcessNeighbor].numberOfNodes * 2, 0);
+        (size_t)para->getParH(level)->sendProcessNeighborsAfterFtoCY[indexOfProcessNeighbor].numberOfNodes * 2, 0);
 
     communicator.receive_send(
         recvIndicesForCommAfterFtoCPositions.data(), (int)recvIndicesForCommAfterFtoCPositions.size(),
@@ -114,16 +114,23 @@ std::vector<uint> IndexRearrangementForStreams::exchangeIndicesForCommAfterFtoCX
         sendIndicesForCommAfterFtoCPositions.data(), (int)sendIndicesForCommAfterFtoCPositions.size(),
         para->getParH(level)->sendProcessNeighborX[indexOfProcessNeighbor].rankNeighbor);
 
-    // resize receiving vector to correct size (remove all zeros)
-    auto it = std::find(recvIndicesForCommAfterFtoCPositions.begin(), recvIndicesForCommAfterFtoCPositions.end(), 0);
-    recvIndicesForCommAfterFtoCPositions.erase(it, recvIndicesForCommAfterFtoCPositions.end());
+    // resize receiving vector to correct size
+    if ((uint)recvIndicesForCommAfterFtoCPositions.size() > 0) {
+        auto it = std::unique(
+            recvIndicesForCommAfterFtoCPositions.begin(),
+            recvIndicesForCommAfterFtoCPositions.end()); // finds the second zero when there are multiple zeros in a row
+        recvIndicesForCommAfterFtoCPositions.erase(
+            std::prev(it, 1),                            // begin erasing at the first zero
+            recvIndicesForCommAfterFtoCPositions.end()); // TODO: Find a better way
+    }
+
     return recvIndicesForCommAfterFtoCPositions;
 }
 
 std::vector<uint> IndexRearrangementForStreams::exchangeIndicesForCommAfterFtoCY(
     uint level, int indexOfProcessNeighbor, std::vector<uint> &sendIndicesForCommAfterFtoCPositions) const
 {
-    // fill the receive vector with zeros as placeholders (0 is never a valid fluid node)
+    // fill the receive vector with zeros as placeholders (0 is not a valid fluid node)
     // give vector an arbitrary size (larger than needed) // TODO: Find a better way
     std::vector<uint> recvIndicesForCommAfterFtoCPositions(
         (size_t)para->getParH(level)->sendProcessNeighborsAfterFtoCY[indexOfProcessNeighbor].numberOfNodes * 2, 0);
@@ -134,16 +141,23 @@ std::vector<uint> IndexRearrangementForStreams::exchangeIndicesForCommAfterFtoCY
         sendIndicesForCommAfterFtoCPositions.data(), (int)sendIndicesForCommAfterFtoCPositions.size(),
         para->getParH(level)->sendProcessNeighborY[indexOfProcessNeighbor].rankNeighbor);
 
-    // resize receiving vector to correct size (remove all zeros)
-    auto it = std::find(recvIndicesForCommAfterFtoCPositions.begin(), recvIndicesForCommAfterFtoCPositions.end(), 0);
-    recvIndicesForCommAfterFtoCPositions.erase(it, recvIndicesForCommAfterFtoCPositions.end());
+    // resize receiving vector to correct size
+    if ((uint)recvIndicesForCommAfterFtoCPositions.size() > 0) {
+        auto it = std::unique(
+            recvIndicesForCommAfterFtoCPositions.begin(),
+            recvIndicesForCommAfterFtoCPositions.end()); // finds the second zero when there are multiple zeros in a row
+        recvIndicesForCommAfterFtoCPositions.erase(
+            std::prev(it, 1),                            // begin erasing at the first zero
+            recvIndicesForCommAfterFtoCPositions.end()); // TODO: Find a better way
+    }
+
     return recvIndicesForCommAfterFtoCPositions;
 }
 
 std::vector<uint> IndexRearrangementForStreams::exchangeIndicesForCommAfterFtoCZ(
     uint level, int indexOfProcessNeighbor, std::vector<uint> &sendIndicesForCommAfterFtoCPositions) const
 {
-    // fill the receive vector with zeros as placeholders (0 is never a valid fluid node)
+    // fill the receive vector with zeros as placeholders (0 is not a valid fluid node)
     // give vector an arbitrary size (larger than needed) // TODO: Find a better way
     std::vector<uint> recvIndicesForCommAfterFtoCPositions(
         (size_t)para->getParH(level)->sendProcessNeighborsAfterFtoCZ[indexOfProcessNeighbor].numberOfNodes * 2, 0);
@@ -154,9 +168,16 @@ std::vector<uint> IndexRearrangementForStreams::exchangeIndicesForCommAfterFtoCZ
         sendIndicesForCommAfterFtoCPositions.data(), (int)sendIndicesForCommAfterFtoCPositions.size(),
         para->getParH(level)->sendProcessNeighborZ[indexOfProcessNeighbor].rankNeighbor);
 
-    // resize receiving vector to correct size (remove all zeros)
-    auto it = std::find(recvIndicesForCommAfterFtoCPositions.begin(), recvIndicesForCommAfterFtoCPositions.end(), 0);
-    recvIndicesForCommAfterFtoCPositions.erase(it, recvIndicesForCommAfterFtoCPositions.end());
+    // resize receiving vector to correct size
+    if ((uint)recvIndicesForCommAfterFtoCPositions.size() > 0) {
+        auto it = std::unique(
+            recvIndicesForCommAfterFtoCPositions.begin(),
+            recvIndicesForCommAfterFtoCPositions.end()); // finds the second zero when there are multiple zeros in a row
+        recvIndicesForCommAfterFtoCPositions.erase(
+            std::prev(it, 1),                            // begin erasing at the first zero
+            recvIndicesForCommAfterFtoCPositions.end()); // TODO: Find a better way
+    }
+
     return recvIndicesForCommAfterFtoCPositions;
 }
 
-- 
GitLab