6
6
* LICENSE file in the root directory of this source tree.
7
7
*/
8
8
9
+ #include < executorch/extension/threadpool/cpuinfo_utils.h>
9
10
#include < executorch/extension/threadpool/threadpool.h>
10
11
11
12
#include < algorithm>
12
13
#include < memory>
13
14
14
15
#include < executorch/extension/threadpool/threadpool_guard.h>
15
16
#include < executorch/runtime/platform/assert.h>
17
+ #include < executorch/runtime/platform/runtime.h>
16
18
17
19
#include < cpuinfo.h>
18
20
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
+
19
37
namespace executorch ::extension::threadpool {
20
38
21
39
#if !(defined(WIN32))
@@ -95,13 +113,22 @@ void ThreadPool::run(
95
113
// get_threadpool is not thread safe due to leak_corrupted_threadpool
96
114
// Make this part threadsafe: TODO(kimishpatel)
97
115
ThreadPool* get_threadpool () {
116
+ executorch::runtime::runtime_init ();
117
+
98
118
if (!cpuinfo_initialize ()) {
99
119
ET_LOG (Error, " cpuinfo initialization failed" );
100
120
return nullptr ; // NOLINT(facebook-hte-NullableReturn)
101
121
}
102
122
103
123
static const int num_threads = ([]() {
104
- int result = cpuinfo_get_processors_count ();
124
+ #if defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES)
125
+ // Use threads=cores.
126
+ auto result = cpuinfo_get_processors_count ();
127
+ #else
128
+ // Set threads equal to the number of performance cores.
129
+ auto result =
130
+ ::executorch::extension::cpuinfo::get_num_performant_cores ();
131
+ #endif
105
132
106
133
/*
107
134
* For llvm-tsan, holding limit for the number of locks for a single thread
@@ -114,6 +141,7 @@ ThreadPool* get_threadpool() {
114
141
constexpr int tsan_thread_limit = 63 ;
115
142
return std::min (result, tsan_thread_limit);
116
143
})();
144
+
117
145
static auto threadpool = std::make_unique<ThreadPool>(num_threads);
118
146
119
147
// Inheriting from old threadpool to get around segfault issue
0 commit comments