Skip to content

MNT Fix build when SKLEARN_OPENMP_PARALLELISM_ENABLED=False #24682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions doc/developers/performance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
cimport numpy as cnp
cimport openmp

from cython cimport final
from cython.operator cimport dereference as deref
Expand Down Expand Up @@ -73,7 +72,8 @@ cpdef DTYPE_t[::1] _sqeuclidean_row_norms32(
)

with nogil, parallel(num_threads=num_threads):
thread_num = openmp.omp_get_thread_num()
thread_num = _openmp_thread_num()

for i in prange(n, schedule='static'):
# Upcasting the i-th row of X from float32 to float64
for j in range(d):
Expand Down