Filter 按键值设置的推力滤波器
在我的应用程序中,我有这样一个类:Filter 按键值设置的推力滤波器,filter,cuda,gpu,thrust,Filter,Cuda,Gpu,Thrust,在我的应用程序中,我有这样一个类: class sample{ thrust::device_vector<int> edge_ID; thrust::device_vector<float> weight; thrust::device_vector<int> layer_ID; /*functions, zip_iterators etc. */ }; void filter(const sample& src
class sample{
thrust::device_vector<int> edge_ID;
thrust::device_vector<float> weight;
thrust::device_vector<int> layer_ID;
/*functions, zip_iterators etc. */
};
void filter(const sample& src, sample& dest, const int& target_layer){
for(...){
if( src.layer_ID[x] == target_layer)/*copy values to dest*/;
}
}
void filter(const sample& src, sample& dest, const int& target_layer){
thrust::copy_if(src.begin(),
src.end(),
dest.begin(),
comparing_functor() );
}
我发现最好的方法是使用推力::copy_if(…)
它看起来是这样的:
class sample{
thrust::device_vector<int> edge_ID;
thrust::device_vector<float> weight;
thrust::device_vector<int> layer_ID;
/*functions, zip_iterators etc. */
};
void filter(const sample& src, sample& dest, const int& target_layer){
for(...){
if( src.layer_ID[x] == target_layer)/*copy values to dest*/;
}
}
void filter(const sample& src, sample& dest, const int& target_layer){
thrust::copy_if(src.begin(),
src.end(),
dest.begin(),
comparing_functor() );
}
这就是我的问题所在:
comparing_functor()
是一个一元函数,这意味着我不能将我的target_层
值传递给它
任何人都知道如何解决这个问题,或者有想法在保持类的数据结构完整的情况下实现它吗?除了通常传递给函子的数据之外,还可以将特定的值传递给函子,以便在谓词测试中使用。下面是一个成功的例子:
#include <iostream>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sequence.h>
#include <thrust/copy.h>
#define DSIZE 10
#define FVAL 5
struct test_functor
{
const int a;
test_functor(int _a) : a(_a) {}
__device__
bool operator()(const int& x ) {
return (x==a);
}
};
int main(){
int target_layer = FVAL;
thrust::host_vector<int> h_vals(DSIZE);
thrust::sequence(h_vals.begin(), h_vals.end());
thrust::device_vector<int> d_vals = h_vals;
thrust::device_vector<int> d_result(DSIZE);
thrust::copy_if(d_vals.begin(), d_vals.end(), d_result.begin(), test_functor(target_layer));
thrust::host_vector<int> h_result = d_result;
std::cout << "Data :" << std::endl;
thrust::copy(h_vals.begin(), h_vals.end(), std::ostream_iterator<int>( std::cout, " "));
std::cout << std::endl;
std::cout << "Filter Value: " << target_layer << std::endl;
std::cout << "Results :" << std::endl;
thrust::copy(h_result.begin(), h_result.end(), std::ostream_iterator<int>( std::cout, " "));
std::cout << std::endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义DSIZE 10
#定义FVAL 5
结构测试函数
{
常数INTA;
test_函子(int_a):a(_a){
__装置__
布尔运算符()(常量int&x){
返回(x==a);
}
};
int main(){
int target_layer=FVAL;
推力:主机向量h值(DSIZE);
推力::序列(h_vals.begin(),h_vals.end());
推力:设备向量d\u vals=h\u vals;
推力:设备矢量d结果(DSIZE);
推力:复制函数if(d_vals.begin(),d_vals.end(),d_result.begin(),test_函子(目标层));
推力::主向量h_结果=d_结果;
标准::cout