5

thrust::device_vector values

thrust::device_vector keys;

After initialization, keys contains some elements equal to -1. I wanted to delete the elements in keys and in the same position of values.

But I do not know how to deal with it parallel?

GaoYuan
  • 153
  • 1
  • 10

1 Answers1

5

There are probably many ways to do this. One possible way:

  1. use the stencil version of thrust::remove_if (documentation), with the keys as your stencil, removing the elements in values where the corresponding key is -1. You will need to create a functor for the predicate test.
  2. use thrust::remove (documentation) on the keys to remove the values that are -1

Here's an example:

#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <thrust/remove.h>
#include <thrust/sequence.h>

#define N 12
typedef thrust::device_vector<int>::iterator dintiter;

struct is_minus_one
{
  __host__ __device__
  bool operator()(const int x)
  {
    return (x == -1);
  }
};

int main(){

  thrust::device_vector<int> keys(N);
  thrust::device_vector<int> values(N);

  thrust::sequence(keys.begin(), keys.end());
  thrust::sequence(values.begin(), values.end());

  keys[3] = -1;
  keys[9] = -1;

  dintiter nve = thrust::remove_if(values.begin(), values.end(), keys.begin(), is_minus_one());
  dintiter nke = thrust::remove(keys.begin(), keys.end(), -1);

  std::cout << "results  values:" << std::endl;
  thrust::copy(values.begin(), nve, std::ostream_iterator<int>( std::cout, " "));
  std::cout << std::endl << "results keys:" << std::endl;
  thrust::copy(keys.begin(), nke, std::ostream_iterator<int>( std::cout, " "));
  std::cout << std::endl;

  return 0;
}
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • 2
    Another way to do it would be to only use `remove_if` and zip together the `keys` and `values` arrays. The predicate given to `remove_if` would check the first element of the tuple for the key of interest. This might be a bit faster because the elements of the keys array would only need to be loaded once. – Jared Hoberock Jul 11 '13 at 16:54
  • I am having a problem with this implementation when calling the predicate function. The program does not build and yields the following error: "A type defined inside a function scope cannot be used in the template argument type of a __global__ function template instantiation" Any suggestions on how to rectify this? – danieljovan Nov 22 '17 at 01:25
  • I just tested this again by recompiling this code verbatim on CUDA 9 on linux, and it compiles cleanly with no issue.You may possibly have an incorrect build environment or compile command. Or if your code is not an exact duplicate of what is posted here, then you may have introduced an error. I wouldn't be able to comment on that without seeing it. Anyway, it may not be possible to sort out in the space of the comments here. I'm pretty convinced there is nothing wrong with the posted code. If you have a new question, usually the best advice is to post a new question. You can link to this one. – Robert Crovella Nov 22 '17 at 01:39
  • You're right...I was attempting to try the remove_if() for removing multiple values (in addition to -1), where the values are specified in the predicate function. I will take your advice thanks. – danieljovan Nov 22 '17 at 02:06
  • Rather than specify the values in the functor, you could pass the desired values via a functor initialization parameter (i.e. at run-time). You can find thrust codes here on SO that demonstrate passing initialization values to a functor. – Robert Crovella Nov 22 '17 at 02:19