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

Dispatches tasks to parallel threads using OpenMP. More...

#include <tomographer/multiprocomp.h>

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

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 TaskLoggerType_ 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_, TaskCountIntType n_chunk_=1)
 Task dispatcher constructor. More...
 
 TaskDispatcher (TaskDispatcher &&x)
 
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 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, typename TaskLoggerType_ = ThreadSanitizerLogger<LoggerType_>>
class Tomographer::MultiProc::OMP::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_, TaskLoggerType_ >

Dispatches tasks to parallel threads using OpenMP.

Uses OpenMP to parallelize the repetition of a same task with different inputs.

Check out this good tutorial on OpenMP.

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.

  • TaskLoggerType is the type of the logger which will be provided to tasks inside the parallel section. Such logger should ensure that the logging is thread-safe. By default TaskLoggerType is nothing else than an appropriate ThreadSanitizerLogger.

    (Note that if the originally given logger is thread-safe (see Logger::LoggerTraits), then ThreadSanitizerLogger directly relays calls without wrapping them into OMP critical sections.)

    For each task, a new TaskLoggerType will be created. The constructor is expected to accept the following arguments:

    TaskLoggerType(LoggerType & baselogger, const TaskCData * pcdata, CountIntType k)

    where baselogger is the logger given to the TaskDispatcher constructor, pcdata is the constant shared data pointer also given to the constructor, and k is the task number (which may range from 0 to the total number of tasks - 1). The task logger is NOT constructed in a thread-safe code region, so use "\#pragma omp critical" if necessary. You may use omp_get_thread_num() and omp_get_num_threads() to get the current thread number and the total number of threads, respectively.

  • 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 280 of file multiprocomp.h.

Member Typedef Documentation

§ FullStatusReportCallbackType

template<typename TaskType_ , typename TaskCData_ , typename LoggerType_ , typename TaskCountIntType_ = int, typename TaskLoggerType_ = ThreadSanitizerLogger<LoggerType_>>
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, typename TaskLoggerType_ = ThreadSanitizerLogger<LoggerType_>>
Tomographer::MultiProc::OMP::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_, TaskLoggerType_ >::TaskDispatcher ( TaskCData pcdata_,
LoggerType logger_,
TaskCountIntType  num_total_runs_,
TaskCountIntType  n_chunk_ = 1 
)
inline

Task dispatcher constructor.

Parameters
pcdata_The constant shared data, which will be accessible by all tasks
logger_The logger instance to use to log messages. This logger does not need to be thread safe.
num_total_runs_The 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).
n_chunk_How many tasks to chunk together into one thread. This corresponds to OpenMP's chunk argument in the instruction schedule(dynamic,chunk) in a #pragma omp for instruction (see this page).

Definition at line 387 of file multiprocomp.h.

Member Function Documentation

§ collectedTaskResult()

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

Get the result of a specific given task.

Definition at line 513 of file multiprocomp.h.

§ collectedTaskResults()

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

Get all the task results.

Definition at line 506 of file multiprocomp.h.

§ numTaskRuns()

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

Total number of task run instances.

Definition at line 499 of file multiprocomp.h.

§ requestInterrupt()

template<typename TaskType_ , typename TaskCData_ , typename LoggerType_ , typename TaskCountIntType_ = int, typename TaskLoggerType_ = ThreadSanitizerLogger<LoggerType_>>
void Tomographer::MultiProc::OMP::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_, TaskLoggerType_ >::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 593 of file multiprocomp.h.

§ requestPeriodicStatusReport()

template<typename TaskType_ , typename TaskCData_ , typename LoggerType_ , typename TaskCountIntType_ = int, typename TaskLoggerType_ = ThreadSanitizerLogger<LoggerType_>>
void Tomographer::MultiProc::OMP::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_, TaskLoggerType_ >::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 572 of file multiprocomp.h.

§ requestStatusReport()

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

Request a 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 550 of file multiprocomp.h.

§ run()

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

Run the specified tasks.

Do everything, run tasks, collect results etc.

Definition at line 408 of file multiprocomp.h.

§ setStatusReportHandler()

template<typename TaskType_ , typename TaskCData_ , typename LoggerType_ , typename TaskCountIntType_ = int, typename TaskLoggerType_ = ThreadSanitizerLogger<LoggerType_>>
void Tomographer::MultiProc::OMP::TaskDispatcher< TaskType_, TaskCData_, LoggerType_, TaskCountIntType_, TaskLoggerType_ >::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, that is, the one with omp_get_thread_num() == 0.

Definition at line 531 of file multiprocomp.h.


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