|
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>
|
|
14 | 15 |
|
15 | 16 | #include <executorch/extension/threadpool/threadpool_guard.h>
|
16 | 17 | #include <executorch/runtime/platform/assert.h>
|
| 18 | +#include <executorch/runtime/platform/runtime.h> |
17 | 19 |
|
18 | 20 | #include <cpuinfo.h>
|
19 | 21 |
|
| 22 | +// At most one mode should be set. |
| 23 | +#if ( \ |
| 24 | + defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES) && \ |
| 25 | + defined(EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES)) |
| 26 | +#error Multiple \ |
| 27 | + threadpool size specifiers are set.At most one of \ |
| 28 | + EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES, \ |
| 29 | + and EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES may be defined. |
| 30 | +#endif |
| 31 | + |
| 32 | +// Default to EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES if no mode is set. |
| 33 | +#if !defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES) && \ |
| 34 | + !defined(EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES) |
| 35 | +#define EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES 1 |
| 36 | +#endif |
| 37 | + |
20 | 38 | namespace executorch::extension::threadpool {
|
21 | 39 |
|
22 | 40 | #if !(defined(WIN32))
|
@@ -96,12 +114,25 @@ void ThreadPool::run(
|
96 | 114 | // get_threadpool is not thread safe due to leak_corrupted_threadpool
|
97 | 115 | // Make this part threadsafe: TODO(kimishpatel)
|
98 | 116 | ThreadPool* get_threadpool() {
|
| 117 | + executorch::runtime::runtime_init(); |
| 118 | + |
99 | 119 | if (!cpuinfo_initialize()) {
|
100 | 120 | ET_LOG(Error, "cpuinfo initialization failed");
|
101 | 121 | return nullptr; // NOLINT(facebook-hte-NullableReturn)
|
102 | 122 | }
|
103 | 123 |
|
104 |
| - int num_threads = cpuinfo_get_processors_count(); |
| 124 | + // Choose the number of threads according to the EXECUTORCH_THREADPOOL_ |
| 125 | + // options. See the description in threadpool.h. |
| 126 | + |
| 127 | +#if defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES) |
| 128 | + // Use threads=cores. |
| 129 | + static int num_threads = cpuinfo_get_processors_count(); |
| 130 | +#else |
| 131 | + // Set threads equal to the number of performance cores. |
| 132 | + static int num_threads = |
| 133 | + ::executorch::extension::cpuinfo::get_num_performant_cores(); |
| 134 | +#endif |
| 135 | + |
105 | 136 | /*
|
106 | 137 | * For llvm-tsan, holding limit for the number of locks for a single thread
|
107 | 138 | * is 63 (because of comparison < 64 instead of <=). pthreadpool's worst
|
|
0 commit comments