Cpp-Taskflow
2.1.0
|
It is very common for a parallel program to spawn task dependency graphs at runtime. In Cpp-Taskflow, we call this dynamic tasking.
Dynamic tasks are those created during the execution of a dispatched graph. These tasks are spawned from a parent task and are grouped together to a subflow dependency graph. Cpp-Taskflow has an unified interface for static and dynamic tasking. To create a subflow for dynamic tasking, emplace a callable that takes one argument of type tf::SubflowBuilder. A tf::SubflowBuilder object will be created during the runtime and passed to the task. All graph building methods you find in taskflow are applicable for a subflow builder.
Debrief:
Line 7-13 is the main coding block to enable dynamic tasking. Cpp-Taskflow uses a std::variant date type to unify the interface of static tasking and dynamic tasking. The runtime will create a tf::SubflowBuilder passing it to task B, and spawn a dependency graph as described by the associated callable. This new subflow graph will be added to the topology to which its parent task B belongs to. Due to the property of dynamic tasking, we cannot dump its structure before execution. We will need to dispatch the graph first and call the method tf::Taskflow::dump_topologies.
By default, a spawned subflow joins its parent task. That is, all nodes of zero outgoing edges in the subflow will precede the parent task. This forces a subflow to follow the dependency constraints after its parent task. Having said that, you can detach a subflow from its parent task, allowing its execution to flow independently.
The figure below demonstrates a detached subflow based on the previous example. A detached subflow will eventually join the end of the topology of its parent task.
A subflow can be nested or recursive. You can create another subflow from the execution of a subflow and so on.
Debrief:
Similarly, you can detach a nested subflow from its parent subflow. A detached subflow will run independently and eventually join the topology of its parent subflow.