在CUDA中的大型数组转换期间删除项目

时间:2018-06-08 14:45:15

标签: cuda thrust

给定大量的值A转换为数组B,因此B =变换(A)。其中A和B的类型不同,变换Transform()相当昂贵,B的数据大小也大于A.但是结果也要根据谓词Keep(B)过滤掉。

有没有一种不错的方法可以在不首先编写B数组然后修剪B条目的情况下执行此操作?

我开始尝试:

typedef int A;
struct B { int a, b, c; };


struct FTransform : thrust::unary_function<A, B>
{
    __device__ B operator()(A a) const { return B{ a, a, a }; }
};

struct FKeep : thrust::unary_function<B, bool>
{
    __device__ bool operator()(B b) const { return (b.a & 1) == 0; }
};


thrust::device_vector<B> outputs(8);
thrust::device_vector<A> inputs(8);

std::generate(inputs.begin(), inputs.end(), rand);

auto first = thrust::make_transform_iterator(inputs.begin(), FTransform());
auto last = thrust::make_transform_iterator(inputs.end(), FTransform());

auto end = thrust::copy_if(first, last, outputs, FKeep());

然而,这会产生编译错误(Cuda 9.2):

thrust/iterator/iterator_traits.h(49): error : class "thrust::device_vector<B, thrust::device_malloc_allocator<B>>" has no member "iterator_category"

thrust/detail/copy_if.inl(78): error : incomplete type is not allowed

thrust/detail/copy_if.inl(80): error : no instance of overloaded function "select_system" matches the argument list

thrust/detail/copy_if.inl(80): error : no instance of overloaded function "thrust::copy_if" matches the argument list

1 个答案:

答案 0 :(得分:2)

下面:

auto end = thrust::copy_if(first, last, outputs, FKeep());
                                        ^^^^^^^

outputs不是迭代器。你应该在那里通过outputs.begin()

通过这项更改,您的代码将为我编译。