Search code examples
cudathrust

cuda thrust: selective copying and resizing results


I am copying items selectively between two thrust device arrays using copy_if as follows:

thrust::device_vector<float4> collated = thrust::device_vector<float4> 
                       original_vec.size());
thrust::copy_if(original_vec.begin(), original_vec.end(),
                collated.begin(), is_valid_pt());
collated.shrink_to_fit();

The is_valid_pt is implemented as:

struct is_valid_kpt
{
    __host__ __device__ bool operator()(const float4 x)
    {
        return x.w >= 0;
    }
}; 

Now after running this code, I was expecting the size of the collated vector to be much less than the original array but they are still the same size.


Solution

  • Thrust doesn't resize vectors as part of any algorithm call. The size of vectors going into a thrust algorithm will be exactly the size of those vectors coming out of the algorithm.

    shrink_to_fit also has no impact on a vector's size, but it may impact the capacity, which has to do with allocation.

    If you want to reduce the size of collated to the actual number of elements that were copied into it, you will need to use the return value of the copy_if function, to compute its size, and then do a resize.

    Something like this:

    size_t my_size = thrust::copy_if(original_vec.begin(), original_vec.end(), collated.begin(), is_valid_pt()) - collated.begin();
    collated.resize(my_size);