From c67fbf5d07d1a705ea52b7ab54f924be3ebd213d Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Fri, 19 Aug 2022 17:44:55 +0200 Subject: [PATCH] Memory squeeze --- src/INTERP_KERNEL/BBTree.txx | 35 ++++++++++++++----------- src/INTERP_KERNEL/BoundingBox.cxx | 24 +++++++++++++++++ src/INTERP_KERNEL/BoundingBox.hxx | 9 +++++-- src/INTERP_KERNEL/Interpolation1D0D.txx | 5 +--- src/INTERP_KERNEL/Interpolation2D3D.txx | 25 +++--------------- src/INTERP_KERNEL/Interpolation3D.txx | 25 ++++-------------- src/INTERP_KERNEL/Interpolation3D1D.txx | 23 +++------------- src/INTERP_KERNEL/MeshElement.hxx | 11 +++----- src/INTERP_KERNEL/MeshElement.txx | 20 ++++---------- 9 files changed, 72 insertions(+), 105 deletions(-) diff --git a/src/INTERP_KERNEL/BBTree.txx b/src/INTERP_KERNEL/BBTree.txx index b9bb357fd..2a78e6dbc 100644 --- a/src/INTERP_KERNEL/BBTree.txx +++ b/src/INTERP_KERNEL/BBTree.txx @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -76,24 +77,26 @@ public: _terminal=true; } - double* nodes=new double [nbelems]; - _elems.resize(nbelems); - for (ConnType i=0; i::max(); + { + std::unique_ptr nodes( new double [nbelems] ); + _elems.resize(nbelems); + for (ConnType i=0; i(nodes, nodes+nbelems/2, nodes+nbelems); - double median = *(nodes+nbelems/2); - delete[] nodes; + std::nth_element(nodes.get(), nodes.get()+nbelems/2, nodes.get()+nbelems); + median = *(nodes.get()+nbelems/2); + } // std:: cout << *median < new_elems_left; diff --git a/src/INTERP_KERNEL/BoundingBox.cxx b/src/INTERP_KERNEL/BoundingBox.cxx index db5fd0918..fe9e99204 100644 --- a/src/INTERP_KERNEL/BoundingBox.cxx +++ b/src/INTERP_KERNEL/BoundingBox.cxx @@ -36,6 +36,30 @@ namespace INTERP_KERNEL * */ BoundingBox::BoundingBox(const double** pts, const unsigned numPts) + { + initializeWith(pts,numPts); + } + + void BoundingBox::fillInXMinXmaxYminYmaxZminZmaxFormat(double data[6]) const + { + data[0] = this->getCoordinate(BoundingBox::XMIN); + data[1] = this->getCoordinate(BoundingBox::XMAX); + data[2] = this->getCoordinate(BoundingBox::YMIN); + data[3] = this->getCoordinate(BoundingBox::YMAX); + data[4] = this->getCoordinate(BoundingBox::ZMIN); + data[5] = this->getCoordinate(BoundingBox::ZMAX); + } + + /** + * Constructor creating box from an array of the points corresponding + * to the vertices of the element. + * Each point is represented by an array of three doubles. + * + * @param pts array of points + * @param numPts number of vertices + * + */ + void BoundingBox::initializeWith(const double** pts, const unsigned numPts) { // initialize with first two points const double *pt0(pts[0]); diff --git a/src/INTERP_KERNEL/BoundingBox.hxx b/src/INTERP_KERNEL/BoundingBox.hxx index 52641ccf3..4c7919538 100644 --- a/src/INTERP_KERNEL/BoundingBox.hxx +++ b/src/INTERP_KERNEL/BoundingBox.hxx @@ -33,15 +33,20 @@ namespace INTERP_KERNEL class INTERPKERNEL_EXPORT BoundingBox { public: - /// Enumeration representing the six coordinates that define the bounding box enum BoxCoord { XMIN = 0, YMIN = 1, ZMIN = 2, XMAX = 3, YMAX = 4, ZMAX = 5 }; - + + BoundingBox() { } + BoundingBox(const double** pts, const unsigned numPts); BoundingBox(const BoundingBox& box1, const BoundingBox& box2); ~BoundingBox() { } + + void fillInXMinXmaxYminYmaxZminZmaxFormat(double data[6]) const; + + void initializeWith(const double** pts, const unsigned numPts); bool isDisjointWith(const BoundingBox& box) const; diff --git a/src/INTERP_KERNEL/Interpolation1D0D.txx b/src/INTERP_KERNEL/Interpolation1D0D.txx index dcd25a938..fc1a2e439 100755 --- a/src/INTERP_KERNEL/Interpolation1D0D.txx +++ b/src/INTERP_KERNEL/Interpolation1D0D.txx @@ -73,19 +73,17 @@ namespace INTERP_KERNEL // create BBTree structure // - get bounding boxes std::vector bboxes(2*SPACEDIM*numSrcElems); - ConnType* srcElemIdx = new ConnType[numSrcElems]; for(ConnType i = 0; i < numSrcElems ; ++i) { // get source bboxes in right order srcElems[i]->getBoundingBox()->toCompactData(bboxes.data()+6*i); - srcElemIdx[i] = srcElems[i]->getIndex(); } adjustBoundingBoxes(bboxes); const double *bboxPtr(nullptr); if(numSrcElems>0) bboxPtr=&bboxes[0]; - BBTree tree(bboxPtr, srcElemIdx, 0, numSrcElems); + BBTree tree(bboxPtr, nullptr, 0, numSrcElems); const ConnType *trgConnPtr(targetMesh.getConnectivityPtr()),*trgConnIPtr(targetMesh.getConnectivityIndexPtr()); const ConnType *srcConnPtr(srcMesh.getConnectivityPtr()),*srcConnIPtr(srcMesh.getConnectivityIndexPtr()); const double *trgCooPtr(targetMesh.getCoordinatesPtr()),*srcCooPtr(srcMesh.getCoordinatesPtr()); @@ -109,7 +107,6 @@ namespace INTERP_KERNEL } } } - delete [] srcElemIdx; for(ConnType i = 0 ; i < numSrcElems ; ++i) delete srcElems[i]; return srcMesh.getNumberOfNodes(); diff --git a/src/INTERP_KERNEL/Interpolation2D3D.txx b/src/INTERP_KERNEL/Interpolation2D3D.txx index 6069c689e..e813a11db 100755 --- a/src/INTERP_KERNEL/Interpolation2D3D.txx +++ b/src/INTERP_KERNEL/Interpolation2D3D.txx @@ -111,54 +111,37 @@ namespace INTERP_KERNEL // create BBTree structure // - get bounding boxes std::vector bboxes(6 * numSrcElems); - ConnType* srcElemIdx = new ConnType[numSrcElems]; for(ConnType i = 0; i < numSrcElems ; ++i) { // get source bboxes in right order const BoundingBox* box = srcElems[i]->getBoundingBox(); - bboxes[6*i+0] = box->getCoordinate(BoundingBox::XMIN); - bboxes[6*i+1] = box->getCoordinate(BoundingBox::XMAX); - bboxes[6*i+2] = box->getCoordinate(BoundingBox::YMIN); - bboxes[6*i+3] = box->getCoordinate(BoundingBox::YMAX); - bboxes[6*i+4] = box->getCoordinate(BoundingBox::ZMIN); - bboxes[6*i+5] = box->getCoordinate(BoundingBox::ZMAX); - - // source indices have to begin with zero for BBox, I think - srcElemIdx[i] = srcElems[i]->getIndex(); + box->fillInXMinXmaxYminYmaxZminZmaxFormat(bboxes.data()+6*i); } // [ABN] Adjust 2D bounding box (those might be flat in the cases where the 2D surf are perfectly aligned with the axis) performAdjustmentOfBB(intersector, bboxes); - BBTree<3,ConnType> tree(bboxes.data(), srcElemIdx, 0, numSrcElems, 0.); + BBTree<3,ConnType> tree(bboxes.data(), nullptr, 0, numSrcElems, 0.); // for each target element, get source elements with which to calculate intersection // - calculate intersection by calling intersectCells for(ConnType i = 0; i < numTargetElems; ++i) { const BoundingBox* box = targetElems[i]->getBoundingBox(); - const ConnType targetIdx = targetElems[i]->getIndex(); // get target bbox in right order double targetBox[6]; - targetBox[0] = box->getCoordinate(BoundingBox::XMIN); - targetBox[1] = box->getCoordinate(BoundingBox::XMAX); - targetBox[2] = box->getCoordinate(BoundingBox::YMIN); - targetBox[3] = box->getCoordinate(BoundingBox::YMAX); - targetBox[4] = box->getCoordinate(BoundingBox::ZMIN); - targetBox[5] = box->getCoordinate(BoundingBox::ZMAX); + box->fillInXMinXmaxYminYmaxZminZmaxFormat(targetBox); std::vector intersectElems; tree.getIntersectingElems(targetBox, intersectElems); if ( !intersectElems.empty() ) - intersector->intersectCells(targetIdx, intersectElems, matrix); + intersector->intersectCells(i, intersectElems, matrix); } - delete [] srcElemIdx; - DuplicateFacesType::iterator iter; for (iter = intersectFaces.begin(); iter != intersectFaces.end(); ++iter) { diff --git a/src/INTERP_KERNEL/Interpolation3D.txx b/src/INTERP_KERNEL/Interpolation3D.txx index 38ca91132..76f3056de 100755 --- a/src/INTERP_KERNEL/Interpolation3D.txx +++ b/src/INTERP_KERNEL/Interpolation3D.txx @@ -309,46 +309,31 @@ namespace INTERP_KERNEL // create BBTree structure // - get bounding boxes std::unique_ptr bboxes( new double[6 * numSrcElems] ); - std::unique_ptr srcElemIdx( new ConnType[numSrcElems] ); for(ConnType i = 0; i < numSrcElems ; ++i) { // get source bboxes in right order - const BoundingBox* box = srcElems[i]->getBoundingBox(); - bboxes[6*i+0] = box->getCoordinate(BoundingBox::XMIN); - bboxes[6*i+1] = box->getCoordinate(BoundingBox::XMAX); - bboxes[6*i+2] = box->getCoordinate(BoundingBox::YMIN); - bboxes[6*i+3] = box->getCoordinate(BoundingBox::YMAX); - bboxes[6*i+4] = box->getCoordinate(BoundingBox::ZMIN); - bboxes[6*i+5] = box->getCoordinate(BoundingBox::ZMAX); - - // source indices have to begin with zero for BBox, I think - srcElemIdx[i] = srcElems[i]->getIndex(); + const BoundingBox *box = srcElems[i]->getBoundingBox(); + box->fillInXMinXmaxYminYmaxZminZmaxFormat(bboxes.get()+6*i); } - BBTree<3,ConnType> tree(bboxes.get(), srcElemIdx.get(), 0, numSrcElems); + BBTree<3,ConnType> tree(bboxes.get(), nullptr, 0, numSrcElems); // for each target element, get source elements with which to calculate intersection // - calculate intersection by calling intersectCells for(ConnType i = 0; i < numTargetElems; ++i) { const BoundingBox* box = targetElems[i]->getBoundingBox(); - const ConnType targetIdx = targetElems[i]->getIndex(); // get target bbox in right order double targetBox[6]; - targetBox[0] = box->getCoordinate(BoundingBox::XMIN); - targetBox[1] = box->getCoordinate(BoundingBox::XMAX); - targetBox[2] = box->getCoordinate(BoundingBox::YMIN); - targetBox[3] = box->getCoordinate(BoundingBox::YMAX); - targetBox[4] = box->getCoordinate(BoundingBox::ZMIN); - targetBox[5] = box->getCoordinate(BoundingBox::ZMAX); + box->fillInXMinXmaxYminYmaxZminZmaxFormat(targetBox); std::vector intersectElems; tree.getIntersectingElems(targetBox, intersectElems); if ( !intersectElems.empty() ) - intersector->intersectCells(targetIdx,intersectElems,result); + intersector->intersectCells(i,intersectElems,result); } #endif diff --git a/src/INTERP_KERNEL/Interpolation3D1D.txx b/src/INTERP_KERNEL/Interpolation3D1D.txx index fdec33c6c..63b4cc147 100755 --- a/src/INTERP_KERNEL/Interpolation3D1D.txx +++ b/src/INTERP_KERNEL/Interpolation3D1D.txx @@ -85,49 +85,34 @@ namespace INTERP_KERNEL // create BBTree structure // - get bounding boxes std::vector bboxes(6*numSrcElems); - std::unique_ptr srcElemIdx{ new ConnType[numSrcElems] }; for(ConnType i = 0; i < numSrcElems ; ++i) { // get source bboxes in right order const BoundingBox* box = srcElems[i]->getBoundingBox(); - bboxes[6*i+0] = box->getCoordinate(BoundingBox::XMIN); - bboxes[6*i+1] = box->getCoordinate(BoundingBox::XMAX); - bboxes[6*i+2] = box->getCoordinate(BoundingBox::YMIN); - bboxes[6*i+3] = box->getCoordinate(BoundingBox::YMAX); - bboxes[6*i+4] = box->getCoordinate(BoundingBox::ZMIN); - bboxes[6*i+5] = box->getCoordinate(BoundingBox::ZMAX); - - srcElemIdx[i] = srcElems[i]->getIndex(); + box->fillInXMinXmaxYminYmaxZminZmaxFormat(bboxes.data()+6*i); } adjustBoundingBoxes(bboxes); const double *bboxPtr = nullptr; if(numSrcElems>0) bboxPtr=bboxes.data(); - BBTree<3,ConnType> tree(bboxPtr, srcElemIdx.get(), 0, numSrcElems); + BBTree<3,ConnType> tree(bboxPtr, nullptr, 0, numSrcElems); // for each target element, get source elements with which to calculate intersection // - calculate intersection by calling intersectCells for(ConnType i = 0; i < numTargetElems; ++i) { const BoundingBox* box = targetElems[i]->getBoundingBox(); - const ConnType targetIdx = targetElems[i]->getIndex(); - // get target bbox in right order double targetBox[6]; - targetBox[0] = box->getCoordinate(BoundingBox::XMIN); - targetBox[1] = box->getCoordinate(BoundingBox::XMAX); - targetBox[2] = box->getCoordinate(BoundingBox::YMIN); - targetBox[3] = box->getCoordinate(BoundingBox::YMAX); - targetBox[4] = box->getCoordinate(BoundingBox::ZMIN); - targetBox[5] = box->getCoordinate(BoundingBox::ZMAX); + box->fillInXMinXmaxYminYmaxZminZmaxFormat(targetBox); std::vector intersectElems; tree.getIntersectingElems(targetBox, intersectElems); if ( !intersectElems.empty() ) - intersector->intersectCells(targetIdx,intersectElems,result); + intersector->intersectCells(i,intersectElems,result); } return intersector->getNumberOfColsOfResMatrix(); } diff --git a/src/INTERP_KERNEL/MeshElement.hxx b/src/INTERP_KERNEL/MeshElement.hxx index b0e7b8272..1ff56a61d 100644 --- a/src/INTERP_KERNEL/MeshElement.hxx +++ b/src/INTERP_KERNEL/MeshElement.hxx @@ -41,13 +41,11 @@ namespace INTERP_KERNEL template MeshElement(const ConnType index, const MyMeshType& mesh); - ~MeshElement(); - - ConnType getIndex() const { return _index; } + ~MeshElement() { } nbnodesincelltype getNumberOfNodes() const { return _number; } - const BoundingBox* getBoundingBox() const { return _box; } + const BoundingBox *getBoundingBox() const { return &_box; } private: /// disallow copying @@ -56,13 +54,10 @@ namespace INTERP_KERNEL /// disallow assignment MeshElement& operator=(const MeshElement& elem); - /// global number of the element - const ConnType _index; - nbnodesincelltype _number; /// bounding box of the element - does not change after having been initialised - BoundingBox* _box; + BoundingBox _box; }; /** diff --git a/src/INTERP_KERNEL/MeshElement.txx b/src/INTERP_KERNEL/MeshElement.txx index 2a21431ec..7aee10798 100755 --- a/src/INTERP_KERNEL/MeshElement.txx +++ b/src/INTERP_KERNEL/MeshElement.txx @@ -28,6 +28,7 @@ #include #include #include +#include namespace INTERP_KERNEL { @@ -41,35 +42,24 @@ namespace INTERP_KERNEL template template MeshElement::MeshElement(const ConnType index, const MyMeshType& mesh) - : _index(index), _number( 0 ), _box(nullptr) + : _number( 0 ) { auto numberCore = mesh.getNumberOfNodesOfElement(OTT::indFC(index)); if(numberCore < std::numeric_limits::max()) { _number = static_cast< nbnodesincelltype >(numberCore); - const double**vertices = new const double*[_number]; + std::unique_ptr vertices( new const double*[_number] ); for( nbnodesincelltype i = 0 ; i < _number ; ++i) vertices[i] = getCoordsOfNode(i , OTT::indFC(index), mesh); // create bounding box - _box = new BoundingBox(vertices,_number); - delete [] vertices; + _box.initializeWith(vertices.get(),_number); } else { - std::cerr << "ERROR at index " << index << " : exceeding capacity !" << std::endl; + THROW_IK_EXCEPTION("ERROR at index " << index << " : exceeding capacity !"); } } - - /** - * Destructor - * - */ - template - MeshElement::~MeshElement() - { - delete _box; - } ///////////////////////////////////////////////////////////////////// /// ElementBBoxOrder ///////////// -- 2.39.2