如何知道PUSIT::PARTITION_COPY的结果中有多少个元素 [英] How to know how many elements are in the result of thrust::partition_copy

查看:93
本文介绍了如何知道PUSIT::PARTITION_COPY的结果中有多少个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用推力库的PARTITION_COPY函数对阵列进行分区。

我看过传递指针的示例,但我需要知道每个分区中有多少个元素。

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

#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());

尝试编译时出现此错误:

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]"

所以我的问题是:如何使用HUTHING::PARTITION或HUTHING::PARTITION_COPY并知道每个分区中最终有多少元素?

推荐答案

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

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

相反,您应该基于这些容器传递迭代器:

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

为了获得结果的长度,我们必须使用return value of thrust::partition copy()

退货 一对p,使得p.first是从OUT_TRUE开始的输出范围的结束,p.Second是从OUT_FALSE开始的输出范围的结束。

如下所示:

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();
请注意,类似的方法可以用于其他推力算法。不返回元组的那些将更易于使用。

例如:

auto length = (thrust::remove_if(A.begin(), A.end(), ...) - A.begin());

这篇关于如何知道PUSIT::PARTITION_COPY的结果中有多少个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆