Skip to content

PRF Use bigger chunks of samples in dense Lloyd with many threads - #24

Closed
cakedev0 wants to merge 2 commits into
mainfrom
kmeans/lloyd_chunk_size
Closed

cakedev0 wants to merge 2 commits into
mainfrom
kmeans/lloyd_chunk_size

Conversation

@cakedev0

@cakedev0 cakedev0 commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner

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_dense does 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:

  • by 2**18 // n_clusters, so that the per-thread buffer of pairwise distances of shape (chunk_size, n_clusters) stays cache friendly,
  • by 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:

  • Code generation
  • Test/benchmark generation
  • Research and understanding (profiling)

Benchmarks

Time of a Lloyd iteration (ms/it, _kmeans_single_lloyd with 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:

dataset (n x d), k main #26 this PR
sift (1M x 128), 10 44.9 41.3 11.2
sift (1M x 128), 100 44.1 42.7 12.8
sift (100k x 128), 1000 19.4 13.0 13.0
fashion_mnist (60k x 784), 100 12.8 6.4 5.1
nytimes (290k x 256), 10 14.5 11.4 3.3
nytimes (290k x 256), 100 15.2 14.3 6.5
road_network (348k x 3), 10 13.7 11.5 4.0
road_network (348k x 3), 1000 12.5 12.2 11.4

Synthetic data (randn), 64 threads and 14 threads pinned to 14 cores:

n x d, k 64 threads 14 threads
5000 x 50, 8 0.97 -> 0.75 -> 0.69 0.29 -> 0.17 -> 0.17
20000 x 10, 8 10.5 -> 1.2 -> 1.2 0.47 -> 0.38 -> 0.36
20000 x 50, 100 7.0 -> 2.3 -> 2.3 1.04 -> 1.07 -> 1.07
100000 x 50, 10 11.2 -> 5.4 -> 2.8 1.90 -> 1.71 -> 1.73
200000 x 128, 100 (float32) 16.4 -> 12.8 -> 7.9 9.2 -> 9.1 -> 9.1
50000 x 784, 100 (float32) 17.2 -> 10.7 -> 9.1 10.1 -> 10.2 -> 10.3

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 // 16 with this version.

I'll add end-to-end results from scikit-learn-benchmarks.

🤖 Generated with Claude Code

cakedev0 and others added 2 commits October 10, 2026 12:12
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
cakedev0 force-pushed the kmeans/lloyd_chunk_size branch from 4c3da59 to c600ae5 Compare October 10, 2026 10:21
@cakedev0 cakedev0 changed the title PRF Use bigger chunks of samples in dense Lloyd iterations PRF Use bigger chunks of samples in dense Lloyd with many threads Oct 10, 2026
@cakedev0

Copy link
Copy Markdown
Owner Author

Dropped: with MKL this doesn't add much on top of #26 (within ±15% of #26 alone on the GNR at 64 threads), the gain is specific to OpenBLAS's lock in each call. Users who need the extra performance with many threads can use MKL instead. The branch is kept for reference.

@cakedev0 cakedev0 closed this Oct 10, 2026
@ogrisel

ogrisel commented Oct 12, 2026

Copy link
Copy Markdown

@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.

@cakedev0

Copy link
Copy Markdown
Owner Author

Noted, but I think the improvement/fix belongs more to OpenBlas than to scikit-learn.

This is bad:

Many concurrent single-threaded BLAS calls scale badly: OpenBLAS takes a global mutex twice per call

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants