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>
1213#include < memory>
1314
1415#include < executorch/extension/threadpool/threadpool_guard.h>
1516#include < executorch/runtime/platform/assert.h>
17+ #include < executorch/runtime/platform/runtime.h>
1618
1719#include < cpuinfo.h>
1820
21+ // At most one mode should be set.
22+ #if ( \
23+ defined (EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES) && \
24+ defined(EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES))
25+ #error Multiple \
26+ threadpool size specifiers are set.At most one of \
27+ EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES, \
28+ and EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES may be defined .
29+ #endif
30+
31+ // Default to EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES if no mode is set.
32+ #if !defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES) && \
33+ !defined (EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES)
34+ #define EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES 1
35+ #endif
36+
1937namespace executorch ::extension::threadpool {
2038
2139#if !(defined(WIN32))
@@ -97,13 +115,22 @@ void ThreadPool::run(
97115// get_threadpool is not thread safe due to leak_corrupted_threadpool
98116// Make this part threadsafe: TODO(kimishpatel)
99117ThreadPool* get_threadpool () {
118+ executorch::runtime::runtime_init ();
119+
100120 if (!cpuinfo_initialize ()) {
101121 ET_LOG (Error, " cpuinfo initialization failed" );
102122 return nullptr ; // NOLINT(facebook-hte-NullableReturn)
103123 }
104124
105125 static const int num_threads = ([]() {
106- int result = cpuinfo_get_processors_count ();
126+ #if defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES)
127+ // Use threads=cores.
128+ auto result = cpuinfo_get_processors_count ();
129+ #else
130+ // Set threads equal to the number of performance cores.
131+ auto result =
132+ ::executorch::extension::cpuinfo::get_num_performant_cores ();
133+ #endif
107134
108135 /*
109136 * For llvm-tsan, holding limit for the number of locks for a single thread
@@ -116,6 +143,7 @@ ThreadPool* get_threadpool() {
116143 constexpr int tsan_thread_limit = 63 ;
117144 return std::min (result, tsan_thread_limit);
118145 })();
146+
119147 static auto threadpool = std::make_unique<ThreadPool>(num_threads);
120148
121149// Inheriting from old threadpool to get around segfault issue
0 commit comments