【发布时间】:2021-07-27 04:54:55
【问题描述】:
当我运行这段代码时,编译器说我正在从全局函数调用主机函数。 我想为 A[i] 和 B[i] 分配比零更复杂的东西,但我只是想测试功能。 我需要修改两个向量中的值。 稍后我想减少表 A。
int main(void){
const int numElements = 100000;
thrust::device_vector<double> A(numElements);
thrust::device_vector<double> B(numElements);
int threadsPerBlock = 256;
int blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock;
vectorCount<<<blocksPerGrid, threadsPerBlock>>>(A, B, numElements);
}
__global__ void vectorCount(thrust::device_vector<double> A, thrust::device_vector<double> B, int numElements, int sequence_k){
int i = blockDim.x * blockIdx.x + threadIdx.x;
A[i] = 0;
B[i] = 0;
}
我试过改成
struct saxpy_functor
{
const int numElements;
saxpy_functor(int _a) : numElements(_a) {}
__host__ __device__
double operator()(double& x) const {
x = 0;
return 0;
}
};
//in main
thrust::transform(A.begin(), A.end(), B.begin(), saxpy_functor(numElements));
但我无法找到如何获得 i,因为我想根据 Vector 中的 位置 执行计算?
【问题讨论】: