Skip to content

Commit 1ee7835

Browse files
committed
Update threadpool size options, add CMake options
1 parent 825ff42 commit 1ee7835

File tree

4 files changed

+97
-28
lines changed

4 files changed

+97
-28
lines changed

extension/threadpool/CMakeLists.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ if(NOT CMAKE_CXX_STANDARD)
2020
set(CMAKE_CXX_STANDARD 17)
2121
endif()
2222

23+
# Threadpool size specifiers. Mutual exclusion is checking in default.cmake.
24+
set(_threadpool_size_flag)
25+
if(EXECUTORCH_THREADPOOL_SIZE)
26+
set(_threadpool_size_flag
27+
"EXECUTORCH_THREADPOOL_SIZE=${EXECUTORCH_THREADPOOL_SIZE}"
28+
)
29+
elseif(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES)
30+
set(_threadpool_size_flag "EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES")
31+
elseif(EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES)
32+
set(_threadpool_size_flag "EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES")
33+
endif()
34+
2335
add_library(
2436
extension_threadpool threadpool.cpp threadpool_guard.cpp thread_parallel.cpp
2537
cpuinfo_utils.cpp
@@ -36,7 +48,9 @@ target_include_directories(
3648
$<BUILD_INTERFACE:${EXECUTORCH_ROOT}/backends/xnnpack/third-party/cpuinfo/include>
3749
$<BUILD_INTERFACE:${EXECUTORCH_ROOT}/backends/xnnpack/third-party/pthreadpool/include>
3850
)
39-
target_compile_definitions(extension_threadpool PUBLIC ET_USE_THREADPOOL)
51+
target_compile_definitions(
52+
extension_threadpool PUBLIC ET_USE_THREADPOOL ${threadpool_size_flag}
53+
)
4054
target_compile_options(extension_threadpool PUBLIC ${_common_compile_options})
4155

4256
# Install libraries

extension/threadpool/threadpool.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@
1818

1919
#include <cpuinfo.h>
2020

21+
// At most one mode should be set.
22+
#if ( \
23+
defined(EXECUTORCH_THREADPOOL_SIZE) && \
24+
defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES) || \
25+
defined(EXECUTORCH_THREADPOOL_SIZE) && \
26+
defined(EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES) || \
27+
defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES) && \
28+
defined(EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES))
29+
#error Multiple \
30+
threadpool size specifiers are set.At most one of \
31+
EXECUTORCH_THREADPOOL_SIZE, \
32+
EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES, \
33+
and EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES may be defined.
34+
#endif
35+
36+
// Default to EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES if no mode is set.
37+
#if !defined(EXECUTORCH_THREADPOOL_SIZE) && \
38+
!defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES) && \
39+
!defined(EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES)
40+
#define EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES 1
41+
#endif
42+
2143
namespace executorch::extension::threadpool {
2244

2345
#if !(defined(WIN32))
@@ -105,16 +127,15 @@ ThreadPool* get_threadpool() {
105127
// Choose the number of threads according to the EXECUTORCH_THREADPOOL_SIZE
106128
// value. See the description in threadpool.h.
107129

108-
#if defined(EXECUTORCH_THREADPOOL_SIZE) && ((EXECUTORCH_THREADPOOL_SIZE) > 0)
130+
#ifdef EXECUTORCH_THREADPOOL_SIZE
109131
// Use an explicit threadpool size.
110-
int num_threads = EXECUTORCH_THREADPOOL_SIZE;
111-
#elif defined(EXECUTORCH_THREADPOOL_SIZE) && \
112-
((EXECUTORCH_THREADPOOL_SIZE) == -1)
132+
static int num_threads = EXECUTORCH_THREADPOOL_SIZE;
133+
#elif defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES)
113134
// Use threads=cores.
114-
int num_threads = cpuinfo_get_processors_count();
135+
static int num_threads = cpuinfo_get_processors_count();
115136
#else
116-
// Use a performance heuristic.
117-
int num_threads =
137+
// Set threads equal to the number of performance cores.
138+
static int num_threads =
118139
::executorch::extension::cpuinfo::get_num_performant_cores();
119140
#endif
120141

extension/threadpool/threadpool.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,20 @@
1717
/*
1818
* Threadpool Options:
1919
*
20-
* Threadpool size has a sizble affect on performance. The following
21-
* options are exposed to control the threadpool size.
20+
* Threadpool size has a sizble affect on performance. By default, the
21+
* threadpool will be sized according to the number of performance cores. This
22+
* behavior can be overriden with the following build-time options. Note that
23+
* these options are mutually exclusive.
2224
*
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.
25+
* - EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES (flag) - Sizes the threadpool
26+
* equal to the number of performance cores on the system. This is the default
27+
* behavior.
28+
* - EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES (flag) - Sizes the threadpool
29+
* equal to the number of logical cores on system. This is the historical
30+
* behavior.
31+
* - EXECUTORCH_THREADPOOL_SIZE: int - An explicit number of threads to use.
3632
*/
3733

38-
#ifndef EXECUTORCH_THREADPOOL_SIZE
39-
// Default to using a runtime heuristic.
40-
#define EXECUTORCH_THREADPOOL_SIZE 0
41-
#endif
42-
4334
namespace executorch::extension::threadpool {
4435

4536
class ThreadPool final {

tools/cmake/preset/default.cmake

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,49 @@ define_overridable_option(
176176
${_default_executorch_build_cpuinfo}
177177
)
178178

179+
# Threadpool size options. At most one can be specified. Note that the default
180+
# is managed in threadpool.cpp to allow the user to specify an alternate mode
181+
# without needing to explicitly set the default to off.
182+
define_overridable_option(
183+
EXECUTORCH_THREADPOOL_SIZE
184+
"The number of threads to use for CPU parallel computation." STRING ""
185+
)
186+
define_overridable_option(
187+
EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES
188+
"Set the number of threads used for CPU parallel computation equal to the number of performant CPU cores."
189+
BOOL
190+
OFF
191+
)
192+
define_overridable_option(
193+
EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES
194+
"Set the number of threads used for CPU parallel computation equal to the number of logical CPU cores."
195+
BOOL
196+
OFF
197+
)
198+
199+
check_required_options_on(
200+
IF_ON EXECUTORCH_THREADPOOL_SIZE REQUIRES EXECUTORCH_BUILD_PTHREADPOOL
201+
EXECUTORCH_BUILD_CPUINFO
202+
)
203+
check_required_options_on(
204+
IF_ON EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES REQUIRES
205+
EXECUTORCH_BUILD_PTHREADPOOL EXECUTORCH_BUILD_CPUINFO
206+
)
207+
check_required_options_on(
208+
IF_ON EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES REQUIRES
209+
EXECUTORCH_BUILD_PTHREADPOOL EXECUTORCH_BUILD_CPUINFO
210+
)
211+
212+
check_conflicting_options_on(
213+
IF_ON EXECUTORCH_THREADPOOL_SIZE CONFLICTS_WITH
214+
EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES
215+
EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES
216+
)
217+
check_conflicting_options_on(
218+
IF_ON EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES CONFLICTS_WITH
219+
EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES
220+
)
221+
179222
# TODO(jathu): move this to platform specific presets when created
180223
set(_default_executorch_build_executor_runner ON)
181224
if(APPLE AND "${SDK_NAME}" STREQUAL "iphoneos")

0 commit comments

Comments
 (0)