tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
threading_backend.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 #ifndef TVM_RUNTIME_THREADING_BACKEND_H_
25 #define TVM_RUNTIME_THREADING_BACKEND_H_
26 
27 #include <functional>
28 #include <memory>
29 #include <vector>
30 
31 #if defined(__linux__) || defined(__ANDROID__)
32 #if defined(__ANDROID__)
33 #ifndef CPU_SET
34 #define CPU_SETSIZE 1024
35 #define __NCPUBITS (8 * sizeof(uint64_t))
36 typedef struct {
37  uint64_t __bits[CPU_SETSIZE / __NCPUBITS];
38 } cpu_set_t;
39 
40 #define CPU_SET(cpu, cpusetp) \
41  ((cpusetp)->__bits[(cpu) / __NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))
42 #define CPU_ZERO(cpusetp) memset((cpusetp), 0, sizeof(cpu_set_t))
43 #define CPU_ISSET(cpu, cpusetp) \
44  (1UL << ((cpu) % __NCPUBITS)) == \
45  ((cpusetp)->__bits[(cpu) / __NCPUBITS] & (1UL << ((cpu) % __NCPUBITS)))
46 #define CPU_EQUAL(left, right) (memcmp(&left, &right, sizeof(cpu_set_t)) == 0)
47 
48 #endif
49 #endif
50 #endif
51 
52 namespace tvm {
53 namespace runtime {
54 namespace threading {
55 
60 class ThreadGroup {
61  public:
62  class Impl;
63 
76  ThreadGroup(int num_workers, std::function<void(int)> worker_callback,
77  bool exclude_worker0 = false);
79 
83  void Join();
84 
85  enum AffinityMode : int {
86  kBig = 1,
87  kLittle = -1,
88  /*Different threads will get different affinities.*/
90  /*All threads will get the same core group affinity.*/
92  };
106  int Configure(AffinityMode mode, int nthreads, bool exclude_worker0,
107  std::vector<unsigned int> cpus = {});
108 
109  private:
110  Impl* impl_;
111 };
112 
116 void Yield();
124 void SetMaxConcurrency(int value);
132 
141  std::vector<unsigned int> cpus);
142 
147 int32_t NumThreads();
148 
149 } // namespace threading
150 } // namespace runtime
151 } // namespace tvm
152 
153 #endif // TVM_RUNTIME_THREADING_BACKEND_H_
A platform-agnostic abstraction for managing a collection of thread pool threads.
Definition: threading_backend.h:60
void Join()
Blocks until all non-main threads in the pool finish.
AffinityMode
Definition: threading_backend.h:85
@ kBig
Definition: threading_backend.h:86
@ kLittle
Definition: threading_backend.h:87
@ kSpecifyOneCorePerThread
Definition: threading_backend.h:89
@ kSpecifyThreadShareAllCore
Definition: threading_backend.h:91
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.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36