|
| 1 | +# Spawn a Task Dependency Graph at Runtime |
| 2 | + |
| 3 | +It is very common for a parallel program to |
| 4 | +spawn tasks at runtime. |
| 5 | +In Cpp-Taskflow, we call this *dynamic tasking* - |
| 6 | +creating another task dependency graph |
| 7 | +during the execution of a task. |
| 8 | +In this tutorial, we are going to demonstrate how to enable dynamic tasking |
| 9 | +in Cpp-Taskflow. |
| 10 | + |
| 11 | ++ [Subflow Dependency Graph](#Subflow-Dependency-Graph) |
| 12 | ++ [Detach a Subflow Dependency Graph](#Detach-a-Subflow-Dependency-Graph) |
| 13 | ++ [Nested Subflow](#Nested-Subflow) |
| 14 | + |
| 15 | +# Subflow Dependency Graph |
| 16 | + |
| 17 | +Dynamic tasks are those created during the execution of a dispatched graph. |
| 18 | +These tasks are spawned from a parent task and are grouped together to a |
| 19 | +*subflow* dependency graph. |
| 20 | +Cpp-Taskflow has an unified interface for static and dynamic tasking. |
| 21 | +To create a subflow for dynamic tasking, emplace a callable |
| 22 | +that takes one argument of type `SubflowBuilder`. |
| 23 | +A `SubflowBuilder` object will be created during the runtime and |
| 24 | +passed to the task. |
| 25 | +All graph building methods you find in taskflow can be also used in a subflow builder. |
| 26 | + |
| 27 | +```cpp |
| 28 | + 1: tf::Taskflow tf(4); // create a taskflow object with four worker threads |
| 29 | + 2: |
| 30 | + 3: auto A = tf.silent_emplace([] () {}).name("A"); // static task A |
| 31 | + 4: auto C = tf.silent_emplace([] () {}).name("C"); // static task C |
| 32 | + 5: auto D = tf.silent_emplace([] () {}).name("D"); // static task D |
| 33 | + 6: |
| 34 | + 7: auto B = tf.silent_emplace([] (tf::SubflowBuilder& subflow) { // static task B to spawn a subflow |
| 35 | + 8: auto B1 = subflow.silent_emplace([] () {}).name("B1"); // dynamic task B1 |
| 36 | + 9: auto B2 = subflow.silent_emplace([] () {}).name("B2"); // dynamic task B2 |
| 37 | +10: auto B3 = subflow.silent_emplace([] () {}).name("B3"); // dynamic task B3 |
| 38 | +11: B1.precede(B3); // B1 runs bofore B3 |
| 39 | +12: B2.precede(B3); // B2 runs before B3 |
| 40 | +13: }).name("B"); |
| 41 | +14: |
| 42 | +15: A.precede(B); // B runs after A |
| 43 | +16: A.precede(C); // C runs after A |
| 44 | +17: B.precede(D); // D runs after B |
| 45 | +18: C.precede(D); // D runs after C |
| 46 | +19: |
| 47 | +20: tf.dispatch().get(); // execute the graph without cleanning up topologies |
| 48 | +21: std::cout << tf.dump_topologies(); |
| 49 | +``` |
| 50 | +
|
| 51 | + |
| 52 | +
|
| 53 | +Debrief: |
| 54 | ++ Line 1 creates a taskflow object with four worker threads |
| 55 | ++ Line 3-5 creates three tasks, A, C, and D |
| 56 | ++ Line 7-13 creates a task B that spawns a task dependency graph of three tasks B1, B2, and B3 |
| 57 | ++ Line 15-18 add dependencies among A, B, C, and D |
| 58 | ++ Line 20 dispatches the graph and waits until it finishes without cleaning up the topology |
| 59 | ++ Line 21 dumps the topology that represents the entire task dependency graph |
| 60 | +
|
| 61 | +Line 7-13 is the main coding block to enable dynamic tasking. |
| 62 | +Cpp-Taskflow uses a variant date type to unify the interface of static tasking and dynamic tasking. |
| 63 | +The runtime will create a *subflow builder* passing it to task B, |
| 64 | +and spawn a dependency graph as described by the associated callable. |
| 65 | +This new subflow graph will be added to the topology to which its parent task B belongs to. |
| 66 | +Due to the property of dynamic tasking, |
| 67 | +we cannot dump its structure before execution. |
| 68 | +We will need to dispatch the graph first and call the method `dump_topologies`. |
| 69 | +
|
| 70 | +# Detach a Subflow Dependency Graph |
| 71 | +
|
| 72 | +By default, a spawned subflow joins its parent task. |
| 73 | +That is, all nodes of zero outgoing edges in the subflow will precede the parent task. |
| 74 | +This forces a subflow to follow the dependency constraints after its parent task. |
| 75 | +Having said that, |
| 76 | +you can detach a subflow from its parent task, allowing its execution to flow independently. |
| 77 | +
|
| 78 | +```cpp |
| 79 | + 1: tf::Taskflow tf(4); // create a taskflow object with four worker threads |
| 80 | + 2: |
| 81 | + 3: auto A = tf.silent_emplace([] () {}).name("A"); // static task A |
| 82 | + 4: auto C = tf.silent_emplace([] () {}).name("C"); // static task C |
| 83 | + 5: auto D = tf.silent_emplace([] () {}).name("D"); // static task D |
| 84 | + 6: |
| 85 | + 7: auto B = tf.silent_emplace([] (tf::SubflowBuilder& subflow) { // task B to spawn a subflow |
| 86 | + 8: auto B1 = subflow.silent_emplace([] () {}).name("B1"); // dynamic task B1 |
| 87 | + 9: auto B2 = subflow.silent_emplace([] () {}).name("B2"); // dynamic task B2 |
| 88 | +10: auto B3 = subflow.silent_emplace([] () {}).name("B3"); // dynamic task B3 |
| 89 | +11: B1.precede(B3); // B1 runs bofore B3 |
| 90 | +12: B2.precede(B3); // B2 runs before B3 |
| 91 | +13: subflow.detach(); // detach this subflow |
| 92 | +14: }).name("B"); |
| 93 | +15: |
| 94 | +16: A.precede(B); // B runs after A |
| 95 | +17: A.precede(C); // C runs after A |
| 96 | +18: B.precede(D); // D runs after B |
| 97 | +19: C.precede(D); // D runs after C |
| 98 | +20: |
| 99 | +21: tf.dispatch().get(); // execute the graph without cleanning up topologies |
| 100 | +22: std::cout << tf.dump_topologies(); |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +The above figure demonstrates a detached subflow based on the example |
| 106 | +in the previous section. |
| 107 | +A detached subflow will eventually join the end of the topology of its parent task. |
| 108 | + |
| 109 | +# Nested Subflow |
| 110 | + |
| 111 | +A subflow can be nested or recursive. |
| 112 | +You can create another subflow from the execution of a subflow and so on. |
| 113 | + |
| 114 | +```cpp |
| 115 | + 1: tf::Taskflow tf; |
| 116 | + 2: |
| 117 | + 3: auto A = tf.silent_emplace([] (auto& sbf){ |
| 118 | + 4: std::cout << "A spawns A1 & subflow A2\n"; |
| 119 | + 5: auto A1 = sbf.silent_emplace([] () { |
| 120 | + 6: std::cout << "subtask A1\n"; |
| 121 | + 7: }).name("A1"); |
| 122 | + 8: |
| 123 | + 9: auto A2 = sbf.silent_emplace([] (auto& sbf2){ |
| 124 | +10: std::cout << "A2 spawns A2_1 & A2_2\n"; |
| 125 | +11: auto A2_1 = sbf2.silent_emplace([] () { |
| 126 | +12: std::cout << "subtask A2_1\n"; |
| 127 | +13: }).name("A2_1"); |
| 128 | +14: auto A2_2 = sbf2.silent_emplace([] () { |
| 129 | +15: std::cout << "subtask A2_2\n"; |
| 130 | +16: }).name("A2_2"); |
| 131 | +17: A2_1.precede(A2_2); |
| 132 | +18: }).name("A2"); |
| 133 | +19: A1.precede(A2); |
| 134 | +20: }).name("A"); |
| 135 | +21: |
| 136 | +22: // execute the graph without cleanning up topologies |
| 137 | +23: tf.dispatch().get(); |
| 138 | +24: std::cout << tf.dump_topologies(); |
| 139 | +``` |
| 140 | +
|
| 141 | + |
| 142 | +
|
| 143 | +Debrief: |
| 144 | ++ Line 1 creates a taskflow object |
| 145 | ++ Line 3-20 creates a task to spawn a subflow of two tasks A1 and A2 |
| 146 | ++ Line 9-18 spawns another subflow of two tasks A2_1 and A2_2 out of its parent task A2 |
| 147 | ++ Line 23-24 dispatches the graph asynchronously and dump its structure when it finishes |
| 148 | +
|
| 149 | +Similarly, you can detach a nested subflow from its parent subflow. |
| 150 | +A detached subflow will run independently and eventually join the topology |
| 151 | +of its parent subflow. |
| 152 | +
|
| 153 | +
|
| 154 | +
|
| 155 | +
|
0 commit comments