我正在使用 Thrust 库的 Thrust::copy_if 函数以及计数迭代器来获取数组中非零元素的索引。我还需要获取复制元素的数量。
我正在使用“counting_iterator.cu”示例中的代码,但在我的应用程序中,我需要重用预先分配的数组,因此我用推力::device_ptr包装它们,然后将它们传递给推力::copy_if功能。这是代码:
using namespace thrust;
int output[5];
thrust::device_ptr<int> tp_output = device_pointer_cast(output);
float stencil[5];
stencil[0] = 0;
stencil[1] = 0;
stencil[2] = 1;
stencil[3] = 0;
stencil[4] = 1;
device_ptr<float> tp_stencil = device_pointer_cast(stencil);
device_vector<int>::iterator output_end = copy_if(make_counting_iterator<int>(0),
make_counting_iterator<int>(5),
tp_stencil,
tp_output,
_1 == 1);
int number_of_ones = output_end - tp_output;
如果我注释最后一行代码,该函数将正确填充输出数组。但是,当我取消注释时,出现以下编译错误:
1>C:\Program Files\NVIDIA GPUComputing Toolkit\CUDA\v5.5\include\thrust/iterator/iterator_adaptor.h(223): 错误:没有运算符“-”与这些操作数匹配
1> 操作数类型为:int *const - const Twitter::device_ptr
1> 检测到: 1> 实例化“thrust::iterator_adaptor::difference_type Thrust::iterator_adaptor::distance_to(const Thrust::iterator_adaptor &) const [with Derived=thrust::detail::normal_iterator>, Base=thrust::device_ptr, Value= Thrust::use_default、System=thrust::use_default、遍历=thrust::use_default、Reference=thrust::use_default、Difference=thrust::use_default、OtherDerived=thrust::device_ptr、OtherIterator=int *、V=signed int、 S=thrust::device_system_tag,T=thrust::random_access_traversal_tag,R=thrust::device_reference,D=ptrdiff_t]” 1> C:\Program Files\NVIDIA GPUComputing Toolkit\CUDA\v5.5\include\thrust/iterator/iterator_facade.h(181):这里 1> 实例化“Facade1::difference_type Thrust::iterator_core_access::distance_from(const Facade1 &, const Facade2 &, Thrust::detail::true_type) [with Facade1=thrust::detail::normal_iterator>, Facade2=thrust: :device_ptr]" 1> C:\Program Files\NVIDIA GPUComputing Toolkit\CUDA\v5.5\include\thrust/iterator/iterator_facade.h(202):这里 1> 实例化“thrust::detail::distance_from_result::type Thrust::iterator_core_access::distance_from(const Facade1 &, const Facade2 &) [with Facade1=thrust::detail::normal_iterator>, Facade2=thrust::device_ptr ]” 1> C:\Program Files\NVIDIA GPUComputing Toolkit\CUDA\v5.5\include\thrust/iterator/iterator_facade.h(506):这里 1> 实例化“thrust::detail::distance_from_result, Thrust::iterator_facade>::type Twitter::operator-(const Twitter::iterator_facade &, const Twitter::iterator_facade &) [with Derived1=thrust::detail: :normal_iterator>、Value1=有符号整数、System1=thrust::device_system_tag、Traversal1=thrust::random_access_traversal_tag、Reference1=thrust::device_reference、Difference1=有符号整数、Derived2=thrust::device_ptr、Value2=有符号整数、System2=thrust::device_system_tag、Traversal2=thrust::random_access_traversal_tag、Reference2=thrust::device_reference、Difference2=signed int]" 1> C:/ProgramData/NVIDIA Corporation/CUDA Samples/v5.5/7_CUDALibraries/nsgaIIparallelo_23ott/rank_cuda.cu(70):此处
如果我使用 Throw::device_vector 作为输出数组,则一切正常:
using namespace thrust;
thrust::device_vector<int> output(5);
float stencil[5];
stencil[0] = 0;
stencil[1] = 0;
stencil[2] = 1;
stencil[3] = 0;
stencil[4] = 1;
device_ptr<float> tp_stencil = device_pointer_cast(stencil);
device_vector<int>::iterator output_end = copy_if(make_counting_iterator<int>(0),
make_counting_iterator<int>(5),
tp_stencil,
output.begin(),
_1 == 1);
int number_of_ones = output_end - output.begin();
你能建议解决这个问题的方法吗?谢谢。
最佳答案
尝试在 copy_if 调用中使用 device_ptr 而不是 device_vector::iterator:
thrust::device_ptr<int> output_end = copy_if(make_counting_iterator<int>(0),
make_counting_iterator<int>(5),
tp_stencil,
tp_output,
_1 == 1);
关于cuda - 推力:使用device_ptr时如何获取copy_if函数复制的元素数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19568529/