<li><ahref="#SYCLReduceItemsWithAnInitialValue">Reduce Items with an Initial Value</a></li>
<li><ahref="#SYCLReduceItemsWithoutAnInitialValue">Reduce Items without an Initial Value</a></li>
</ul>
</div>
<p><ahref="classtf_1_1syclFlow.html" class="m-doc">tf::<wbr/>syclFlow</a> provides two template methods, <ahref="classtf_1_1syclFlow.html#acddd05832e53597ae9d30dc913d422ca" class="m-doc">tf::<wbr/>syclFlow::<wbr/>reduce</a> and <ahref="classtf_1_1syclFlow.html#a7a3af92f105114b1095a2cec2ccdffbf" class="m-doc">tf::<wbr/>syclFlow::<wbr/>uninitialized_reduce</a>, for creating tasks to perform parallel reductions over a range of items.</p><sectionid="SYCLReduceItemsWithAnInitialValue"><h2><ahref="#SYCLReduceItemsWithAnInitialValue">Reduce Items with an Initial Value</a></h2><p>The reduction task created by <ahref="classtf_1_1syclFlow.html#acddd05832e53597ae9d30dc913d422ca" class="m-doc">tf::<wbr/>syclFlow::<wbr/>reduce(I first, I last, T* result, C&& bop)</a> performs 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 SYCL device:</p><preclass="m-code"><spanclass="k">while</span><spanclass="p">(</span><spanclass="n">first</span><spanclass="o">!=</span><spanclass="n">last</span><spanclass="p">)</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="kt">size_t</span><spanclass="n">N</span><spanclass="o">=</span><spanclass="mi">1000000</span><spanclass="p">;</span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">sol</span><spanclass="o">==</span><spanclass="n">N</span><spanclass="o">+</span><spanclass="mi">1000</span><spanclass="p">);</span></pre></section><sectionid="SYCLReduceItemsWithoutAnInitialValue"><h2><ahref="#SYCLReduceItemsWithoutAnInitialValue">Reduce Items without an Initial Value</a></h2><p>You can use <ahref="classtf_1_1syclFlow.html#a7a3af92f105114b1095a2cec2ccdffbf" class="m-doc">tf::<wbr/>syclFlow::<wbr/>uninitialized_reduce</a> to perform parallel reduction without any initial value. This method represents a parallel execution of the following reduction loop on a SYCL device that does not assum any initial value to reduce.</p><preclass="m-code"><spanclass="o">*</span><spanclass="n">result</span><spanclass="o">=</span><spanclass="o">*</span><spanclass="n">first</span><spanclass="o">++</span><spanclass="p">;</span><spanclass="c1">// no initial values participate in the reduction loop</span>
<spanclass="p">}</span></pre><p>The variable <code>result</code> is overwritten with the reduced value and no initial values participate 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="kt">size_t</span><spanclass="n">N</span><spanclass="o">=</span><spanclass="mi">1000000</span><spanclass="p">;</span>
<spanclass="kt">int</span><spanclass="o">*</span><spanclass="n">data</span><spanclass="o">=</span><spanclass="n">sycl</span><spanclass="o">::</span><spanclass="n">malloc_shared</span><spanclass="o"><</span><spanclass="kt">int</span><spanclass="o">></span><spanclass="p">(</span><spanclass="n">N</span><spanclass="p">);</span><spanclass="c1">// data </span>
<spanclass="o">*</span><spanclass="n">soln</span><spanclass="o">=</span><spanclass="mi">1000</span><spanclass="p">;</span><spanclass="c1">// no effect</span>
<spanclass="c1">// create a syclflow to perform parallel reduction on a SYCL device</span>