Repository navigation
Conversation
This was referenced Oct 8, 2026
The thread local buffers are now added to the result after the parallel region instead of under a lock inside it, and the chunks are passed as indices instead of memoryview slices (which made Cython acquire the GIL in every thread at the start and end of the parallel region). Both are serial costs that grow with the number of threads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each chunk does a BLAS call from an OpenMP thread, and many concurrent BLAS calls scale badly with the number of threads (OpenBLAS takes a global lock in each call). With 32 threads or more, the chunks are now bigger, as long as the per-thread buffer of pairwise distances stays cache friendly and there is at least one chunk per thread. Co-Authored-By: Claude Opus 5.5 <[email protected]>
cakedev0
force-pushed
the
kmeans/lloyd_chunk_size
branch
from
October 10, 2026 10:21
4c3da59 to
c600ae5
Compare
Owner
Author
|
@cakedev0 note the MKL is not available for ARM based machines such as macOS laptops that can only use OpenBLAS (or macOS' own BLAS implementation named Accelerate). So OpenBLAS or Accelerate specific optimizations might still be interesting. |
Owner
Author
|
Noted, but I think the improvement/fix belongs more to OpenBlas than to scikit-learn. This is bad:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
Follow-up of #26: this PR is on top of it, only the last commit is new.
What does this implement/fix? Explain your changes.
lloyd_iter_chunked_densedoes one BLAS call (X_chunk @ centers.T) per chunk of 256 samples, from the OpenMP threads (BLAS limited to 1 thread). Many concurrent single-threaded BLAS calls scale badly: OpenBLAS takes a global mutex twice per call (at least in the 0.3.31 to 0.3.34 builds of the wheels, see also OpenMathLib/OpenBLAS#1782), so with 64 threads a lot of time goes into contention.This PR uses bigger chunks when there are many threads:
256 * max(1, n_threads // 16)samples, i.e. unchanged up to 31 threads and 1024 with 64 threads. It is still capped:2**18 // n_clusters, so that the per-thread buffer of pairwise distances of shape(chunk_size, n_clusters)stays cache friendly,ceil(n_samples / n_threads)(and at least 256), so that there is at least one chunk per thread.Bigger chunks for any number of threads didn't work: they were 10-30% slower with 14-16 threads on medium datasets, on a hybrid laptop CPU and on the Xeon pinned to 14 cores (fewer chunks per thread, so the work is less balanced).
The new test checks one dense Lloyd iteration against the sparse one with several chunks and a remainder, with 2 and 64 threads (256 and 1024 samples per chunk). The existing tests mostly use 100 samples, i.e. a single chunk.
AI usage disclosure
I used AI assistance for:
Benchmarks
Time of a Lloyd iteration (ms/it,
_kmeans_single_lloydwith the same initial centers, best of 2-3 runs), main -> #26 -> this PR. Intel Xeon 6787P (2 x 86 cores), OpenBLAS 0.3.33 (pthreads) from the numpy/scipy wheels.TLDR: with 64 threads, another 2.2-3.7x on top of #26 on 5 of the 8 scikit-learn-benchmarks datasets (1.1-4.3x faster than main overall). Unchanged with 31 threads or less.
Datasets from scikit-learn-benchmarks, 64 threads:
Synthetic data (
randn), 64 threads and 14 threads pinned to 14 cores:An earlier version with chunks of up to 4096 samples was faster on sift 1M with k=10 (5.8 ms/it with 64 threads), but slower with 14-16 threads. I haven't tried bigger factors than
n_threads // 16with this version.I'll add end-to-end results from scikit-learn-benchmarks.
🤖 Generated with Claude Code