Skip to content

Commit 42e3489

Browse files
Expose thread count through the parallel interface (pytorch#22908)
## Summary Expose the active threadpool size through the self-contained `thread_parallel_interface.h` API. This lets installed-wheel consumers such as TorchAO use ExecuTorch parallelism without including `extension/threadpool/threadpool.h`, which is intentionally not shipped because it exposes the unvendored pthreadpool dependency. The non-threaded implementation reports one available thread. The installed SDK link probe now exercises the new symbol. ## Test plan - `buck2 test fbsource//xplat/executorch/extension/threadpool/test:thread_parallel_test` - Built the TorchAO ExecuTorch operator targets against the new interface - `git diff --check` - Python syntax check for the wheel SDK test AI-assisted: Authored with Codex.
1 parent a36a252 commit 42e3489

4 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎.ci/scripts/wheel/test_cpp_sdk.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,8 @@ def test_shipped_headers_have_implementations(work_dir: Path) -> None:
10981098
"#define ET_USE_THREADPOOL\n"
10991099
"#include <executorch/runtime/kernel/thread_parallel_interface.h>\n"
11001100
"using namespace executorch::extension;\n"
1101-
"int main() { return parallel_for(0, 1, 1, [](int64_t, int64_t) {}) ? 0 : 1; }\n"
1101+
"int main() { return get_thread_count() > 0 && "
1102+
"parallel_for(0, 1, 1, [](int64_t, int64_t) {}) ? 0 : 1; }\n"
11021103
),
11031104
}
11041105

‎extension/threadpool/test/thread_parallel_test.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <executorch/runtime/platform/platform.h>
1616

1717
using namespace ::testing;
18+
using ::executorch::extension::get_thread_count;
1819
using ::executorch::extension::parallel_for;
1920

2021
class ParallelTest : public ::testing::TestWithParam<bool> {
@@ -230,6 +231,10 @@ TEST_P(ParallelTest, TestChunkSizeTooLarge) {
230231
}
231232
}
232233

234+
TEST(ThreadParallelInterfaceTest, GetThreadCount) {
235+
EXPECT_GT(get_thread_count(), 0);
236+
}
237+
233238
INSTANTIATE_TEST_SUITE_P(
234239
ParallelTestWithOrWithoutThreadpool,
235240
ParallelTest,

‎extension/threadpool/thread_parallel.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,11 @@ bool parallel_for(
8888
return true;
8989
}
9090

91+
int64_t get_thread_count() {
92+
return static_cast<int64_t>(
93+
::executorch::extension::threadpool::get_threadpool()
94+
->get_thread_count());
95+
}
96+
9197
} // namespace extension
9298
} // namespace executorch

‎runtime/kernel/thread_parallel_interface.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ bool parallel_for(
7979
const int64_t grain_size,
8080
runtime::FunctionRef<void(int64_t, int64_t)> f);
8181

82+
/** Returns the number of threads available to parallel_for. */
83+
int64_t get_thread_count();
84+
8285
int64_t get_thread_num();
8386

8487
void set_thread_num(int64_t thread_num);
@@ -96,6 +99,10 @@ inline int64_t get_thread_num() {
9699
return 0;
97100
}
98101

102+
inline int64_t get_thread_count() {
103+
return 1;
104+
}
105+
99106
inline void set_thread_num(int64_t thread_num) {
100107
ET_DCHECK_MSG(false, "cannot set_thread_num without threading support!");
101108
}

0 commit comments

Comments
 (0)