1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Function reduce</title> 5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Compute"> 8<link rel="up" href="../../boost_compute/reference.html#header.boost.compute.algorithm.reduce_hpp" title="Header <boost/compute/algorithm/reduce.hpp>"> 9<link rel="prev" href="random_shuffle.html" title="Function template random_shuffle"> 10<link rel="next" href="reduce_by_key.html" title="Function reduce_by_key"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="random_shuffle.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../boost_compute/reference.html#header.boost.compute.algorithm.reduce_hpp"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reduce_by_key.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="refentry"> 26<a name="boost.compute.reduce"></a><div class="titlepage"></div> 27<div class="refnamediv"> 28<h2><span class="refentrytitle">Function reduce</span></h2> 29<p>boost::compute::reduce</p> 30</div> 31<h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2> 32<div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv"><pre class="synopsis"><span class="comment">// In header: <<a class="link" href="../../boost_compute/reference.html#header.boost.compute.algorithm.reduce_hpp" title="Header <boost/compute/algorithm/reduce.hpp>">boost/compute/algorithm/reduce.hpp</a>> 33 34</span> 35<span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> InputIterator<span class="special">,</span> <span class="keyword">typename</span> OutputIterator<span class="special">,</span> 36 <span class="keyword">typename</span> BinaryFunction<span class="special">></span> 37 <span class="keyword">void</span> <span class="identifier">reduce</span><span class="special">(</span><span class="identifier">InputIterator</span> first<span class="special">,</span> <span class="identifier">InputIterator</span> last<span class="special">,</span> <span class="identifier">OutputIterator</span> result<span class="special">,</span> 38 <span class="identifier">BinaryFunction</span> function<span class="special">,</span> 39 <span class="identifier">command_queue</span> <span class="special">&</span> queue <span class="special">=</span> <span class="identifier">system</span><span class="special">::</span><span class="identifier">default_queue</span><span class="special">(</span><span class="special">)</span><span class="special">)</span><span class="special">;</span> 40<span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> InputIterator<span class="special">,</span> <span class="keyword">typename</span> OutputIterator<span class="special">></span> 41 <span class="keyword">void</span> <span class="identifier">reduce</span><span class="special">(</span><span class="identifier">InputIterator</span> first<span class="special">,</span> <span class="identifier">InputIterator</span> last<span class="special">,</span> <span class="identifier">OutputIterator</span> result<span class="special">,</span> 42 <span class="identifier">command_queue</span> <span class="special">&</span> queue <span class="special">=</span> <span class="identifier">system</span><span class="special">::</span><span class="identifier">default_queue</span><span class="special">(</span><span class="special">)</span><span class="special">)</span><span class="special">;</span></pre></div> 43<div class="refsect1"> 44<a name="idm45849519975888"></a><h2>Description</h2> 45<p>Returns the result of applying <code class="computeroutput">function</code> to the elements in the range [<code class="computeroutput">first</code>, <code class="computeroutput">last</code>).</p> 46<p>If no function is specified, <code class="computeroutput">plus</code> will be used.</p> 47<p> 48The <code class="computeroutput">reduce()</code> algorithm assumes that the binary reduction function is associative. When used with non-associative functions the result may be non-deterministic and vary in precision. Notably this affects the <code class="computeroutput">plus<float>()</code> function as floating-point addition is not associative and may produce slightly different results than a serial algorithm.</p> 49<p>This algorithm supports both host and device iterators for the result argument. This allows for values to be reduced and copied to the host all with a single function call.</p> 50<p>For example, to calculate the sum of the values in a device vector and copy the result to a value on the host:</p> 51<pre class="programlisting"></pre> 52<p> Note that while the the <code class="computeroutput">reduce()</code> algorithm is conceptually identical to the <code class="computeroutput">accumulate()</code> algorithm, its implementation is substantially more efficient on parallel hardware. For more information, see the documentation on the <code class="computeroutput">accumulate()</code> algorithm.</p> 53<p>Space complexity on GPUs: \Omega(n)<br> 54 Space complexity on CPUs: \Omega(1)</p> 55<p><span class="bold"><strong>See Also:</strong></span></p> 56<p>accumulate() </p> 57<p> 58</p> 59<p> 60</p> 61<div class="variablelist"><table border="0" class="variablelist compact"> 62<colgroup> 63<col align="left" valign="top"> 64<col> 65</colgroup> 66<tbody><tr> 67<td><p><span class="term">Parameters:</span></p></td> 68<td><div class="variablelist"><table border="0" class="variablelist compact"> 69<colgroup> 70<col align="left" valign="top"> 71<col> 72</colgroup> 73<tbody> 74<tr> 75<td><p><span class="term"><code class="computeroutput">first</code></span></p></td> 76<td><p>first element in the input range </p></td> 77</tr> 78<tr> 79<td><p><span class="term"><code class="computeroutput">function</code></span></p></td> 80<td><p>binary reduction function </p></td> 81</tr> 82<tr> 83<td><p><span class="term"><code class="computeroutput">last</code></span></p></td> 84<td><p>last element in the input range </p></td> 85</tr> 86<tr> 87<td><p><span class="term"><code class="computeroutput">queue</code></span></p></td> 88<td><p>command queue to perform the operation</p></td> 89</tr> 90<tr> 91<td><p><span class="term"><code class="computeroutput">result</code></span></p></td> 92<td><p>iterator pointing to the output </p></td> 93</tr> 94</tbody> 95</table></div></td> 96</tr></tbody> 97</table></div> 98</div> 99</div> 100<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 101<td align="left"></td> 102<td align="right"><div class="copyright-footer">Copyright © 2013, 2014 Kyle Lutz<p> 103 Distributed under the Boost Software License, Version 1.0. (See accompanying 104 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 105 </p> 106</div></td> 107</tr></table> 108<hr> 109<div class="spirit-nav"> 110<a accesskey="p" href="random_shuffle.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../boost_compute/reference.html#header.boost.compute.algorithm.reduce_hpp"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reduce_by_key.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 111</div> 112</body> 113</html> 114