1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Function reduce_by_key</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_by_key_hpp" title="Header <boost/compute/algorithm/reduce_by_key.hpp>"> 9<link rel="prev" href="reduce.html" title="Function reduce"> 10<link rel="next" href="remove.html" title="Function template remove"> 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="reduce.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../boost_compute/reference.html#header.boost.compute.algorithm.reduce_by_key_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="remove.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="refentry"> 26<a name="boost.compute.reduce_by_key"></a><div class="titlepage"></div> 27<div class="refnamediv"> 28<h2><span class="refentrytitle">Function reduce_by_key</span></h2> 29<p>boost::compute::reduce_by_key</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_by_key_hpp" title="Header <boost/compute/algorithm/reduce_by_key.hpp>">boost/compute/algorithm/reduce_by_key.hpp</a>> 33 34</span> 35<span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> InputKeyIterator<span class="special">,</span> <span class="keyword">typename</span> InputValueIterator<span class="special">,</span> 36 <span class="keyword">typename</span> OutputKeyIterator<span class="special">,</span> <span class="keyword">typename</span> OutputValueIterator<span class="special">,</span> 37 <span class="keyword">typename</span> BinaryFunction<span class="special">,</span> <span class="keyword">typename</span> BinaryPredicate<span class="special">></span> 38 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span> <span class="identifier">OutputKeyIterator</span><span class="special">,</span> <span class="identifier">OutputValueIterator</span> <span class="special">></span> 39 <span class="identifier">reduce_by_key</span><span class="special">(</span><span class="identifier">InputKeyIterator</span> keys_first<span class="special">,</span> <span class="identifier">InputKeyIterator</span> keys_last<span class="special">,</span> 40 <span class="identifier">InputValueIterator</span> values_first<span class="special">,</span> 41 <span class="identifier">OutputKeyIterator</span> keys_result<span class="special">,</span> 42 <span class="identifier">OutputValueIterator</span> values_result<span class="special">,</span> <span class="identifier">BinaryFunction</span> function<span class="special">,</span> 43 <span class="identifier">BinaryPredicate</span> predicate<span class="special">,</span> 44 <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> 45<span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> InputKeyIterator<span class="special">,</span> <span class="keyword">typename</span> InputValueIterator<span class="special">,</span> 46 <span class="keyword">typename</span> OutputKeyIterator<span class="special">,</span> <span class="keyword">typename</span> OutputValueIterator<span class="special">,</span> 47 <span class="keyword">typename</span> BinaryFunction<span class="special">></span> 48 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span> <span class="identifier">OutputKeyIterator</span><span class="special">,</span> <span class="identifier">OutputValueIterator</span> <span class="special">></span> 49 <span class="identifier">reduce_by_key</span><span class="special">(</span><span class="identifier">InputKeyIterator</span> keys_first<span class="special">,</span> <span class="identifier">InputKeyIterator</span> keys_last<span class="special">,</span> 50 <span class="identifier">InputValueIterator</span> values_first<span class="special">,</span> 51 <span class="identifier">OutputKeyIterator</span> keys_result<span class="special">,</span> 52 <span class="identifier">OutputValueIterator</span> values_result<span class="special">,</span> <span class="identifier">BinaryFunction</span> function<span class="special">,</span> 53 <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> 54<span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> InputKeyIterator<span class="special">,</span> <span class="keyword">typename</span> InputValueIterator<span class="special">,</span> 55 <span class="keyword">typename</span> OutputKeyIterator<span class="special">,</span> <span class="keyword">typename</span> OutputValueIterator<span class="special">></span> 56 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span> <span class="identifier">OutputKeyIterator</span><span class="special">,</span> <span class="identifier">OutputValueIterator</span> <span class="special">></span> 57 <span class="identifier">reduce_by_key</span><span class="special">(</span><span class="identifier">InputKeyIterator</span> keys_first<span class="special">,</span> <span class="identifier">InputKeyIterator</span> keys_last<span class="special">,</span> 58 <span class="identifier">InputValueIterator</span> values_first<span class="special">,</span> 59 <span class="identifier">OutputKeyIterator</span> keys_result<span class="special">,</span> 60 <span class="identifier">OutputValueIterator</span> values_result<span class="special">,</span> 61 <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> 62<div class="refsect1"> 63<a name="idm45849519776288"></a><h2>Description</h2> 64<p>The <code class="computeroutput">reduce_by_key()</code> algorithm performs reduction for each contiguous subsequence of values determinate by equivalent keys.</p> 65<p>Returns a pair of iterators at the end of the ranges [<code class="computeroutput">keys_result</code>, keys_result_last) and [<code class="computeroutput">values_result</code>, <code class="computeroutput">values_result_last</code>).</p> 66<p>If no function is specified, <code class="computeroutput">plus</code> will be used. If no predicate is specified, <code class="computeroutput">equal_to</code> will be used.</p> 67<p> 68The <code class="computeroutput">reduce_by_key()</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> 69<p>For example, to calculate the sum of the values for each key:</p> 70<pre class="programlisting"></pre> 71<p> Space complexity on GPUs: \Omega(2n)<br> 72 Space complexity on CPUs: \Omega(1)</p> 73<p><span class="bold"><strong>See Also:</strong></span></p> 74<p>reduce() </p> 75<p> 76</p> 77<p> 78</p> 79<div class="variablelist"><table border="0" class="variablelist compact"> 80<colgroup> 81<col align="left" valign="top"> 82<col> 83</colgroup> 84<tbody><tr> 85<td><p><span class="term">Parameters:</span></p></td> 86<td><div class="variablelist"><table border="0" class="variablelist compact"> 87<colgroup> 88<col align="left" valign="top"> 89<col> 90</colgroup> 91<tbody> 92<tr> 93<td><p><span class="term"><code class="computeroutput">function</code></span></p></td> 94<td><p>binary reduction function </p></td> 95</tr> 96<tr> 97<td><p><span class="term"><code class="computeroutput">keys_first</code></span></p></td> 98<td><p>the first key </p></td> 99</tr> 100<tr> 101<td><p><span class="term"><code class="computeroutput">keys_last</code></span></p></td> 102<td><p>the last key </p></td> 103</tr> 104<tr> 105<td><p><span class="term"><code class="computeroutput">keys_result</code></span></p></td> 106<td><p>iterator pointing to the key output </p></td> 107</tr> 108<tr> 109<td><p><span class="term"><code class="computeroutput">predicate</code></span></p></td> 110<td><p>binary predicate which returns true only if two keys are equal </p></td> 111</tr> 112<tr> 113<td><p><span class="term"><code class="computeroutput">queue</code></span></p></td> 114<td><p>command queue to perform the operation</p></td> 115</tr> 116<tr> 117<td><p><span class="term"><code class="computeroutput">values_first</code></span></p></td> 118<td><p>the first input value </p></td> 119</tr> 120<tr> 121<td><p><span class="term"><code class="computeroutput">values_result</code></span></p></td> 122<td><p>iterator pointing to the reduced value output </p></td> 123</tr> 124</tbody> 125</table></div></td> 126</tr></tbody> 127</table></div> 128</div> 129</div> 130<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 131<td align="left"></td> 132<td align="right"><div class="copyright-footer">Copyright © 2013, 2014 Kyle Lutz<p> 133 Distributed under the Boost Software License, Version 1.0. (See accompanying 134 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>) 135 </p> 136</div></td> 137</tr></table> 138<hr> 139<div class="spirit-nav"> 140<a accesskey="p" href="reduce.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../boost_compute/reference.html#header.boost.compute.algorithm.reduce_by_key_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="remove.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 141</div> 142</body> 143</html> 144