cudaMemcpy从device到host失败后,cudaMalloc好像出问题了

各位高手好,最近刚学CUDA,改写自带的例子时显示下面的错误,即输出Failed to copy vector C from device to host

err = cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost);
if (err != cudaSuccess)
{
fprintf(stderr, “Failed to copy vector C from device to host (error code %s)!\n”, cudaGetErrorString(err));
exit(EXIT_FAILURE);
}

请问一般出现这种错误的原因,还有就是出现这个错误后,导致再次运行这个程序或其他程序时,代码运行到cudaMalloc函数时好像卡住了,是不是因为由于前面代码出现错误,后面的cudaFree函数没执行导致的,还是其他原因。

LZ您好,cudaMemcpy这个函数这里报错的话,您可以检查一下该函数的几个参数是否正确。

比如h_C是否确实是指向host端内存的指针,以及是否合适地申请了空间(要大于等于size BYTE)。
比如d_C是否确实是指向device端显存的指针,申请的空间是否大于等于size BYTE。
size的大小合适么?

按照您给出的代码,报错信息应该还有cudaGetErrorString()给出的cuda错误类型,您也不妨贴出来。

在一段程序内,如果前面有CUDA API的错误或者kernel的错误,那么后面所有的cuda操作都将不执行,提示错误。
但是,一般程序关掉之后,下次运行与上次无关。

大致分析如上,您第二个情况有待其他人补充。

祝您好运~

2次启动进程实际上会依次建立2个无关的cuda context.

楼主你不应该观察到这种情况。

我建议您,请给出足够的示例支撑您的“前一个进程挂了。再次运行下一个进程会导致使用cuda失败”的观点。

以及,在您的进程结束的时候,该进程建立的cuda context里的所有资源均被释放。所以你的假设不成立。

所以,请楼主给出足够例子。
或者无需给出例子,直接接受我的“不会影响下一个运行的进程”的观点,而从其他方面排查问题。

谢谢两位版主的热心帮助,就是现在运行代码出现错误后,再次调试会出现下面的问题

“vectorAdd.exe”: 已加载“D:\Documents\Visual Studio 2010\bin\win32\Debug\vectorAdd.exe”,已加载符号。
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\ntdll.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\kernel32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\bin\cudart32_50_35.dll”,未使用调试信息生成二进制文件。
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\nvcuda.dll”,未使用调试信息生成二进制文件。
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\user32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\gdi32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\advapi32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\rpcrt4.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\secur32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\shell32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\msvcrt.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\shlwapi.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\imm32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\lpk.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\usp10.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.6028_x-ww_61e65202\comctl32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\comctl32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\nvapi.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\ole32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\oleaut32.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\setupapi.dll”,Cannot find or open the PDB file
“vectorAdd.exe”: 已加载“C:\WINDOWS\system32\version.dll”,Cannot find or open the PDB file
线程 ‘Win32 线程’ (0x1164) 已退出,返回值为 -1073741510 (0xc000013a)。
线程 ‘Win32 线程’ (0x135c) 已退出,返回值为 -1073741510 (0xc000013a)。
线程 ‘Win32 线程’ (0x1044) 已退出,返回值为 -1073741510 (0xc000013a)。
程序“[860] vectorAdd.exe: 本机”已退出,返回值为 -1073741510 (0xc000013a)。

这个说明不了什么,你的图实际上是任何一次普通调试都可能出现的。和你的上文完全无关。

实际上"对system32下的dll找不到调试信息“,是因为你用的不是调试版的windows(例如你的普通正式零售版的windows就无调试信息),以及,您没有配置您的VS从MSDN上同步回来windows组建的调试信息文件。

以及,cudart的dll无调试信息是正常的,nv也没有发布带有调试符号信息的runtime dll.

所以我不认为你此图代表什么问题。

你说呢?