title: CUDA Thrust: reduce_by_key on only some values in an array, based off values in a "key" array
body: <p>Let's say I have two device_vector&lt;byte&gt; arrays, <code>d_keys</code> and <code>d_data</code>.</p>
  
  <p>If <code>d_data</code> is, for example, a flattened 2D 3x5 array ( e.g. { 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3 } ) and <code>d_keys</code> is a 1D array of size 5 ( e.g. { 1, 0, 0, 1, 1 } ), how can I do a reduction such that I'd end up only adding values on a per-row basis if the corresponding <code>d_keys</code> value is one ( e.g. ending up with a result of { 10, 23, 14 } )?</p>
  
  <p>The <a href="https://code.google.com/p/thrust/source/browse/examples/sum_rows.cu" rel="nofollow">sum_rows.cu</a> example allows me to add every value in <code>d_data</code>, but that's not quite right.</p>
  
  <p>Alternatively, I can, on a per-row basis, use a <code>zip_iterator</code> and combine <code>d_keys</code> with one row of <code>d_data</code> at a time, and do a <code>transform_reduce</code>, adding only if the key value is one, but then I'd have to loop through the <code>d_data</code> array.</p>
  
  <p>What I really need is some sort of <code>transform_reduce_by_key</code> functionality that isn't built-in, but surely there must be a way to make it!</p>