SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
thread_pool.h
1 #ifndef _UTILITY_THREAD_POOL_H_
2 #define _UTILITY_THREAD_POOL_H_
3 
4 #include <pthread.h>
5 #include <vector>
6 
7 namespace smaug {
8 
23 class ThreadPool {
24  public:
31  ThreadPool(int nthreads);
32  ~ThreadPool();
33 
35  typedef void* (*WorkerThreadFunc)(void*);
36 
38  int size() const { return workers.size(); }
39 
48  void initThreadPool();
49 
51  int dispatchThread(WorkerThreadFunc func, void* args);
52 
54  void joinThreadPool();
55 
56  protected:
58  enum ThreadStatus { Uninitialized, Idle, Running };
59 
61  struct WorkerThread {
62  WorkerThreadFunc func;
64  void* args;
65 
67  pthread_t thread;
72  pthread_mutex_t statusMutex;
74  bool exit;
76  bool valid;
77  ThreadStatus status;
82  pthread_cond_t wakeupCond;
87  pthread_cond_t statusCond;
89  int cpuid;
90 
91  WorkerThread() {
92  func = NULL;
93  args = NULL;
94  exit = false;
95  valid = false;
96  status = Uninitialized;
97  pthread_mutex_init(&statusMutex, NULL);
98  pthread_cond_init(&wakeupCond, NULL);
99  pthread_cond_init(&statusCond, NULL);
100  }
101  };
102 
103  struct ThreadInitArgs {
104  WorkerThread* worker;
105  pthread_mutex_t cpuidMutex;
106  pthread_cond_t cpuidCond;
107  int cpuid;
108 
109  ThreadInitArgs(WorkerThread* _worker) : worker(_worker) {
110  pthread_mutex_init(&cpuidMutex, NULL);
111  pthread_cond_init(&cpuidCond, NULL);
112  cpuid = -1;
113  }
114  };
115 
117  static void* workerLoop(void* args);
118 
120  std::vector<WorkerThread> workers;
121 };
122 
123 } // namespace smaug
124 
125 #endif
smaug::ThreadPool::WorkerThread::wakeupCond
pthread_cond_t wakeupCond
The main thread signals this condition variable to wake up the thread and have it check for work (ind...
Definition: thread_pool.h:82
smaug::ThreadPool::WorkerThread::args
void * args
User-provided arguments.
Definition: thread_pool.h:64
smaug::ThreadPool::joinThreadPool
void joinThreadPool()
Wait for all threads in the pool to finish work.
Definition: thread_pool.cpp:103
smaug::ThreadPool::workerLoop
static void * workerLoop(void *args)
The main event loop executed by all worker threads.
Definition: thread_pool.cpp:29
smaug::ThreadPool::WorkerThread::statusMutex
pthread_mutex_t statusMutex
This mutex protects all of the subsequent fields of this struct.
Definition: thread_pool.h:72
smaug::ThreadPool::WorkerThread::exit
bool exit
Set to true to inform the worker thread to terminate.
Definition: thread_pool.h:74
smaug::ThreadPool::ThreadPool
ThreadPool(int nthreads)
Create a ThreadPool with N threads.
Definition: thread_pool.cpp:8
smaug::ThreadPool::workers
std::vector< WorkerThread > workers
Worker threads.
Definition: thread_pool.h:120
smaug::ThreadPool::initThreadPool
void initThreadPool()
Initialize the thread pool.
Definition: thread_pool.cpp:66
smaug::ThreadPool::WorkerThread::statusCond
pthread_cond_t statusCond
The worker thread signals this condition variable to inform the main thread of a change in status (us...
Definition: thread_pool.h:87
smaug::ThreadPool::WorkerThread::valid
bool valid
Set to true if the func and args are valid and need to be executed.
Definition: thread_pool.h:76
smaug::ThreadPool::WorkerThread::cpuid
int cpuid
The gem5 simulation CPU ID assigned to this worker thread.
Definition: thread_pool.h:89
smaug::ThreadPool::ThreadStatus
ThreadStatus
Possible worker thread states.
Definition: thread_pool.h:58
smaug::ThreadPool::WorkerThreadFunc
void *(* WorkerThreadFunc)(void *)
Function signature for any work to be executed on a worker thread.
Definition: thread_pool.h:35
smaug
The smaug namespace is the parent namespace of all C++ code in SMAUG.
Definition: backend.cpp:38
smaug::ThreadPool::size
int size() const
Returns the number of worker threads.
Definition: thread_pool.h:38
smaug::ThreadPool::dispatchThread
int dispatchThread(WorkerThreadFunc func, void *args)
Dispatch the function to a worker in the thread pool.
Definition: thread_pool.cpp:85
smaug::ThreadPool::WorkerThread::thread
pthread_t thread
pthread handle.
Definition: thread_pool.h:67
smaug::ThreadPool::ThreadInitArgs
Definition: thread_pool.h:103
smaug::ThreadPool
A user-space cooperatve thread pool implementation designed for gem5 in SE mode.
Definition: thread_pool.h:23
smaug::ThreadPool::WorkerThread
All state and metadata for a worker thread.
Definition: thread_pool.h:61