MADNESS 0.10.1

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

#include <thread.h>

Collaboration diagram for madness::ThreadPool:

Public Member Functions

 ThreadPool (const ThreadPool &)=delete
 ThreadPool (ThreadPool &&)=delete
 ~ThreadPool ()
 Destructor.
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.
static void add (PoolTaskInterface *task)
 Add a new task to the pool.
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.
static void begin (int nthread=-1)
 Please invoke while in a single-threaded environment.
static int default_nthread ()
 Get the number of threads from the environment.
static void end ()
static const DQStats & get_stats ()
 Returns queue statistics.
static const ThreadPoolThread * get_threads ()
static ThreadPool * instance ()
 Return a pointer to the only instance, constructing as necessary.
static std::size_t queue_size ()
 Returns the number of tasks in the queue.
static bool run_task ()
 An otherwise idle thread can all this to run a task.
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.

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.
bool run_task (bool wait, ThreadPoolThread *this_thread)
 Run the next task.
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.

Private Attributes

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

Static Private Attributes

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

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

◆ ThreadPool() [2/3]

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

References ThreadPool().

◆ ThreadPool() [3/3]

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

The constructor is private to enforce the singleton model.

Todo
Description needed.
Parameters
[in]nthreadDescription needed.

References SafeMPI::COMM_WORLD, default_nthread(), finish, instance_ptr, MADNESS_ASSERT, MADNESS_EXCEPTION, main_thread, nfinished, nthreads, pool_thread_main(), tbb_arena, tbb_control, madness::ThreadBase::thread_key, and threads.

◆ ~ThreadPool()

madness::ThreadPool::~ThreadPool ( )
inline

Destructor.

References threads.

Member Function Documentation

◆ add() [1/2]

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]

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>
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(), madness::thread_purge(), and madness::MutexWaiter::wait().

Referenced by madness::World::await(), 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, madness::ThreadBase::init_thread_key(), instance_ptr, madness::profiling::TaskProfiler::output_file_name_, madness::quiet(), and size().

Referenced by madness::World::initialize.

◆ 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

◆ 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()

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()

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

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

Returns
A pointer to the only instance.

References ThreadPool(), 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(), size(), and madness::thread_purge().

◆ operator=() [1/2]

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

References ThreadPool().

◆ operator=() [2/2]

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

References ThreadPool().

◆ 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()

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]

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(), queue, and task().

◆ 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, madness::PoolTaskInterface::set_event(), and task().

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()

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

◆ size()

◆ thread_main()

void madness::ThreadPool::thread_main ( ThreadPoolThread *const thread)
private
Todo
Brief description needed.
Todo
Description needed.
Parameters
[in,out]threadDescription needed.

References ThreadPool(), madness::binder, finish, nfinished, PROFILE_MEMBER_FUNC, madness::ThreadPoolThread::profiler(), run_task(), run_tasks(), and madness::profiling::TaskProfiler::write_to_file().

Referenced by pool_thread_main().

◆ WorldTaskQueue

friend class WorldTaskQueue
friend

References WorldTaskQueue.

Referenced by WorldTaskQueue.

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 ThreadPool(), 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().

◆ nfinished

AtomicInt madness::ThreadPool::nfinished
private

Thread pool exit counter.

Referenced by ThreadPool(), 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: