C++ 当存在泄漏检查错误时,如何使cuda memcheck返回非零

C++ 当存在泄漏检查错误时,如何使cuda memcheck返回非零,c++,unit-testing,memory-leaks,cuda,C++,Unit Testing,Memory Leaks,Cuda,我使用的是Ubuntu14.04和CUDA 7.5。假设我有简单的代码 int main() { float *test; cudaMallocManaged(reinterpret_cast<void **>(&test), 100 * sizeof(float), cudaMemAttachGlobal); cudaDeviceReset(); return 0; } 显然会有泄漏错误,因为我没有释放内存,但

我使用的是Ubuntu14.04和CUDA 7.5。假设我有简单的代码

int main()
{
  float *test;

  cudaMallocManaged(reinterpret_cast<void **>(&test), 100 * sizeof(float),
                    cudaMemAttachGlobal);

  cudaDeviceReset();

  return 0;
}
显然会有泄漏错误,因为我没有释放内存,但是当我键入

echo $?
运行memcheck后,它立即显示

0
即使确实存在错误,也表示成功。当确实存在泄漏错误时,如何使cuda memcheck返回非零?这对于单元测试很重要。

表示要从
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获取非零错误返回,必须指定
--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 |grep exitcode
 --error-exitcode <number> [Default : 0]
                       When this is set, memcheck will return the given exitcod  when any errors are detected
$