forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubflowTasking.xml
More file actions
157 lines (157 loc) · 24.9 KB
/
Copy pathSubflowTasking.xml
File metadata and controls
157 lines (157 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<doxygen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="compound.xsd" version="1.12.0" xml:lang="en-US">
<compounddef id="SubflowTasking" kind="page">
<compoundname>SubflowTasking</compoundname>
<title>Subflow Tasking</title>
<tableofcontents>
<tocsect>
<name>Create a Subflow</name>
<reference>SubflowTasking_1CreateASubflow</reference>
</tocsect>
<tocsect>
<name>Retain a Subflow</name>
<reference>SubflowTasking_1RetainASubflow</reference>
</tocsect>
<tocsect>
<name>Join a Subflow Explicitly</name>
<reference>SubflowTasking_1JoinASubflow</reference>
</tocsect>
<tocsect>
<name>Create a Nested Subflow</name>
<reference>SubflowTasking_1CreateANestedSubflow</reference>
</tocsect>
</tableofcontents>
<briefdescription>
</briefdescription>
<detaileddescription>
<para>It is very common for a parallel program to spawn task dependency graphs at runtime. In Taskflow, we call this <emphasis>subflow tasking</emphasis>.</para>
<sect1 id="SubflowTasking_1CreateASubflow">
<title>Create a Subflow</title><para>Subflow tasks are those created during the execution of a graph. These tasks are spawned from a parent task and are grouped together to a <emphasis>subflow</emphasis> dependency graph. To create a subflow, emplace a callable that takes an argument of type <ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>. A <ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref> object will be created and forwarded to the execution context of the task. All methods you find in <ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref> are applicable for <ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><sp/>1:<sp/><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><sp/>2:<sp/><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor;</highlight></codeline>
<codeline><highlight class="normal"><sp/>3:</highlight></codeline>
<codeline><highlight class="normal"><sp/>4:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>A<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>()<sp/>{}).name(</highlight><highlight class="stringliteral">"A"</highlight><highlight class="normal">);<sp/><sp/></highlight><highlight class="comment">//<sp/>static<sp/>task<sp/>A</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><sp/>5:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>C<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>()<sp/>{}).name(</highlight><highlight class="stringliteral">"C"</highlight><highlight class="normal">);<sp/><sp/></highlight><highlight class="comment">//<sp/>static<sp/>task<sp/>C</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><sp/>6:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>D<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>()<sp/>{}).name(</highlight><highlight class="stringliteral">"D"</highlight><highlight class="normal">);<sp/><sp/></highlight><highlight class="comment">//<sp/>static<sp/>task<sp/>D</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><sp/>7:</highlight></codeline>
<codeline><highlight class="normal"><sp/>8:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>B<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>(<ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>&<sp/>subflow)<sp/>{<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/>9:<sp/><sp/><sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>B1<sp/>=<sp/>subflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>()<sp/>{}).name(</highlight><highlight class="stringliteral">"B1"</highlight><highlight class="normal">);<sp/><sp/></highlight><highlight class="comment">//<sp/>subflow<sp/>task<sp/>B1</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">10:<sp/><sp/><sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>B2<sp/>=<sp/>subflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>()<sp/>{}).name(</highlight><highlight class="stringliteral">"B2"</highlight><highlight class="normal">);<sp/><sp/></highlight><highlight class="comment">//<sp/>subflow<sp/>task<sp/>B2</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">11:<sp/><sp/><sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>B3<sp/>=<sp/>subflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>()<sp/>{}).name(</highlight><highlight class="stringliteral">"B3"</highlight><highlight class="normal">);<sp/><sp/></highlight><highlight class="comment">//<sp/>subflow<sp/>task<sp/>B3</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">12:<sp/><sp/><sp/>B1.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B3);<sp/><sp/></highlight><highlight class="comment">//<sp/>B1<sp/>runs<sp/>before<sp/>B3</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">13:<sp/><sp/><sp/>B2.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B3);<sp/><sp/></highlight><highlight class="comment">//<sp/>B2<sp/>runs<sp/>before<sp/>B3</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">14:<sp/>}).name(</highlight><highlight class="stringliteral">"B"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal">15:</highlight></codeline>
<codeline><highlight class="normal">16:<sp/>A.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B);<sp/><sp/></highlight><highlight class="comment">//<sp/>B<sp/>runs<sp/>after<sp/>A</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">17:<sp/>A.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(C);<sp/><sp/></highlight><highlight class="comment">//<sp/>C<sp/>runs<sp/>after<sp/>A</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">18:<sp/>B.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(D);<sp/><sp/></highlight><highlight class="comment">//<sp/>D<sp/>runs<sp/>after<sp/>B</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">19:<sp/>C.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(D);<sp/><sp/></highlight><highlight class="comment">//<sp/>D<sp/>runs<sp/>after<sp/>C</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">20:</highlight></codeline>
<codeline><highlight class="normal">21:<sp/>executor.<ref refid="classtf_1_1Executor_1a519777f5783981d534e9e53b99712069" kindref="member">run</ref>(taskflow).get();<sp/><sp/></highlight><highlight class="comment">//<sp/>execute<sp/>the<sp/>graph<sp/>to<sp/>spawn<sp/>the<sp/>subflow</highlight></codeline>
</programlisting></para>
<para><parblock><para><dotfile name="subflow-join.dot"></dotfile>
</para>
</parblock></para>
<para>Debrief: <itemizedlist>
<listitem><para>Lines 1-2 create a taskflow and an executor </para>
</listitem>
<listitem><para>Lines 4-6 create three tasks, A, C, and D </para>
</listitem>
<listitem><para>Lines 8-14 create a task B that spawns a task dependency graph of three tasks B1, B2, and B3 </para>
</listitem>
<listitem><para>Lines 16-19 add dependencies among A, B, C, and D </para>
</listitem>
<listitem><para>Line 21 submits the graph to an executor and waits until it finishes</para>
</listitem>
</itemizedlist>
Lines 8-14 are the main block to enable subflow tasking at task B. The runtime will create a <ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref> 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 of its parent task B.</para>
</sect1>
<sect1 id="SubflowTasking_1RetainASubflow">
<title>Retain a Subflow</title><para>By default, a <ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref> automatically clears its internal task graph once it is joined. After a subflow joins, its structure and associated resources are no longer accessible. This behavior is designed to reduce memory usage, particularly in applications that recursively spawn many subflows. For applications that require post-processing, such as visualizing the subflow through <ref refid="classtf_1_1Taskflow_1ac433018262e44b12c4cc9f0c4748d758" kindref="member">tf::Taskflow::dump</ref>, users can disable this default cleanup behavior by calling <ref refid="classtf_1_1Subflow_1ac585638d8ca8fb2f34c4826cb0d4f39f" kindref="member">tf::Subflow::retain</ref> on <computeroutput>true</computeroutput>. This instructs the runtime to retain the subflow's task graph even after it has joined, enabling further inspection or visualization.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](<ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>&<sp/>sf){</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>sf.<ref refid="classtf_1_1Subflow_1ac585638d8ca8fb2f34c4826cb0d4f39f" kindref="member">retain</ref>(</highlight><highlight class="keyword">true</highlight><highlight class="normal">);<sp/><sp/></highlight><highlight class="comment">//<sp/>retain<sp/>the<sp/>subflow<sp/>after<sp/>join<sp/>for<sp/>visualization</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>A<sp/>=<sp/>sf.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"A\n"</highlight><highlight class="normal">;<sp/>});</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>B<sp/>=<sp/>sf.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"B\n"</highlight><highlight class="normal">;<sp/>});</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>C<sp/>=<sp/>sf.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"C\n"</highlight><highlight class="normal">;<sp/>});</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>A.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B,<sp/>C);<sp/><sp/></highlight><highlight class="comment">//<sp/>A<sp/>runs<sp/>before<sp/>B<sp/>and<sp/>C</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">});<sp/><sp/></highlight><highlight class="comment">//<sp/>subflow<sp/>implicitly<sp/>joins<sp/>here</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a519777f5783981d534e9e53b99712069" kindref="member">run</ref>(taskflow).wait();</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>The<sp/>subflow<sp/>graph<sp/>is<sp/>now<sp/>retained<sp/>and<sp/>can<sp/>be<sp/>visualized<sp/>using<sp/>taskflow.dump(...)</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">taskflow.<ref refid="classtf_1_1Taskflow_1ac433018262e44b12c4cc9f0c4748d758" kindref="member">dump</ref>(<ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref>);</highlight></codeline>
</programlisting></para>
</sect1>
<sect1 id="SubflowTasking_1JoinASubflow">
<title>Join a Subflow Explicitly</title><para>By default, a subflow <emphasis>implicitly</emphasis> joins its parent task when execution leaves its context. All terminal nodes (i.e., nodes with no outgoing edges) in the subflow are guaranteed to precede the parent task. Upon joining, the subflow's task graph and associated resources are automatically cleaned up. If your application needs to access variables defined within the subflow after it joins, you can explicitly join the subflow and handle post-processing accordingly. A common use case is parallelizing recursive computations such as the Fibonacci sequence:</para>
<para><programlisting filename=".cpp"><codeline><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>spawn(</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>n,<sp/><ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>&<sp/>sbf)<sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">if</highlight><highlight class="normal"><sp/>(n<sp/><<sp/>2)<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>n;</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>res1,<sp/>res2;</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>sbf.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&res1,<sp/>n]<sp/>(<ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>&<sp/>sbf)<sp/>{<sp/>res1<sp/>=<sp/>spawn(n<sp/>-<sp/>1,<sp/>sbf);<sp/>}<sp/>);</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>sbf.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&res2,<sp/>n]<sp/>(<ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>&<sp/>sbf)<sp/>{<sp/>res2<sp/>=<sp/>spawn(n<sp/>-<sp/>2,<sp/>sbf);<sp/>}<sp/>);</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>sbf.<ref refid="classtf_1_1Subflow_1a59fcac1323e70d920088dd37bd0be245" kindref="member">join</ref>();<sp/><sp/><sp/><sp/></highlight><highlight class="comment">//<sp/>join<sp/>to<sp/>materialize<sp/>the<sp/>subflow<sp/>immediately</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>res1<sp/>+<sp/>res2;</highlight></codeline>
<codeline><highlight class="normal">}</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight></codeline>
<codeline><highlight class="normal">taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&res]<sp/>(<ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>&<sp/>sbf)<sp/>{<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>res<sp/>=<sp/>spawn(5,<sp/>sbf);<sp/><sp/></highlight></codeline>
<codeline><highlight class="normal">});</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a519777f5783981d534e9e53b99712069" kindref="member">run</ref>(taskflow).wait();</highlight></codeline>
</programlisting></para>
<para>The code above computes the fifth Fibonacci number using recursive subflow. Calling <ref refid="classtf_1_1Subflow_1a59fcac1323e70d920088dd37bd0be245" kindref="member">tf::Subflow::join</ref> <emphasis>immediately</emphasis> materializes the subflow by executing all associated tasks to recursively compute Fibonacci numbers. The taskflow graph is shown below:</para>
<para><dotfile name="fibonacci_7.dot"></dotfile>
</para>
<para><simplesect kind="attention"><para>Using <ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref> to implement recursive parallelism like finding Fibonacci numbers may not be as efficient as <ref refid="classtf_1_1Runtime" kindref="compound">tf::Runtime</ref> due to additional task graph overhead. For more details, readers can refer to <ref refid="fibonacci" kindref="compound">Fibonacci Number</ref>.</para>
</simplesect>
</para>
</sect1>
<sect1 id="SubflowTasking_1CreateANestedSubflow">
<title>Create a Nested Subflow</title><para>A subflow can be nested or recursive. You can create another subflow from the execution of a subflow and so on.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><sp/>1:<sp/><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><sp/>2:</highlight></codeline>
<codeline><highlight class="normal"><sp/>3:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>A<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>(<ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>&<sp/>sf){</highlight></codeline>
<codeline><highlight class="normal"><sp/>4:<sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"A<sp/>spawns<sp/>A1<sp/>&<sp/>subflow<sp/>A2\n"</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal"><sp/>5:<sp/><sp/><sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>A1<sp/>=<sp/>sf.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>()<sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/>6:<sp/><sp/><sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"subtask<sp/>A1\n"</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal"><sp/>7:<sp/><sp/><sp/>}).name(</highlight><highlight class="stringliteral">"A1"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><sp/>8:</highlight></codeline>
<codeline><highlight class="normal"><sp/>9:<sp/><sp/><sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>A2<sp/>=<sp/>sf.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>(<ref refid="classtf_1_1Subflow" kindref="compound">tf::Subflow</ref>&<sp/>sf2){</highlight></codeline>
<codeline><highlight class="normal">10:<sp/><sp/><sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"A2<sp/>spawns<sp/>A2_1<sp/>&<sp/>A2_2\n"</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal">11:<sp/><sp/><sp/><sp/><sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>A2_1<sp/>=<sp/>sf2.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>()<sp/>{</highlight></codeline>
<codeline><highlight class="normal">12:<sp/><sp/><sp/><sp/><sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"subtask<sp/>A2_1\n"</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal">13:<sp/><sp/><sp/><sp/><sp/>}).name(</highlight><highlight class="stringliteral">"A2_1"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal">14:<sp/><sp/><sp/><sp/><sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>A2_2<sp/>=<sp/>sf2.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([]<sp/>()<sp/>{</highlight></codeline>
<codeline><highlight class="normal">15:<sp/><sp/><sp/><sp/><sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"subtask<sp/>A2_2\n"</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal">16:<sp/><sp/><sp/><sp/><sp/>}).name(</highlight><highlight class="stringliteral">"A2_2"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal">17:<sp/><sp/><sp/><sp/><sp/>A2_1.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(A2_2);</highlight></codeline>
<codeline><highlight class="normal">18:<sp/><sp/><sp/>}).name(</highlight><highlight class="stringliteral">"A2"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal">19:<sp/><sp/><sp/>A1.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(A2);</highlight></codeline>
<codeline><highlight class="normal">20:<sp/>}).name(</highlight><highlight class="stringliteral">"A"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal">21:</highlight></codeline>
<codeline><highlight class="normal">22:<sp/></highlight><highlight class="comment">//<sp/>execute<sp/>the<sp/>graph<sp/>to<sp/>spawn<sp/>the<sp/>subflow</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">23:<sp/><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref>().<ref refid="classtf_1_1Executor_1a519777f5783981d534e9e53b99712069" kindref="member">run</ref>(taskflow).get();</highlight></codeline>
</programlisting></para>
<para><dotfile name="nested_subflow.dot"></dotfile>
</para>
<para>Debrief: <itemizedlist>
<listitem><para>Line 1 creates a taskflow object </para>
</listitem>
<listitem><para>Lines 3-20 create a task to spawn a subflow of two tasks A1 and A2 </para>
</listitem>
<listitem><para>Lines 9-18 spawn another subflow of two tasks A2_1 and A2_2 out of its parent task A2 </para>
</listitem>
<listitem><para>Lines 23 runs the defined taskflow graph</para>
</listitem>
</itemizedlist>
<simplesect kind="attention"><para>To properly visualize subflows, you must call <ref refid="classtf_1_1Subflow_1ac585638d8ca8fb2f34c4826cb0d4f39f" kindref="member">tf::Subflow::retain</ref> on each subflow and execute the taskflow once to ensure all associated subflows are spawned. </para>
</simplesect>
</para>
</sect1>
</detaileddescription>
<location file="doxygen/cookbook/subflow_tasking.dox"/>
</compounddef>
</doxygen>