- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在正确创建仿函数以访问设备 vector 时遇到了一些问题。基本上,我有两个我想在仿函数中使用的设备 vector 。在 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 算法可以在 CUDA 设备代码中以多种方式使用,其中一些已涵盖 here . thrust::device_vector
在 CUDA 设备代码中通常仍然不可用(尽管底层数据可用)。
不能在 cuda 设备代码中使用推力算法(例如转换、归约等)。
这是任何以__global__
开头的函数或 __device__
不能使用推力(例如不能使用 thrust::reduce
)。
因此你的仿函数将无法在设备代码中工作,因为它使用推力结构(例如 thrust::reduce
)。
我知道你说你的代码可以编译,但我不确定我是否相信。如果我尝试声明 thrust::device_vector
里面__device__
代码,我得到编译错误。由于您在此处显示的代码在很多方面都不完整,由于您发布的内容存在其他问题,我无法使用您的代码轻松证明这一点。
关于c++ - 如何从仿函数访问 device_vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20157815/
我是一名优秀的程序员,十分优秀!