|
15 | 15 |
|
16 | 16 | #include <executorch/extension/threadpool/threadpool_guard.h>
|
17 | 17 | #include <executorch/runtime/platform/assert.h>
|
| 18 | +#include <executorch/runtime/platform/runtime.h> |
18 | 19 |
|
19 | 20 | #include <cpuinfo.h>
|
20 | 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 | + |
21 | 38 | namespace executorch::extension::threadpool {
|
22 | 39 |
|
23 | 40 | #if !(defined(WIN32))
|
@@ -97,24 +114,22 @@ void ThreadPool::run(
|
97 | 114 | // get_threadpool is not thread safe due to leak_corrupted_threadpool
|
98 | 115 | // Make this part threadsafe: TODO(kimishpatel)
|
99 | 116 | ThreadPool* get_threadpool() {
|
| 117 | + executorch::runtime::runtime_init(); |
| 118 | + |
100 | 119 | if (!cpuinfo_initialize()) {
|
101 | 120 | ET_LOG(Error, "cpuinfo initialization failed");
|
102 | 121 | return nullptr; // NOLINT(facebook-hte-NullableReturn)
|
103 | 122 | }
|
104 | 123 |
|
105 |
| - // Choose the number of threads according to the EXECUTORCH_THREADPOOL_SIZE |
106 |
| - // value. See the description in threadpool.h. |
| 124 | + // Choose the number of threads according to the EXECUTORCH_THREADPOOL_ |
| 125 | + // options. See the description in threadpool.h. |
107 | 126 |
|
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) |
| 127 | +#if defined(EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES) |
113 | 128 | // Use threads=cores.
|
114 |
| - int num_threads = cpuinfo_get_processors_count(); |
| 129 | + static int num_threads = cpuinfo_get_processors_count(); |
115 | 130 | #else
|
116 |
| - // Use a performance heuristic. |
117 |
| - int num_threads = |
| 131 | + // Set threads equal to the number of performance cores. |
| 132 | + static int num_threads = |
118 | 133 | ::executorch::extension::cpuinfo::get_num_performant_cores();
|
119 | 134 | #endif
|
120 | 135 |
|
|
0 commit comments