diff --git a/ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp b/ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp index 1318caa5d0..57d00b9c16 100644 --- a/ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp +++ b/ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp @@ -37,6 +37,7 @@ #include #include #include +#include RigMainGrid::RigMainGrid() : RigGridBase( this ) @@ -1088,83 +1089,76 @@ void RigMainGrid::buildCellSearchTree( size_t cellsPerBoundingBox ) const } } - int threadCount = RiaOpenMPTools::availableThreadCount(); + // Each column of the main grid is split into leaves of cellsPerBoundingBox cells along K. The leaves are ordered + // by column and then K, so the leaf index can be computed directly from the cell IJK. + const size_t leavesPerColumn = ( cellCountK() + cellsPerBoundingBox - 1 ) / cellsPerBoundingBox; + const size_t mainGridLeafCount = cellCountI() * cellCountJ() * leavesPerColumn; + const size_t leafCount = mainGridLeafCount + subCellIndicesWithoutMainGridCell.size(); - std::vector>> threadCellIndicesForBoundingBoxes( threadCount ); - std::vector> threadCellBoundingBoxes( threadCount ); - -#pragma omp parallel + // Visit the valid main grid cells of a leaf, and the LGR cells inside them also when the main grid cell is invalid + auto forEachCellInLeaf = [&]( size_t leafIndex, auto&& visitCell ) { - int myThread = RiaOpenMPTools::currentThreadIndex(); - -#pragma omp for - for ( int i = 0; i < static_cast( cellCountI() ); i++ ) + if ( leafIndex >= mainGridLeafCount ) { - for ( size_t j = 0; j < cellCountJ(); j++ ) + visitCell( subCellIndicesWithoutMainGridCell[leafIndex - mainGridLeafCount] ); + return; + } + + const size_t column = leafIndex / leavesPerColumn; + const size_t i = column / cellCountJ(); + const size_t j = column % cellCountJ(); + const size_t kStart = ( leafIndex % leavesPerColumn ) * cellsPerBoundingBox; + const size_t kEnd = std::min( kStart + cellsPerBoundingBox, cellCountK() ); + + for ( size_t k = kStart; k < kEnd; k++ ) + { + const size_t cellIdx = cellIndexFromIJK( i, j, k ); + if ( !cell( cellIdx ).isInvalid() ) visitCell( static_cast( cellIdx ) ); + + if ( auto it = subCellIndicesForMainGridCells.find( cellIdx ); it != subCellIndicesForMainGridCells.end() ) { - size_t k = 0; - while ( k < cellCountK() ) + for ( int subCellIdx : it->second ) { - size_t kCount = 0; - - std::vector aggregatedCellIndices; - cvf::BoundingBox accumulatedBB; - - while ( ( kCount < cellsPerBoundingBox ) && ( k + kCount < cellCountK() ) ) - { - size_t cellIdx = cellIndexFromIJK( i, j, k + kCount ); - - if ( !cell( cellIdx ).isInvalid() ) - { - aggregatedCellIndices.push_back( static_cast( cellIdx ) ); - - cvf::BoundingBox cellBB = cellBoundingBox( cellIdx ); - if ( cellBB.isValid() ) accumulatedBB.add( cellBB ); - } - - // Add the LGR cells also when the main grid cell is invalid - if ( auto it = subCellIndicesForMainGridCells.find( cellIdx ); it != subCellIndicesForMainGridCells.end() ) - { - for ( int subCellIdx : it->second ) - { - aggregatedCellIndices.push_back( subCellIdx ); - - cvf::BoundingBox cellBB = cellBoundingBox( subCellIdx ); - if ( cellBB.isValid() ) accumulatedBB.add( cellBB ); - } - } - kCount++; - } - - k += kCount; - - threadCellIndicesForBoundingBoxes[myThread].emplace_back( std::move( aggregatedCellIndices ) ); - threadCellBoundingBoxes[myThread].emplace_back( accumulatedBB ); + visitCell( subCellIdx ); } } } + }; + + // Compute the bounding box and number of cells of each leaf, then store the cell indices of all leaves in one array + std::vector cellBoundingBoxes( leafCount ); + std::vector cellIndexOffsets( leafCount + 1, 0 ); + +#pragma omp parallel for + for ( int leafIndex = 0; leafIndex < static_cast( leafCount ); leafIndex++ ) + { + cvf::BoundingBox& leafBB = cellBoundingBoxes[leafIndex]; + size_t& cellCount = cellIndexOffsets[leafIndex + 1]; + + forEachCellInLeaf( leafIndex, + [&]( int cellIdx ) + { + cvf::BoundingBox cellBB = cellBoundingBox( cellIdx ); + if ( cellBB.isValid() ) leafBB.add( cellBB ); + cellCount++; + } ); } - std::vector> cellIndicesForBoundingBoxes; - std::vector cellBoundingBoxes; + std::partial_sum( cellIndexOffsets.begin(), cellIndexOffsets.end(), cellIndexOffsets.begin() ); - for ( auto i = 0; i < threadCount; i++ ) + std::vector cellIndices( cellIndexOffsets.back() ); + +#pragma omp parallel for + for ( int leafIndex = 0; leafIndex < static_cast( leafCount ); leafIndex++ ) { - cellIndicesForBoundingBoxes.insert( cellIndicesForBoundingBoxes.end(), - std::make_move_iterator( threadCellIndicesForBoundingBoxes[i].begin() ), - std::make_move_iterator( threadCellIndicesForBoundingBoxes[i].end() ) ); - - cellBoundingBoxes.insert( cellBoundingBoxes.end(), threadCellBoundingBoxes[i].begin(), threadCellBoundingBoxes[i].end() ); - } - - for ( int subCellIdx : subCellIndicesWithoutMainGridCell ) - { - cellIndicesForBoundingBoxes.push_back( { subCellIdx } ); - cellBoundingBoxes.push_back( cellBoundingBox( subCellIdx ) ); + size_t position = cellIndexOffsets[leafIndex]; + forEachCellInLeaf( leafIndex, [&]( int cellIdx ) { cellIndices[position++] = cellIdx; } ); } m_cellSearchTree = new cvf::BoundingBoxTree; - m_cellSearchTree->buildTreeFromBoundingBoxesOptimized( cellBoundingBoxes, cellIndicesForBoundingBoxes ); + m_cellSearchTree->buildTreeFromBoundingBoxesOptimized( std::move( cellBoundingBoxes ), + std::move( cellIndices ), + std::move( cellIndexOffsets ) ); } //-------------------------------------------------------------------------------------------------- diff --git a/Fwk/VizFwk/LibGeometry/cvfBoundingBoxTree.cpp b/Fwk/VizFwk/LibGeometry/cvfBoundingBoxTree.cpp index 1ff186ed72..4578794245 100644 --- a/Fwk/VizFwk/LibGeometry/cvfBoundingBoxTree.cpp +++ b/Fwk/VizFwk/LibGeometry/cvfBoundingBoxTree.cpp @@ -42,6 +42,7 @@ #include "cvfObject.h" #include "cvfBoundingBox.h" +#include #include #include #include @@ -980,30 +981,39 @@ void BoundingBoxTree::buildTreeFromBoundingBoxes(const std::vector& boundingBoxes, - const std::vector>& optionalBoundingBoxIds) +void BoundingBoxTree::buildTreeFromBoundingBoxesOptimized(std::vector&& boundingBoxes, + std::vector&& boundingBoxIds, + std::vector&& boundingBoxIdOffsets) { - std::vector validBoundingBoxes; - std::vector validBoundingBoxIds; - std::vector validBoundingBoxIdOffsets; + CVF_ASSERT(boundingBoxIdOffsets.size() == boundingBoxes.size() + 1); - validBoundingBoxes.reserve(boundingBoxes.size()); - validBoundingBoxIdOffsets.reserve(boundingBoxes.size() + 1); - validBoundingBoxIdOffsets.push_back(0); - - for (int i = 0; i < (int)boundingBoxes.size(); ++i) + size_t validCount = 0; + size_t idEnd = 0; + for (size_t i = 0; i < boundingBoxes.size(); ++i) { - if (boundingBoxes[i].isValid()) + if (!boundingBoxes[i].isValid()) continue; + + const size_t idBegin = boundingBoxIdOffsets[i]; + const size_t idCount = boundingBoxIdOffsets[i + 1] - idBegin; + if (idBegin != idEnd) { - validBoundingBoxes.push_back(boundingBoxes[i]); - validBoundingBoxIds.insert(validBoundingBoxIds.end(), optionalBoundingBoxIds[i].begin(), optionalBoundingBoxIds[i].end()); - validBoundingBoxIdOffsets.push_back(validBoundingBoxIds.size()); + std::copy(boundingBoxIds.begin() + idBegin, boundingBoxIds.begin() + idBegin + idCount, boundingBoxIds.begin() + idEnd); } + + boundingBoxes[validCount] = boundingBoxes[i]; + boundingBoxIdOffsets[validCount] = idEnd; + idEnd += idCount; + validCount++; } - m_implTree->buildTree(std::move(validBoundingBoxes), std::move(validBoundingBoxIds), std::move(validBoundingBoxIdOffsets)); + boundingBoxes.resize(validCount); + boundingBoxIds.resize(idEnd); + boundingBoxIdOffsets.resize(validCount + 1); + boundingBoxIdOffsets[validCount] = idEnd; + + m_implTree->buildTree(std::move(boundingBoxes), std::move(boundingBoxIds), std::move(boundingBoxIdOffsets)); } //-------------------------------------------------------------------------------------------------- diff --git a/Fwk/VizFwk/LibGeometry/cvfBoundingBoxTree.h b/Fwk/VizFwk/LibGeometry/cvfBoundingBoxTree.h index 070c95fc5c..01a1f7a126 100644 --- a/Fwk/VizFwk/LibGeometry/cvfBoundingBoxTree.h +++ b/Fwk/VizFwk/LibGeometry/cvfBoundingBoxTree.h @@ -58,8 +58,11 @@ public: void buildTreeFromBoundingBoxes(const std::vector& boundingBoxes, const std::vector* optionalBoundingBoxIds); - void buildTreeFromBoundingBoxesOptimized(const std::vector& boundingBoxes, - const std::vector>& optionalBoundingBoxIds); + // Leaf i has the IDs boundingBoxIds[boundingBoxIdOffsets[i]] to boundingBoxIds[boundingBoxIdOffsets[i + 1] - 1]. + // boundingBoxIdOffsets must have one more element than boundingBoxes. + void buildTreeFromBoundingBoxesOptimized(std::vector&& boundingBoxes, + std::vector&& boundingBoxIds, + std::vector&& boundingBoxIdOffsets); void findIntersections(const cvf::BoundingBox& inputBB, std::vector* bbIdsOrIndexesIntersected) const;