7

I successfully used cuda-memcheck to get errors about wrong memory accesses. Compiling the cuda code with -g -G gave nice source locations like this:

========= Error: process didn't terminate successfully
========= Invalid __global__ read of size 1
=========     at 0x00000710 in /some/path/somefile.cu:117:some_function
=========     by thread (0,14,0) in block (1,16,0)
=========     Address 0x00abac20 is out of bounds

Now I tried to use the -l switch to get also memory leak information. Here, however, I get only addresses:

========= CUDA-MEMCHECK
========= Leaked 3630 bytes at 0x007d2800
=========
========= Leaked 14740 bytes at 0x008e0700
...
=========
========= LEAK SUMMARY: 11122140 bytes leaked in 39 allocations
========= ERROR SUMMARY: 0 errors
400 bytes at 0x005d2000

How can I get the actual code locations from this?

Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105

1 Answers1

4

The addresses provided for the leak are not code addresses but rather data locations. Unfortunately it's not easy to see where these locations were allocated.

Given that the memory could have been allocated anywhere (remember that pointers can be passed around, aliased, etc.) the only way to check for leaks (i.e. allocated memory that is not freed) is when the program exits. So when your program exits cuda-memcheck checks for chunks of memory that were allocated but not freed and gives you the address of the chunk of memory, but it has no way to tie that back to when it was allocated.

Instead the easiest is to manually inspect your code to check that all cudaMalloc() calls have a matching cudaFree() call. This can, however, be quite a laborious process...

Tom
  • 20,852
  • 4
  • 42
  • 54
  • VC++ handles this by assigning a serial number to each allocation as it it performed, and then outputting that serial number together with the leak summary at exit. By running the same code with the same data twice, one can first find the serial number of the allocation that leaked and then where that allocation occurred. – Roger Dahl Mar 08 '12 at 17:18
  • 1
    To follow up on my previous comment, you can create a wrapper for cudaMalloc() that increases a statically declared serial number and write the serial number and the allocated memory address to stdout or a file. After the program ends, look up the leaked address in what was printed by the cudaMalloc() wrapper to find the serial number for the allocation that leaked. Then modify the cudaMalloc() wrapper to have it break when that serial number comes up and rerun the program. – Roger Dahl Mar 08 '12 at 23:41