MADNESS  0.10.1
Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members

A singleton pool of threads for dynamic execution of tasks. More...

#include <thread.h>

Collaboration diagram for madness::ThreadPool:
Collaboration graph
[legend]

Public Member Functions

 ThreadPool (const ThreadPool &)=delete
 
 ThreadPool (ThreadPool &&)=delete
 
 ~ThreadPool ()
 Destructor. More...
 
void flush_prebuf ()
 
void operator= (const ThreadPool &)=delete
 
void operator= (ThreadPool &&)=delete
 
template<typename opT >
void scan (opT &op)
 

Static Public Member Functions

static void add (const std::vector< PoolTaskInterface * > &tasks)
 Add a vector of tasks to the pool. More...
 
static void add (PoolTaskInterface *task)
 Add a new task to the pool. More...
 
template<typename Probe >
static void await (const Probe &probe, bool dowork=true, bool sleep=false)
 Gracefully wait for a condition to become true, executing any tasks in the queue. More...
 
static void begin (int nthread=-1)
 Please invoke while in a single-threaded environment. More...
 
static int default_nthread ()
 Get the number of threads from the environment. More...
 
static void end ()
 
static const DQStatsget_stats ()
 Returns queue statistics. More...
 
static const ThreadPoolThreadget_threads ()
 
static ThreadPoolinstance ()
 Return a pointer to the only instance, constructing as necessary. More...
 
static std::size_t queue_size ()
 Returns the number of tasks in the queue. More...
 
static bool run_task ()
 An otherwise idle thread can all this to run a task. More...
 
static void set_wait_policy (WaitPolicy policy, int sleep_duration_in_microseconds=0)
 
static std::size_t size ()
 Returns the number of threads in the pool. More...
 

Static Public Attributes

static std::unique_ptr< tbb::task_arena > tbb_arena = nullptr
 
static std::unique_ptr< tbb::global_control > tbb_control = nullptr
 

Private Member Functions

 ThreadPool (int nthread=-1)
 The constructor is private to enforce the singleton model. More...
 
bool run_task (bool wait, ThreadPoolThread *this_thread)
 Run the next task. More...
 
bool run_tasks (bool wait, ThreadPoolThread *const this_thread)
 
void thread_main (ThreadPoolThread *const thread)
 

Static Private Member Functions

static void * pool_thread_main (void *v)
 Forwards the thread to bound member function. More...
 

Private Attributes

volatile bool finish
 Set to true when time to stop. More...
 
ThreadPoolThread main_thread
 Placeholder for main thread tls. More...
 
AtomicInt nfinished
 Thread pool exit counter. More...
 
int nthreads
 Number of threads. More...
 
DQueue< PoolTaskInterface * > queue
 Queue of tasks. More...
 
ThreadPoolThreadthreads
 Array of threads. More...
 

Static Private Attributes

static double await_timeout = 900.0
 Waiter timeout. More...
 
static ThreadPoolinstance_ptr = 0
 Singleton pointer. More...
 
static const int nmax = 128
 Number of task a worker thread will pop from the task queue. More...
 

Friends

class WorldTaskQueue
 

Detailed Description

A singleton pool of threads for dynamic execution of tasks.

Attention
You must instantiate the pool while running with just one thread.

Constructor & Destructor Documentation

◆ ThreadPool() [1/3]

madness::ThreadPool::ThreadPool ( const ThreadPool )
delete

Referenced by begin().

◆ ThreadPool() [2/3]

madness::ThreadPool::ThreadPool ( ThreadPool &&  )
delete

◆ ThreadPool() [3/3]

madness::ThreadPool::ThreadPool ( int  nthread = -1)
private

◆ ~ThreadPool()

madness::ThreadPool::~ThreadPool ( )
inline

Destructor.

References threads.

Member Function Documentation

◆ add() [1/2]

static void madness::ThreadPool::add ( const std::vector< PoolTaskInterface * > &  tasks)
inlinestatic

Add a vector of tasks to the pool.

Parameters
[in]tasksVector of tasks to add to the pool.

References add(), and MADNESS_EXCEPTION.

◆ add() [2/2]

static void madness::ThreadPool::add ( PoolTaskInterface task)
inlinestatic

Add a new task to the pool.

Todo:
Description needed.
Parameters
[in,out]taskDescription needed.

References instance(), MADNESS_EXCEPTION, queue, task(), and tbb_arena.

Referenced by add(), end(), main(), and madness::TaskInterface::Submit::notify().

◆ await()

template<typename Probe >
static void madness::ThreadPool::await ( const Probe &  probe,
bool  dowork = true,
bool  sleep = false 
)
inlinestatic

Gracefully wait for a condition to become true, executing any tasks in the queue.

Probe should be an object that, when called, returns the status.

Todo:
Descriptions needed/need verification.
Template Parameters
ProbeType of the probe.
Parameters
[in]probeThe probe.
[in]doworkDo work while waiting - default is true
[in]sleepSleep instead of spin while waiting (e.g., to avoid pounding on MPI) - default is false

References await_timeout, bufsize, madness::cpu_time(), current_time, madness::myusleep(), madness::MutexWaiter::reset(), run_task(), and madness::MutexWaiter::wait().

Referenced by madness::World::await(), and madness::WorldTaskQueue::fence().

◆ begin()

void madness::ThreadPool::begin ( int  nthread = -1)
static

Please invoke while in a single-threaded environment.

Todo:
Verify documentation.
Parameters
[in]nthreadThe number of threads.

References ThreadPool(), await_timeout, SafeMPI::COMM_WORLD, SafeMPI::Intracomm::Get_rank(), madness::ThreadBase::init_thread_key(), instance_ptr, madness::profiling::TaskProfiler::output_file_name_, madness::quiet(), and size().

◆ default_nthread()

int madness::ThreadPool::default_nthread ( )
static

Get the number of threads from the environment.

Returns
The number of threads.

References MADNESS_EXCEPTION, madness::ThreadBase::num_hw_processors(), and shift.

Referenced by ThreadPool().

◆ end()

void madness::ThreadPool::end ( )
static

◆ flush_prebuf()

void madness::ThreadPool::flush_prebuf ( )
inline

References queue.

Referenced by end(), and madness::RMI::RmiTask::process_some().

◆ get_stats()

const DQStats & madness::ThreadPool::get_stats ( )
static

Returns queue statistics.

Returns
Queue statistics.

References instance(), and queue.

Referenced by madness::print_stats().

◆ get_threads()

static const ThreadPoolThread* madness::ThreadPool::get_threads ( )
inlinestatic

Access the pool thread array

Returns
ptr to the pool thread array, its size is given by size()

References instance(), and threads.

◆ instance()

static ThreadPool* madness::ThreadPool::instance ( )
inlinestatic

Return a pointer to the only instance, constructing as necessary.

Returns
A pointer to the only instance.

References instance_ptr, and MADNESS_EXCEPTION.

Referenced by add(), end(), get_stats(), get_threads(), pool_thread_main(), madness::RMI::RmiTask::process_some(), queue_size(), run_task(), set_wait_policy(), and size().

◆ operator=() [1/2]

void madness::ThreadPool::operator= ( const ThreadPool )
delete

◆ operator=() [2/2]

void madness::ThreadPool::operator= ( ThreadPool &&  )
delete

◆ pool_thread_main()

void * madness::ThreadPool::pool_thread_main ( void *  v)
staticprivate

Forwards the thread to bound member function.

Todo:
Descriptions needed.
Parameters
[in]vDescription needed.
Returns
Description needed.

References instance(), thread_main(), and v.

Referenced by ThreadPool().

◆ queue_size()

static std::size_t madness::ThreadPool::queue_size ( )
inlinestatic

Returns the number of tasks in the queue.

Returns
The number of tasks in the queue.

References instance(), and queue.

◆ run_task() [1/2]

static bool madness::ThreadPool::run_task ( )
inlinestatic

An otherwise idle thread can all this to run a task.

Returns
True if a task was run.

References instance(), run_tasks(), and madness::ThreadBase::this_thread().

Referenced by await(), and thread_main().

◆ run_task() [2/2]

bool madness::ThreadPool::run_task ( bool  wait,
ThreadPoolThread this_thread 
)
inlineprivate

Run the next task.

Todo:
Verify and complete this documentation.
Parameters
[in]waitBlock of true.
[in,out]this_threadDescription needed.
Returns
True if a task was run.

References madness::profiling::TaskEventList::event(), MADNESS_EXCEPTION, madness::profiling::TaskProfiler::new_list(), madness::ThreadPoolThread::profiler(), and queue.

◆ run_tasks()

bool madness::ThreadPool::run_tasks ( bool  wait,
ThreadPoolThread *const  this_thread 
)
inlineprivate
Todo:
Brief description needed.
Todo:
Descriptions needed.
Parameters
[in]waitDescription needed.
[in,out]this_threadDescription needed.
Returns
Description needed.

References madness::profiling::TaskEventList::event(), MADNESS_EXCEPTION, madness::profiling::TaskProfiler::new_list(), nmax, madness::ThreadPoolThread::profiler(), queue, and madness::PoolTaskInterface::set_event().

Referenced by run_task(), and thread_main().

◆ scan()

template<typename opT >
void madness::ThreadPool::scan ( opT &  op)
inline
Todo:
Brief description needed.
Todo:
Descriptions needed.
Template Parameters
opTDescription needed.
Parameters
[in,out]opDescription needed.

References op(), and queue.

◆ set_wait_policy()

static void madness::ThreadPool::set_wait_policy ( WaitPolicy  policy,
int  sleep_duration_in_microseconds = 0 
)
inlinestatic

◆ size()

static std::size_t madness::ThreadPool::size ( )
inlinestatic

◆ thread_main()

void madness::ThreadPool::thread_main ( ThreadPoolThread *const  thread)
private

Friends And Related Function Documentation

◆ WorldTaskQueue

friend class WorldTaskQueue
friend

Member Data Documentation

◆ await_timeout

double madness::ThreadPool::await_timeout = 900.0
staticprivate

Waiter timeout.

Referenced by await(), and begin().

◆ finish

volatile bool madness::ThreadPool::finish
private

Set to true when time to stop.

Referenced by end(), and thread_main().

◆ instance_ptr

ThreadPool * madness::ThreadPool::instance_ptr = 0
staticprivate

Singleton pointer.

Referenced by ThreadPool(), begin(), end(), and instance().

◆ main_thread

ThreadPoolThread madness::ThreadPool::main_thread
private

Placeholder for main thread tls.

Referenced by ThreadPool(), and end().

◆ nfinished

AtomicInt madness::ThreadPool::nfinished
private

Thread pool exit counter.

Referenced by ThreadPool(), end(), and thread_main().

◆ nmax

const int madness::ThreadPool::nmax = 128
staticprivate

Number of task a worker thread will pop from the task queue.

Referenced by run_tasks().

◆ nthreads

int madness::ThreadPool::nthreads
private

Number of threads.

Referenced by ThreadPool(), end(), and size().

◆ queue

DQueue<PoolTaskInterface*> madness::ThreadPool::queue
private

◆ tbb_arena

std::unique_ptr< tbb::task_arena > madness::ThreadPool::tbb_arena = nullptr
static

◆ tbb_control

std::unique_ptr< tbb::global_control > madness::ThreadPool::tbb_control = nullptr
static
Todo:
Description needed.

Referenced by ThreadPool().

◆ threads

ThreadPoolThread* madness::ThreadPool::threads
private

Array of threads.

Referenced by ThreadPool(), ~ThreadPool(), and get_threads().


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