From 521f38cc6d653ada81cefdfde10799bca58d0a58 Mon Sep 17 00:00:00 2001 From: Alexandre Talon Date: Wed, 29 Nov 2023 00:42:34 +0100 Subject: [PATCH] Performance issues and removed one useless parameter (nThread, inherited by the object). --- core/base/geometry/Geometry.h | 26 ++++++++++++++++++++++++++ core/base/topoMap/TopoMap.h | 28 ++++++++++++---------------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/core/base/geometry/Geometry.h b/core/base/geometry/Geometry.h index 8f14f596b0..3ba20813fd 100644 --- a/core/base/geometry/Geometry.h +++ b/core/base/geometry/Geometry.h @@ -195,6 +195,32 @@ namespace ttk { template T distance(const std::vector &p0, const std::vector &p1); + template + inline T distance2D(const T *p0, const T *p1, const int &dimension = 3) + { + T distance = 0; + T di0 = (p0[0]-p1[0]); + T di1 = (p0[1]-p1[1]); + if constexpr(std::is_same::value) { + distance = fmaf(di0, di0, distance); + distance = fmaf(di1, di1, distance); + } + else { + distance = fma(di0, di0, distance); + distance = fma(di1, di1, distance); + } + + return sqrt(distance); + } + + /// Compute the Euclidean distance between two vectors by first flattening + /// them + /// \param p0 xyz coordinates of the first input point. + /// \param p1 xyz coordinates of the second input point. + template + T distanceFlatten(const std::vector> &p0, + const std::vector> &p1); + /// Compute the Euclidean distance between two vectors by first flattening /// them /// \param p0 xyz coordinates of the first input point. diff --git a/core/base/topoMap/TopoMap.h b/core/base/topoMap/TopoMap.h index 9a97110fa7..c5899c5339 100644 --- a/core/base/topoMap/TopoMap.h +++ b/core/base/topoMap/TopoMap.h @@ -140,8 +140,7 @@ namespace ttk { const std::vector &distMatrix, T *allCoords, size_t n, - size_t angularSampleNb, - size_t nThread); + size_t angularSampleNb); template bool computeConvexHull(T *allCoords, @@ -439,7 +438,7 @@ namespace ttk { = {goalCoordChosenSmall[0] - outputCoords[idChosenSmall * 2], goalCoordChosenSmall[1] - outputCoords[idChosenSmall * 2 + 1]}; - T distBaryPointSmall = ttk::Geometry::distance( + T distBaryPointSmall = ttk::Geometry::distance2D( &outputCoords[idChosenSmall * 2], coordscenterSmall, 2); T preFinalPosBarySmall[2] = {coordscenterSmall[0] + smallCompMoveVect[0], coordscenterSmall[1] + smallCompMoveVect[1]}; @@ -469,14 +468,13 @@ namespace ttk { if(nBig > 1) { finalDistortion = rotateMergingCompsBest( idsInHullSmall, idsInHullBig, compSmall, compBig, idChosenSmall, - idChosenBig, distMatrix, outputCoords, n, this->AngularSampleNb, - this->threadNumber_); + idChosenBig, distMatrix, outputCoords, n, this->AngularSampleNb); if(finalDistortion < -1) { return 1; } } - T finalDist = ttk::Geometry::distance( + T finalDist = ttk::Geometry::distance2D( &outputCoords[2 * idChosenSmall], &outputCoords[2 * idChosenBig], 2); if(fabs(finalDist - edgeCost) > Epsilon) { this->printErr( @@ -533,7 +531,7 @@ namespace ttk { for(size_t u1 = 0; u1 < n; u1++) { for(size_t u2 = u1 + 1; u2 < n; u2++) { edgeHeapAfter.push( - std::make_pair(ttk::Geometry::distance( + std::make_pair(ttk::Geometry::distance2D( &outputCoords[2 * u1], &outputCoords[2 * u2], 2), std::make_pair(u1, u2))); } @@ -541,7 +539,7 @@ namespace ttk { } else if(this->Strategy == STRATEGY::PRIM) { for(size_t u1 = 0; u1 < n; u1++) { edgeHeapAfter.push(std::make_pair( - ttk::Geometry::distance(&outputCoords[0], &outputCoords[2 * u1], 2), + ttk::Geometry::distance2D(&outputCoords[0], &outputCoords[2 * u1], 2), std::make_pair(0, u1))); } } @@ -594,7 +592,7 @@ namespace ttk { stillToDo.erase(v); for(size_t uToDo : stillToDo) { edgeHeapAfter.push(std::make_pair( - ttk::Geometry::distance( + ttk::Geometry::distance2D( &outputCoords[2 * v], &outputCoords[2 * uToDo], 2), std::make_pair(v, uToDo))); } @@ -680,12 +678,10 @@ namespace ttk { const std::vector &distMatrix, T *allCoords, size_t n, - size_t angularSampleNb, - size_t nThread) { // TODO remove nThread - TTK_FORCE_USE(nThread); + size_t angularSampleNb) { // The distance between the two components. T shortestDistPossible - = ttk::Geometry::distance(&allCoords[2 * iPt1], &allCoords[2 * iPt2], 2); + = ttk::Geometry::distance2D(&allCoords[2 * iPt1], &allCoords[2 * iPt2], 2); T coordPt1[2] = {allCoords[2 * iPt1], allCoords[2 * iPt1 + 1]}; T coordPt2[2] = {allCoords[2 * iPt2], allCoords[2 * iPt2 + 1]}; size_t comp1Size = comp1.size(), comp2Size = comp2.size(); @@ -750,7 +746,7 @@ namespace ttk { bool errorDuringLoop = false; bool foundValidAngle = false; #ifdef TTK_ENABLE_OPENMP -#pragma omp parallel for num_threads(nThread) shared(allCoords) +#pragma omp parallel for num_threads(this->threadNumber_) shared(allCoords) #endif // We enumerate the rotation angles for the first component. for(size_t i1 = 0; i1 < nbIter1; i1++) { @@ -774,7 +770,7 @@ namespace ttk { T coordARotate[2] = {coords1Test[2 * i], coords1Test[2 * i + 1]}; for(size_t j = 0; j < comp2Size; j++) { T coordBRotate[2] = {coords2Test[2 * j], coords2Test[2 * j + 1]}; - T newDist = ttk::Geometry::distance(coordARotate, coordBRotate, 2); + T newDist = ttk::Geometry::distance2D(coordARotate, coordBRotate, 2); curScore += (newDist - origDistMatrix[i][j]) * (newDist - origDistMatrix[i][j]); if(newDist + Epsilon < shortestDistPossible) { @@ -852,7 +848,7 @@ namespace ttk { idsInHull.push_back(compPtsIds[0]); if(nbPoint == 2) { double dist - = ttk::Geometry::distance(&compCoords[0], &compCoords[2], 2); + = ttk::Geometry::distance2D(&compCoords[0], &compCoords[2], 2); if(dist > Epsilon) { idsInHull.push_back(compPtsIds[1]);