Cuda 推力-初始装置_矢量
感谢您回答我的问题@Eric Shiyin Kang,但不是前缀“主机”或“设备”导致了我的问题,经过一些尝试和错误,我发现错误是“成员数据始终是常量” 举个例子:Cuda 推力-初始装置_矢量,cuda,thrust,Cuda,Thrust,感谢您回答我的问题@Eric Shiyin Kang,但不是前缀“主机”或“设备”导致了我的问题,经过一些尝试和错误,我发现错误是“成员数据始终是常量” 举个例子: struct OP { int N; __host__ __device__ OP(const int n): N(n){}; __host__ __device__ UI operator()(const UI a) { int b = a * N; N++
struct OP {
int N;
__host__ __device__
OP(const int n): N(n){};
__host__ __device__
UI operator()(const UI a) {
int b = a * N;
N++;
return b;
}
}
thrust::transform(A.begin(), A.end(), B.begin(), OP(2) );
在这种情况下,如果A是{0,1,2,3,…},那么B是{0,2,4,6,8},
但是实际的B应该是{0,3(1*(2+1)),8(2*(3+1)),15(3*(4+1)),…}
我不知道是什么原因造成这种情况,推力设计的原因?有人能告诉我吗?对于您更新的Q,主机var
N
无法在设备代码中更新。在并行算法中多次更新共享变量通常是不安全的
事实上,初始化dev向量的最快方法应该是在对象构造阶段使用奇特的迭代器,比如
// v[]={0,2,4,6,8...}
thrust::device_vector<float> v(
thrust::make_transform_iterator(
thrust::counting_iterator<float>(0.0),
_1 * 2.0),
thrust::make_transform_iterator(
thrust::counting_iterator<float>(0.0),
_1 * 2.0) + SIZE);
// u[]={0,3,8,15...}
thrust::device_vector<float> u(
thrust::make_transform_iterator(
thrust::counting_iterator<float>(0.0),
_1 * (_1 + 2.0)),
thrust::make_transform_iterator(
thrust::counting_iterator<float>(0.0),
_1 * (_1 + 2.0)) + SIZE);
及
否则编译器将无法为GPU生成正确的设备代码 不要像这样完全改变你的问题。你刚才回答了你原来的问题,让两个人浪费了时间。谢谢@Eric Shiyin Kang提供的有用信息。我很感兴趣的是,只有当你能从一个简单的计数序列计算序列时,迭代器才能生成像{0,1,2,0,1,2…}?@user1995868这样的重复序列。例如,使用
\u 1%3
非常感谢,我想我迷失在了学习的方向上,所以我陷入了这样一个显而易见的问题中:(
struct OP {
__host__ __device__ void operator()(int &a) {
a *=2;
}
}
struct OP {
__host__ __device__ int operator()(int a) {
return a*2;
}
}