Tomographer  v5.3
Tomographer C++ Framework Documentation
Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ > Class Template Reference

Dispatches tasks to parallel threads using C++11 native threads. More...

#include <tomographer/multiprocthreads.h>

+ Inheritance diagram for Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >:
+ Collaboration diagram for Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >:

Public Types

typedef Tomographer::MultiProc::ThreadCommon::TaskDispatcherBase< TaskType_, TaskCountIntType_ > Base
 Base class, provides common functionality to all thread-based MutliProc implementations.
 
typedef TaskCData_ TaskCData
 The type which stores constant, shared data for all tasks to access.
 
typedef LoggerType_ LoggerType
 The logger type specified to the dispatcher (not necessarily thread-safe)
 
typedef ThreadSanitizerLogger< LoggerType_ > TaskLoggerType
 A thread-safe logger type which is passed on to the child tasks.
 
typedef TaskType_ TaskType
 The task type.
 
typedef TaskType::ResultType TaskResultType
 The task result type.
 
typedef TaskType::StatusReportType TaskStatusReportType
 The type used by a single task when providing a status report.
 
typedef TaskCountIntType_ TaskCountIntType
 Integer type used to count the number of tasks to run (or running)
 
typedef FullStatusReport< TaskStatusReportType, TaskCountIntTypeFullStatusReportType
 The type to use to generate a full status report of all running tasks.
 
typedef std::function< void(const FullStatusReportType &)> FullStatusReportCallbackType
 The relevant type for a callback function (or callable) which is provided with the full status report. More...
 
- Public Types inherited from Tomographer::MultiProc::ThreadCommon::TaskDispatcherBase< TaskType_, TaskCountIntType_ >
typedef TaskType_ TaskType
 The task type.
 
typedef TaskCountIntType_ TaskCountIntType
 Integer type used to count the number of tasks to run (or running)
 
typedef TaskType::ResultType TaskResultType
 The task result type.
 
typedef TaskType::StatusReportType TaskStatusReportType
 The type used by a single task when providing a status report.
 
typedef FullStatusReport< TaskStatusReportType, TaskCountIntTypeFullStatusReportType
 The type to use to generate a full status report of all running tasks.
 
typedef std::function< void(const FullStatusReportType &)> FullStatusReportCallbackType
 The relevant type for a callback function (or callable) which is provided with the full status report. More...
 

Public Member Functions

 TaskDispatcher (TaskCData *pcdata, LoggerType &logger, TaskCountIntType num_total_runs, int num_threads=0)
 Task dispatcher constructor. More...
 
 TaskDispatcher (TaskDispatcher &&other)
 
void run ()
 Run the specified tasks. More...
 
TaskCountIntType numTaskRuns () const
 Total number of task run instances. More...
 
const std::vector< TaskResultType * > & collectedTaskResults () const
 Get all the task results. More...
 
const TaskResultTypecollectedTaskResult (std::size_t k) const
 Get the result of a specific given task. More...
 
void setStatusReportHandler (FullStatusReportCallbackType fnstatus)
 assign a callable to be called whenever a status report is requested More...
 
void requestStatusReport ()
 Request a one-time status report. More...
 
void requestPeriodicStatusReport (int milliseconds)
 Request a periodic status report. More...
 
void requestInterrupt ()
 Request an immediate interruption of the tasks. More...
 

Additional Inherited Members

- Protected Types inherited from Tomographer::MultiProc::ThreadCommon::TaskDispatcherBase< TaskType_, TaskCountIntType_ >
typedef std::chrono::steady_clock StdClockType
 
- Protected Member Functions inherited from Tomographer::MultiProc::ThreadCommon::TaskDispatcherBase< TaskType_, TaskCountIntType_ >
 TaskDispatcherBase ()
 Basic constructor. More...
 
 TaskDispatcherBase (TaskDispatcherBase &&)
 
template<typename ThreadPrivateDataType , typename ThreadSharedDataType >
void run_worker_enter (ThreadPrivateDataType &private_data, ThreadSharedDataType &shared_data)
 New worker in the game.
 
template<typename ThreadPrivateDataType , typename ThreadSharedDataType >
void run_worker_exit (ThreadPrivateDataType &private_data, ThreadSharedDataType &shared_data)
 A worker exits the game.
 
template<typename ThreadPrivateDataType , typename ThreadSharedDataType >
void TOMOGRAPHER_CXX_STACK_FORCE_REALIGN run_task (ThreadPrivateDataType &private_data, ThreadSharedDataType &shared_data)
 Run a given task. More...
 
template<typename ThreadPrivateDataType , typename ThreadSharedDataType >
void master_continue_monitoring_status (ThreadPrivateDataType &private_data, ThreadSharedDataType &shared_data)
 To be called by master thread only to continue monitoring for status reports.
 
template<typename ThreadSharedDataType , typename LocalLoggerType >
void run_epilog (ThreadSharedDataType &shared_data, LocalLoggerType &llogger)
 To be called after all workers are done, to e.g. throw proper exception if an error occurred.
 

Detailed Description

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
class Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >

Dispatches tasks to parallel threads using C++11 native threads.

This multiprocessing implementation uses the C++11 standard's API for multithreading.

This task dispatcher is very similar to Tomographer::MultiProc::OMP::TaskDispatcher and conforms to the TaskDispatcher Interface type interface.

Since
Changed in Tomographer 5.0: removed results collector, introduced collectedTaskResults() and friends
  • TaskType must be a Task Interface compliant type. This type specifies the task which has to be run. Objects of this type will be instantiated within separate threads to run the tasks.

  • TaskCData should conform to the TaskCData Interface.

    TaskCData may be any struct which contains all the information which needs to be accessed by the task. It should be read-only, i.e. the task should not need to write to this information. (This typically encodes the data of the problem, ie. experimental measurement results.)

  • LoggerType is a logger type derived from Logger::LoggerBase, for example Logger::FileLogger. This is the type of a logger defined in the caller's scope (and given as constructor argument here) to which messages should be logged to.

  • TaskCountIntType should be a type to use to count the number of tasks. Usually there's no reason not to use an int.

Definition at line 250 of file multiprocthreads.h.

Member Typedef Documentation

§ FullStatusReportCallbackType

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
typedef std::function<void(const FullStatusReportType&)> Tomographer::MultiProc::ThreadCommon::TaskDispatcherBase< TaskType_, TaskCountIntType_ >::FullStatusReportCallbackType

The relevant type for a callback function (or callable) which is provided with the full status report.

This is the type used as argument to a subclass' setStatusReportHandler() method (see TaskDispatcher Interface).

Definition at line 120 of file multiprocthreadcommon.h.

Constructor & Destructor Documentation

§ TaskDispatcher()

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >::TaskDispatcher ( TaskCData pcdata,
LoggerType logger,
TaskCountIntType  num_total_runs,
int  num_threads = 0 
)
inline

Task dispatcher constructor.

Parameters
pcdataThe constant shared data, which will be accessible by all tasks
loggerThe logger instance to use to log messages. This logger does not need to be thread safe.
num_total_runsThe number of tasks to run in total. Recall that the inputs to the different task instances are provided by the TaskCData's getTaskInput() method (see TaskCData Interface).
num_threadsThe number of parallel threads to use as workers. Specify the value zero to auto-detect the number of processor cores.

Definition at line 358 of file multiprocthreads.h.

Member Function Documentation

§ collectedTaskResult()

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
const TaskResultType& Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >::collectedTaskResult ( std::size_t  k) const
inline

Get the result of a specific given task.

Definition at line 509 of file multiprocthreads.h.

§ collectedTaskResults()

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
const std::vector<TaskResultType*>& Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >::collectedTaskResults ( ) const
inline

Get all the task results.

Definition at line 502 of file multiprocthreads.h.

§ numTaskRuns()

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
TaskCountIntType Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >::numTaskRuns ( ) const
inline

Total number of task run instances.

Definition at line 495 of file multiprocthreads.h.

§ requestInterrupt()

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
void Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >::requestInterrupt ( )
inline

Request an immediate interruption of the tasks.

Execution inside the function run() will stop as soon as each workers notices the interrupt request, and will emit the TasksInterruptedException.

The periodic check on the tasks' side is implemented in each tasks' check for a status report, so that any Task Interface -compliant type which periodically checks for status reports is automatically interruptible.

Note
This function is safe to be called from within a signal handler.

Definition at line 581 of file multiprocthreads.h.

§ requestPeriodicStatusReport()

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
void Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >::requestPeriodicStatusReport ( int  milliseconds)
inline

Request a periodic status report.

The status report function callback set with setStatusReportHandler() will be called every milliseconds milliseconds with a status report.

Pass -1 as argument to milliseconds to disable periodic status reports.

Definition at line 562 of file multiprocthreads.h.

§ requestStatusReport()

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
void Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >::requestStatusReport ( )
inline

Request a one-time status report.

This function makes a note that a status report has been requested. Subsequently, the tasks should notice it (provided they regularly query for status report requests as described on the page Task Interface), and provide status reports. When all the reports have been received from all running threads, the full status report is passed on to the callback set with setStatusReportHandler().

Note
This function is safe to be called from within a signal handler.

Definition at line 542 of file multiprocthreads.h.

§ run()

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
void Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >::run ( )
inline

Run the specified tasks.

Do everything, run tasks, collect results etc.

Definition at line 382 of file multiprocthreads.h.

§ setStatusReportHandler()

template<typename TaskType_, typename TaskCData_, typename LoggerType_, typename TaskCountIntType_ = int>
void Tomographer::MultiProc::CxxThreads::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_ >::setStatusReportHandler ( FullStatusReportCallbackType  fnstatus)
inline

assign a callable to be called whenever a status report is requested

This function remembers the given fnstatus callable, so that each time that requestStatusReport() is called at any later point, then this callback will be invoked.

The callback, when invoked, will be called with a single parameter of type FullStatusReport<TaskStatusReportType>. It is guaranteed to be called from within the main thread.

Definition at line 525 of file multiprocthreads.h.


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