treelite
thread_pool.h
Go to the documentation of this file.
1 
7 #ifndef TREELITE_THREAD_POOL_THREAD_POOL_H_
8 #define TREELITE_THREAD_POOL_THREAD_POOL_H_
9 
10 #include <treelite/common.h>
11 #include <vector>
12 #ifdef _WIN32
13 #define NOMINMAX
14 #include <windows.h>
15 #else
16 #include <sched.h>
17 #endif
18 #include "spsc_queue.h"
19 
20 namespace treelite {
21 
22 template <typename InputToken, typename OutputToken, typename TaskContext>
23 class ThreadPool {
24  public:
25  using TaskFunc = void(*)(SpscQueue<InputToken>*, SpscQueue<OutputToken>*,
26  const TaskContext*);
27 
28  ThreadPool(int num_worker, const TaskContext* context, TaskFunc task)
29  : num_worker_(num_worker), context_(context), task_(task) {
30  CHECK(num_worker_ > 0 && num_worker_ <= std::thread::hardware_concurrency())
31  << "Number of worker threads must be between 1 and "
32  << std::thread::hardware_concurrency();
33  for (int i = 0; i < num_worker_; ++i) {
34  incoming_queue_.emplace_back(common::make_unique<SpscQueue<InputToken>>());
35  outgoing_queue_.emplace_back(common::make_unique<SpscQueue<OutputToken>>());
36  }
37  thread_.resize(num_worker_);
38  for (int i = 0; i < num_worker_; ++i) {
39  thread_[i] = std::thread(task_, incoming_queue_[i].get(),
40  outgoing_queue_[i].get(),
41  context_);
42  }
43  /* bind threads to cores */
44  SetAffinity();
45  }
46  ~ThreadPool() {
47  for (int i = 0; i < num_worker_; ++i) {
48  incoming_queue_[i]->SignalForKill();
49  outgoing_queue_[i]->SignalForKill();
50  thread_[i].join();
51  }
52  }
53 
54  void SubmitTask(int tid, InputToken request) {
55  incoming_queue_[tid]->Push(request);
56  }
57 
58  bool WaitForTask(int tid, OutputToken* response) {
59  return outgoing_queue_[tid]->Pop(response);
60  }
61 
62  private:
63  int num_worker_;
64  std::vector<std::thread> thread_;
65  std::vector<std::unique_ptr<SpscQueue<InputToken>>> incoming_queue_;
66  std::vector<std::unique_ptr<SpscQueue<OutputToken>>> outgoing_queue_;
67  TaskFunc task_;
68  const TaskContext* context_;
69 
70  inline void SetAffinity() {
71 #ifdef _WIN32
72  /* Windows */
73  SetThreadAffinityMask(GetCurrentThread(), 0x1);
74  for (int i = 0; i < num_worker_; ++i) {
75  const int core_id = i + 1;
76  SetThreadAffinityMask(thread_[i].native_handle(), (1 << core_id));
77  }
78 #elif defined(__APPLE__) && defined(__MACH__)
79 #include <TargetConditionals.h>
80 #if TARGET_OS_MAC == 1
81  /* Mac OSX */
82  thread_port_t mach_thread = pthread_mach_thread_np(pthread_self());
83  thread_affinity_policy_data_t policy = {0};
84  thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
85  (thread_policy_t)&policy, THREAD_AFFINITY_POLICY_COUNT);
86  for (int i = 0; i < num_worker_; ++i) {
87  const int core_id = i + 1;
88  mach_thread = pthread_mach_thread_np(thread_[i].native_handle());
89  policy = {core_id};
90  thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
91  (thread_policy_t)&policy, THREAD_AFFINITY_POLICY_COUNT);
92  }
93 #else
94  #error "iPhone not supported yet"
95 #endif
96 #else
97  /* Linux and others */
98  cpu_set_t cpuset;
99  CPU_ZERO(&cpuset);
100  CPU_SET(0, &cpuset);
101  pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
102  for (int i = 0; i < num_worker_; ++i) {
103  const int core_id = i + 1;
104  CPU_ZERO(&cpuset);
105  CPU_SET(core_id, &cpuset);
106  pthread_setaffinity_np(thread_[i].native_handle(),
107  sizeof(cpu_set_t), &cpuset);
108  }
109 #endif
110  }
111 };
112 
113 } // namespace treelite
114 
115 #endif // TREELITE_THREAD_POOL_THREAD_POOL_H_
Lock-free single-producer-single-consumer queue.