Repository navigation
Expand file tree
/
Copy paththreadpool.h
More file actions
167 lines (147 loc) · 6.96 KB
/
Copy paththreadpool.h
File metadata and controls
167 lines (147 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <functional>
#include <memory>
#include <mutex>
#include <executorch/runtime/core/function_ref.h>
// The pip package does not ship pthreadpool.h, so the pool type is declared
// here. Where the header is available, it is still included, because existing
// code calls pthreadpool functions through this header.
struct pthreadpool;
typedef struct pthreadpool* pthreadpool_t;
#if __has_include(<pthreadpool.h>)
#include <pthreadpool.h>
#endif
/*
* Threadpool Options:
*
* Threadpool size has a sizble affect on performance. By default, the
* threadpool will be sized according to the number of performance cores. This
* behavior can be overriden with the following build-time options. Note that
* these options are mutually exclusive.
*
* - EXECUTORCH_THREADPOOL_USE_PERFORMANCE_CORES (flag) - Sizes the threadpool
* equal to the number of performance cores on the system. This is the default
* behavior.
* - EXECUTORCH_THREADPOOL_USE_ALL_LOGICAL_CORES (flag) - Sizes the threadpool
* equal to the number of logical cores on system. This is the historical
* behavior.
*
* On Linux other than Android, the threadpool starts with no more threads than
* the CPUs in the affinity mask of the thread that first calls
* get_threadpool(). taskset and cgroup cpusets shrink that mask, CPU quotas do
* not. The mask also sizes the threadpool when the performance core count is
* unknown, as on x86. It is read once: the threadpool does not follow later
* changes to the mask, and a child created by fork() keeps its parent's size.
* If the mask can grow later, set the thread count you need with
* _unsafe_reset_threadpool() before loading any model.
*/
namespace executorch::extension::threadpool {
class ThreadPool final {
public:
explicit ThreadPool(size_t thread_count = 0);
~ThreadPool() = default;
// Make threadpool non copyable
// Non-copyable: threadpool cannot be copied because it will
// effectively require cloning of threadpool.
// Cloning can be done by just calling create_thread_pool.
ThreadPool(const ThreadPool&) = delete;
ThreadPool& operator=(const ThreadPool&) = delete;
// Make threadpool non-movable.
ThreadPool(ThreadPool&&) = delete;
ThreadPool& operator=(ThreadPool&&) = delete;
/**
* Returns the number of threads in the threadpool. This number does not
* reflect temporary lowering via UseNThreadsThreadPoolGuard.
*/
size_t get_thread_count() const;
/**
* INTERNAL: Resets the threadpool by creating a new threadpool with requested
* # of threads. This is not a thread safe call. When calling this method,
* threads of the threadpool might be doing some work. A pointer returned by
* get_pthreadpool() is no longer valid, while one returned by
* get_shared_pthreadpool() keeps the old threadpool alive until it is
* released. So the XNNPACK parts of a method loaded before this call keep
* the old thread count until the method is destroyed, and its CpuBackend
* parts fail to run. Resetting while a CpuBackend part is loading or
* running is not safe. This method is a private API, which will later be
* replaced by something that allows creating of threadpool with requested
* size and use such a threadpool with backend delegates, custom ops or
* optimized lib.
* For Meta internal use, there is
* executorch::extension::threadpool::UseNThreadsThreadPoolGuard API that
* provides a safer way to select a subset of threads, from threadpool, to run
* the model on.
*/
[[deprecated(
"This API is experimental and may change without notice. Consider using UseNThreadsThreadPoolGuard")]]
bool _unsafe_reset_threadpool(uint32_t num_threads);
/**
* INTERNAL: Destroys the threadpool. This is not a thread safe call. When
* calling this method, threads of the threadpool might be doing some work.
* As with _unsafe_reset_threadpool(), a pointer returned by get_pthreadpool()
* is no longer valid, while one returned by get_shared_pthreadpool() keeps
* the threadpool alive until it is released.
*/
[[deprecated("This API is experimental and may change without notice.")]]
void _unsafe_destroy_threadpool();
/**
* Run, in parallel, function fn(task_id) over task_id in range [0, range).
* This function is blocking. All input is processed by the time it returns.
* NoThreadPoolGuard (see threadpool_guard.h) can used to disable use of
* multiple threads with the scope of the guard When NoThreadPoolGuard is not
* used all calls to run method are serialized.
*/
void run(runtime::FunctionRef<void(size_t)> fn, size_t range);
private:
friend pthreadpool_t get_pthreadpool();
friend std::shared_ptr<pthreadpool> get_shared_pthreadpool();
private:
// Guards threadpool_ and thread_count_. run() holds it for the whole
// parallel call, so that a reset or destroy cannot free the pool mid-run.
mutable std::mutex mutex_;
// Held with mutex_ to change threadpool_, and alone to read it in the
// getters, so that they do not wait for run().
std::mutex threadpool_mutex_;
std::shared_ptr<pthreadpool> threadpool_;
// The number of threads in the threadpool.
size_t thread_count_;
};
/**
* Returns the singleton instance of ThreadPool for ATen/TH multithreading.
*/
ThreadPool* get_threadpool();
/**
* Returns the underlying pthreadpool instance used by the implementation of
* ThreadPool returned by `get_threadpool()`. Only for use in external libraries
* so as to unify threading across internal (i.e. ATen, etc.) and external (e.g.
* NNPACK, QNNPACK, XNNPACK) use cases. The pointer is no longer valid after the
* next ThreadPool::_unsafe_reset_threadpool() or
* ThreadPool::_unsafe_destroy_threadpool(), so a caller that keeps the instance
* should use get_shared_pthreadpool().
*/
pthreadpool_t get_pthreadpool();
/**
* Same as get_pthreadpool(), but the caller shares ownership of the instance,
* so it stays valid while the returned pointer is held, even after
* ThreadPool::_unsafe_reset_threadpool() or
* ThreadPool::_unsafe_destroy_threadpool(). For external libraries that keep
* the instance, such as an XNNPACK runtime. Safe to call while another thread
* resets or destroys the threadpool.
*/
std::shared_ptr<pthreadpool> get_shared_pthreadpool();
} // namespace executorch::extension::threadpool
namespace torch::executorch::threadpool { // DEPRECATED
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces. Note that threadpool incorrectly used
// the namespace `torch::executorch` instead of `torch::executor`.
using ::executorch::extension::threadpool::get_pthreadpool; // DEPRECATED
using ::executorch::extension::threadpool::get_threadpool; // DEPRECATED
using ::executorch::extension::threadpool::ThreadPool; // DEPRECATED
} // namespace torch::executorch::threadpool