<li><ahref="#ModuleAlgorithmInclude">Include the Header</a></li>
<li><ahref="#WhatIsAModuleTask">What is a Module Task</a></li>
<li><ahref="#CreateAModuleTaskOverACustomGraph">Create a Module Task over a Custom Graph</a></li>
</ul>
</nav>
<p>Taskflow provides template methods that let users create reusable building blocks called <em>modules</em>. Users can connect modules together to build more complex parallel algorithms.</p><sectionid="ModuleAlgorithmInclude"><h2><ahref="#ModuleAlgorithmInclude">Include the Header</a></h2><p>You need to include the header file, <code><ahref="module_8hpp_source.html" class="m-doc">taskflow/<wbr/>algorithm/<wbr/>module.hpp</a></code>, for creating a module task over a schedulable graph target.</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="w"></span><spanclass="cpf"><taskflow/algorithm/module.hpp></span></pre></section><sectionid="WhatIsAModuleTask"><h2><ahref="#WhatIsAModuleTask">What is a Module Task</a></h2><p>Similar to <ahref="ComposableTasking.html" class="m-doc">Composable Tasking</a>, but in a more generic setting, the template function, <ahref="namespacetf.html#ad13f8d0b6628d895792570515497139c" class="m-doc">tf::<wbr/>make_module_task</a>, allows you to create a task over a taskflow graph that can be executed by an executor. This provides a flexible way to encapsulate and reuse complex task logic within your Taskflow applications. The example below demonstrates how to create and launch multiple taskflows in parallel using asynchronous tasking:</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
</div><p>Since the four taskflows are launched asynchronously without any dependencies between them, we can observe any order of the output message:</p><preclass="m-console"><spanclass="gp"># </span>one<spanclass="w"></span>possible<spanclass="w"></span>output
<spanclass="go">Taskflow C</span></pre><p>If you need to enforce dependencies among these four taskflows, you can use dependent-async tasks. The example below launches the four taskflows one by one in sequential:</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="go">Taskflow D</span></pre><p>The module task maker, <ahref="namespacetf.html#ad13f8d0b6628d895792570515497139c" class="m-doc">tf::<wbr/>make_module_task</a>, functions basically similar to <ahref="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">tf::<wbr/>Taskflow::<wbr/>composed_of</a> but provides a more generic interface that can be used beyond Taskflow. Specifically, the following two approaches achieve the same functionality.</p><preclass="m-code"><spanclass="c1">// approach 1: composition using composed_of</span>
<spanclass="c1">// approach 2: composition using make_module_task</span>
<spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Task</span><spanclass="w"></span><spanclass="n">m1</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">taskflow1</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">(</span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">make_module_task</span><spanclass="p">(</span><spanclass="n">taskflow2</span><spanclass="p">));</span></pre><asideclass="m-note m-warning"><h4>Attention</h4><p>Similar to <ahref="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">tf::<wbr/>Taskflow::<wbr/>composed_of</a>, <ahref="namespacetf.html#ad13f8d0b6628d895792570515497139c" class="m-doc">tf::<wbr/>make_module_task</a> does not assume ownership of the provided taskflow but a soft reference. You are responsible for ensuring that the encapsulated taskflow remains valid throughout its execution.</p></aside></section><sectionid="CreateAModuleTaskOverACustomGraph"><h2><ahref="#CreateAModuleTaskOverACustomGraph">Create a Module Task over a Custom Graph</a></h2><p>In addition to encapsulate taskflow graphs, you can create a module task to schedule a custom graph target. A schedulable target (of type <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 that can be scheduled through making module tasks:</p><preclass="m-code"><spanclass="k">struct</span><spanclass="w"></span><spanclass="nc">CustomGraph</span><spanclass="w"></span><spanclass="p">{</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">async</span><spanclass="p">(</span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">make_module_task</span><spanclass="p">(</span><spanclass="n">target</span><spanclass="p">));</span></pre><asideclass="m-note m-warning"><h4>Attention</h4><p>Users are responsible for ensuring the given custom graph remains valid throughout its execution. The executor does not assume ownership of the custom graph.</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>