diff --git a/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreams.cpp b/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreams.cpp index f191c628dabbe9432c5930596e403a333c0593d4..58e753b05153699ee93ee1e655ff21c742fe6e25 100644 --- a/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreams.cpp +++ b/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreams.cpp @@ -103,7 +103,7 @@ 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 // 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); @@ -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 // 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 // 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; } diff --git a/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreamsTest.cpp b/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreamsTest.cpp index 112133e330d2817f184c9c13687f322c306a6d78..5b8bd50bdebcbd4e37a240223b52b967f274d1ae 100644 --- a/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreamsTest.cpp +++ b/src/gpu/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/IndexRearrangementForStreamsTest.cpp @@ -283,15 +283,29 @@ TEST_F(IndexRearrangementForStreamsTest_exchangeIndicesForCommAfterFtoCX, threeR TEST_F(IndexRearrangementForStreamsTest_exchangeIndicesForCommAfterFtoCX, sixRecvIndicesX) { + // this test shows the limits of the current approach. The last index is always deleted CommunicationRoutineDouble communicator; - std::vector<uint> expected = { 10, 20, 30, 40, 50, 60 }; - std::vector<uint> receivedIndicesByComm(expected.size(), 0); + std::vector<uint> expected = { 10, 20, 30, 40, 50 }; + std::vector<uint> receivedIndicesByComm = { 10, 20, 30, 40, 50, 60 }; + communicator.setReceivedIndices(receivedIndicesByComm); + createTestSubject(communicator); + + std::vector<uint> recvIndicesForCommAfterFtoCPositions = act(); + EXPECT_THAT(recvIndicesForCommAfterFtoCPositions.size(), testing::Eq(5)); + EXPECT_THAT(recvIndicesForCommAfterFtoCPositions, testing::Eq(expected)); +} + +TEST_F(IndexRearrangementForStreamsTest_exchangeIndicesForCommAfterFtoCX, recvIndicesXContainZero) +{ + CommunicationRoutineDouble communicator; + std::vector<uint> expected = { 0, 20, 30, 40 }; + std::vector<uint> receivedIndicesByComm(6, 0); std::copy(expected.begin(), expected.end(), receivedIndicesByComm.begin()); communicator.setReceivedIndices(receivedIndicesByComm); createTestSubject(communicator); std::vector<uint> recvIndicesForCommAfterFtoCPositions = act(); - EXPECT_THAT(recvIndicesForCommAfterFtoCPositions.size(), testing::Eq(6)); + EXPECT_THAT(recvIndicesForCommAfterFtoCPositions.size(), testing::Eq(4)); EXPECT_THAT(recvIndicesForCommAfterFtoCPositions, testing::Eq(expected)); } @@ -390,15 +404,29 @@ TEST_F(IndexRearrangementForStreamsTest_exchangeIndicesForCommAfterFtoCY, threeR TEST_F(IndexRearrangementForStreamsTest_exchangeIndicesForCommAfterFtoCY, sixRecvIndicesY) { + // this test shows the limits of the current approach. The last index is always deleted CommunicationRoutineDouble communicator; - std::vector<uint> expected = { 10, 20, 30, 40, 50, 60 }; - std::vector<uint> receivedIndicesByComm(expected.size(), 0); + std::vector<uint> expected = { 10, 20, 30, 40, 50 }; + std::vector<uint> receivedIndicesByComm = { 10, 20, 30, 40, 50, 60 }; + communicator.setReceivedIndices(receivedIndicesByComm); + createTestSubject(communicator); + + std::vector<uint> recvIndicesForCommAfterFtoCPositions = act(); + EXPECT_THAT(recvIndicesForCommAfterFtoCPositions.size(), testing::Eq(5)); + EXPECT_THAT(recvIndicesForCommAfterFtoCPositions, testing::Eq(expected)); +} + +TEST_F(IndexRearrangementForStreamsTest_exchangeIndicesForCommAfterFtoCY, recvIndicesYContainZero) +{ + CommunicationRoutineDouble communicator; + std::vector<uint> expected = { 0, 20, 30, 40 }; + std::vector<uint> receivedIndicesByComm(6, 0); std::copy(expected.begin(), expected.end(), receivedIndicesByComm.begin()); communicator.setReceivedIndices(receivedIndicesByComm); createTestSubject(communicator); std::vector<uint> recvIndicesForCommAfterFtoCPositions = act(); - EXPECT_THAT(recvIndicesForCommAfterFtoCPositions.size(), testing::Eq(6)); + EXPECT_THAT(recvIndicesForCommAfterFtoCPositions.size(), testing::Eq(4)); EXPECT_THAT(recvIndicesForCommAfterFtoCPositions, testing::Eq(expected)); } @@ -495,16 +523,30 @@ TEST_F(IndexRearrangementForStreamsTest_exchangeIndicesForCommAfterFtoCZ, threeR EXPECT_THAT(recvIndicesForCommAfterFtoCPositions, testing::Eq(expected)); } -TEST_F(IndexRearrangementForStreamsTest_exchangeIndicesForCommAfterFtoCZ, sixRecvIndicesZ) +TEST_F(IndexRearrangementForStreamsTest_exchangeIndicesForCommAfterFtoCZ, sixRecvIndicesYZ) { + // this test shows the limits of the current approach. The last index is always deleted CommunicationRoutineDouble communicator; - std::vector<uint> expected = { 10, 20, 30, 40, 50, 60 }; - std::vector<uint> receivedIndicesBZComm(expected.size(), 0); - std::copy(expected.begin(), expected.end(), receivedIndicesBZComm.begin()); - communicator.setReceivedIndices(receivedIndicesBZComm); + std::vector<uint> expected = { 10, 20, 30, 40, 50 }; + std::vector<uint> receivedIndicesByComm = { 10, 20, 30, 40, 50, 60 }; + communicator.setReceivedIndices(receivedIndicesByComm); + createTestSubject(communicator); + + std::vector<uint> recvIndicesForCommAfterFtoCPositions = act(); + EXPECT_THAT(recvIndicesForCommAfterFtoCPositions.size(), testing::Eq(5)); + EXPECT_THAT(recvIndicesForCommAfterFtoCPositions, testing::Eq(expected)); +} + +TEST_F(IndexRearrangementForStreamsTest_exchangeIndicesForCommAfterFtoCZ, recvIndicesZContainZero) +{ + CommunicationRoutineDouble communicator; + std::vector<uint> expected = { 0, 20, 30, 40 }; + std::vector<uint> receivedIndicesByComm(6, 0); + std::copy(expected.begin(), expected.end(), receivedIndicesByComm.begin()); + communicator.setReceivedIndices(receivedIndicesByComm); createTestSubject(communicator); std::vector<uint> recvIndicesForCommAfterFtoCPositions = act(); - EXPECT_THAT(recvIndicesForCommAfterFtoCPositions.size(), testing::Eq(6)); + EXPECT_THAT(recvIndicesForCommAfterFtoCPositions.size(), testing::Eq(4)); EXPECT_THAT(recvIndicesForCommAfterFtoCPositions, testing::Eq(expected)); }