<li><ahref="#ComposeATaskflow">Compose a Taskflow</a></li>
<li><ahref="#CreateAModuleTaskFromATaskflow">Create a Module Task from a Taskflow</a></li>
<li><ahref="#CreateACustomComposableGraph">Create a Custom Composable Graph</a></li>
</ul>
</nav>
<p>Composition is a key to improve the programmability of a complex workflow. This chapter describes how to create a large parallel graph through composition of modular and reusable blocks that are easier to optimize.</p><sectionid="ComposeATaskflow"><h2><ahref="#ComposeATaskflow">Compose a Taskflow</a></h2><p>A powerful feature of <ahref="classtf_1_1Taskflow.html" class="m-doc">tf::<wbr/>Taskflow</a> is its <em>composable</em> interface. You can break down a large parallel workload into smaller pieces each designed to run a specific task dependency graph. This largely facilitates the <em>modularity</em> of writing a parallel task program.</p><preclass="m-code"><spanclass="w"></span><spanclass="mi">1</span><spanclass="o">:</span><spanclass="w"></span><spanclass="c1">// f1 has three independent tasks</span>
</div><p>Debrief:</p><ul><li>Lines 1-12 create a taskflow of three tasks f1A, f1B, and f1C with f1A and f1B preceding f1C</li><li>Lines 17-30 create a taskflow of four tasks f2A, f2B, f2C, and f2D</li><li>Line 32 creates a module task from taskflow f1 through the method <ahref="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">Taskflow::<wbr/>composed_of</a></li><li>Line 33 enforces task f2C to run before the module task</li><li>Line 34 enforces the module task to run before task f2D</li></ul></section><sectionid="CreateAModuleTaskFromATaskflow"><h2><ahref="#CreateAModuleTaskFromATaskflow">Create a Module Task from a Taskflow</a></h2><p>The task created from <ahref="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">Taskflow::<wbr/>composed_of</a> is a <em>module</em> task that runs on a pre-defined taskflow. A module task does not own the taskflow but maintains a soft mapping to the taskflow. You can create multiple module tasks from the same taskflow but only one module task can run at one time. For example, the following composition is valid. Even though the two module tasks <code>module1</code> and <code>module2</code> refer to the same taskflow <code>F1</code>, the dependency link prevents <code>F1</code> from multiple executions at the same time.</p><divclass="m-graph"><svgstyle="width: 31.600rem; height: 38.300rem;" viewBox="0.00 0.00 316.00 383.25">
</div><p>However, the following composition is <em>invalid</em>. Both module tasks refer to the same taskflow. They can not run at the same time because they are associated with the same graph.</p><divclass="m-graph"><svgstyle="width: 43.300rem; height: 31.100rem;" viewBox="0.00 0.00 433.00 311.25">
</div></section><sectionid="CreateACustomComposableGraph"><h2><ahref="#CreateACustomComposableGraph">Create a Custom Composable Graph</a></h2><p>Taskflow allows you to create a custom graph object that can participate in the scheduling using composition. To become a module task, your class <code>T</code> must define the method <code>T::graph()</code> that returns a reference to the <ahref="classtf_1_1Graph.html" class="m-doc">tf::<wbr/>Graph</a> object managed by <code>T</code>. The following example defines a custom graph object that can be assembled in a taskflow throw composition:</p><preclass="m-code"><spanclass="w"></span><spanclass="mi">1</span><spanclass="o">:</span><spanclass="w"></span><spanclass="k">struct</span><spanclass="w"></span><spanclass="nc">CustomGraph</span><spanclass="w"></span><spanclass="p">{</span>
<spanclass="w"></span><spanclass="mi">4</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">FlowBuilder</span><spanclass="w"></span><spanclass="n">builder</span><spanclass="p">(</span><spanclass="n">graph</span><spanclass="p">);</span><spanclass="w"></span><spanclass="c1">// inherit all task builders in tf::Taskflow</span>
<spanclass="w"></span><spanclass="mi">9</span><spanclass="o">:</span><spanclass="w"></span><spanclass="c1">// returns a reference to the graph for taskflow composition</span>
<spanclass="mi">14</span><spanclass="o">:</span><spanclass="w"></span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Task</span><spanclass="w"></span><spanclass="n">comp</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">composed_of</span><spanclass="p">(</span><spanclass="n">obj</span><spanclass="p">);</span></pre><p>Debrief:</p><ul><li>Lines 1-11 define a custom graph interface to participate in taskflow composition</li><li>Line 2 defines the graph object using <ahref="classtf_1_1Graph.html" class="m-doc">tf::<wbr/>Graph</a></li><li>Lines 3-8 defines the constructor that constructs the task graph using <ahref="classtf_1_1FlowBuilder.html" class="m-doc">tf::<wbr/>FlowBuilder</a></li><li>Line 10 defines the required method for taskflow composition</li><li>Lines 13-14 creates a module task for the declared graph object in the taskflow</li></ul><p>The composition method <ahref="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">tf::<wbr/>Taskflow::<wbr/>composed_of</a> requires the target to define the <code>graph()</code> method that returns a reference to a <ahref="classtf_1_1Graph.html" class="m-doc">tf::<wbr/>Graph</a> object defined by the target. At runtime, the executor will schedule tasks in that graph using the same work-stealing algorithm as other taskflows.</p><asideclass="m-note m-warning"><h4>Attention</h4><p>Users are responsible for ensuring the given target remains valid throughout its execution. The executor does not assume ownership of the target object.</p></aside></section>
</div>
</div>
</div>
</article></main>
<divclass="m-doc-search" id="search">
<ahref="#!" onclick="return hideSearch()"></a>
<divclass="m-container">
<divclass="m-row">
<divclass="m-col-m-8 m-push-m-2">
<divclass="m-doc-search-header m-text m-small">
<div><spanclass="m-label m-default">Tab</span> / <spanclass="m-label m-default">T</span> to search, <spanclass="m-label m-default">Esc</span> to close</div>