From a01d4a81fd6375d867f77c18940d18ed9d7af11b Mon Sep 17 00:00:00 2001 From: Paul Leroy Date: Tue, 13 Feb 2024 14:35:17 +0100 Subject: [PATCH] remove breaks from omp parallel loops --- ContextBasedFeature.cpp | 9 ++++----- PointFeature.cpp | 12 ++++++------ q3DMASCClassifier.cpp | 13 +++++++++---- q3DMASCTools.cpp | 16 ++++++++-------- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/ContextBasedFeature.cpp b/ContextBasedFeature.cpp index eb158d5..acb6950 100644 --- a/ContextBasedFeature.cpp +++ b/ContextBasedFeature.cpp @@ -23,9 +23,6 @@ //qCC_db #include -//CCPluginAPI -#include - //Qt #include @@ -197,10 +194,12 @@ bool ContextBasedFeature::prepare( const CorePoints& corePoints, bool cancelled = false; #ifndef _DEBUG #if defined(_OPENMP) -#pragma omp parallel for num_threads(ccQtHelpers::GetMaxThreadCount(omp_get_max_threads())) +#pragma omp parallel for num_threads(omp_get_max_threads()) #endif #endif for (int i = 0; i < static_cast(pointCount); ++i) + { + if (!cancelled) { const CCVector3* P = corePoints.cloud->getPoint(i); CCCoreLib::ReferenceCloud Yk(&classCloud); @@ -262,10 +261,10 @@ bool ContextBasedFeature::prepare( const CorePoints& corePoints, { //process cancelled by the user errorMessage = "Process cancelled"; - break; } } } + } if (progressCb) { diff --git a/PointFeature.cpp b/PointFeature.cpp index 0017b69..a7d0312 100644 --- a/PointFeature.cpp +++ b/PointFeature.cpp @@ -89,9 +89,6 @@ constexpr const char* LAS_FIELD_NAMES[22] = {"X", //CCLib #include -//CCPluginAPI -#include - //system #include @@ -427,10 +424,13 @@ static bool ComputeMathOpWithNearestNeighbor( const CorePoints& corePoints, error.clear(); #ifndef _DEBUG #if defined(_OPENMP) -#pragma omp parallel for num_threads(ccQtHelpers::GetMaxThreadCount(omp_get_max_threads())) + bool cancelled = false; +#pragma omp parallel for num_threads(omp_get_max_threads()) #endif #endif for (int i = 0; i < static_cast(pointCount); ++i) + { + if (!cancelled) { const CCVector3* P = corePoints.cloud->getPoint(i); CCCoreLib::ReferenceCloud Yk(&cloud2); @@ -474,16 +474,16 @@ static bool ComputeMathOpWithNearestNeighbor( const CorePoints& corePoints, if (progressCb) { mutex.lock(); - bool cancelled = !nProgress.oneStep(); + cancelled = !nProgress.oneStep(); mutex.unlock(); if (cancelled) { //process cancelled by the user error = "Process cancelled"; - break; } } } + } outSF->computeMinAndMax(); diff --git a/q3DMASCClassifier.cpp b/q3DMASCClassifier.cpp index ea3356e..2b141b7 100644 --- a/q3DMASCClassifier.cpp +++ b/q3DMASCClassifier.cpp @@ -32,7 +32,6 @@ //CCPluginAPI #include -#include //Qt #include @@ -210,10 +209,12 @@ bool Classifier::classify( const Feature::Source::Set& featureSources, int numberOfTrees = static_cast(m_rtrees->getRoots().size()); #ifndef _DEBUG #if defined(_OPENMP) -#pragma omp parallel for num_threads(ccQtHelpers::GetMaxThreadCount(omp_get_max_threads())) + bool cancelled = false; +#pragma omp parallel for num_threads(omp_get_max_threads()) #endif #endif for (int i = 0; i < static_cast(cloud->size()); ++i) + { { //allocate the data matrix cv::Mat test_data; @@ -225,9 +226,11 @@ bool Classifier::classify( const Feature::Source::Set& featureSources, { errorMessage = cvex.msg.c_str(); success = false; - break; + cancelled = true; } + if (!cancelled) + { for (int fIndex = 0; fIndex < attributesPerSample; ++fIndex) { double value = wrappers[fIndex]->pointValue(i); @@ -258,8 +261,10 @@ bool Classifier::classify( const Feature::Source::Set& featureSources, { //process cancelled by the user success = false; - break; + cancelled = true; } + } + } } classificationSF->computeMinAndMax(); diff --git a/q3DMASCTools.cpp b/q3DMASCTools.cpp index 32833dd..181625c 100644 --- a/q3DMASCTools.cpp +++ b/q3DMASCTools.cpp @@ -30,9 +30,6 @@ #include #include -//CCPluginAPI -#include - //qPDALIO #include "../../../core/IO/qPDALIO/include/LASFields.h" @@ -1226,25 +1223,28 @@ bool Tools::PrepareFeatures(const CorePoints& corePoints, Feature::Set& features unsigned char octreeLevel = octree->findBestLevelForAGivenNeighbourhoodSizeExtraction(largestRadius); unsigned pointCount = corePoints.size(); - QString logMessage = QString("Computing %1 features on cloud %2 (%3 core points)").arg(fas.featureCount).arg(sourceCloud->getName()).arg(pointCount); + QString logMessage = QString("Computing %1 features on cloud %2 at %3 core points").arg(fas.featureCount).arg(sourceCloud->getName()).arg(pointCount); if (progressCb) { progressCb->setMethodTitle("Compute features"); progressCb->setInfo(qPrintable(logMessage)); } - ccLog::Print(logMessage + ", nb points " + QString::number(pointCount)); + ccLog::Print(logMessage); CCCoreLib::NormalizedProgress nProgress(progressCb, pointCount); QMutex mutex; #ifndef _DEBUG #if defined(_OPENMP) - int num_threads = ccQtHelpers::GetMaxThreadCount(omp_get_max_threads()); + int num_threads = omp_get_max_threads(); ccLog::Print("Using OpenMP with " + QString::number(num_threads) + " threads "); + bool cancelled = false; #pragma omp parallel for num_threads(num_threads) #endif #endif for (int i = 0; i < static_cast(pointCount); ++i) + { + if (!cancelled) { //spherical neighborhood extraction structure CCCoreLib::DgmOctree::NearestNeighboursSearchStruct nNSS; @@ -1384,7 +1384,7 @@ bool Tools::PrepareFeatures(const CorePoints& corePoints, Feature::Set& features if (progressCb) { mutex.lock(); - bool cancelled = !nProgress.oneStep(); + cancelled = !nProgress.oneStep(); mutex.unlock(); if (cancelled) { @@ -1392,9 +1392,9 @@ bool Tools::PrepareFeatures(const CorePoints& corePoints, Feature::Set& features ccLog::Warning("Process cancelled"); errorStr = "Process cancelled, iteration " + QString::number(i); success = false; - break; } } + } } //for each point } //for each cloud