如何从函数访问device_vector
How to access a device_vector from a functor
我在正确创建函子以访问设备向量时遇到了一些问题。基本上,我有两个设备向量,我想在函子中使用。在for_each过程中调用函子。
这是我的函数:
struct likelihood_functor
{
int N;
float* v1;
float* v2;
likelihood_functor(int _N, float* _v1, float* _v2) : N(_N),v1(_v1),v2(_v2) {}
template <typename Tuple>
__host__ __device__ void operator()(Tuple t)
{
float A = thrust::get<0>(t);
float rho = thrust::get<1>(t);
float mux = thrust::get<2>(t);
float muy = thrust::get<3>(t);
float sigx = thrust::get<4>(t);
float sigy = thrust::get<5>(t);
thrust::device_ptr<float> v1_p(v1);
thrust::device_vector<float> X(v1_p,v1_p+N);
thrust::device_ptr<float> v2_p(v2);
thrust::device_vector<float> Y(v2_p,v2_p+N);
thrust::get<6>(t) = 600*logf(A)
- 600/2*logf(sigx*sigx*sigy*sigy*(1-rho*rho))
- thrust::reduce(X.begin(),X.end())
- thrust::reduce(Y.begin(),Y.end())
- 2*rho/(sigx*sigy);
}
};
这是我的main():
int main(void)
{
// create a 2D dataset
const int N=2500; //number of counts
thrust::device_vector<float> data_x(N);
thrust::device_vector<float> data_y(N);
thrust::counting_iterator<unsigned int> begin(0);
thrust::transform(begin,
begin + N,
data_x.begin(),
get_normal(5.f,1.f,2.f));
thrust::transform(begin,
begin + N,
data_y.begin(),
get_normal(5.f,1.f,2.f));
//
// Some code here to initiate A_n, rho_na, mux_n etc...
//
// apply the transformation
thrust::for_each(
thrust::make_zip_iterator(
thrust::make_tuple(A_n.begin(), rho_n.begin(), mux_n.begin(), muy_n.begin(), sigx_n.begin(),sigy_n.begin(), L.begin())
),
thrust::make_zip_iterator(
thrust::make_tuple(A_n.end(), rho_n.end(), mux_n.end(), muy_n.end(), sigx_n.end(),sigy_n.end(),L.end())
),
likelihood_functor(N,thrust::raw_pointer_cast(&(data_x[0])),thrust::raw_pointer_cast(&(data_y[0])))
);
// print the output
for(int i=0; i<4096; i++)
{
std::cout << "[" << i << "] : " << L[i] <<std::endl;
}
}
代码已编译,但未运行。我知道这是因为在我的函子中,device_vector X和Y没有正确完成。
我在主函数中使用了相同的代码来创建X和Y,当我这样做时,程序运行良好(在这种情况下,我不调用函子)。函子内部会使某些东西在主程序中工作,而不是在函子中工作,这与函子内部有什么不同?
还有其他方法可以做我想做的事情吗?
谢谢你的帮助!
thrust::device_vector
在CUDA设备代码中通常仍然不可用(尽管底层数据是)。
推力算法(例如转换、归约等)不能用于cuda设备代码。
也就是说,以__global__
或__device__
开头的任何函数都不能使用推力(例如,不能使用例如thrust::reduce
)。
因此,您的函子不会在设备代码中工作,因为它使用了推力结构(如thrust::reduce
)。
我知道你说你的代码可以编译,但我真的不确定我是否相信。如果我试图在__device__
代码中声明一个thrust::device_vector
,就会出现编译错误。由于您在这里展示的代码在很多方面都是不完整的,由于您发布的内容存在其他问题,我无法用您的代码轻松地证明这一点。
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中用vector填充一个简单的动态数组
- vector.resize()中的分配错误
- 使用std::vector的OpenCL矩阵乘法
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 在某些循环内使用vector.push_back时出现分段错误
- 当vector是tje全局变量时,c++中vector的内存管理
- std::vector的包装器,使数组的结构看起来像结构的数组
- 为什么(-1)%vector::size()总是返回0
- 在C++中将类(带有Vector成员)保存为二进制文件
- 编译器如何区分std::vector的构造函数
- 将 int 数组转换为 std::vector<int*>
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 在std::vector上存储带有模板的类实例
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 为什么std::vector比数组慢
- std::vector::迭代器是否可以合法地作为指针
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么std::vector和std::valarray初始化构造函数不同