如果存在泄漏检查错误,如何使cuda-memcheck返回非零

时间:2016-02-11 00:12:28

标签: c++ unit-testing memory-leaks cuda

我使用CUDA 7.5在Ubuntu 14.04上。假设我有简单的代码

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返回非零值?这对于单元测试很重要。

1 个答案:

答案 0 :(得分:3)

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
$

您还可以在命令行帮助(error-exitcode)中找到cuda-memcheck --help命令行选项的提及:

$ 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
$