cuda程序常见异常汇总

cuda程序常见异常汇总,第1张

1. CUDA out of memory

跑cuda 程序遇到下面错误:

RuntimeError: CUDA out of memory. Tried to allocate 588.00 MiB (GPU 011.00 GiB total capacity8.97 GiB already allocated190.44 MiB free9.00 GiB reserved in total by PyTorch)

运行程序之前,使用nvidia-smi 查看显存有没有被占用,如果有被占用5M以上,可能是显存没有被释放。通过如下命令来释放显存。

linux上使用命令行,云主机可以重启电脑

fuser -k /dev/nvidia* 或者 kill $(lsof -t /dev/nvidia*)

2. 减小batch size。

3. 更换更大显存的云主机。

如果用的是Windows中的Visual Studio的Nsight插件,可以在kernel中设置断点,然后选择Nsight插件中的Start CUDA Debugging来看出错的语句。

如果是Mac或Linux,请考虑使用CUDA-GDB。

另,在每次调用完kernel后写一句话可以检查基本的kernel调用错误,例如

// This will output the proper error string when calling cudaGetLastError

#define getLastCudaError(msg)      __getLastCudaError (msg, __FILE__, __LINE__)

inline void __getLastCudaError(const char *errorMessage, const char *file, const int line)

{

    cudaError_t err = cudaGetLastError()

    if (cudaSuccess != err)

    {

        fprintf(stderr, "%s(%i) : getLastCudaError() CUDA error : %s : (%d) %s.\n",

                file, line, errorMessage, (int)err, cudaGetErrorString(err))

        DEVICE_RESET

        exit(EXIT_FAILURE)

    }

}

#endif

kernel<<<1,1>>>()

getLastCudaError("Error in Calling 'kernel'")


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7159058.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-02
下一篇2023-04-02

发表评论

登录后才能评论

评论列表(0条)

    保存