From dfd970b1b15ead80ec11401d89dcf432e39cb107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Mon, 17 Oct 2022 15:22:13 +0200 Subject: [PATCH 1/5] MNT Fix build when SKLEARN_OPENMP_PARALLELISM_ENABLED=False --- .../_pairwise_distances_reduction/_base.pyx.tp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp b/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp index d3303511b64d9..3304ab87248d6 100644 --- a/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp +++ b/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp @@ -1,5 +1,7 @@ cimport numpy as cnp -cimport openmp + +IF SKLEARN_OPENMP_PARALLELISM_ENABLED: + cimport openmp from cython cimport final from cython.operator cimport dereference as deref @@ -73,7 +75,12 @@ cpdef DTYPE_t[::1] _sqeuclidean_row_norms32( ) with nogil, parallel(num_threads=num_threads): - thread_num = openmp.omp_get_thread_num() + + IF SKLEARN_OPENMP_PARALLELISM_ENABLED: + thread_num = openmp.omp_get_thread_num() + ELSE: + thread_num = 1 + for i in prange(n, schedule='static'): # Upcasting the i-th row of X from 32bit to 64bit for j in range(d): From 5d52786494d210df799cddc0a7e5b189bd403e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 18 Oct 2022 09:26:58 +0200 Subject: [PATCH 2/5] Use _openmp_thread_num --- sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp b/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp index 3304ab87248d6..e9a8cc51a4392 100644 --- a/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp +++ b/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp @@ -75,11 +75,7 @@ cpdef DTYPE_t[::1] _sqeuclidean_row_norms32( ) with nogil, parallel(num_threads=num_threads): - - IF SKLEARN_OPENMP_PARALLELISM_ENABLED: - thread_num = openmp.omp_get_thread_num() - ELSE: - thread_num = 1 + thread_num = _openmp_thread_num() for i in prange(n, schedule='static'): # Upcasting the i-th row of X from 32bit to 64bit From fdf9b387da001b9570a2cf00d03f9bbccd9d486c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 18 Oct 2022 09:29:27 +0200 Subject: [PATCH 3/5] openmp import not needed anymore --- sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp | 3 --- 1 file changed, 3 deletions(-) diff --git a/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp b/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp index e9a8cc51a4392..b16ec65e3999a 100644 --- a/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp +++ b/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp @@ -1,8 +1,5 @@ cimport numpy as cnp -IF SKLEARN_OPENMP_PARALLELISM_ENABLED: - cimport openmp - from cython cimport final from cython.operator cimport dereference as deref from cython.parallel cimport parallel, prange From 58970ffc16bcfb4fd9f078b46e5c4d748e67397b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 18 Oct 2022 10:19:55 +0200 Subject: [PATCH 4/5] trigger From 0973d2f54cdbbb593fa003c4ddcc9689cec4fc23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 18 Oct 2022 10:41:03 +0200 Subject: [PATCH 5/5] Mention OpenMP helpers in the doc --- doc/developers/performance.rst | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/doc/developers/performance.rst b/doc/developers/performance.rst index 36419894eafd6..c6fcc99b26102 100644 --- a/doc/developers/performance.rst +++ b/doc/developers/performance.rst @@ -341,9 +341,16 @@ memory alignment, direct blas calls... Using OpenMP ------------ -Since scikit-learn can be built without OpenMP, it's necessary to -protect each direct call to OpenMP. This can be done using the following -syntax:: +Since scikit-learn can be built without OpenMP, it's necessary to protect each +direct call to OpenMP. + +There are some helpers in +[sklearn/utils/_openmp_helpers.pyx](https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/utils/_openmp_helpers.pyx) +that you can reuse for the main useful functionalities and already have the +necessary protection to be built without OpenMP. + +If the helpers are not enough, you need to protect your OpenMP code using the +following syntax:: # importing OpenMP IF SKLEARN_OPENMP_PARALLELISM_ENABLED: