<spanclass="m-breadcrumb"><ahref="cudaStandardAlgorithms.html">CUDA Standard Algorithms</a> »</span>
Parallel Reduction
</h1>
<navclass="m-block m-default">
<h3>Contents</h3>
<ul>
<li><ahref="#CUDASTDParallelReductionIncludeTheHeader">Include the Header</a></li>
<li><ahref="#CUDASTDReduceItemsWithAnInitialValue">Reduce a Range of Items with an Initial Value</a></li>
<li><ahref="#CUDASTDReduceItemsWithoutAnInitialValue">Reduce a Range of Items without an Initial Value</a></li>
<li><ahref="#CUDASTDReduceTransformedItemsWithAnInitialValue">Reduce a Range of Transformed Items with an Initial Value</a></li>
<li><ahref="#CUDASTDReduceTransformedItemsWithoutAnInitialValue">Reduce a Range of Transformed Items without an Initial Value</a></li>
</ul>
</nav>
<p>Taskflow provides standard template methods for reducing a range of items on a CUDA GPU.</p><sectionid="CUDASTDParallelReductionIncludeTheHeader"><h2><ahref="#CUDASTDParallelReductionIncludeTheHeader">Include the Header</a></h2><p>You need to include the header file, <code>taskflow/cuda/algorithm/reduce.hpp</code>, for using the parallel-reduction algorithm.</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="w"></span><spanclass="cpf"><taskflow/cuda/algorithm/reduce.hpp></span></pre></section><sectionid="CUDASTDReduceItemsWithAnInitialValue"><h2><ahref="#CUDASTDReduceItemsWithAnInitialValue">Reduce a Range of Items with an Initial Value</a></h2><p><ahref="namespacetf.html#a8a872d2a0ac73a676713cb5be5aa688c" class="m-doc">tf::<wbr/>cuda_reduce</a> performs a parallel reduction over a range of elements specified by <code>[first, last)</code> using the binary operator <code>bop</code> and stores the reduced result in <code>result</code>. It represents the parallel execution of the following reduction loop on a GPU:</p><preclass="m-code"><spanclass="k">while</span><spanclass="w"></span><spanclass="p">(</span><spanclass="n">first</span><spanclass="w"></span><spanclass="o">!=</span><spanclass="w"></span><spanclass="n">last</span><spanclass="p">)</span><spanclass="w"></span><spanclass="p">{</span>
<spanclass="p">}</span></pre><p>The variable <code>result</code> participates in the reduction loop and must be initialized with an initial value. The following code performs a parallel reduction to sum all the numbers in the given range with an initial value <code>1000</code>:</p><preclass="m-code"><spanclass="k">const</span><spanclass="w"></span><spanclass="kt">size_t</span><spanclass="w"></span><spanclass="n">N</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="mi">1000000</span><spanclass="p">;</span>
<spanclass="n">cudaFree</span><spanclass="p">(</span><spanclass="n">vec</span><spanclass="p">);</span></pre><p>The reduce algorithm runs <em>asynchronously</em> through the stream specified in the execution policy. You need to synchronize the stream to obtain correct results. Since the GPU reduction algorithm may require extra buffer to store the temporary results, you need to provide a buffer of size at least larger or equal to the value returned from <code><ahref="classtf_1_1cudaExecutionPolicy.html#a446cee95bb839ee180052059e2ad7fd6" class="m-doc">tf::<wbr/>cudaDefaultExecutionPolicy::<wbr/>reduce_bufsz</a></code>.</p><asideclass="m-note m-warning"><h4>Attention</h4><p>You must keep the buffer alive before the reduction completes.</p></aside></section><sectionid="CUDASTDReduceItemsWithoutAnInitialValue"><h2><ahref="#CUDASTDReduceItemsWithoutAnInitialValue">Reduce a Range of Items without an Initial Value</a></h2><p><ahref="namespacetf.html#a492e8410db032a0273a99dd905486161" class="m-doc">tf::<wbr/>cuda_uninitialized_reduce</a> performs a parallel reduction over a range of items without an initial value. This method represents a parallel execution of the following reduction loop on a GPU:</p><preclass="m-code"><spanclass="o">*</span><spanclass="n">result</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="o">*</span><spanclass="n">first</span><spanclass="o">++</span><spanclass="p">;</span><spanclass="w"></span><spanclass="c1">// no initial values to participate in the reduction loop</span>
<spanclass="p">}</span></pre><p>The variable <code>result</code> is directly assigned the reduced value without any initial value participating in the reduction loop. The following code performs a parallel reduction to sum all the numbers in the given range without any initial value:</p><preclass="m-code"><spanclass="k">const</span><spanclass="w"></span><spanclass="kt">size_t</span><spanclass="w"></span><spanclass="n">N</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="mi">1000000</span><spanclass="p">;</span>
<spanclass="n">cudaFree</span><spanclass="p">(</span><spanclass="n">buffer</span><spanclass="p">);</span></pre></section><sectionid="CUDASTDReduceTransformedItemsWithAnInitialValue"><h2><ahref="#CUDASTDReduceTransformedItemsWithAnInitialValue">Reduce a Range of Transformed Items with an Initial Value</a></h2><p><ahref="namespacetf.html#a4463d06240d608bc31d8b3546a851e4e" class="m-doc">tf::<wbr/>cuda_transform_reduce</a> performs a parallel reduction over a range of <em>transformed</em> elements specified by <code>[first, last)</code> using a binary reduce operator <code>bop</code> and a unary transform operator <code>uop</code>. It represents the parallel execution of the following reduction loop on a GPU:</p><preclass="m-code"><spanclass="k">while</span><spanclass="w"></span><spanclass="p">(</span><spanclass="n">first</span><spanclass="w"></span><spanclass="o">!=</span><spanclass="w"></span><spanclass="n">last</span><spanclass="p">)</span><spanclass="w"></span><spanclass="p">{</span>
<spanclass="p">}</span></pre><p>The variable <code>result</code> participates in the reduction loop and must be initialized with an initial value. The following code performs a parallel reduction to sum all the transformed numbers multiplied by <code>10</code> in the given range with an initial value <code>1000</code>:</p><preclass="m-code"><spanclass="k">const</span><spanclass="w"></span><spanclass="kt">size_t</span><spanclass="w"></span><spanclass="n">N</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="mi">1000000</span><spanclass="p">;</span>
<spanclass="n">cudaFree</span><spanclass="p">(</span><spanclass="n">buffer</span><spanclass="p">);</span></pre></section><sectionid="CUDASTDReduceTransformedItemsWithoutAnInitialValue"><h2><ahref="#CUDASTDReduceTransformedItemsWithoutAnInitialValue">Reduce a Range of Transformed Items without an Initial Value</a></h2><p>tf::cuda_transform_uninitialized_reduce performs a parallel reduction over a range of transformed items without an initial value. This method represents a parallel execution of the following reduction loop on a GPU:</p><preclass="m-code"><spanclass="o">*</span><spanclass="n">result</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="o">*</span><spanclass="n">first</span><spanclass="o">++</span><spanclass="p">;</span><spanclass="w"></span><spanclass="c1">// no initial values to participate in the reduction loop</span>
<spanclass="p">}</span></pre><p>The variable <code>result</code> is directly assigned the reduced value without any initial value participating in the reduction loop. The following code performs a parallel reduction to sum all the transformed numbers multiplied by <code>10</code> in the given range without any initial value:</p><preclass="m-code"><spanclass="k">const</span><spanclass="w"></span><spanclass="kt">size_t</span><spanclass="w"></span><spanclass="n">N</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="mi">1000000</span><spanclass="p">;</span>