我在 Ubuntu 14.04 和 CUDA 7.5 上。假设我有简单的代码
int main()
{
float *test;
cudaMallocManaged(reinterpret_cast<void **>(&test), 100 * sizeof(float),
cudaMemAttachGlobal);
cudaDeviceReset();
return 0;
}
当我运行它时
cuda-memcheck --leak-check full ./Test
显然会出现泄漏错误,因为我没有释放内存,但是当我输入时
echo $?
运行memcheck后立即显示
0
即使实际上存在错误也指示成功。当实际存在泄漏错误时,我该怎么做才能使 cuda-memcheck 返回非零值?这对于单元测试很重要。
最佳答案
documentation表示要从 cuda-memcheck
获得非零错误返回,必须指定 --error-exitcode ?
选项,其中 ?
替换为所需的(非零)值,如果 cuda-memcheck 识别出错误,将返回该值。这是使用您的示例的完整工作序列:
$ cat t23.cu
int main()
{
float *test;
cudaMallocManaged(reinterpret_cast<void **>(&test), 100 * sizeof(float),
cudaMemAttachGlobal);
cudaDeviceReset();
return 0;
}
$ nvcc -arch=sm_52 -o t23 t23.cu
$ cuda-memcheck --leak-check full --error-exitcode 1 ./t23
========= CUDA-MEMCHECK
========= Leaked 400 bytes at 0x600000000
========= Saved host backtrace up to driver entry point at cudaMalloc time
========= Host Frame:/lib64/libcuda.so.1 (cuMemAllocManaged + 0x193) [0x13eb73]
========= Host Frame:./t23 [0x2f125]
========= Host Frame:./t23 [0xc3a1]
========= Host Frame:./t23 [0x3f6f0]
========= Host Frame:./t23 [0x269e]
========= Host Frame:/lib64/libc.so.6 (__libc_start_main + 0xf5) [0x21d65]
========= Host Frame:./t23 [0x2589]
=========
========= LEAK SUMMARY: 400 bytes leaked in 1 allocations
========= ERROR SUMMARY: 0 errors
$ echo $?
1
$
您还可以在命令行帮助 (cuda-memcheck --help
) 中找到对 error-exitcode
命令行选项的提及:
$ cuda-memcheck --help |grep exitcode
--error-exitcode <number> [Default : 0]
When this is set, memcheck will return the given exitcod when any errors are detected
$
关于c++ - 发生泄漏检查错误时如何使cuda-memcheck返回非零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35328518/