Skip to content

FIX Use "sequential_blas_under_openmp" to limit BLAS under OpenMP - #35180

Merged
ogrisel merged 2 commits into
scikit-learn:mainfrom
cakedev0:utils/sequential_blas_under_openmp
Oct 27, 2026
Merged

ogrisel merged 2 commits into
scikit-learn:mainfrom
cakedev0:utils/sequential_blas_under_openmp

Conversation

@cakedev0

@cakedev0 cakedev0 commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Closes joblib/threadpoolctl#272

Found while working on kmeans++ optimization: since threadpoolctl 3.7, with OpenBLAS's OpenMP threading layer, limits=1, user_api="blas" also sets the number of OpenMP threads of the calling thread to 1.

What does this implement/fix? Explain your changes.

ArgKmin.compute (used by kneighbors, pairwise_distances_argmin, ...) creates its object inside its BLAS limit, and the constructor reads the number of OpenMP threads with _openmp_effective_n_threads(). With OpenBLAS's OpenMP threading layer (the default of conda-forge on macOS) and threadpoolctl 3.7, it gets 1, and the whole k-NN search runs on a single thread.

This PR replaces limits=1, user_api="blas" with limits="sequential_blas_under_openmp" where needed. threadpoolctl added this option in 3.1 for this use case (joblib/threadpoolctl#114): it limits BLAS to 1 thread, except for OpenBLAS with the OpenMP threading layer where it does nothing: inside an OpenMP parallel region, OpenBLAS then uses blas_omp_threads_local threads, which is 1 by default (common_thread.h).

Only ArgKmin is affected: the other reductions create their object before the BLAS limit, and KMeans computes its number of threads in fit. Still, I changed them too, so that all the internal limits are the same.

The first commit (dc5a54a) only adds the test, which fails on main in the macOS arm64 conda-forge job (OpenBLAS 0.3.34 with OpenMP threading, threadpoolctl 3.7.0): CI log (assert [1] == [2]). I also checked it locally on Linux with conda-forge's OpenBLAS with OpenMP threading: the test fails on main and passes with this PR.

AI usage disclosure

I used AI assistance for:

  • Code generation
  • Test/benchmark generation
  • Research and understanding

Benchmarks

MacBook Pro M2, conda-forge OpenBLAS 0.3.34 with OpenMP threading (llvm-openmp), 8 threads, best of 3, two runs each, in ABBA order. "3.7 + this PR" is main with threadpoolctl 3.7.0 and ArgKmin patched to use "sequential_blas_under_openmp".

threadpoolctl 3.6.0 threadpoolctl 3.7.0 3.7.0 + this PR
kneighbors, euclidean, 50k x 32, k=5 1463 / 1484 ms 7095 / 7190 ms 1500 / 1522 ms
kneighbors, manhattan, 5k queries 675 / 676 ms 2784 / 2783 ms 687 / 692 ms
pairwise_distances_argmin, 50k vs 1k 34 / 31 ms 140 / 139 ms 31 / 32 ms

With OpenBLAS's OpenMP threading layer, threadpoolctl 3.7 limits BLAS
with omp_set_num_threads, which also limits the OpenMP code of the
calling thread. ArgKmin reads its number of threads inside its BLAS
limit, so it ran on a single thread.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added size/M and removed size/S labels Oct 20, 2026
@cakedev0
cakedev0 marked this pull request as ready for review October 20, 2026 06:48
@cakedev0

Copy link
Copy Markdown
Member Author

cc @itamarst @ogrisel

@ogrisel

ogrisel commented Oct 20, 2026

Copy link
Copy Markdown
Member

Thanks for the fix!

@ogrisel

ogrisel commented Oct 20, 2026

Copy link
Copy Markdown
Member

Also cc @jeremiedbb. I remember we implemented this option together in threadpoolctl but we apparently forgot to use it where appropriate in scikit-learn once we could.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

threadpool_limits(1, user_api="blas") also limits OpenMP to 1 thread with OpenBLAS's OpenMP threading layer (3.7.0)

3 participants