24 #ifndef TVM_RUNTIME_THREADING_BACKEND_H_
25 #define TVM_RUNTIME_THREADING_BACKEND_H_
34 #if defined(__linux__) || defined(__ANDROID__)
35 #if defined(__ANDROID__)
37 #define CPU_SETSIZE 1024
38 #define __NCPUBITS (8 * sizeof(uint64_t))
40 uint64_t __bits[CPU_SETSIZE / __NCPUBITS];
43 #define CPU_SET(cpu, cpusetp) \
44 ((cpusetp)->__bits[(cpu) / __NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))
45 #define CPU_ZERO(cpusetp) memset((cpusetp), 0, sizeof(cpu_set_t))
46 #define CPU_ISSET(cpu, cpusetp) \
47 (1UL << ((cpu) % __NCPUBITS)) == \
48 ((cpusetp)->__bits[(cpu) / __NCPUBITS] & (1UL << ((cpu) % __NCPUBITS)))
49 #define CPU_EQUAL(left, right) (memcmp(&left, &right, sizeof(cpu_set_t)) == 0)
79 TVM_DLL
ThreadGroup(
int num_workers, std::function<
void(
int)> worker_callback,
80 bool exclude_worker0 =
false);
110 std::vector<unsigned int> cpus = {});
144 std::vector<unsigned int> cpus);
173 template <
typename T>
182 template <
typename T>
183 struct ParallelForWithThreadingBackendLambdaInvoker {
187 T* lambda_ptr =
static_cast<T*
>(cdata);
189 (*lambda_ptr)(task_id, num_task);
194 template <
typename T>
195 inline void parallel_launch_with_threading_backend(T flambda) {
197 void* cdata = &flambda;
204 template <
typename T>
206 if (end - begin == 1) {
211 auto flaunch = [begin, end, flambda](
int task_id,
int num_task) {
213 int64_t total_len = end - begin;
214 int64_t step = (total_len + num_task - 1) / num_task;
215 int64_t local_begin =
std::min(begin + step * task_id, end);
216 int64_t local_end =
std::min(local_begin + step, end);
217 for (int64_t i = local_begin; i < local_end; ++i) {
222 detail::parallel_launch_with_threading_backend(flaunch);
int TVMBackendParallelLaunch(FTVMParallelLambda flambda, void *cdata, int num_task)
Backend function for running parallel jobs.
A platform-agnostic abstraction for managing a collection of thread pool threads.
Definition: threading_backend.h:63
void Join()
Blocks until all non-main threads in the pool finish.
AffinityMode
Definition: threading_backend.h:88
@ kBig
Definition: threading_backend.h:89
@ kLittle
Definition: threading_backend.h:90
@ kSpecifyOneCorePerThread
Definition: threading_backend.h:92
@ kSpecifyThreadShareAllCore
Definition: threading_backend.h:94
int Configure(AffinityMode mode, int nthreads, bool exclude_worker0, std::vector< unsigned int > cpus={})
configure the CPU id affinity
ThreadGroup(int num_workers, std::function< void(int)> worker_callback, bool exclude_worker0=false)
Creates a collection of threads which run a provided function.
void Configure(tvm::runtime::threading::ThreadGroup::AffinityMode mode, int nthreads, std::vector< unsigned int > cpus)
Configuring the CPU affinity mode for the working threads.
int32_t NumThreads()
Get the number of threads being used by the TVM runtime.
void ResetThreadPool()
Reset the threads in the pool. All current threads are destroyed and new ones are created.
void Yield()
Platform-agnostic no-op.
void SetMaxConcurrency(int value)
Setting the maximum number of available cores.
void parallel_for_with_threading_backend(T flambda, int64_t begin, int64_t end)
Definition: threading_backend.h:205
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr min(PrimExpr a, PrimExpr b, Span span=Span())
take minimum of two values
Environment for TVM parallel task.
Definition: c_backend_api.h:119
int32_t num_task
total amount of task
Definition: c_backend_api.h:125