Cuda 推力收集/过滤

Cuda 推力收集/过滤,cuda,thrust,Cuda,Thrust,我试图做的是在向量上创建一个过滤器,这样它就可以删除未通过谓词测试的元素;但我不太确定我该怎么做 我根据谓词对inputer向量中的每个元素求值,例如在我的代码中,设备向量中的is_偶数函子。如果通过测试,则为真;如果未通过测试,则为假 现在我被卡住了,因为我现在有这个布尔向量,我想收集通过这个谓词测试的元素。我将其存储在布尔向量中,因为我希望保留结果以过滤其他向量 #include ... template<typename T> struct is_even : thrust:

我试图做的是在向量上创建一个过滤器,这样它就可以删除未通过谓词测试的元素;但我不太确定我该怎么做

我根据谓词对inputer向量中的每个元素求值,例如在我的代码中,设备向量中的is_偶数函子。如果通过测试,则为真;如果未通过测试,则为假

现在我被卡住了,因为我现在有这个布尔向量,我想收集通过这个谓词测试的元素。我将其存储在布尔向量中,因为我希望保留结果以过滤其他向量

#include ...

template<typename T>
struct is_even : thrust::unary_function<T, bool>
{
    __host__ __device__
    bool operator()(const T &x)
    {
        return (x%2)==0;
    }
};

int main(void)
{
    std::cout << "Loading test!" << std::endl;
    const int N = 1000000;
    thrust::device_vector<int> col1(N);
    thrust::device_vector<float> col2(N, 1); 
    thrust::sequence(col1.begin(), col1.end());

    thrust::device_vector<bool> filter(N);
    thrust::transform(col1.begin(), col1.end(), filter.begin(), is_even<int>());

    // filter col1 and col2 based on filter

    return 0;
}
在中,您可能对推力::copy_感兴趣,如果

我们可以使用您定义的谓词直接将偶数元素选择到新向量中,而无需生成中间过滤器向量:


不清楚你在问什么。输入和期望输出是什么?描述你的过滤操作。你试过什么吗?什么东西不起作用?运行代码时,您是否遇到编译错误或错误?您显示的代码为我正确编译。好的,我编辑了,希望它会更清晰。所以您只想知道如何收集col1的元素,其中is_甚至为真?是的,但基于过滤器向量,而不是使用copy_if。我试图实现的是过滤一组向量col1,col2。。。基于对col1的谓词的coln;模拟SQL where过滤器;因此,理想情况下,它应该一次过滤所有列,而不是单独过滤。此外,它应该是一个副本,以保持原始数据不变。是否可以使用模具在一次调用中复制多个流?也就是说,根据模板在一次线性扫描中过滤多个列向量,避免对col1应用谓词。我建议您将其作为一个新的SO问题进行提问。大多数推力操作只采用单个矢量,而不是矢量阵列。您可以使用各种方法连接列向量,也可以复制筛选向量并在一个操作中完成所有操作,但它也会为输出生成一个向量,这会令人困惑。@user2991422使用zip_迭代器一次筛选多个数组。
thrust::copy_if(col1.begin(), col1.end(), result.begin(), is_even<int>());
$ cat t267.cu
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/sequence.h>
#include <thrust/transform.h>
#include <thrust/copy.h>

template<typename T>
struct is_even : thrust::unary_function<T, bool>
{
    __host__ __device__
    bool operator()(const T &x)
    {
        return (x%2)==0;
    }
};


struct is_true : thrust::unary_function<bool, bool>
{
    __host__ __device__
    bool operator()(const bool &x)
    {
        return x;
    }
};

int main(void)
{
    std::cout << "Loading test!" << std::endl;
    const int N = 1000000;
    thrust::device_vector<int> col1(N);
    thrust::device_vector<float> col2(N, 1);
    thrust::sequence(col1.begin(), col1.end());

    thrust::device_vector<bool> filter(N);
    thrust::device_vector<int> result(N);
    thrust::transform(col1.begin(), col1.end(), filter.begin(), is_even<int>());
    // filter col1 based on filter
    thrust::device_vector<int>::iterator end = thrust::copy_if(col1.begin(), col1.end(), filter.begin(), result.begin(), is_true());
    int len = end - result.begin();
    thrust::host_vector<int> h_result(len);
    thrust::copy_n(result.begin(), len, h_result.begin());
    thrust::copy_n(h_result.begin(), 10, std::ostream_iterator<int>(std::cout, "\n"));


    return 0;
}
$ nvcc -arch=sm_20 -o t267 t267.cu
$ ./t267
Loading test!
0
2
4
6
8
10
12
14
16
18
$