diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a18965..e671b87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,20 +158,15 @@ endif() # TBB (optional) # Must come before CGAL so it can use TBB properly if ( CCCORELIB_USE_TBB ) - find_package( TBB COMPONENTS tbb CONFIG ) + find_package( TBB COMPONENTS tbb 2022.0.0 CONFIG ) + set( CCCORELIB_USE_QT_CONCURRENT OFF ) if ( TBB_FOUND ) - if ( ${TBB_VERSION} VERSION_GREATER 2021.0.0 ) - target_link_libraries( CCCoreLib - PUBLIC - TBB::tbb - ) - else() - target_link_libraries( CCCoreLib - PUBLIC - ${TBB_IMPORTED_TARGETS} - ) - endif() + target_link_libraries( CCCoreLib + PUBLIC + TBB::tbb + ) + target_compile_definitions( CCCoreLib PUBLIC CC_CORE_LIB_USES_TBB @@ -192,14 +187,7 @@ if ( CCCORELIB_USE_CGAL ) if ( CCCORELIB_USE_TBB ) if ( TBB_FOUND ) - # Once Linux libcgal-dev >= 5.0, target_compile_definitions replaced by: - # CGAL_target_use_TBB( CCCoreLib ) - - target_compile_definitions( CCCoreLib - PRIVATE - CGAL_LINKED_WITH_TBB - NOMINMAX - ) + CGAL_target_use_TBB( CCCoreLib ) else() message( WARNING "CGAL cannot compile with TBB (TBB not found)" ) endif() @@ -232,7 +220,7 @@ if ( CCCORELIB_USE_QT_CONCURRENT ) target_link_libraries( CCCoreLib PUBLIC - Qt6::Concurrent + Qt6::Concurrent ) target_compile_definitions( CCCoreLib @@ -268,9 +256,9 @@ install( include( CMakePackageConfigHelpers ) configure_package_config_file(${CMAKE_CURRENT_LIST_DIR}/cmake/${PROJECT_NAME}Config.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake - INSTALL_DESTINATION lib/cmake/${PROJECT_NAME} - NO_CHECK_REQUIRED_COMPONENTS_MACRO + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake + INSTALL_DESTINATION lib/cmake/${PROJECT_NAME} + NO_CHECK_REQUIRED_COMPONENTS_MACRO ) write_basic_package_version_file( CCCoreLibConfigVersion.cmake diff --git a/include/ParallelSort.h b/include/ParallelSort.h index 07fec9b..a1f63f5 100644 --- a/include/ParallelSort.h +++ b/include/ParallelSort.h @@ -10,29 +10,28 @@ #if defined(_MSC_VER) && (_MSC_VER >= 1800) - //Parallel Patterns Library (for parallel sort) - #include + //Parallel Patterns Library (for parallel sort) + #include - #define ParallelSort Concurrency::parallel_sort + #define ParallelSort Concurrency::parallel_sort #elif CC_CORE_LIB_USES_TBB - #ifndef Q_MOC_RUN - #if defined(emit) - #undef emit - #include - #define emit // restore the macro definition of "emit", as it was defined in gtmetamacros.h - #else - #include - #endif // defined(emit) - #endif // Q_MOC_RUN + #ifndef Q_MOC_RUN + #if defined(emit) + #undef emit + #include + #define emit // restore the macro definition of "emit", as it was defined in gtmetamacros.h + #else + #include + #endif // defined(emit) + #endif // Q_MOC_RUN - #define ParallelSort tbb::parallel_sort + #define ParallelSort oneapi::tbb::parallel_sort #else - #include - - #define ParallelSort std::sort + #include + #define ParallelSort std::sort #endif diff --git a/src/DgmOctree.cpp b/src/DgmOctree.cpp index 60ef26a..3e490f1 100644 --- a/src/DgmOctree.cpp +++ b/src/DgmOctree.cpp @@ -35,12 +35,15 @@ #ifndef Q_MOC_RUN #if defined(emit) #undef emit - #include + #include + #include #define emit // restore the macro definition of "emit", as it was defined in gtmetamacros.h #else - #include + #include + #include #endif // defined(emit) #endif // Q_MOC_RUN +using namespace oneapi; #endif //#endif // #ifndef CC_DEBUG @@ -3111,7 +3114,7 @@ unsigned DgmOctree::executeFunctionForAllCellsAtLevel( unsigned char level, progressCb->update(0); progressCb->start(); } - + NormalizedProgress nprogress(progressCb, m_theAssociatedCloud->size()); #ifdef COMPUTE_NN_SEARCH_STATISTICS @@ -3250,8 +3253,10 @@ unsigned DgmOctree::executeFunctionForAllCellsAtLevel( unsigned char level, progressCb->update(0); m_MT_wrapper.normProgressCb = new NormalizedProgress(progressCb, m_theAssociatedCloud->size()); progressCb->start(); +#if defined(CC_CORE_LIB_USES_QT_CONCURRENT) #ifndef __APPLE__ QCoreApplication::processEvents(QEventLoop::EventLoopExec); // to allow the GUI to refresh itself +#endif #endif } @@ -3266,15 +3271,17 @@ unsigned DgmOctree::executeFunctionForAllCellsAtLevel( unsigned char level, QThreadPool::globalInstance()->setMaxThreadCount(maxThreadCount); QtConcurrent::blockingMap(cells, [this](const octreeCellDesc& desc) { m_MT_wrapper.launchOctreeCellFunc(desc); }); #elif defined(CC_CORE_LIB_USES_TBB) - tbb::task_scheduler_init init(maxThreadCount != 0 ? maxThreadCount : tbb::task_scheduler_init::automatic); - tbb::parallel_for(tbb::blocked_range(0, cells.size()), - [&](tbb::blocked_range r) + tbb::task_arena local_arena(maxThreadCount != 0 ? maxThreadCount : tbb::info::default_concurrency()); + local_arena.execute([&] { + tbb::parallel_for(tbb::blocked_range(0, cells.size()), + [&](tbb::blocked_range r) { - for (auto i = r.begin(); i < r.end(); ++i) - { - m_MT_wrapper.launchOctreeCellFunc(cells[i]); - } + for (auto i = r.begin(); i < r.end(); ++i) + { + m_MT_wrapper.launchOctreeCellFunc(cells[i]); + } }); + }); #endif #ifdef COMPUTE_NN_SEARCH_STATISTICS FILE* fp = fopen("octree_log.txt", "at"); @@ -3873,15 +3880,18 @@ unsigned DgmOctree::executeFunctionForAllCellsStartingAtLevel(unsigned char star QtConcurrent::blockingMap(cells, [this](const octreeCellDesc& desc) { m_MT_wrapper.launchOctreeCellFunc(desc); } ); #elif defined(CC_CORE_LIB_USES_TBB) // Otherwise we use TBB if we can - tbb::task_scheduler_init init(maxThreadCount != 0 ? maxThreadCount : tbb::task_scheduler_init::automatic); - tbb::parallel_for(tbb::blocked_range(0, cells.size()), - [&](tbb::blocked_range r) + tbb::task_arena local_arena(maxThreadCount != 0 ? maxThreadCount : tbb::info::default_concurrency()); + local_arena.execute([&] + { + tbb::parallel_for(tbb::blocked_range(0, cells.size()), + [&](tbb::blocked_range r) { for (auto i = r.begin(); i < r.end(); ++i) { m_MT_wrapper.launchOctreeCellFunc(cells[i]); } }); + }); #endif #ifdef COMPUTE_NN_SEARCH_STATISTICS diff --git a/src/DistanceComputationTools.cpp b/src/DistanceComputationTools.cpp index 679d040..a0b1500 100644 --- a/src/DistanceComputationTools.cpp +++ b/src/DistanceComputationTools.cpp @@ -30,16 +30,20 @@ #elif defined(CC_CORE_LIB_USES_TBB) //enables multi-threading handling with TBB #define ENABLE_CLOUD2MESH_DIST_MT -#include #ifndef Q_MOC_RUN #if defined(emit) #undef emit - #include + #include + #include + #include #define emit // restore the macro definition of "emit", as it was defined in gtmetamacros.h #else - #include + #include + #include + #include #endif // defined(emit) #endif // Q_MOC_RUN +using namespace oneapi; #else //Note that there is the case CC_DEBUG=OFF and neither TBB nor Qt #undef ENABLE_CLOUD2MESH_DIST_MT @@ -934,7 +938,7 @@ namespace CCCoreLib #if defined(CC_CORE_LIB_USES_QT_CONCURRENT) QMutex currentBitMaskMutex; #elif defined(CC_CORE_LIB_USES_TBB) - std::mutex currentBitMaskMutex; + tbb::mutex currentBitMaskMutex; #endif }; } @@ -956,8 +960,9 @@ static void CloudMeshDistCellFunc_MT(const DgmOctree::IndexAndCode& desc) if (s_multiThreadingWrapper.normProgressCb) { +#if defined(CC_CORE_LIB_USES_QT_CONCURRENT) QCoreApplication::processEvents(QEventLoop::EventLoopExec); // to allow the GUI to refresh itself - +#endif if (!s_multiThreadingWrapper.normProgressCb->oneStep()) { s_multiThreadingWrapper.cellFunc_success = false; @@ -1565,7 +1570,7 @@ int DistanceComputationTools::computePoint2MeshDistancesWithOctree( const CCVect return DISTANCE_COMPUTATION_RESULTS::SUCCESS; } -int DistanceComputationTools::computeCloud2MeshDistancesWithOctree( const DgmOctree* octree, +int DistanceComputationTools::computeCloud2MeshDistancesWithOctree( const DgmOctree* octree, const GridAndMeshIntersection& intersection, Cloud2MeshDistancesComputationParams& params, GenericProgressCallback* progressCb/*=nullptr*/) @@ -1819,15 +1824,18 @@ int DistanceComputationTools::computeCloud2MeshDistancesWithOctree( const DgmOct QThreadPool::globalInstance()->setMaxThreadCount(params.maxThreadCount); QtConcurrent::blockingMap(cellsDescs, CloudMeshDistCellFunc_MT); #elif defined(CC_CORE_LIB_USES_TBB) - tbb::parallel_for(tbb::blocked_range(0, cellsDescs.size()), - [&](tbb::blocked_range r) + tbb::task_arena local_arena(params.maxThreadCount != 0 ? params.maxThreadCount : tbb::info::default_concurrency()); + local_arena.execute([&] + { + tbb::parallel_for(tbb::blocked_range(0, cellsDescs.size()), + [&](tbb::blocked_range r) { - for (auto i = r.begin(); r.end(); ++i) + for (auto i = r.begin(); i < r.end(); ++i) { CloudMeshDistCellFunc_MT(cellsDescs[i]); } - } - ); + }); + }); #endif s_multiThreadingWrapper.octree = nullptr; @@ -1859,7 +1867,7 @@ int DistanceComputationTools::computeCloud2MeshDistances( GenericIndexedCloudPer DgmOctree* cloudOctree/*=nullptr*/) { //check the input - if (!pointCloud) + if (!pointCloud) { assert(false); return DISTANCE_COMPUTATION_RESULTS::ERROR_NULL_COMPAREDCLOUD; @@ -1921,7 +1929,7 @@ int DistanceComputationTools::computeCloud2MeshDistances( GenericIndexedCloudPer maxBB.u[k] = std::max(meshMaxBB.u[k], cloudMaxBB.u[k]); } - //max cubical bounding-box + //max cubical bounding-box cubicalMinBB = minBB; cubicalMaxBB = maxBB;