Skip to content

Commit c620856

Browse files
author
TSUNG-WEI HUANG
committed
tried fixing windows compiler error
1 parent eb7318e commit c620856

13 files changed

Lines changed: 84 additions & 158 deletions

docs/ExecuteTaskflow.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,16 @@ <h3>Contents</h3>
277277
<span class="m">7</span>
278278
<span class="m">8</span>
279279
worker <span class="m">2</span> finished running G
280-
worker <span class="m">3</span> finished running H</pre><p>It is expected each line of <a href="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><section id="ModifyWorkerProperty"><h2><a href="#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 <a href="classtf_1_1WorkerInterface.html" class="m-doc">tf::<wbr />WorkerInterface</a> to the executor. The example demonstrates the usage of <a href="classtf_1_1WorkerInterface.html" class="m-doc">tf::<wbr />WorkerInterface</a> and use <a href="namespacetf.html#aca023076d72224028bdfb5503011a8f5" class="m-doc">tf::<wbr />affine</a> to affine a worker to a specific CPU core equal to its id:</p><pre class="m-code"><span class="k">class</span><span class="w"> </span><span class="nc">CustomWorkerBehavior</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">WorkerInterface</span><span class="w"> </span><span class="p">{</span>
280+
worker <span class="m">3</span> finished running H</pre><p>It is expected each line of <a href="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><section id="ModifyWorkerProperty"><h2><a href="#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 <a href="classtf_1_1WorkerInterface.html" class="m-doc">tf::<wbr />WorkerInterface</a> to the executor. The example demonstrates the usage of <a href="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><pre class="m-code"><span class="c1">// affine the given thread to the given core index (linux-specific)</span>
281+
<span class="kt">bool</span><span class="w"> </span><span class="nf">affine</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="kr">thread</span><span class="o">&amp;</span><span class="w"> </span><span class="kr">thread</span><span class="p">,</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">core_id</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
282+
<span class="w"> </span><span class="kt">cpu_set_t</span><span class="w"> </span><span class="n">cpuset</span><span class="p">;</span>
283+
<span class="w"> </span><span class="n">CPU_ZERO</span><span class="p">(</span><span class="o">&amp;</span><span class="n">cpuset</span><span class="p">);</span>
284+
<span class="w"> </span><span class="n">CPU_SET</span><span class="p">(</span><span class="n">core_id</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">cpuset</span><span class="p">);</span>
285+
<span class="w"> </span><span class="n">pthread_t</span><span class="w"> </span><span class="n">native_handle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kr">thread</span><span class="p">.</span><span class="n">native_handle</span><span class="p">();</span>
286+
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">pthread_setaffinity_np</span><span class="p">(</span><span class="n">native_handle</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">cpu_set_t</span><span class="p">),</span><span class="w"> </span><span class="o">&amp;</span><span class="n">cpuset</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
287+
<span class="p">}</span>
288+
289+
<span class="k">class</span><span class="w"> </span><span class="nc">CustomWorkerBehavior</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">WorkerInterface</span><span class="w"> </span><span class="p">{</span>
281290

282291
<span class="w"> </span><span class="k">public</span><span class="o">:</span>
283292
<span class="w"> </span>
@@ -286,7 +295,7 @@ <h3>Contents</h3>
286295
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;worker %lu prepares to enter the work-stealing loop</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">w</span><span class="p">.</span><span class="n">id</span><span class="p">());</span>
287296
<span class="w"> </span>
288297
<span class="w"> </span><span class="c1">// now affine the worker to a particular CPU core equal to its id</span>
289-
<span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="n">tf</span><span class="o">::</span><span class="n">affine</span><span class="p">(</span><span class="n">w</span><span class="p">.</span><span class="kr">thread</span><span class="p">(),</span><span class="w"> </span><span class="n">w</span><span class="p">.</span><span class="n">id</span><span class="p">()))</span><span class="w"> </span><span class="p">{</span>
298+
<span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="n">affine</span><span class="p">(</span><span class="n">w</span><span class="p">.</span><span class="kr">thread</span><span class="p">(),</span><span class="w"> </span><span class="n">w</span><span class="p">.</span><span class="n">id</span><span class="p">()))</span><span class="w"> </span><span class="p">{</span>
290299
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;successfully affines worker %lu to CPU core %lu</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">w</span><span class="p">.</span><span class="n">id</span><span class="p">(),</span><span class="w"> </span><span class="n">w</span><span class="p">.</span><span class="n">id</span><span class="p">());</span>
291300
<span class="w"> </span><span class="p">}</span>
292301
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>

docs/namespacetf.html

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,6 @@ <h2><a href="#func-members">Functions</a></h2>
422422
<span class="m-doc-wrap-bumper">void <a href="#a3abe09ef55c4f46e64ba88bff175c4f6" class="m-doc">spin_until</a>(</span><span class="m-doc-wrap">P&amp;&amp; predicate)</span>
423423
</dt>
424424
<dd>spins until the given predicate becomes true</dd>
425-
<dt>
426-
<span class="m-doc-wrap-bumper">auto <a href="#aca023076d72224028bdfb5503011a8f5" class="m-doc">affine</a>(</span><span class="m-doc-wrap"><a href="http://en.cppreference.com/w/cpp/thread/thread.html" class="m-doc-external">std::<wbr />thread</a>&amp; thread,
427-
unsigned int core_id) -&gt; bool</span>
428-
</dt>
429-
<dd>affines the given thread to a specific CPU core</dd>
430425
<dt>
431426
<div class="m-doc-template">template&lt;typename T, typename... ArgsT&gt;</div>
432427
<span class="m-doc-wrap-bumper">auto <a href="#a97960776f3c78e18e68c79c336ba4f2d" class="m-doc">make_worker_interface</a>(</span><span class="m-doc-wrap">ArgsT &amp;&amp; ... args) -&gt; <a href="http://en.cppreference.com/w/cpp/memory/unique_ptr.html" class="m-doc-external">std::<wbr />unique_ptr</a>&lt;T&gt;</span>
@@ -1549,35 +1544,6 @@ <h3>
15491544
</tbody>
15501545
</table>
15511546
<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><a href="#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><a href="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><a href="#aef9af2433294beb7d41680e5b33eafdd" class="m-doc">pause()</a></code> to reduce power consumption during the spin-wait.</li><li>After 100 iterations, it uses <code><a href="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><aside class="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>
1552-
</div></section>
1553-
<section class="m-doc-details" id="aca023076d72224028bdfb5503011a8f5"><div>
1554-
<h3>
1555-
<span class="m-doc-wrap-bumper">bool tf::<wbr /></span><span class="m-doc-wrap"><span class="m-doc-wrap-bumper"><a href="#aca023076d72224028bdfb5503011a8f5" class="m-doc-self">affine</a>(</span><span class="m-doc-wrap"><a href="http://en.cppreference.com/w/cpp/thread/thread.html" class="m-doc-external">std::<wbr />thread</a>&amp; thread,
1556-
unsigned int core_id)</span></span>
1557-
</h3>
1558-
<p>affines the given thread to a specific CPU core</p>
1559-
<table class="m-table m-fullwidth m-flat">
1560-
<thead>
1561-
<tr><th colspan="2">Parameters</th></tr>
1562-
</thead>
1563-
<tbody>
1564-
<tr>
1565-
<td style="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><aside class="m-note m-warning"><h4>Attention</h4><p>You must ensure the specified core ID is within the valid range for the system&#x27;s CPU cores. Passing an invalid core ID may result in undefined behavior or runtime errors.</p></aside>
15811547
</div></section>
15821548
<section class="m-doc-details" id="a97960776f3c78e18e68c79c336ba4f2d"><div>
15831549
<h3>

0 commit comments

Comments
 (0)