<p>After you create a task dependency graph, you need to submit it to threads for execution. In this chapter, we will show you how to execute a task dependency graph.</p><sectionid="CreateAnExecutor"><h2><ahref="#CreateAnExecutor">Create an Executor</a></h2><p>To execute a taskflow, you need to create an <em>executor</em> of type <ahref="classtf_1_1Executor.html" class="m-doc">tf::<wbr/>Executor</a>. An executor is a <em>thread-safe</em> object that manages a set of worker threads and executes tasks through an efficient <em>work-stealing</em> algorithm. Issuing a call to run a taskflow creates a <em>topology</em>, a data structure to keep track of the execution status of a running graph. <ahref="classtf_1_1Executor.html" class="m-doc">tf::<wbr/>Executor</a> takes an unsigned integer to construct with <code>N</code> worker threads. The default value is <ahref="http://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency.html" class="m-doc-external">std::<wbr/>thread::<wbr/>hardware_concurrency</a>.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="n">executor1</span><spanclass="p">;</span><spanclass="c1">// create an executor with the number of workers</span>
<spanclass="c1">// equal to std::thread::hardware_concurrency</span>
<spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="n">executor2</span><spanclass="p">(</span><spanclass="mi">4</span><spanclass="p">);</span><spanclass="c1">// create an executor of 4 worker threads</span></pre><p>An executor can be reused to execute multiple taskflows. In most workloads, you may need only one executor to run multiple taskflows where each taskflow represents a part of a parallel decomposition.</p></section><sectionid="ExecuteATaskflow"><h2><ahref="#ExecuteATaskflow">Execute a Taskflow</a></h2><p><ahref="classtf_1_1Executor.html" class="m-doc">tf::<wbr/>Executor</a> provides a set of <code>run_*</code> methods, <ahref="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069" class="m-doc">tf::<wbr/>Executor::<wbr/>run</a>, <ahref="classtf_1_1Executor.html#a6d0617eebc9421f1ba1f82ce6dd02c00" class="m-doc">tf::<wbr/>Executor::<wbr/>run_n</a>, and <ahref="classtf_1_1Executor.html#a0f52e9dd64b65aba32ca0e13c1ed300a" class="m-doc">tf::<wbr/>Executor::<wbr/>run_until</a> to run a taskflow for one time, multiple times, or until a given predicate evaluates to true. All methods accept an optional callback to invoke after the execution completes, and return a <ahref="classtf_1_1Future.html" class="m-doc">tf::<wbr/>Future</a> for users to access the execution status. The code below shows several ways to run a taskflow.</p><preclass="m-code"><spanclass="mi">1</span><spanclass="o">:</span><spanclass="c1">// Declare an executor and a taskflow</span>
<spanclass="mi">5</span><spanclass="o">:</span><spanclass="c1">// Add three tasks into the taskflow</span>
<spanclass="mi">6</span><spanclass="o">:</span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Task</span><spanclass="n">A</span><spanclass="o">=</span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([]</span><spanclass="p">()</span><spanclass="p">{</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="o"><<</span><spanclass="s">"This is TaskA</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span><spanclass="p">});</span>
<spanclass="mi">7</span><spanclass="o">:</span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Task</span><spanclass="n">B</span><spanclass="o">=</span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([]</span><spanclass="p">()</span><spanclass="p">{</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="o"><<</span><spanclass="s">"This is TaskB</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span><spanclass="p">});</span>
<spanclass="mi">8</span><spanclass="o">:</span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Task</span><spanclass="n">C</span><spanclass="o">=</span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([]</span><spanclass="p">()</span><spanclass="p">{</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="o"><<</span><spanclass="s">"This is TaskC</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span><spanclass="p">});</span>
<spanclass="mi">9</span><spanclass="o">:</span>
<spanclass="mi">10</span><spanclass="o">:</span><spanclass="c1">// Build precedence between tasks</span>
<spanclass="mi">14</span><spanclass="o">:</span><spanclass="n">fu</span><spanclass="p">.</span><spanclass="n">wait</span><spanclass="p">();</span><spanclass="c1">// block until the execution completes</span>
<spanclass="mi">15</span><spanclass="o">:</span>
<spanclass="mi">16</span><spanclass="o">:</span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">,</span><spanclass="p">[](){</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="o"><<</span><spanclass="s">"end of 1 run"</span><spanclass="p">;</span><spanclass="p">}).</span><spanclass="n">wait</span><spanclass="p">();</span>
<spanclass="mi">18</span><spanclass="o">:</span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">wait_for_all</span><spanclass="p">();</span><spanclass="c1">// block until all associated executions finish</span>
<spanclass="mi">19</span><spanclass="o">:</span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run_n</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">,</span><spanclass="mi">4</span><spanclass="p">,</span><spanclass="p">[](){</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="o"><<</span><spanclass="s">"end of 4 runs"</span><spanclass="p">;</span><spanclass="p">}).</span><spanclass="n">wait</span><spanclass="p">();</span>
<spanclass="mi">20</span><spanclass="o">:</span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run_until</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">,</span><spanclass="p">[</span><spanclass="n">cnt</span><spanclass="o">=</span><spanclass="mi">0</span><spanclass="p">]</span><spanclass="p">()</span><spanclass="k">mutable</span><spanclass="p">{</span><spanclass="k">return</span><spanclass="o">++</span><spanclass="n">cnt</span><spanclass="o">==</span><spanclass="mi">10</span><spanclass="p">;</span><spanclass="p">});</span></pre><p>Debrief:</p><ul><li>Lines 6-8 create a taskflow of three tasks A, B, and C</li><li>Lines 13-14 run the taskflow once and wait for completion</li><li>Line 16 runs the taskflow once with a callback to invoke when the execution finishes</li><li>Lines 17-18 run the taskflow four times and use <ahref="classtf_1_1Executor.html#ab9aa252f70e9a40020a1e5a89d485b85" class="m-doc">tf::<wbr/>Executor::<wbr/>wait_for_all</a> to wait for completion</li><li>Line 19 runs the taskflow four times and invokes a callback at the end of the forth execution</li><li>Line 20 keeps running the taskflow until the predicate returns true</li></ul><p>Issuing multiple runs on the same taskflow will automatically <em>synchronize</em> to a sequential chain of executions in the order of run calls.</p><preclass="m-code"><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">);</span><spanclass="c1">// execution 1</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">wait_for_all</span><spanclass="p">();</span><spanclass="c1">// execution 1 -> execution 2 -> execution 3</span></pre><asideclass="m-note m-warning"><h4>Attention</h4><p>A running taskflow must remain alive during its execution. It is your responsibility to ensure a taskflow not being destructed when it is running. For example, the code below can result undefined behavior.</p></aside><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="n">executor</span><spanclass="p">;</span><spanclass="c1">// create an executor</span>
<spanclass="c1">// create a taskflow whose lifetime is restricted by the scope</span>
<spanclass="p">}</span><spanclass="c1">// leaving the scope will destroy taskflow while it is running, </span>
<spanclass="c1">// resulting in undefined behavior</span></pre><p>Similarly, you should avoid touching a taskflow while it is running.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="n">taskflow</span><spanclass="p">;</span>
<spanclass="c1">// Add tasks into the taskflow</span>
<spanclass="c1">// alter the taskflow while running leads to undefined behavior </span>
<spanclass="n">f</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([](){</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="o"><<</span><spanclass="s">"Add a new task</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span><spanclass="p">});</span></pre><p>You must always keep a taskflow alive and must not modify it while it is running on an executor.</p></section><sectionid="ExecuteATaskflowWithTransferredOwnership"><h2><ahref="#ExecuteATaskflowWithTransferredOwnership">Execute a Taskflow with Transferred Ownership</a></h2><p>You can transfer the ownership of a taskflow to an executor and run it without wrangling with the lifetime issue of that taskflow. Each <code>run_*</code> method discussed in the previous section comes with an overload that takes a <em>moved</em> taskflow object.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="n">taskflow</span><spanclass="p">;</span>
<spanclass="c1">// now taskflow has no tasks</span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">num_tasks</span><spanclass="p">()</span><spanclass="o">==</span><spanclass="mi">0</span><spanclass="p">);</span></pre><p>However, you should avoid moving a <em>running</em> taskflow which can result in undefined behavior.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="n">taskflow</span><spanclass="p">;</span>
<spanclass="c1">// error! you cannot move a taskflow while it is running</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">move</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">));</span></pre><p>The correct way to submit a taskflow with moved ownership to an executor is to ensure all previous runs have completed. The executor will automatically release the resources of a moved taskflow right <em>after</em> its execution completes.</p><preclass="m-code"><spanclass="c1">// submit the taskflow and wait until it completes</span>
<spanclass="c1">// now it's safe to move the taskflow to the executor and run it</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">move</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">));</span></pre><p>Likewise, you cannot move a taskflow that is running on an executor. You must wait until all the previous fires of runs on that taskflow complete before calling move.</p><preclass="m-code"><spanclass="c1">// submit the taskflow and wait until it completes</span>
<spanclass="c1">// now it's safe to move the taskflow to another</span>
<spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="n">moved_taskflow</span><spanclass="p">(</span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">move</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">));</span></pre></section><sectionid="ThreadSafety"><h2><ahref="#ThreadSafety">Touch an Executor from Multiple Threads</a></h2><p>All <code>run_*</code> methods are <em>thread-safe</em>. You can have multiple threads call these methods from an executor to run different taskflows. However, the order which taskflow runs first is non-deterministic and is up to the runtime.</p><preclass="m-code"><spanclass="mi">1</span><spanclass="o">:</span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="mi">5</span><spanclass="o">:</span><spanclass="c1">// ... modify my taskflow at i</span>
<spanclass="mi">6</span><spanclass="o">:</span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflows</span><spanclass="p">[</span><spanclass="n">i</span><spanclass="p">]);</span><spanclass="c1">// run my taskflow at i</span>
<spanclass="mi">10</span><spanclass="o">:</span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">wait_for_all</span><spanclass="p">();</span></pre></section><sectionid="QueryTheWorkerID"><h2><ahref="#QueryTheWorkerID">Query the Worker Identieir</a></h2><p>Each worker in an executor has an unique integer identifier in the range <code>[0, N)</code> that can be queried by the caller thread using <ahref="classtf_1_1Executor.html#a6487d589cb1f6b078b69fd3bb1082345" class="m-doc">tf::<wbr/>Executor::<wbr/>this_worker_id</a>. If the caller thread is not a worker in the executor, <code>-1</code> is returned. This method is convenient for users to maintain a one-to-one mapping between a worker and its application data structure.</p><preclass="m-code"><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">vector</span><spanclass="o"><</span><spanclass="kt">int</span><spanclass="o">></span><spanclass="n">worker_vectors</span><spanclass="p">[</span><spanclass="mi">8</span><spanclass="p">];</span><spanclass="c1">// one vector per worker</span>
<spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="n">executor</span><spanclass="p">(</span><spanclass="mi">8</span><spanclass="p">);</span><spanclass="c1">// an executor of eight workers</span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">this_worker_id</span><spanclass="p">()</span><spanclass="o">==</span><spanclass="mi">-1</span><spanclass="p">);</span><spanclass="c1">// master thread is not a worker</span>
<spanclass="kt">int</span><spanclass="n">id</span><spanclass="o">=</span><spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">this_worker_id</span><spanclass="p">();</span><spanclass="c1">// in the range [0, 8)</span>
<spanclass="p">});</span></pre></section><sectionid="ObserveThreadActivities"><h2><ahref="#ObserveThreadActivities">Observe Thread Activities</a></h2><p>You can observe thread activities in an executor when a worker thread participates in executing a task and leaves the execution using <ahref="classtf_1_1ObserverInterface.html" class="m-doc">tf::<wbr/>ObserverInterface</a> – an <em>interface</em> class that provides a set of methods for you to define what to do when a thread enters and leaves the execution context of a task.</p><preclass="m-code"><spanclass="k">class</span><spanclass="nc">ObserverInterface</span><spanclass="p">{</span>
<spanclass="p">};</span></pre><p>There are three methods you must define in your derived class, <ahref="classtf_1_1ObserverInterface.html#a41e6e62f12bf9d9dc4fa74632f6825d9" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>set_up</a>, <ahref="classtf_1_1ObserverInterface.html#a3e3a9f7f1d43691794e487b8787b01a0" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>on_entry</a>, and <ahref="classtf_1_1ObserverInterface.html#aa59a59a59eadd4eb6ba20ecdfcae882b" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>on_exit</a>. The method, <ahref="classtf_1_1ObserverInterface.html#a41e6e62f12bf9d9dc4fa74632f6825d9" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>set_up</a>, is a constructor-like method that will be called by the executor when the observer is constructed. It passes an argument of the number of workers to observer in the executor. You may use it to preallocate or initialize data storage, e.g., an independent vector for each worker. The methods, <ahref="classtf_1_1ObserverInterface.html#a3e3a9f7f1d43691794e487b8787b01a0" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>on_entry</a> and <ahref="classtf_1_1ObserverInterface.html#aa59a59a59eadd4eb6ba20ecdfcae882b" class="m-doc">tf::<wbr/>ObserverInterface::<wbr/>on_exit</a>, are called by a worker thread before and after the execution context of a task, respectively. You may use them to record timepoints and calculate the elapsed time of a task.</p><p>You can associate an executor with one or multiple observers (though one is common) using <ahref="classtf_1_1Executor.html#aff77def96ae740d648dd84e571237c83" class="m-doc">tf::<wbr/>Executor::<wbr/>make_observer</a>. We use <ahref="http://en.cppreference.com/w/cpp/memory/shared_ptr.html" class="m-doc-external">std::<wbr/>shared_ptr</a> to manage the ownership of an observer. The executor loops through each observer and invoke the corresponding methods accordingly.</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="cpf"><taskflow/taskflow.hpp></span><spanclass="cp"></span>
<spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="o"><<</span><spanclass="s">"setting up observer with "</span><spanclass="o"><<</span><spanclass="n">num_workers</span><spanclass="o"><<</span><spanclass="s">" workers</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span>
<spanclass="n">oss</span><spanclass="o"><<</span><spanclass="s">"worker "</span><spanclass="o"><<</span><spanclass="n">w</span><spanclass="o"><<</span><spanclass="s">" ready to run "</span><spanclass="o"><<</span><spanclass="n">tv</span><spanclass="p">.</span><spanclass="n">name</span><spanclass="p">()</span><spanclass="o"><<</span><spanclass="sc">'\n'</span><spanclass="p">;</span>
<spanclass="p">}</span></pre><p>The above code produces the following output:</p><preclass="m-code">constructing observer MyObserver
setting up observer with <spanclass="m">4</span> workers
worker <spanclass="m">2</span> ready to run A
<spanclass="m">1</span>
worker <spanclass="m">2</span> finished running A
worker <spanclass="m">2</span> ready to run B
<spanclass="m">2</span>
worker <spanclass="m">1</span> ready to run C
worker <spanclass="m">2</span> finished running B
<spanclass="m">3</span>
worker <spanclass="m">2</span> ready to run D
worker <spanclass="m">3</span> ready to run E
worker <spanclass="m">1</span> finished running C
<spanclass="m">4</span>
<spanclass="m">5</span>
worker <spanclass="m">1</span> ready to run F
worker <spanclass="m">2</span> finished running D
worker <spanclass="m">3</span> finished running E
<spanclass="m">6</span>
worker <spanclass="m">2</span> ready to run G
worker <spanclass="m">3</span> ready to run H
worker <spanclass="m">1</span> finished running F
<spanclass="m">7</span>
<spanclass="m">8</span>
worker <spanclass="m">2</span> finished running G
worker <spanclass="m">3</span> finished running H</pre><p>It is expected each line of <ahref="http://en.cppreference.com/w/cpp/io/basic_ostream.html" class="m-doc-external">std::<wbr/>cout</a> interleaves with each other as there are four workers participating in task scheduling. However, the <em>ready</em> message always appears before the corresponding task message (e.g., numbers) and then the <em>finished</em> message.</p></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>