Tomographer
v4.0
Tomographer C++ Framework Documentation
|
Dispatches tasks to parallel threads using OpenMP. More...
#include <tomographer/multiprocomp.h>
Public Types | |
typedef TaskType_ | TaskType |
The task type. | |
typedef TaskType::StatusReportType | TaskStatusReportType |
The type used by a single task when providing a status report. | |
typedef TaskCData_ | TaskCData |
The type which stores constant, shared data for all tasks to access. | |
typedef ResultsCollector_ | ResultsCollector |
The type which is responsible to collect the final results of the individual tasks. | |
typedef LoggerType_ | LoggerType |
The logger type specified to the dispatcher (not necessarily thread-safe) | |
typedef CountIntType_ | CountIntType |
Integer type used to count the number of tasks to run (or running) | |
typedef TaskLoggerType_ | TaskLoggerType |
A thread-safe logger type which is passed on to the child tasks. | |
typedef FullStatusReport< TaskStatusReportType > | FullStatusReportType |
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_, ResultsCollector *results_, LoggerType &logger_, CountIntType num_total_runs_, CountIntType n_chunk_=1) | |
Task dispatcher constructor. More... | |
void | run () |
Run the specified tasks. 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... | |
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.
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.)
ResultsCollector must be a ResultsCollector Interface compliant type
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:
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.
CountIntType 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 353 of file multiprocomp.h.
typedef std::function<void(const FullStatusReportType&)> Tomographer::MultiProc::OMP::TaskDispatcher< TaskType_, TaskCData_, ResultsCollector_, LoggerType_, CountIntType_, TaskLoggerType_ >::FullStatusReportCallbackType |
The relevant type for a callback function (or callable) which is provided with the full status report.
Definition at line 378 of file multiprocomp.h.
|
inline |
Task dispatcher constructor.
pcdata_ | The constant shared data, which will be accessible by all tasks |
results_ | The results collector instance, responsible for collecting the results of all individual 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 695 of file multiprocomp.h.
|
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 pageInterfaceTask-compliant type which periodically checks for status reports is automatically interruptible.
Definition at line 945 of file multiprocomp.h.
|
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 926 of file multiprocomp.h.
|
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().
Definition at line 904 of file multiprocomp.h.
|
inline |
Run the specified tasks.
Do everything, run tasks, collect results etc.
Definition at line 705 of file multiprocomp.h.
|
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 886 of file multiprocomp.h.