首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何知道thrust::partition_copy结果中有多少元素

如何知道thrust::partition_copy结果中有多少元素
EN

Stack Overflow用户
提问于 2020-02-13 11:50:52
回答 1查看 197关注 0票数 1

我试图用推力库的partition_copy函数对数组进行分区。

我已经看到了传递指针的例子,但是我需要知道每个分区中有多少个元素。

我尝试的是将设备向量作为OutputIterator参数传递,如下所示:

代码语言:javascript
复制
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/partition.h>

struct is_even {
    __host__ __device__ bool operator()(const int &x) {
        return (x % 2) == 0;
    }
};

int N;
int *d_data;
cudaMalloc(&d_data, N*sizeof(int));

//... Some data is put in the d_data array

thrust::device_ptr<int> dptr_data(d_data);

thrust::device_vector<int> out_true(N);
thrust::device_vector<int> out_false(N);

thrust::partition_copy(dptr_data, dptr_data + N, out_true, out_false, is_even());

当我试图编译时,我得到了以下错误:

代码语言:javascript
复制
error: class "thrust::iterator_system<thrust::device_vector<int, thrust::device_allocator<int>>>" has no member "type"
      detected during instantiation of "thrust::pair<OutputIterator1, OutputIterator2> thrust::partition_copy(InputIterator, InputIterator, OutputIterator1, OutputIterator2, Predicate) [with InputIterator=thrust::device_ptr<int>, OutputIterator1=thrust::device_vector<int, thrust::device_allocator<int>>, OutputIterator2=thrust::device_vector<int, thrust::device_allocator<int>>, Predicate=leq]"

因此,我的问题是:如何使用推力::分区或thrust::partition_copy,并知道每个分区中有多少个元素?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-13 13:32:17

您的编译错误是由于您在这里传递向量而不是迭代器:

代码语言:javascript
复制
thrust::partition_copy(dptr_data, dptr_data + N, out_true, out_false, is_even());
                                                 ^^^^^^^^^^^^^^^^^^^

相反,您应该根据这些容器传递迭代器:

代码语言:javascript
复制
thrust::partition_copy(dptr_data, dptr_data + N, out_true.begin(), out_false.begin(), is_even());

为了得到结果的长度,我们必须使用推力的返回值::分区副本()

返回一对p,使得p.first是从out_true开始的输出范围的结束,而p.second是从out_false开始的输出范围的结束。

就像这样:

代码语言:javascript
复制
auto r = thrust::partition_copy(dptr_data, dptr_data + N, out_true.begin(), out_false.begin(), is_even());
int length_true = r.first - out_true.begin();
int length_false = r.second - out_false.begin();

注意,类似的方法可以与其他推力算法一起使用。那些不返回元组的用户将更容易使用。

例如:

代码语言:javascript
复制
auto length = (thrust::remove_if(A.begin(), A.end(), ...) - A.begin());
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60207167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档