user3667089
user3667089

Reputation: 3288

How to make cuda-memcheck return non zero when there is a leak check error

I am on Ubuntu 14.04 with CUDA 7.5. Suppose I have the simple code

int main()
{
  float *test;

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

  cudaDeviceReset();

  return 0;
}

When I run it with

cuda-memcheck --leak-check full ./Test

Obviously there will be leak error because I didn't free the memory, but when I type

echo $?

immediately after running the memcheck, it shows

0

indicating success even when there actually are errors. What can I do to make cuda-memcheck to return non zero when there is actually leak errors? This is important for unit testing.

Upvotes: 0

Views: 328

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 152173

The documentation indicates that to get a non-zero error return from cuda-memcheck, it's necessary to specify the --error-exitcode ? option, where ? is replaced by the desired (non-zero) value that will be returned if an error is identified by cuda-memcheck. Here's a fully worked sequence using your example:

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

You can also find mention of the error-exitcode command line option in the command line help (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
$

Upvotes: 3

Related Questions