【发布时间】:2017-08-23 03:02:54
【问题描述】:
我正在尝试在主机上创建一个指针数组。数组中的每个指针都指向一个大小为 4 的数组。当我尝试将指针复制到设备时,复制失败并且设备无法访问指针指向的数组的内容。如何将指针数组中的指针从指向主机的数组复制到设备?
__global__ void kernel(int* D)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < 4)
{
printf("Device = %d\n", D[tid]);
tid += blockDim.x * gridDim.x;
}
}
int main(void)
{
cudaProfilerStart();
int* H[2];
int* D[2];
int test1[4] = { 1, 2, 3, 4 };
int test2[4] = { 10, 20, 30, 40 };
H[0] = test1;
H[1] = test2;
HANDLE_ERROR(cudaMalloc((void**)&D[0], 4 * sizeof(int)));
HANDLE_ERROR(cudaMemcpy(D[0], H[0], 4 * sizeof(int), cudaMemcpyHostToDevice));
kernel <<<1, 4 >>>(D[0]);
cudaProfilerStop();
return 0;
}
【问题讨论】:
-
您发布的代码没有任何问题,它按我的预期工作。你确定你有一个功能性的 CUDA 安装吗?如果您使用它运行代码,
cuda-memcheck会报告哪些运行时错误?