From 2fd6f14cbe1c949d5724ffe762ce27ab9ab95941 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Wed, 6 Jun 2018 21:33:27 +0000 Subject: Merge paralellized version of Nerve_GIC fix for CoordGIC from Nerve_GIC branch git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/trunk@3552 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 384a6362a19da625b09879ad6eb7573c48a52940 --- src/Nerve_GIC/include/gudhi/GIC.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/Nerve_GIC/include/gudhi') diff --git a/src/Nerve_GIC/include/gudhi/GIC.h b/src/Nerve_GIC/include/gudhi/GIC.h index aa6478e5..05c6a386 100644 --- a/src/Nerve_GIC/include/gudhi/GIC.h +++ b/src/Nerve_GIC/include/gudhi/GIC.h @@ -415,7 +415,6 @@ class Cover_complex { double set_graph_from_automatic_rips(Distance distance, int N = 100) { int m = floor(n / std::exp((1 + rate_power) * std::log(std::log(n) / std::log(rate_constant)))); m = std::min(m, n - 1); - std::vector samples(m); double delta = 0; if (verbose) std::cout << n << " points in R^" << data_dimension << std::endl; @@ -424,7 +423,9 @@ class Cover_complex { if (distances.size() == 0) compute_pairwise_distances(distance); #ifdef GUDHI_USE_TBB - tbb::parallel_for(0, N, [&](int i){ + tbb::mutex deltamutex; + tbb::parallel_for(0, N, [&](int i){ + std::vector samples(m); SampleWithoutReplacement(n, m, samples); double hausdorff_dist = 0; for (int j = 0; j < n; j++) { @@ -432,10 +433,13 @@ class Cover_complex { for (int k = 1; k < m; k++) mj = std::min(mj, distances[j][samples[k]]); hausdorff_dist = std::max(hausdorff_dist, mj); } + deltamutex.lock(); delta += hausdorff_dist / N; + deltamutex.unlock(); }); #else for (int i = 0; i < N; i++) { + std::vector samples(m); SampleWithoutReplacement(n, m, samples); double hausdorff_dist = 0; for (int j = 0; j < n; j++) { @@ -717,6 +721,7 @@ class Cover_complex { #ifdef GUDHI_USE_TBB if (verbose) std::cout << "Computing connected components (parallelized)..." << std::endl; + tbb::mutex covermutex, idmutex; tbb::parallel_for(0, res, [&](int i){ // Compute connected components Graph G = one_skeleton.create_subgraph(); @@ -735,16 +740,20 @@ class Cover_complex { int identifier = ((i + component[j])*(i + component[j]) + 3 * i + component[j]) / 2; // Update covers + covermutex.lock(); cover[preimages[i][j]].push_back(identifier); cover_back[identifier].push_back(preimages[i][j]); cover_fct[identifier] = i; cover_std[identifier] = funcstd[i]; cover_color[identifier].second += func_color[preimages[i][j]]; cover_color[identifier].first += 1; + covermutex.unlock(); } // Maximal dimension is total number of connected components + idmutex.lock(); id += max + 1; + idmutex.unlock(); }); #else if (verbose) std::cout << "Computing connected components..." << std::endl; -- cgit v1.2.3 From 027dac0358de7d07f772c32cbdf2306dfeae714b Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Wed, 6 Jun 2018 22:00:46 +0000 Subject: Merge of branch thread_local_optional_vincent to take into account XCode < v.8 as thread local is not available git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/trunk@3554 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 9dcf072a4606d1131a925bca4571f0f1b5547d14 --- src/Nerve_GIC/include/gudhi/GIC.h | 10 +++-- src/Simplex_tree/include/gudhi/Simplex_tree.h | 6 ++- src/cmake/modules/GUDHI_compilation_flags.cmake | 49 ++++++++++++++++++++++--- src/cython/CMakeLists.txt | 27 +------------- 4 files changed, 56 insertions(+), 36 deletions(-) (limited to 'src/Nerve_GIC/include/gudhi') diff --git a/src/Nerve_GIC/include/gudhi/GIC.h b/src/Nerve_GIC/include/gudhi/GIC.h index 05c6a386..ebe463dd 100644 --- a/src/Nerve_GIC/include/gudhi/GIC.h +++ b/src/Nerve_GIC/include/gudhi/GIC.h @@ -142,6 +142,9 @@ class Cover_complex { std::string point_cloud_name; std::string color_name; + // 2 threads using 2 different GIC will have their own random engine + std::default_random_engine re; + // Remove all edges of a graph. void remove_edges(Graph& G) { boost::graph_traits::edge_iterator ei, ei_end; @@ -150,8 +153,7 @@ class Cover_complex { // Find random number in [0,1]. double GetUniform() { - thread_local std::default_random_engine re; - thread_local std::uniform_real_distribution Dist(0, 1); + std::uniform_real_distribution Dist(0, 1); return Dist(re); } @@ -422,7 +424,9 @@ class Cover_complex { if (distances.size() == 0) compute_pairwise_distances(distance); - #ifdef GUDHI_USE_TBB + // This cannot be parallelized if thread_local is not defined + // thread_local is not defined for XCode < v.8 + #if defined(GUDHI_USE_TBB) && defined(GUDHI_CAN_USE_CXX11_THREAD_LOCAL) tbb::mutex deltamutex; tbb::parallel_for(0, N, [&](int i){ std::vector samples(m); diff --git a/src/Simplex_tree/include/gudhi/Simplex_tree.h b/src/Simplex_tree/include/gudhi/Simplex_tree.h index 74a7281d..ee96d5a2 100644 --- a/src/Simplex_tree/include/gudhi/Simplex_tree.h +++ b/src/Simplex_tree/include/gudhi/Simplex_tree.h @@ -689,7 +689,11 @@ class Simplex_tree { return { null_simplex(), true }; // ----->> // Copy before sorting - thread_local std::vector copy; + // Thread local is not available on XCode version < V.8 - It will slow down computation +#ifdef GUDHI_CAN_USE_CXX11_THREAD_LOCAL + thread_local +#endif // GUDHI_CAN_USE_CXX11_THREAD_LOCAL + std::vector copy; copy.clear(); copy.insert(copy.end(), first, last); std::sort(std::begin(copy), std::end(copy)); diff --git a/src/cmake/modules/GUDHI_compilation_flags.cmake b/src/cmake/modules/GUDHI_compilation_flags.cmake index 394f1f42..a01d6e13 100644 --- a/src/cmake/modules/GUDHI_compilation_flags.cmake +++ b/src/cmake/modules/GUDHI_compilation_flags.cmake @@ -1,6 +1,7 @@ # This files manage compilation flags required by GUDHI include(TestCXXAcceptsFlag) +include(CheckCXXSourceCompiles) # add a compiler flag only if it is accepted macro(add_cxx_compiler_flag _flag) @@ -11,6 +12,32 @@ macro(add_cxx_compiler_flag _flag) endif() endmacro() +function(can_cgal_use_cxx11_thread_local) + # This is because of https://github.com/CGAL/cgal/blob/master/Installation/include/CGAL/tss.h + # CGAL is using boost thread if thread_local is not ready (requires XCode 8 for Mac). + # The test in https://github.com/CGAL/cgal/blob/master/Installation/include/CGAL/config.h + # #if __has_feature(cxx_thread_local) || \ + # ( (__GNUC__ * 100 + __GNUC_MINOR__) >= 408 && __cplusplus >= 201103L ) || \ + # ( _MSC_VER >= 1900 ) + # #define CGAL_CAN_USE_CXX11_THREAD_LOCAL + # #endif + set(CGAL_CAN_USE_CXX11_THREAD_LOCAL " + int main() { + #ifndef __has_feature + #define __has_feature(x) 0 // Compatibility with non-clang compilers. + #endif + #if __has_feature(cxx_thread_local) || \ + ( (__GNUC__ * 100 + __GNUC_MINOR__) >= 408 && __cplusplus >= 201103L ) || \ + ( _MSC_VER >= 1900 ) + bool has_feature_thread_local = true; + #else + // Explicit error of compilation for CMake test purpose - has_feature_thread_local is not defined + #endif + bool result = has_feature_thread_local; + } ") + check_cxx_source_compiles("${CGAL_CAN_USE_CXX11_THREAD_LOCAL}" CGAL_CAN_USE_CXX11_THREAD_LOCAL_RESULT) +endfunction() + set (CMAKE_CXX_STANDARD 11) enable_testing() @@ -22,14 +49,24 @@ endif() add_cxx_compiler_flag("-Wall") -if(CMAKE_BUILD_TYPE MATCHES Debug) - message("++ Debug compilation flags are: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG}") -else() - message("++ Release compilation flags are: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}") -endif() - if (DEBUG_TRACES) # For programs to be more verbose message(STATUS "DEBUG_TRACES are activated") add_definitions(-DDEBUG_TRACES) endif() + +set(GUDHI_CAN_USE_CXX11_THREAD_LOCAL " + int main() { + thread_local int result = 0; + return result; + } ") +check_cxx_source_compiles("${GUDHI_CAN_USE_CXX11_THREAD_LOCAL}" GUDHI_CAN_USE_CXX11_THREAD_LOCAL_RESULT) +if (GUDHI_CAN_USE_CXX11_THREAD_LOCAL_RESULT) + add_definitions(-DGUDHI_CAN_USE_CXX11_THREAD_LOCAL) +endif() + +if(CMAKE_BUILD_TYPE MATCHES Debug) + message("++ Debug compilation flags are: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG}") +else() + message("++ Release compilation flags are: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}") +endif() diff --git a/src/cython/CMakeLists.txt b/src/cython/CMakeLists.txt index 158c7561..17d440ee 100644 --- a/src/cython/CMakeLists.txt +++ b/src/cython/CMakeLists.txt @@ -1,7 +1,5 @@ project(Cython) -include(CheckCXXSourceCompiles) - function( add_gudhi_cython_lib THE_LIB ) if(EXISTS ${THE_LIB}) get_filename_component(THE_LIB_FILE_NAME ${THE_LIB} NAME_WE) @@ -71,30 +69,7 @@ if(CYTHON_FOUND) endif (NOT CGAL_WITH_EIGEN3_VERSION VERSION_LESS 4.6.0) if(CGAL_FOUND) - # This is because of https://github.com/CGAL/cgal/blob/master/Installation/include/CGAL/tss.h - # CGAL is using boost thread if thread_local is not ready (requires XCode 8 for Mac). - # The test in https://github.com/CGAL/cgal/blob/master/Installation/include/CGAL/config.h - # #if __has_feature(cxx_thread_local) || \ - # ( (__GNUC__ * 100 + __GNUC_MINOR__) >= 408 && __cplusplus >= 201103L ) || \ - # ( _MSC_VER >= 1900 ) - # #define CGAL_CAN_USE_CXX11_THREAD_LOCAL - # #endif - set(CGAL_CAN_USE_CXX11_THREAD_LOCAL " - int main() { - #ifndef __has_feature - #define __has_feature(x) 0 // Compatibility with non-clang compilers. - #endif - #if __has_feature(cxx_thread_local) || \ - ( (__GNUC__ * 100 + __GNUC_MINOR__) >= 408 && __cplusplus >= 201103L ) || \ - ( _MSC_VER >= 1900 ) - bool has_feature_thread_local = true; - #else - // Explicit error of compilation for CMake test purpose - has_feature_thread_local is not defined - #endif - bool result = has_feature_thread_local; - } ") - check_cxx_source_compiles("${CGAL_CAN_USE_CXX11_THREAD_LOCAL}" CGAL_CAN_USE_CXX11_THREAD_LOCAL_RESULT) - + can_cgal_use_cxx11_thread_local() if (NOT CGAL_CAN_USE_CXX11_THREAD_LOCAL_RESULT) add_gudhi_cython_lib(${Boost_THREAD_LIBRARY}) set(GUDHI_CYTHON_LIBRARY_DIRS "${GUDHI_CYTHON_LIBRARY_DIRS}'${Boost_LIBRARY_DIRS}', ") -- cgit v1.2.3 From 1ab1ffb38c49443ebef69dbb86e5da582c1cd767 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Thu, 7 Jun 2018 09:29:16 +0000 Subject: Code review : Use thread_local when necessary git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/trunk@3558 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 0893cfa1065dcd9988c9d910e764b7a1edbcdc11 --- src/Nerve_GIC/include/gudhi/GIC.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/Nerve_GIC/include/gudhi') diff --git a/src/Nerve_GIC/include/gudhi/GIC.h b/src/Nerve_GIC/include/gudhi/GIC.h index ebe463dd..7aa95210 100644 --- a/src/Nerve_GIC/include/gudhi/GIC.h +++ b/src/Nerve_GIC/include/gudhi/GIC.h @@ -142,17 +142,25 @@ class Cover_complex { std::string point_cloud_name; std::string color_name; - // 2 threads using 2 different GIC will have their own random engine - std::default_random_engine re; - // Remove all edges of a graph. void remove_edges(Graph& G) { boost::graph_traits::edge_iterator ei, ei_end; for (boost::tie(ei, ei_end) = boost::edges(G); ei != ei_end; ++ei) boost::remove_edge(*ei, G); } + // Thread local is not available on XCode version < V.8 + // If not available, random engine is a class member. +#ifndef GUDHI_CAN_USE_CXX11_THREAD_LOCAL + std::default_random_engine re; +#endif // GUDHI_CAN_USE_CXX11_THREAD_LOCAL + // Find random number in [0,1]. double GetUniform() { + // Thread local is not available on XCode version < V.8 + // If available, random engine is defined for each thread. +#ifdef GUDHI_CAN_USE_CXX11_THREAD_LOCAL + thread_local std::default_random_engine re; +#endif // GUDHI_CAN_USE_CXX11_THREAD_LOCAL std::uniform_real_distribution Dist(0, 1); return Dist(re); } -- cgit v1.2.3 From 106372ae36905c2892c3dde6b7c36557baf54b34 Mon Sep 17 00:00:00 2001 From: vrouvrea Date: Mon, 2 Jul 2018 15:09:43 +0000 Subject: Merge Nerve_GIC modifications from Mathieu (11th of June 2018) svn merge -r3468:3579 ^/branches/Nerve_GIC git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/python_nerve_gic_vincent@3659 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 9e0e70cccf2829d7d55d241b807aeff0b73094bc --- src/Nerve_GIC/include/gudhi/GIC.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/Nerve_GIC/include/gudhi') diff --git a/src/Nerve_GIC/include/gudhi/GIC.h b/src/Nerve_GIC/include/gudhi/GIC.h index 7aa95210..fea0b861 100644 --- a/src/Nerve_GIC/include/gudhi/GIC.h +++ b/src/Nerve_GIC/include/gudhi/GIC.h @@ -1193,8 +1193,8 @@ class Cover_complex { } Cboot.set_graph_from_automatic_rips(Gudhi::Euclidean_distance()); - Cboot.set_automatic_resolution(); Cboot.set_gain(); + Cboot.set_automatic_resolution(); Cboot.set_cover_from_function(); Cboot.find_simplices(); Cboot.compute_PD(); @@ -1215,7 +1215,9 @@ class Cover_complex { */ double compute_distance_from_confidence_level(double alpha) { unsigned int N = distribution.size(); - return distribution[std::floor(alpha * N)]; + double d = distribution[std::floor(alpha * N)]; + if (verbose) std::cout << "Distance corresponding to confidence " << alpha << " is " << d << std::endl; + return d; } public: @@ -1229,6 +1231,7 @@ class Cover_complex { double level = 1; for (unsigned int i = 0; i < N; i++) if (distribution[i] > d){ level = i * 1.0 / N; break; } + if (verbose) std::cout << "Confidence level of distance " << d << " is " << level << std::endl; return level; } @@ -1238,9 +1241,8 @@ class Cover_complex { * */ double compute_p_value() { - double distancemin = -std::numeric_limits::lowest(); - int N = PD.size(); - for (int i = 0; i < N; i++) distancemin = std::min(distancemin, 0.5 * (PD[i].second - PD[i].first)); + double distancemin = std::numeric_limits::max(); int N = PD.size(); + for (int i = 0; i < N; i++) distancemin = std::min(distancemin, 0.5 * std::abs(PD[i].second - PD[i].first)); double p_value = 1 - compute_confidence_level_from_distance(distancemin); if (verbose) std::cout << "p value = " << p_value << std::endl; return p_value; -- cgit v1.2.3