Skip to content

Add parallel dispatch helper for CPU encoder#3805

Closed
varshneydevansh wants to merge 1 commit into
ml-explore:mainfrom
varshneydevansh:multi_thread_cpu_kernel
Closed

Add parallel dispatch helper for CPU encoder#3805
varshneydevansh wants to merge 1 commit into
ml-explore:mainfrom
varshneydevansh:multi_thread_cpu_kernel

Conversation

@varshneydevansh

Copy link
Copy Markdown
Contributor

Proposed changes

  • Adds a CommandEncoder::dispatch_parallel helper for CPU backend work.
    The helper schedules one ordered stream task, splits a [0, n) range into
    chunks, runs those chunks on a shared CPU thread pool, and waits for the
    futures before the stream task completes.

  • This gives custom CPU primitive authors a built-in way to parallelize large
    kernel loops without managing their own thread pool.

  • Fixes How to implement custom multi-thread cpu kernels? #2185

Tests: added TEST_CASE("test cpu dispatch parallel") in tests/scheduler_tests.cpp

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

Comment thread mlx/backend/cpu/encoder.h
}

template <class F>
void dispatch_parallel(size_t n, F&& f, size_t grain_size = 32768) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default grain_size is 32768 is a conservative default that can be overridden :)

size_t thread_pool_size() {
static size_t size = [] {
auto n = std::thread::hardware_concurrency();
return n == 0 ? 4 : n;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept here 4 as I found this in couple places -

mlx/mlx/io/load.cpp

Lines 337 to 347 in de7b4ed

ThreadPool& thread_pool() {
// Leak - see Scheduler singleton comment in scheduler.cpp.
static ThreadPool* pool_ = new ThreadPool{4};
return *pool_;
}
ThreadPool& ParallelFileReader::thread_pool() {
// Leak - see Scheduler singleton comment in scheduler.cpp.
static ThreadPool* thread_pool = new ThreadPool{4};
return *thread_pool;
}

and in this test file too int num_threads = 4;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a fallback choice.

@varshneydevansh

Copy link
Copy Markdown
Contributor Author

from my terminal -

devanshvarshney ❯❯ DEVICE=cpu ./build-cpu/tests/tests --test-case="test cpu dispatch parallel"
[doctest] doctest version is "2.4.12"
[doctest] run with "--help" for options
===============================================================================
[doctest] test cases:    1 |    1 passed | 0 failed | 243 skipped
[doctest] assertions: 1025 | 1025 passed | 0 failed |
[doctest] Status: SUCCESS!

devanshvarshney ❯❯ DEVICE=cpu ./build-cpu/tests/tests
[doctest] doctest version is "2.4.12"
[doctest] run with "--help" for options
===============================================================================
[doctest] test cases:  244 |  244 passed | 0 failed | 0 skipped
[doctest] assertions: 4253 | 4253 passed | 0 failed |
[doctest] Status: SUCCESS!

@zcbenz

zcbenz commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR but we plan to merge the thread pool implementation in #3019, it is something that we wound't nave a good idea what the interface should look like without real world uses, so we don't want to add the API without actual multi-threaded cpu kernels, but still thanks anyway.

@zcbenz zcbenz closed this Jul 6, 2026
@varshneydevansh

Copy link
Copy Markdown
Contributor Author

Gotcha.

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.

How to implement custom multi-thread cpu kernels?

2 participants