<p>Taskflow provides standard template methods for performing parallel iterations over a range of items a CUDA GPU.</p><sectionid="CUDASTDParallelIterationIncludeTheHeader"><h2><ahref="#CUDASTDParallelIterationIncludeTheHeader">Include the Header</a></h2><p>You need to include the header file, <code>taskflow/cuda/algorithm/for_each.hpp</code>, for using the parallel-iteration algorithm.</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="w"></span><spanclass="cpf"><taskflow/cuda/algorithm/for_each.hpp></span></pre></section><sectionid="CUDASTDIndexBasedParallelFor"><h2><ahref="#CUDASTDIndexBasedParallelFor">Index-based Parallel Iterations</a></h2><p>Index-based parallel-for performs parallel iterations over a range <code>[first, last)</code> with the given <code>step</code> size. The task created by <ahref="namespacetf.html#a01ad7ce62fa6f42f2f2fbff3659b7884" class="m-doc">tf::<wbr/>cuda_for_each_index</a> represents a kernel of parallel execution for the following loop:</p><preclass="m-code"><spanclass="c1">// positive step: first, first+step, first+2*step, ...</span>
<spanclass="p">}</span></pre><p>Each iteration <code>i</code> is independent of each other and is assigned one kernel thread to run the callable. The following example creates a kernel that assigns each entry of <code>data</code> to 1 over the range [0, 100) with step size 1.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">cudaDefaultExecutionPolicy</span><spanclass="w"></span><spanclass="n">policy</span><spanclass="p">;</span>
<spanclass="c1">// synchronize the execution</span>
<spanclass="n">policy</span><spanclass="p">.</span><spanclass="n">synchronize</span><spanclass="p">();</span></pre><p>The parallel-iteration algorithm runs <em>asynchronously</em> through the stream specified in the execution policy. You need to synchronize the stream to obtain correct results.</p></section><sectionid="CUDASTDIteratorBasedParallelFor"><h2><ahref="#CUDASTDIteratorBasedParallelFor">Iterator-based Parallel Iterations</a></h2><p>Iterator-based parallel-for performs parallel iterations over a range specified by two STL-styled iterators, <code>first</code> and <code>last</code>. The task created by <ahref="namespacetf.html#a7c449cec0b93503b8280d05add35e9f4" class="m-doc">tf::<wbr/>cuda_for_each</a> represents a parallel execution of the following loop:</p><preclass="m-code"><spanclass="k">for</span><spanclass="p">(</span><spanclass="k">auto</span><spanclass="w"></span><spanclass="n">i</span><spanclass="o">=</span><spanclass="n">first</span><spanclass="p">;</span><spanclass="w"></span><spanclass="n">i</span><spanclass="o"><</span><spanclass="n">last</span><spanclass="p">;</span><spanclass="w"></span><spanclass="n">i</span><spanclass="o">++</span><spanclass="p">)</span><spanclass="w"></span><spanclass="p">{</span>
<spanclass="p">}</span></pre><p>The two iterators, <code>first</code> and <code>last</code>, are typically two raw pointers to the first element and the next to the last element in the range in GPU memory space. The following example creates a <code>for_each</code> kernel that assigns each element in <code>gpu_data</code> to 1 over the range <code>[data, data + 1000)</code>.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">cudaDefaultExecutionPolicy</span><spanclass="w"></span><spanclass="n">policy</span><spanclass="p">;</span>
<spanclass="c1">// synchronize the execution</span>
<spanclass="n">policy</span><spanclass="p">.</span><spanclass="n">synchronize</span><spanclass="p">();</span></pre><p>Each iteration is independent of each other and is assigned one kernel thread to run the callable. Since the callable runs on GPU, it must be declared with a <code>__device__</code> specifier.</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>