Main Content

Debug CUDA MEX Functions

You can debug your generated CUDA® MEX function using MATLAB® or a CUDA debugger. To debug your CUDA MEX functions in MATLAB, use the disp function to inspect the contents of your MEX function variables. You cannot use save to debug MEX function variables because code generation does not support it. Code generation does not support declaration of save as extrinsic. You can also use the fprintf function to inspect the contents of your MEX function variables.

Debug CUDA MEX Functions by Using a Debugger

This example shows how to debug CUDA MEX functions using a debugger.

  1. Consider an entry-point function foo that squares each element of a matrix x and scales the result by a factor of 1/(i+j), where i,j are the row and column indexes.

    function [y] = foo(x) %#codegen
    
    y = coder.nullcopy(zeros(size(x)));
    coder.gpu.kernelfun();
    for i = 1:size(x,1)
        for j = 1:size(x,2)
            y(i,j)=(x(i,j)^2)/(i+j);
        end
    end
    end
    
  2. To build a CUDA MEX function with debugging symbols included, set the MEX configuration object property EnableDebugging to 1.

    cfg = coder.gpuConfig('mex');
    cfg.EnableDebugging = 1;
    input = rand(32);
    
    codegen -config cfg -args {input} foo
    
    Alternatively, you can debug your MEX function by executing this command:

    codegen -g -args {input} foo
    

You can debug the generated CUDA MEX (foo_mex) by using the Visual Studio® CUDA debugger on Windows® or the CUDA GNU® debugger cuda-gdb on Linux® systems.

Debug on Microsoft Windows Platforms

This example shows the general steps to debug foo_mex by using the NVIDIA® Nsight Visual Studio Edition CUDADebugger. For specific information about using Nsight VSE, refer to NVIDIA documentation.

  1. After generating the CUDA MEX function, start Visual Studio. Do not exit your MATLAB session.

  2. Attach the debugger to the running MATLAB process by selecting Debug > Attach to Process or press Ctrl + Alt + p in Visual Studio. For more information, refer to your Visual Studio documentation.

  3. Set breakpoints in code. Select Debug > New Breakpoint in Visual Studio. For more information, refer to your Visual Studio documentation.

  4. Open MATLAB and type:

    out = foo_mex(inputs);
    

    foo.cu is opened in the Visual Studio CUDA debugger at the first breakpoint.

  5. If you select Debug > Continue, code execution completes and the results can be verified in MATLAB.

Debug on Linux Platforms

The CUDA GNU Debugger cuda-gdb, available for Linux systems as part of the CUDA Toolkit installation, provides complete source code debugging, including the ability to set breakpoints, examine variables, and step through the source code line-by-line.

In this procedure, the MATLAB command prompt >> is shown in front of MATLAB commands, and linux> represents a Linux prompt; your system might show a different prompt. The debugger prompt is <cuda-gdb>.

  1. To debug with cuda-gdb, at the Linux prompt, start the cuda-gdb debugger using the matlab function -D option.

    linux> matlab -Dcuda-gdb
    
  2. Tell cuda-gdb to stop for debugging.

    <cuda-gdb> handle SIGSEGV SIGBUS nostop noprint
    <cuda-gdb> handle SIGUSR1 stop print
    
  3. Start MATLAB without the Java® Virtual Machine (JVM™) by using the -nojvm startup flag.

    <cuda-gdb> run -nojvm
    
  4. In MATLAB, enable debugging with the dbmex function and run your binary MEX file.

    >> dbmex on
    >> out = foo_mex(rand(32));
    
  5. You are ready to start debugging.

    It is often convenient to set a breakpoint at mexFunction so you stop at the beginning of the gateway routine.

    <cuda-gdb> break mexFunction
    <cuda-gdb> r
    
  6. Once you hit one of your breakpoints, you can make full use of any commands the debugger provides to examine variables, display memory, or inspect registers.

    To proceed from a breakpoint, type:

    <cuda-gdb> continue
    
  7. After stopping at the last breakpoint, type:

    <cuda-gdb> continue

    Code execution finishes and the results can be verified on MATLAB.

  8. From the MATLAB prompt you can return control to the debugger by typing:

    >> dbmex stop
    

    Or, if you are finished running MATLAB, type:

    >> quit
    

    When you are finished with the debugger, type:

    <cuda-gdb> quit
    

    You return to the Linux prompt.

For more information on CUDA debugger, refer to NVIDIA documentation.

See Also

| | | |

Related Topics