我已经被这个错误困扰了很长一段时间,所以我决定在这里发布它。
调用 cudaMemcpy 时会发生此分段错误:
CurrentGrid->cdata[i] = new float[size];
cudaMemcpy(CurrentGrid->cdata[i], Grid_dev->cdata[i], size*sizeof(float),\
cudaMemcpyDeviceToHost);
CurrentGrid
并且Grid_dev
分别是指向grid
主机和设备上的类对象的指针,在此上下文中 i=0。类成员cdata
是一个浮点型指针数组。为了调试,就在调用 cudaMemcpy 之前,我打印了 的每个元素的值、和Grid_Dev->cdata[i]
的地址CurrentGrid->cdata[i]
以及Grid_dev->cdata[i]
的值size
,看起来都不错。但它仍然以“Segmentation fault (core dumped)”告终,这是唯一的错误消息。cuda-memcheck 只给出“进程没有成功终止”。我目前无法使用 cuda-gdb。关于去哪里有什么建议吗?
更新:现在看来我已经通过 cudaMalloc 解决了这个问题,设备上的另一个浮点指针 A 和 cudaMemcpy Grid_dev->cdata[i] 的值到 A,然后 cudaMemcpy A 到主机。所以上面写的这段代码就变成了:
float * A;
cudaMalloc((void**)&A, sizeof(float));
...
...
cudaMemcpy(&A, &(Grid_dev->cdata[i]), sizeof(float *), cudaMemcpyDeviceToHost);
CurrentGrid->cdata[i] = new float[size];
cudaMemcpy(CurrentGrid->cdata[i], A, size*sizeof(float), cudaMemcpyDeviceToHost);
我这样做是因为 valgrind 弹出“大小为 8 的无效读取”,我认为这是指Grid_dev->cdata[i]
. 我用 gdb 再次检查它,打印出Grid_dev->cdata[i]
为 NULL 的值。所以我想即使在这个 cudaMemcpy 调用中我也不能直接取消引用设备指针。但为什么 ?根据该线程底部的评论,我们应该能够在 cudaMemcpy 函数中取消引用设备指针。
另外,我不知道 cudaMalloc 和 cudaMemcpy 如何工作的底层机制,但我认为通过 cudaMalloc 一个指针,在这里说 A,我们实际上分配这个指针指向设备上的某个地址。并且通过 cudaMemcpyGrid_dev->cdata[i]
到 A 如上面修改的代码中,我们重新分配指针 A 以指向数组。那我们在cudaMalloced时不就丢失了A指向的上一个地址的轨迹吗?这会导致内存泄漏还是什么?如果是,我应该如何正确解决这种情况?谢谢!
作为参考,我将发生此错误的完整函数的代码放在下面。
非常感谢!
__global__ void Print(grid *, int);
__global__ void Printcell(grid *, int);
void CopyDataToHost(param_t p, grid * CurrentGrid, grid * Grid_dev){
cudaMemcpy(CurrentGrid, Grid_dev, sizeof(grid), cudaMemcpyDeviceToHost);
#if DEBUG_DEV
cudaCheckErrors("cudaMemcpy1 error");
#endif
printf("\nBefore copy cell data\n");
Print<<<1,1>>>(Grid_dev, 0); //Print out some Grid_dev information for
cudaDeviceSynchronize(); //debug
int NumberOfBaryonFields = CurrentGrid->ReturnNumberOfBaryonFields();
int size = CurrentGrid->ReturnSize();
int vsize = CurrentGrid->ReturnVSize();
CurrentGrid->FieldType = NULL;
CurrentGrid->FieldType = new int[NumberOfBaryonFields];
printf("CurrentGrid size is %d\n", size);
for( int i = 0; i < p.NumberOfFields; i++){
CurrentGrid->cdata[i] = NULL;
CurrentGrid->vdata[i] = NULL;
CurrentGrid->cdata[i] = new float[size];
CurrentGrid->vdata[i] = new float[vsize];
Printcell<<<1,1>>>(Grid_dev, i);//Print out element value of Grid_dev->cdata[i]
cudaDeviceSynchronize();
cudaMemcpy(CurrentGrid->cdata[i], Grid_dev->cdata[i], size*sizeof(float),\
cudaMemcpyDeviceToHost); //where error occurs
#if DEBUG_DEV
cudaCheckErrors("cudaMemcpy2 error");
#endif
printf("\nAfter copy cell data\n");
Print<<<1,1>>>(Grid_dev, i);
cudaDeviceSynchronize();
cudaMemcpy(CurrentGrid->vdata[i], Grid_dev->vdata[i], vsize*sizeof(float),\
cudaMemcpyDeviceToHost);
#if DEBUG_DEV
cudaCheckErrors("cudaMemcpy3 error");
#endif
}
cudaMemcpy(CurrentGrid->FieldType, Grid_dev->FieldType,\
NumberOfBaryonFields*sizeof(int), cudaMemcpyDeviceToHost);
#if DEBUG_DEV
cudaCheckErrors("cudaMemcpy4 error");
#endif
}
编辑:这是来自 valgrind 的信息,我试图从中找出内存泄漏发生的位置。
==19340== Warning: set address range perms: large range [0x800000000, 0xd00000000) (noaccess)
==19340== Warning: set address range perms: large range [0x200000000, 0x400000000) (noaccess)
==19340== Invalid read of size 8
==19340== at 0x402C79: CopyDataToHost(param_t, grid*, grid*) (CheckDevice.cu:48)
==19340== by 0x403646: CheckDevice(param_t, grid*, grid*) (CheckDevice.cu:186)
==19340== by 0x40A6CD: main (Transport.cu:81)
==19340== Address 0x2003000c0 is not stack'd, malloc'd or (recently) free'd
==19340==
==19340==
==19340== Process terminating with default action of signal 11 (SIGSEGV)
==19340== Bad permissions for mapped region at address 0x2003000C0
==19340== at 0x402C79: CopyDataToHost(param_t, grid*, grid*) (CheckDevice.cu:48)
==19340== by 0x403646: CheckDevice(param_t, grid*, grid*) (CheckDevice.cu:186)
==19340== by 0x40A6CD: main (Transport.cu:81)
==19340==
==19340== HEAP SUMMARY:
==19340== in use at exit: 2,611,365 bytes in 5,017 blocks
==19340== total heap usage: 5,879 allocs, 862 frees, 4,332,278 bytes allocated
==19340==
==19340== LEAK SUMMARY:
==19340== definitely lost: 0 bytes in 0 blocks
==19340== indirectly lost: 0 bytes in 0 blocks
==19340== possibly lost: 37,416 bytes in 274 blocks
==19340== still reachable: 2,573,949 bytes in 4,743 blocks
==19340== suppressed: 0 bytes in 0 blocks
==19340== Rerun with --leak-check=full to see details of leaked memory
==19340==
==19340== For counts of detected and suppressed errors, rerun with: -v
==19340== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)