[英]How to use pointer to pointer in cuda
I have a problem with using of pointer to pointer in cuda. 我在CUDA中使用指针指向有问题。 Code snippet is below.
代码段如下。
char** d_ppcPtr, *d_pcPtr, *h_pcPtr;
cudaMalloc(&d_ppcPtr, sizeof(char*) * 10);
h_pcPtr = (char*)malloc(sizeof(char) * 100);
for(int i = 0; i < 10; i ++)
{
cudaMalloc(&d_pcPtr, sizeof(char) * 100);
cudaMemset(d_pcPtr, 1, sizeof(char) * 100);
cudaMemcpy(&d_ppcPtr[i], &d_pcPtr, sizeof(char*), cudaMemcpyHostToDevice);
cudaMemcpy(h_pcPtr, d_ppcPtr[i], sizeof(char) * 100, cudaMemcpyDeviceToHost); //crash here
cudaFree(d_ppcPtr[i]); //crash also here
}
cudaFree(d_ppcPtr);
how can i fix above two crashes? 我该如何解决两次以上的崩溃? Thanks in advance.
提前致谢。
The following modification will "fix" your code (fully worked example, including host and device verification): 以下修改将“修复”您的代码(完全有效的示例,包括主机和设备验证):
$ cat t583.cu
#include <stdio.h>
__global__ void testkernel(char **data, unsigned n){
for (int i = 0; i < 100; i++) if (data[n][i] != 1) printf("kernel error\n");
}
int main(){
char** d_ppcPtr, *d_pcPtr, *h_pcPtr;
cudaMalloc(&d_ppcPtr, sizeof(char*) * 10);
h_pcPtr = (char*)malloc(sizeof(char) * 100);
for(int i = 0; i < 10; i ++)
{
cudaMalloc(&d_pcPtr, sizeof(char) * 100);
cudaMemset(d_pcPtr, 1, sizeof(char) * 100);
cudaMemcpy(&d_ppcPtr[i], &d_pcPtr, sizeof(char*), cudaMemcpyHostToDevice);
memset(h_pcPtr, 0, sizeof(char)*100);
testkernel<<<1,1>>>(d_ppcPtr, i);
cudaMemcpy(h_pcPtr, d_pcPtr, sizeof(char) * 100, cudaMemcpyDeviceToHost);
cudaFree(d_pcPtr);
for (int i = 0; i < 100; i++) if (h_pcPtr[i] != 1) printf("Error!");
}
cudaFree(d_ppcPtr);
}
$ nvcc -arch=sm_20 -o t583 t583.cu
$ cuda-memcheck ./t583
========= CUDA-MEMCHECK
========= ERROR SUMMARY: 0 errors
Note that conceptually, there is no difference between my code and yours, because the pointer that you are attempting to use in location d_ppcPtr[i]
, (and is crashing, because it is located on the device,) is already contained in d_pcPtr
, which is on the host. 请注意,从概念上讲,我的代码与您的代码没有区别,因为您尝试在
d_ppcPtr[i]
位置使用的指针(并且由于位于设备上而崩溃)已经包含在d_pcPtr
,在主机上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.