Skip to content

Commit 825ff42

Browse files
committed
Add EXECUTORCH_THREADPOOL_SIZE option and heuristic
1 parent 9ffcf4b commit 825ff42

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

extension/threadpool/targets.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def define_common_targets():
2424
deps = [
2525
"//executorch/runtime/core:core",
2626
"//executorch/runtime/core/portable_type/c10/c10:c10",
27+
":cpuinfo_utils",
2728
],
2829
exported_headers = _THREADPOOL_HEADERS,
2930
exported_deps = [
@@ -34,7 +35,10 @@ def define_common_targets():
3435
],
3536
exported_preprocessor_flags = [
3637
"-DET_USE_THREADPOOL",
37-
],
38+
] + (
39+
# Default to perf heuristic (0) in OSS, all cores (-1) otherwise.
40+
["-DEXECUTORCH_THREADPOOL_SIZE=0"] if runtime.is_oss else ["-DEXECUTORCH_THREADPOOL_SIZE=-1"]
41+
),
3842
visibility = [
3943
"//executorch/...",
4044
"//executorch/backends/...",

extension/threadpool/threadpool.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
#include <executorch/extension/threadpool/cpuinfo_utils.h>
910
#include <executorch/extension/threadpool/threadpool.h>
1011

1112
#include <algorithm>
@@ -101,7 +102,22 @@ ThreadPool* get_threadpool() {
101102
return nullptr; // NOLINT(facebook-hte-NullableReturn)
102103
}
103104

105+
// Choose the number of threads according to the EXECUTORCH_THREADPOOL_SIZE
106+
// value. See the description in threadpool.h.
107+
108+
#if defined(EXECUTORCH_THREADPOOL_SIZE) && ((EXECUTORCH_THREADPOOL_SIZE) > 0)
109+
// Use an explicit threadpool size.
110+
int num_threads = EXECUTORCH_THREADPOOL_SIZE;
111+
#elif defined(EXECUTORCH_THREADPOOL_SIZE) && \
112+
((EXECUTORCH_THREADPOOL_SIZE) == -1)
113+
// Use threads=cores.
104114
int num_threads = cpuinfo_get_processors_count();
115+
#else
116+
// Use a performance heuristic.
117+
int num_threads =
118+
::executorch::extension::cpuinfo::get_num_performant_cores();
119+
#endif
120+
105121
/*
106122
* For llvm-tsan, holding limit for the number of locks for a single thread
107123
* is 63 (because of comparison < 64 instead of <=). pthreadpool's worst

extension/threadpool/threadpool.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@
1414

1515
#include <pthreadpool.h>
1616

17+
/*
18+
* Threadpool Options:
19+
*
20+
* Threadpool size has a sizble affect on performance. The following
21+
* options are exposed to control the threadpool size.
22+
*
23+
* EXECUTORCH_THREADPOOL_SIZE: int - Set the size of the threadpool,
24+
* in number of threads.
25+
*
26+
* Special Values:
27+
* - 0: Use a perforance heuristic to determine the default size,
28+
* based on the active hardware. This is the default mode
29+
* for CMake.
30+
* - -1: Set the thread count equal to the number of cores on the
31+
* active hardware.
32+
*
33+
* Any other positive value will be interpreted as a thread count.
34+
* For example, setting EXECUTORCH_THREADPOOL_SIZE=4 will default
35+
* the threadpool to use 4 threads.
36+
*/
37+
38+
#ifndef EXECUTORCH_THREADPOOL_SIZE
39+
// Default to using a runtime heuristic.
40+
#define EXECUTORCH_THREADPOOL_SIZE 0
41+
#endif
42+
1743
namespace executorch::extension::threadpool {
1844

1945
class ThreadPool final {

0 commit comments

Comments
 (0)