SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
Classes | Public Types | Public Member Functions | Protected Types | Static Protected Member Functions | Protected Attributes | List of all members
smaug::ThreadPool Class Reference

A user-space cooperatve thread pool implementation designed for gem5 in SE mode. More...

#include <thread_pool.h>

Detailed Description

A user-space cooperatve thread pool implementation designed for gem5 in SE mode.

Multithreading in gem5 SE mode is tricky - while we can spawn pthreads, we cannot let threads terminate when the pthread function returns, because the ThreadContext would be destroyed, which prevents us from ever using the CPU that was assigned to that ThreadContext. The solution is to run an infinite loop on all the threads in the pool and assign work to them from a queue.

To prevent wasting simulation time with spinloops, this thread pool implementation quiesces all inactive CPUs and wakes them up only when there is work to do. This is done via magic gem5 instructions.

Definition at line 23 of file thread_pool.h.

Classes

struct  ThreadInitArgs
 
struct  WorkerThread
 All state and metadata for a worker thread. More...
 

Public Types

typedef void *(* WorkerThreadFunc) (void *)
 Function signature for any work to be executed on a worker thread.
 

Public Member Functions

 ThreadPool (int nthreads)
 Create a ThreadPool with N threads. More...
 
int size () const
 Returns the number of worker threads.
 
void initThreadPool ()
 Initialize the thread pool. More...
 
int dispatchThread (WorkerThreadFunc func, void *args)
 Dispatch the function to a worker in the thread pool.
 
void joinThreadPool ()
 Wait for all threads in the pool to finish work.
 

Protected Types

enum  ThreadStatus { Uninitialized, Idle, Running }
 Possible worker thread states.
 

Static Protected Member Functions

static void * workerLoop (void *args)
 The main event loop executed by all worker threads.
 

Protected Attributes

std::vector< WorkerThreadworkers
 Worker threads.
 

Constructor & Destructor Documentation

◆ ThreadPool()

smaug::ThreadPool::ThreadPool ( int  nthreads)

Create a ThreadPool with N threads.

The simulation must be created with at least N+1 CPUs, since we need one CPU to run the main thread.

Definition at line 8 of file thread_pool.cpp.

Member Function Documentation

◆ initThreadPool()

void smaug::ThreadPool::initThreadPool ( )

Initialize the thread pool.

Initialization must be postponed until after fast-forwarding is finished, or we will get incorrect CPU IDs.

This can only be called once; any subsequent call will assert fail.

Definition at line 66 of file thread_pool.cpp.


The documentation for this class was generated from the following files: