3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Function accumulate</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.accumulate_hpp" title="Header <boost/compute/algorithm/accumulate.hpp>">
9 <link rel="prev" href="../../boost_compute/reference.html" title="Reference">
10 <link rel="next" href="adjacent_difference.html" title="Function adjacent_difference">
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>
22 <div class="spirit-nav">
23 <a accesskey="p" href="../../boost_compute/reference.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../boost_compute/reference.html#header.boost.compute.algorithm.accumulate_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="adjacent_difference.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
25 <div class="refentry">
26 <a name="boost.compute.accumulate"></a><div class="titlepage"></div>
27 <div class="refnamediv">
28 <h2><span class="refentrytitle">Function accumulate</span></h2>
29 <p>boost::compute::accumulate</p>
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.accumulate_hpp" title="Header <boost/compute/algorithm/accumulate.hpp>">boost/compute/algorithm/accumulate.hpp</a>>
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> T<span class="special">,</span> <span class="keyword">typename</span> BinaryFunction<span class="special">></span>
36 <span class="identifier">T</span> <span class="identifier">accumulate</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">T</span> init<span class="special">,</span>
37 <span class="identifier">BinaryFunction</span> function<span class="special">,</span>
38 <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>
39 <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> InputIterator<span class="special">,</span> <span class="keyword">typename</span> T<span class="special">></span>
40 <span class="identifier">T</span> <span class="identifier">accumulate</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">T</span> init<span class="special">,</span>
41 <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>
42 <div class="refsect1">
43 <a name="idp78564960"></a><h2>Description</h2>
44 <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>) and <code class="computeroutput">init</code>.</p>
45 <p>If no function is specified, <code class="computeroutput">plus</code> will be used.</p>
48 In specific situations the call to <code class="computeroutput">accumulate()</code> can be automatically optimized to a call to the more efficient <code class="computeroutput">reduce()</code> algorithm. This occurs when the binary reduction function is recognized as associative (such as the <code class="computeroutput">plus<int></code> function).</p>
49 <p>Note that because floating-point addition is not associative, calling <code class="computeroutput">accumulate()</code> with <code class="computeroutput">plus<float></code> results in a less efficient serial reduction algorithm being executed. If a slight loss in precision is acceptable, the more efficient parallel <code class="computeroutput">reduce()</code> algorithm should be used instead.</p>
51 <pre class="programlisting"><span class="comment">// with vec = boost::compute::vector<int></span>
52 <span class="identifier">accumulate</span><span class="special">(</span><span class="identifier">vec</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="identifier">vec</span><span class="special">.</span><span class="identifier">end</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">plus</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span><span class="special">(</span><span class="special">)</span><span class="special">)</span><span class="special">;</span> <span class="comment">// fast</span>
53 <span class="identifier">reduce</span><span class="special">(</span><span class="identifier">vec</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="identifier">vec</span><span class="special">.</span><span class="identifier">end</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="special">&</span><span class="identifier">result</span><span class="special">,</span> <span class="identifier">plus</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span><span class="special">(</span><span class="special">)</span><span class="special">)</span><span class="special">;</span> <span class="comment">// fast</span>
55 <span class="comment">// with vec = boost::compute::vector<float></span>
56 <span class="identifier">accumulate</span><span class="special">(</span><span class="identifier">vec</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="identifier">vec</span><span class="special">.</span><span class="identifier">end</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">plus</span><span class="special"><</span><span class="keyword">float</span><span class="special">></span><span class="special">(</span><span class="special">)</span><span class="special">)</span><span class="special">;</span> <span class="comment">// slow</span>
57 <span class="identifier">reduce</span><span class="special">(</span><span class="identifier">vec</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="identifier">vec</span><span class="special">.</span><span class="identifier">end</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="special">&</span><span class="identifier">result</span><span class="special">,</span> <span class="identifier">plus</span><span class="special"><</span><span class="keyword">float</span><span class="special">></span><span class="special">(</span><span class="special">)</span><span class="special">)</span><span class="special">;</span> <span class="comment">// fast</span>
59 <p>Space complexity: (1)<br>
60 Space complexity when optimized to <code class="computeroutput">reduce()</code>: (n)</p>
61 <p><span class="bold"><strong>See Also:</strong></span></p>
65 <div class="variablelist"><table border="0" class="variablelist compact">
67 <col align="left" valign="top">
72 <td><p><span class="term">Parameters:</span></p></td>
73 <td><div class="variablelist"><table border="0" class="variablelist compact">
75 <col align="left" valign="top">
80 <td><p><span class="term"><code class="computeroutput">first</code></span></p></td>
81 <td><p>first element in the input range </p></td>
84 <td><p><span class="term"><code class="computeroutput">function</code></span></p></td>
85 <td><p>binary reduction function </p></td>
88 <td><p><span class="term"><code class="computeroutput">init</code></span></p></td>
89 <td><p>initial value </p></td>
92 <td><p><span class="term"><code class="computeroutput">last</code></span></p></td>
93 <td><p>last element in the input range </p></td>
96 <td><p><span class="term"><code class="computeroutput">queue</code></span></p></td>
97 <td><p>command queue to perform the operation</p></td>
103 <td><p><span class="term">Returns:</span></p></td>
104 <td><p>the accumulated result value</p></td>
110 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
111 <td align="left"></td>
112 <td align="right"><div class="copyright-footer">Copyright © 2013, 2014 Kyle Lutz<p>
113 Distributed under the Boost Software License, Version 1.0. (See accompanying
114 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>)
119 <div class="spirit-nav">
120 <a accesskey="p" href="../../boost_compute/reference.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../boost_compute/reference.html#header.boost.compute.algorithm.accumulate_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="adjacent_difference.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>