|
49 | 49 | <span class="m-breadcrumb"><a href="cudaStandardAlgorithms.html">CUDA Standard Algorithms</a> »</span>
|
50 | 50 | Execution Policy
|
51 | 51 | </h1>
|
52 |
| -<p>Taskflow provides standalone template methods for expressing common parallel algorithms on a GPU. Each of these methods is governed by an <em>execution policy object</em> to configure the kernel execution parameters.</p><section id="CUDASTDParameterizePerformance"><h2><a href="#CUDASTDParameterizePerformance">Parameterize Performance</a></h2><p>Taskflow parameterizes most CUDA algorithms in terms of <em>the number of threads per block</em> and <em>units of work per thread</em>, which can be specified in the execution policy template type, <a href="classtf_1_1cudaExecutionPolicy.html" class="m-doc">tf::<wbr />cudaExecutionPolicy</a>. The design is inspired by <a href="https://moderngpu.github.io/">Modern GPU Programming</a> authored by Sean Baxter to achieve high-performance GPU computing.</p></section><section id="CUDASTDDefineAnExecutionPolicy"><h2><a href="#CUDASTDDefineAnExecutionPolicy">Define an Execution Policy</a></h2><p>The following example defines an execution policy object, <code>policy</code>, which configures (1) each block to invoke 512 threads and (2) each of these <code>512</code> threads to perform <code>11</code> units of work. Block size must be a power of two. It is always a good idea to specify an odd number in the second parameter to avoid bank conflicts.</p><pre class="m-code"><span class="cp">#include</span> <span class="cpf"><taskflow/cudaflow.hpp></span><span class="cp"></span> |
| 52 | + <div class="m-block m-default"> |
| 53 | + <h3>Contents</h3> |
| 54 | + <ul> |
| 55 | + <li><a href="#CUDASTDParameterizePerformance">Parameterize Performance</a></li> |
| 56 | + <li><a href="#CUDASTDDefineAnExecutionPolicy">Define an Execution Policy</a></li> |
| 57 | + <li><a href="#CUDASTDAllocateMemoryBufferForAlgorithms">Allocate Memory Buffer for Algorithms</a></li> |
| 58 | + </ul> |
| 59 | + </div> |
| 60 | +<p>Taskflow provides standalone template methods for expressing common parallel algorithms on a GPU. Each of these methods is governed by an <em>execution policy object</em> to configure the kernel execution parameters.</p><section id="CUDASTDParameterizePerformance"><h2><a href="#CUDASTDParameterizePerformance">Parameterize Performance</a></h2><p>Taskflow parameterizes most CUDA algorithms in terms of <em>the number of threads per block</em> and <em>units of work per thread</em>, which can be specified in the execution policy template type, <a href="classtf_1_1cudaExecutionPolicy.html" class="m-doc">tf::<wbr />cudaExecutionPolicy</a>. The design is inspired by <a href="https://moderngpu.github.io/">Modern GPU Programming</a> authored by Sean Baxter to achieve high-performance GPU computing.</p></section><section id="CUDASTDDefineAnExecutionPolicy"><h2><a href="#CUDASTDDefineAnExecutionPolicy">Define an Execution Policy</a></h2><p>The following example defines an execution policy object, <code>policy</code>, which configures (1) each block to invoke 512 threads and (2) each of these <code>512</code> threads to perform <code>11</code> units of work. Block size must be a power of two. It is always a good idea to specify an odd number in the second parameter to avoid bank conflicts.</p><pre class="m-code"><span class="cp">#include</span><span class="w"> </span><span class="cpf"><taskflow/cudaflow.hpp></span><span class="cp"></span> |
53 | 61 |
|
54 |
| -<span class="n">tf</span><span class="o">::</span><span class="n">cudaExecutionPolicy</span><span class="o"><</span><span class="mi">512</span><span class="p">,</span> <span class="mi">11</span><span class="o">></span> <span class="n">policy</span><span class="p">;</span></pre><aside class="m-note m-info"><h4>Note</h4><p>To use CUDA standard algorithms, you need to include the header <a href="cudaflow_8hpp.html" class="m-doc">taskflow/<wbr />cudaflow.hpp</a>.</p></aside><p>By default, the execution policy object is associated with the CUDA <em>default stream</em> (i.e., 0). Default stream can incur significant overhead due to the global synchronization. You can associate an execution policy with another stream as shown below:</p><pre class="m-code"><span class="c1">// assign a stream to a policy at construction time</span> |
55 |
| -<span class="n">tf</span><span class="o">::</span><span class="n">cudaExecutionPolicy</span><span class="o"><</span><span class="mi">512</span><span class="p">,</span> <span class="mi">11</span><span class="o">></span> <span class="n">policy</span><span class="p">(</span><span class="n">my_stream</span><span class="p">);</span> |
| 62 | +<span class="n">tf</span><span class="o">::</span><span class="n">cudaExecutionPolicy</span><span class="o"><</span><span class="mi">512</span><span class="p">,</span><span class="w"> </span><span class="mi">11</span><span class="o">></span><span class="w"> </span><span class="n">policy</span><span class="p">;</span><span class="w"></span></pre><aside class="m-note m-info"><h4>Note</h4><p>To use CUDA standard algorithms, you need to include the header <a href="cudaflow_8hpp.html" class="m-doc">taskflow/<wbr />cudaflow.hpp</a>.</p></aside><p>By default, the execution policy object is associated with the CUDA <em>default stream</em> (i.e., 0). Default stream can incur significant overhead due to the global synchronization. You can associate an execution policy with another stream as shown below:</p><pre class="m-code"><span class="c1">// assign a stream to a policy at construction time</span> |
| 63 | +<span class="n">tf</span><span class="o">::</span><span class="n">cudaExecutionPolicy</span><span class="o"><</span><span class="mi">512</span><span class="p">,</span><span class="w"> </span><span class="mi">11</span><span class="o">></span><span class="w"> </span><span class="n">policy</span><span class="p">(</span><span class="n">my_stream</span><span class="p">);</span><span class="w"></span> |
56 | 64 |
|
57 | 65 | <span class="c1">// assign another stream to the policy</span>
|
58 |
| -<span class="n">policy</span><span class="p">.</span><span class="n">stream</span><span class="p">(</span><span class="n">another_stream</span><span class="p">);</span></pre><p>All the CUDA standard algorithms in Taskflow are asynchronous with respect to the stream assigned to the execution policy. This enables high execution efficiency for large GPU workloads that call for many different algorithms. You can synchronize the execution at your own wish by calling <code>synchronize</code>.</p><pre class="m-code"><span class="n">policy</span><span class="p">.</span><span class="n">synchronize</span><span class="p">();</span> <span class="c1">// synchronize the associated stream</span></pre><p>The best-performing configurations for each algorithm, each GPU architecture, and each data type can vary significantly. You should experiment different configurations and find the optimal tuning parameters for your applications. A default policy is given in <a href="namespacetf.html#aa18f102977c3257b75e21fde05efdb68" class="m-doc">tf::<wbr />cudaDefaultExecutionPolicy</a>.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">cudaDefaultExecutionPolicy</span> <span class="n">default_policy</span><span class="p">;</span></pre></section><section id="CUDASTDAllocateMemoryBufferForAlgorithms"><h2><a href="#CUDASTDAllocateMemoryBufferForAlgorithms">Allocate Memory Buffer for Algorithms</a></h2><p>A key difference between our CUDA standard algorithms and others (e.g., Thrust) is the <em>memory management</em>. Unlike CPU-parallel algorithms, many GPU-parallel algorithms require extra buffer to store the temporary results during the multi-phase computation, for instance, <a href="namespacetf.html#a8a872d2a0ac73a676713cb5be5aa688c" class="m-doc">tf::<wbr />cuda_reduce</a> and <a href="namespacetf.html#a06804cb1598e965febc7bd35fc0fbbb0" class="m-doc">tf::<wbr />cuda_sort</a>. We <em>DO NOT</em> allocate any memory during these algorithms call but ask you to provide the memory buffer required for each of such algorithms. This decision seems to complicate the code a little bit, but it gives applications freedom to optimize the memory; also, it makes all algorithm calls capturable to a CUDA graph to improve the execution efficiency.</p></section> |
| 66 | +<span class="n">policy</span><span class="p">.</span><span class="n">stream</span><span class="p">(</span><span class="n">another_stream</span><span class="p">);</span><span class="w"></span></pre><p>All the CUDA standard algorithms in Taskflow are asynchronous with respect to the stream assigned to the execution policy. This enables high execution efficiency for large GPU workloads that call for many different algorithms. You can synchronize the execution at your own wish by calling <code>synchronize</code>.</p><pre class="m-code"><span class="n">policy</span><span class="p">.</span><span class="n">synchronize</span><span class="p">();</span><span class="w"> </span><span class="c1">// synchronize the associated stream</span></pre><p>The best-performing configurations for each algorithm, each GPU architecture, and each data type can vary significantly. You should experiment different configurations and find the optimal tuning parameters for your applications. A default policy is given in <a href="namespacetf.html#aa18f102977c3257b75e21fde05efdb68" class="m-doc">tf::<wbr />cudaDefaultExecutionPolicy</a>.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">cudaDefaultExecutionPolicy</span><span class="w"> </span><span class="n">default_policy</span><span class="p">;</span><span class="w"></span></pre></section><section id="CUDASTDAllocateMemoryBufferForAlgorithms"><h2><a href="#CUDASTDAllocateMemoryBufferForAlgorithms">Allocate Memory Buffer for Algorithms</a></h2><p>A key difference between our CUDA standard algorithms and others (e.g., Thrust) is the <em>memory management</em>. Unlike CPU-parallel algorithms, many GPU-parallel algorithms require extra buffer to store the temporary results during the multi-phase computation, for instance, <a href="namespacetf.html#a8a872d2a0ac73a676713cb5be5aa688c" class="m-doc">tf::<wbr />cuda_reduce</a> and <a href="namespacetf.html#a06804cb1598e965febc7bd35fc0fbbb0" class="m-doc">tf::<wbr />cuda_sort</a>. We <em>DO NOT</em> allocate any memory during these algorithms call but ask you to provide the memory buffer required for each of such algorithms. This decision seems to complicate the code a little bit, but it gives applications freedom to optimize the memory; also, it makes all algorithm calls capturable to a CUDA graph to improve the execution efficiency.</p></section> |
59 | 67 | </div>
|
60 | 68 | </div>
|
61 | 69 | </div>
|
|
100 | 108 | <div class="m-container">
|
101 | 109 | <div class="m-row">
|
102 | 110 | <div class="m-col-l-10 m-push-l-1">
|
103 |
| - <p>Taskflow handbook is part of the <a href="https://taskflow.github.io">Taskflow project</a>, copyright © <a href="https://tsung-wei-huang.github.io/">Dr. Tsung-Wei Huang</a>, 2018–2021.<br />Generated by <a href="https://doxygen.org/">Doxygen</a> 1.8.13 and <a href="https://mcss.mosra.cz/">m.css</a>.</p> |
| 111 | + <p>Taskflow handbook is part of the <a href="https://taskflow.github.io">Taskflow project</a>, copyright © <a href="https://tsung-wei-huang.github.io/">Dr. Tsung-Wei Huang</a>, 2018–2021.<br />Generated by <a href="https://doxygen.org/">Doxygen</a> 1.8.14 and <a href="https://mcss.mosra.cz/">m.css</a>.</p> |
104 | 112 | </div>
|
105 | 113 | </div>
|
106 | 114 | </div>
|
|
0 commit comments