From 7dd3d047ecca421044de141dc648e4b7baa26b62 Mon Sep 17 00:00:00 2001 From: Benjamin Lozes Date: Tue, 16 Jan 2024 15:56:15 +0100 Subject: [PATCH 1/3] Add an assertion for changing num_threads along execution --- include/graphblas/reference/coordinates.hpp | 11 +++++++++++ include/graphblas/reference/vector.hpp | 3 +++ 2 files changed, 14 insertions(+) diff --git a/include/graphblas/reference/coordinates.hpp b/include/graphblas/reference/coordinates.hpp index 59ea0326f..60253723b 100644 --- a/include/graphblas/reference/coordinates.hpp +++ b/include/graphblas/reference/coordinates.hpp @@ -97,6 +97,13 @@ namespace grb { /** Per-thread capacity for parallel stack updates. */ size_t _buf; + /** Number of threads for which these coordinates have been initialised. */ +#ifdef _H_GRB_REFERENCE_OMP_COORDINATES + const size_t _threads = config::OMP::threads(); +#else + const size_t _threads = 1; +#endif + /** * Increments the number of nonzeroes in the current thread-local stack. * @@ -294,6 +301,10 @@ namespace grb { // blocks are not managed by this class) } + size_t requiredThreadsForUpdate() const noexcept { + return _threads; + } + /** * @returns An empty thread-local stack for new nonzeroes. */ diff --git a/include/graphblas/reference/vector.hpp b/include/graphblas/reference/vector.hpp index f0db908b2..d8cd57cb8 100644 --- a/include/graphblas/reference/vector.hpp +++ b/include/graphblas/reference/vector.hpp @@ -1274,6 +1274,9 @@ namespace grb { template< typename D, typename C > inline C & getCoordinates( Vector< D, reference, C > &x ) noexcept { +#ifdef _H_GRB_REFERENCE_OMP_VECTOR + assert( x._coordinates.requiredThreadsForUpdate() == config::OMP::threads() ); +#endif return x._coordinates; } From 46aefeaaf19afb13ce8bae921384aaf0619d1e29 Mon Sep 17 00:00:00 2001 From: Benjamin Lozes Date: Tue, 16 Jan 2024 17:12:06 +0100 Subject: [PATCH 2/3] Fix the assertion to be callable from parallel context --- include/graphblas/omp/config.hpp | 9 +++++++++ include/graphblas/reference/vector.hpp | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/graphblas/omp/config.hpp b/include/graphblas/omp/config.hpp index 5c5cdb076..106528d68 100644 --- a/include/graphblas/omp/config.hpp +++ b/include/graphblas/omp/config.hpp @@ -79,6 +79,15 @@ namespace grb { return ret; } + /** + * @returns The maximum number of threads reported by OpenMP. + * + * This function can be called from any context. + */ + static size_t maxThreads() { + return omp_get_max_threads(); + } + /** * @returns The number of threads in the current OpenMP parallel section. * diff --git a/include/graphblas/reference/vector.hpp b/include/graphblas/reference/vector.hpp index d8cd57cb8..ef496c4a5 100644 --- a/include/graphblas/reference/vector.hpp +++ b/include/graphblas/reference/vector.hpp @@ -1275,7 +1275,12 @@ namespace grb { template< typename D, typename C > inline C & getCoordinates( Vector< D, reference, C > &x ) noexcept { #ifdef _H_GRB_REFERENCE_OMP_VECTOR - assert( x._coordinates.requiredThreadsForUpdate() == config::OMP::threads() ); + if( x._coordinates.requiredThreadsForUpdate() != config::OMP::maxThreads() ) { + #pragma omp critical + std::cerr << " " << x._coordinates.requiredThreadsForUpdate() + << " != " << config::OMP::maxThreads() << "\n"; + } + assert( x._coordinates.requiredThreadsForUpdate() == config::OMP::maxThreads() ); #endif return x._coordinates; } From 48a6c1abc5196dcc14f7a7583abc1f6f362d3425 Mon Sep 17 00:00:00 2001 From: Benjamin Lozes Date: Mon, 26 Feb 2024 15:01:35 +0100 Subject: [PATCH 3/3] Review fix --- include/graphblas/reference/vector.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/graphblas/reference/vector.hpp b/include/graphblas/reference/vector.hpp index ef496c4a5..ac6284330 100644 --- a/include/graphblas/reference/vector.hpp +++ b/include/graphblas/reference/vector.hpp @@ -1274,7 +1274,7 @@ namespace grb { template< typename D, typename C > inline C & getCoordinates( Vector< D, reference, C > &x ) noexcept { -#ifdef _H_GRB_REFERENCE_OMP_VECTOR +#if defined(_H_GRB_REFERENCE_OMP_VECTOR) && !defined(NDEBUG) if( x._coordinates.requiredThreadsForUpdate() != config::OMP::maxThreads() ) { #pragma omp critical std::cerr << " " << x._coordinates.requiredThreadsForUpdate()