C++ 如何知道推力::分区\复制的结果中有多少个元素

C++ 如何知道推力::分区\复制的结果中有多少个元素,c++,cuda,gpu,thrust,C++,Cuda,Gpu,Thrust,我正在尝试使用推力库的partition\u copy函数对数组进行分区 我见过传递指针的例子,但我需要知道每个分区中有多少个元素 我尝试将设备向量作为输出计算器参数传递,如下所示: #include <thrust/device_vector.h> #include <thrust/device_ptr.h> #include <thrust/partition.h> struct is_even { __host__ __device__ boo

我正在尝试使用推力库的partition\u copy函数对数组进行分区

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

我尝试将设备向量作为输出计算器参数传递,如下所示:

#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());
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();
#包括
#包括
#包括
结构是偶数{
__主机设备布尔运算符()(常量int&x){
返回(x%2)==0;
}
};
int N;
int*d_数据;
Cudamaloc(&d_数据,N*sizeof(int));
//... 一些数据放在d_数据数组中
推力:设备ptr dptr_数据(数据);
推力::设备矢量输出为真(N);
推力::设备矢量输出为假(N);
推力::分区拷贝(dptr_数据,dptr_数据+N,out_真,out_假,is_偶());
当我尝试编译时,出现以下错误:

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]"
错误:类“推力::迭代器系统”没有成员“类型”
在实例化“推力::对推力::分区\复制(输入迭代器,输入迭代器,输出迭代器1,输出迭代器2,谓词)[使用输入迭代器=推力::设备\ ptr,输出迭代器1=推力::设备\向量,输出迭代器2=推力::设备\向量,谓词=leq]时检测到

因此,我的问题是:如何使用推力::分区或推力::分区_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());
为了获得结果的长度,我们必须使用:

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

大概是这样的:

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

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

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());
为了获得结果的长度,我们必须使用:

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

大概是这样的:

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