You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ExecuteTaskflow.html
+11-2Lines changed: 11 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -277,7 +277,16 @@ <h3>Contents</h3>
277
277
<spanclass="m">7</span>
278
278
<spanclass="m">8</span>
279
279
worker <spanclass="m">2</span> finished running G
280
-
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><sectionid="ModifyWorkerProperty"><h2><ahref="#ModifyWorkerProperty">Modify Worker Property</a></h2><p>You can change the property of each worker thread from its executor, such as assigning thread-processor affinity before the worker enters the scheduler loop and post-processing additional information after the worker leaves the scheduler loop, by passing an instance derived from <ahref="classtf_1_1WorkerInterface.html" class="m-doc">tf::<wbr/>WorkerInterface</a> to the executor. The example demonstrates the usage of <ahref="classtf_1_1WorkerInterface.html" class="m-doc">tf::<wbr/>WorkerInterface</a> and use <ahref="namespacetf.html#aca023076d72224028bdfb5503011a8f5" class="m-doc">tf::<wbr/>affine</a> to affine a worker to a specific CPU core equal to its id:</p><preclass="m-code"><spanclass="k">class</span><spanclass="w"></span><spanclass="nc">CustomWorkerBehavior</span><spanclass="w"></span><spanclass="o">:</span><spanclass="w"></span><spanclass="k">public</span><spanclass="w"></span><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">WorkerInterface</span><spanclass="w"></span><spanclass="p">{</span>
280
+
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><sectionid="ModifyWorkerProperty"><h2><ahref="#ModifyWorkerProperty">Modify Worker Property</a></h2><p>You can change the property of each worker thread from its executor, such as assigning thread-processor affinity before the worker enters the scheduler loop and post-processing additional information after the worker leaves the scheduler loop, by passing an instance derived from <ahref="classtf_1_1WorkerInterface.html" class="m-doc">tf::<wbr/>WorkerInterface</a> to the executor. The example demonstrates the usage of <ahref="classtf_1_1WorkerInterface.html" class="m-doc">tf::<wbr/>WorkerInterface</a> to affine a worker to a specific CPU core equal to its id on a linux platform:</p><preclass="m-code"><spanclass="c1">// affine the given thread to the given core index (linux-specific)</span>
<spanclass="w"></span><spanclass="n">printf</span><spanclass="p">(</span><spanclass="s">"worker %lu prepares to enter the work-stealing loop</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">w</span><spanclass="p">.</span><spanclass="n">id</span><spanclass="p">());</span>
287
296
<spanclass="w"></span>
288
297
<spanclass="w"></span><spanclass="c1">// now affine the worker to a particular CPU core equal to its id</span>
<spanclass="w"></span><spanclass="n">printf</span><spanclass="p">(</span><spanclass="s">"successfully affines worker %lu to CPU core %lu</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">w</span><spanclass="p">.</span><spanclass="n">id</span><spanclass="p">(),</span><spanclass="w"></span><spanclass="n">w</span><spanclass="p">.</span><spanclass="n">id</span><spanclass="p">());</span>
<p>This function repeatedly checks the provided predicate in a spin-wait loop and uses a backoff strategy to minimize CPU waste during the wait. Initially, it uses the <code><ahref="#aef9af2433294beb7d41680e5b33eafdd" class="m-doc">pause()</a></code> instruction for the first 100 iterations to hint to the CPU that the thread is waiting, thus reducing power consumption and avoiding unnecessary cycles. After 100 iterations, it switches to yielding the CPU using <code><ahref="http://en.cppreference.com/w/cpp/thread/yield.html" class="m-doc-external">std::<wbr/>this_thread::<wbr/>yield()</a></code> to allow other threads to run and improve system responsiveness.</p><p>The function operates as follows:</p><ol><li>For the first 100 iterations, it invokes <code><ahref="#aef9af2433294beb7d41680e5b33eafdd" class="m-doc">pause()</a></code> to reduce power consumption during the spin-wait.</li><li>After 100 iterations, it uses <code><ahref="http://en.cppreference.com/w/cpp/thread/yield.html" class="m-doc-external">std::<wbr/>this_thread::<wbr/>yield()</a></code> to relinquish the CPU, allowing other threads to execute.</li></ol><asideclass="m-note m-warning"><h4>Attention</h4><p>This function is useful when you need to wait for a condition to be true, but want to optimize CPU usage during the wait by using a busy-wait approach.</p></aside>
<p>affines the given thread to a specific CPU core</p>
1559
-
<tableclass="m-table m-fullwidth m-flat">
1560
-
<thead>
1561
-
<tr><thcolspan="2">Parameters</th></tr>
1562
-
</thead>
1563
-
<tbody>
1564
-
<tr>
1565
-
<tdstyle="width: 1%">thread</td>
1566
-
<td>the given thread</td>
1567
-
</tr>
1568
-
<tr>
1569
-
<td>core_id</td>
1570
-
<td>index of the CPU core to which the thread will be bound.</td>
1571
-
</tr>
1572
-
</tbody>
1573
-
<tfoot>
1574
-
<tr>
1575
-
<th>Returns</th>
1576
-
<td><code>true</code> if the binding was successful; <code>false</code> otherwise.</td>
1577
-
</tr>
1578
-
</tfoot>
1579
-
</table>
1580
-
<p>This function sets the thread affinity, restricting the thread to run on the specified CPU core. It provides a portable implementation that supports various platforms, including Linux, macOS, and Windows.</p><p>Affining a thread to a specific core can improve performance by reducing cache misses and increasing locality for CPU-intensive tasks. However, misuse may lead to suboptimal performance in systems with dynamic workloads or varying resource availability.</p><asideclass="m-note m-warning"><h4>Attention</h4><p>You must ensure the specified core ID is within the valid range for the system's CPU cores. Passing an invalid core ID may result in undefined behavior or runtime errors.</p></aside>
0 commit comments