|
Peano
|
Frequently used implementation for job with a functor. More...
#include <Task.h>


Public Member Functions | |
| TaskWithCopyOfFunctor ()=delete | |
| TaskWithCopyOfFunctor (const TaskWithCopyOfFunctor &)=delete | |
| TaskWithCopyOfFunctor (int taskType, int priority, const std::function< void()> &taskFunctor) | |
| Create new task from a functor. | |
| virtual void | run () override |
| Run the task. | |
| virtual std::string | toString () const override |
| Public Member Functions inherited from tarch::multicore::Task | |
| Task (int taskType, int priority) | |
| Construct task. | |
| virtual | ~Task () |
| int | getTaskType () const |
| int | getPriority () const |
| void | setPriority (int priority) |
| Set priority. | |
| virtual bool | canFuse () const |
| virtual void | fuse (const std::vector< Task * > &otherTasks, int targetDevice=Host) |
| Fuse multiple tasks. | |
Private Attributes | |
| std::function< void()> | _taskFunctor |
| See the outer class description for an explanation why this is an attribute, i.e. | |
Additional Inherited Members | |
| Static Public Attributes inherited from tarch::multicore::Task | |
| static constexpr int | DefaultPriority = 1024 |
| static constexpr int | Host = -1 |
| static constexpr int | DontFuse = -1 |
| Protected Attributes inherited from tarch::multicore::Task | |
| const int | _taskType |
| int | _priority |
Frequently used implementation for job with a functor.
Peano's tasking API is a plain class-based implementation of a task system. Many modern APIs such as oneTBB favour a functor-based API. The latter approach add nothing new, as C++ internally breaks down functors into classes with an operator(). In Peano, we mirror this behaviour, i.e. start with a class design and then offer this class on top which allows you to pipe in a functor instead of implementing your run() within a subclass.
Most people using lambdas for this class will define a lambda within a function which catches all relevant data and then pass this lambda into a task. This implies that this lambda object will be destroyed once the spawning routine terminates, even though the task might not have been executed at this point. Therefore, this routine copies the lambda.
The functor is a 1:1 translation of Task's run(). It takes no arguments, and it returns a bool which indicates if this task has to rerun. Returning false signals that this task is done and can be discarded. Returning true signals that the task is done, but the same task has to be re-executed. It is up to the tasking runtime to decide if it will re-execute immediately again or at a later point.
A typical usage looks similar to
|
delete |
|
delete |
| tarch::multicore::TaskWithCopyOfFunctor::TaskWithCopyOfFunctor | ( | int | taskType, |
| int | priority, | ||
| const std::function< void()> & | taskFunctor ) |
Create new task from a functor.
This is the standard version of a task that should be used with a functor. Obviously, you can use the version without the copying, but if and only if you can ensure that the functor remains valid even once you leave the surrounding scope. This version copies the functor and hence is fine even if the functor had been created (via auto) directly before we construct the task. However, we implicitly assume that the functor's variables all remain available, too, i.e. we'd expect in most cases that the functor catches all surrounding variables through copying.
If tasks capture and alter external data, constructing them through a functor that's copied doesn't work, as C++ implicitly assumes that this functor has to be const. Therefore, you manually have to set the functor to mutuable. Here is an example from the multigrid extension:
You can have functor tasks with generic fusion. For this, simply pass in a proper unique task type, so we can distinguish tasks from each other. This is best done through a factor method as explained in the superclass.
| taskType | See superclass documentation |
| priority | See superclass documentation |
| taskFunctor | Functor that's then internally copied. Functor returns indicator if task has to rerun. See class documentation. |
Definition at line 55 of file Task.cpp.
References _taskFunctor, and tarch::multicore::Task::Task().

|
overridevirtual |
Run the task.
Implements tarch::multicore::Task.
Definition at line 70 of file Task.cpp.
References _taskFunctor.
|
overridevirtual |
Reimplemented from tarch::multicore::Task.
|
private |
See the outer class description for an explanation why this is an attribute, i.e.
why we copy the functor here always.
Definition at line 216 of file Task.h.
Referenced by run(), and TaskWithCopyOfFunctor().