Peano
Loading...
Searching...
No Matches
tarch::multicore::TaskWithCopyOfFunctor Class Reference

Frequently used implementation for job with a functor. More...

#include <Task.h>

Inheritance diagram for tarch::multicore::TaskWithCopyOfFunctor:
Collaboration diagram for tarch::multicore::TaskWithCopyOfFunctor:

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::list< 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
 

Detailed Description

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

[=,this]()->bool {
...
return false;
}
);
newTask,
myNumber
);
Frequently used implementation for job with a functor.
Definition Task.h:211
Abstract super class for a job.
Definition Task.h:21
static constexpr int DontFuse
Definition Task.h:29
static constexpr int DefaultPriority
Definition Task.h:27
const std::set< TaskNumber > NoInDependencies
Definition multicore.h:103
void spawnTask(Task *task, const std::set< TaskNumber > &inDependencies=tarch::multicore::NoInDependencies, const TaskNumber &taskNumber=tarch::multicore::NoOutDependencies)
Spawns a single task in a non-blocking fashion.

Definition at line 211 of file Task.h.

Constructor & Destructor Documentation

◆ TaskWithCopyOfFunctor() [1/3]

tarch::multicore::TaskWithCopyOfFunctor::TaskWithCopyOfFunctor ( )
delete

◆ TaskWithCopyOfFunctor() [2/3]

tarch::multicore::TaskWithCopyOfFunctor::TaskWithCopyOfFunctor ( const TaskWithCopyOfFunctor & )
delete

◆ TaskWithCopyOfFunctor() [3/3]

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.

Tasks capturing external data

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:

auto invertMatrixFunctor = [inverseOperator, marker, OperatorSize]() mutable -> void {
std::memcpy(
...
);
};

Fusion

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.

static int taskType = peano4::parallel::getTaskType("MyFancyClassName");
taskType,
[=](...) -> void {
...
}
);
int getTaskType(const std::string &className)
Get unique number (id) for task.
Definition parallel.cpp:7
Parameters
taskTypeSee superclass documentation
prioritySee superclass documentation
taskFunctorFunctor that's then internally copied. Functor returns indicator if task has to rerun. See class documentation.

Definition at line 55 of file Task.cpp.

Member Function Documentation

◆ run()

void tarch::multicore::TaskWithCopyOfFunctor::run ( )
overridevirtual

Run the task.

Implements tarch::multicore::Task.

Definition at line 70 of file Task.cpp.

◆ toString()

std::string tarch::multicore::TaskWithCopyOfFunctor::toString ( ) const
overridevirtual

Reimplemented from tarch::multicore::Task.

Definition at line 74 of file Task.cpp.

Field Documentation

◆ _taskFunctor

std::function<void()> tarch::multicore::TaskWithCopyOfFunctor::_taskFunctor
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 217 of file Task.h.


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