Add parallel dispatch helper for CPU encoder#3805
Closed
varshneydevansh wants to merge 1 commit into
Closed
Conversation
varshneydevansh
commented
Jul 5, 2026
| } | ||
|
|
||
| template <class F> | ||
| void dispatch_parallel(size_t n, F&& f, size_t grain_size = 32768) { |
Contributor
Author
There was a problem hiding this comment.
The default grain_size is 32768 is a conservative default that can be overridden :)
varshneydevansh
commented
Jul 5, 2026
| size_t thread_pool_size() { | ||
| static size_t size = [] { | ||
| auto n = std::thread::hardware_concurrency(); | ||
| return n == 0 ? 4 : n; |
Contributor
Author
There was a problem hiding this comment.
I kept here 4 as I found this in couple places -
Lines 337 to 347 in de7b4ed
and in this test file too int num_threads = 4;
Contributor
Author
There was a problem hiding this comment.
This is just a fallback choice.
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!
|
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. |
Contributor
Author
|
Gotcha. |
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.
Proposed changes
Adds a
CommandEncoder::dispatch_parallelhelper for CPU backend work.The helper schedules one ordered stream task, splits a
[0, n)range intochunks, 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")intests/scheduler_tests.cppChecklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes