Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Allow in-place reduce_by_key if possible #303

Closed
jaredhoberock opened this issue Dec 6, 2012 · 1 comment
Closed

Allow in-place reduce_by_key if possible #303

jaredhoberock opened this issue Dec 6, 2012 · 1 comment
Labels
type: enhancement New feature or request.

Comments

@jaredhoberock
Copy link
Contributor

This program demonstrates it's not currently possible:

#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/sort.h>
#include <thrust/reduce.h>
#include <thrust/random.h>
#include <cassert>

int main()
{
  size_t n = 36;
  size_t k = 115;

  thrust::host_vector<int> h_keys(n), h_vals(n, 1);

  thrust::default_random_engine rng;
  for(int i = 0; i < n; ++i)
  {
    h_keys[i] = rng() % k;
  }

  thrust::sort(h_keys.begin(), h_keys.end());

  thrust::device_vector<int> d_keys = h_keys;
  thrust::device_vector<int> d_vals = h_vals;

  thrust::pair<
    thrust::host_vector<int>::iterator,
    thrust::host_vector<int>::iterator
  > h_end = 
    thrust::reduce_by_key(h_keys.begin(), h_keys.end(),
                          h_vals.begin(),
                          h_keys.begin(),
                          h_vals.begin());

  h_keys.erase(h_end.first,  h_keys.end());
  h_vals.erase(h_end.second, h_vals.end());

  thrust::pair<
    thrust::device_vector<int>::iterator,
    thrust::device_vector<int>::iterator
  > d_end = 
    thrust::reduce_by_key(d_keys.begin(), d_keys.end(),
                          d_vals.begin(),
                          d_keys.begin(),
                          d_vals.begin());

  d_keys.erase(d_end.first , d_keys.end());
  d_vals.erase(d_end.second, d_vals.end());

  assert(h_keys == d_keys);
}
@jaredhoberock
Copy link
Contributor Author

I think it's not possible to support this, for the same reason that the other compaction algorithms can't be in-place.

We should improve the documentation

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

1 participant