When I got it to work, I attached the NSight debugger to the MATLAB process, it does not matter if the CUDA process is active when you initially attach to MATLAB. I compiled the mex as 64-bit because MATLAB was also 64-bit. I am not sure if it would work if your CUDA app is 32 bit and MATLAB is 64 bit, or vice-versa. I’ll try to reproduce it again when I get the chance and maybe post the relevant steps.
I reproduced it successfully, and here are the steps I took:
Here is a snippet on how to set up the Visual Studio environment for building a mex file (jorre’s answer):
While you’re setting environment variables, create one named: NSIGHT_CUDA_DEBUGGER and give it a value of 1.
Besides the tips mentioned in the stackoverflow link above, I would also name the Visual Studio project the same as the mex file name you want to run/debug with MATLAB, and under the project properties, output directory, just make it: $(SolutionDir)\ This directory should also have the relevant MATLAB code that calls the mex file. Usually Visual Studio makes a directory under the path of the current directory you tell it to – I just end up moving the solution files one level up and deleting that, so for me SolutionDir is where I also keep my source code.
I also had to add cuda.lib in order to be able to compile a function that reports GPU memory usage to the already existing linker libraries when I made a new CUDA 5.5 Visual Studio project type from the existing template.
After compiling with debug symbols in x64 mode, start MATLAB x64, and leave it on standby to execute your M code that calls the created .mexw64 file.
Next, go to DEBUG menu in Visual Studio and select ‘Attach to Process’. Pick ‘Nsight GPU Debugger’ Transport for debugging GPU code, and type: localhost as the qualifier (assuming you’re debugging on your local machine) and click ‘Refresh’. You should see MATLAB process selectable now – select it and click ‘Attach’. At this point, if you stop debugging, your MATLAB process will exit, just a warning. :) Set your breakpoints in Visual Studio at this point.
Now, back to MATLAB, run the M code that calls the mex file, and you should be able to step through the breakpoints you set. Confusing eh? ;) Just replicated this on Visual Studio 2012, MATLAB R2013b, Windows 8.1 RTM, and NSight Visual Studio build 18.104.22.16833, so even under the latest software it works just fine. :)
As an aside, last time I tried to do this, I didn’t even compile the mex file under Visual Studio (because I couldn’t figure out how to do it at the time, ha!), and did it with the help of a gmex script that Accelereyes had bundled into their Jacket SDK distribution. As long as I had compiled with debug symbols (-G), I was able to place breakpoints in the code and attach it in the same way as above, without actually having to compile it under Visual Studio. Obviously if I changed something in the code, I’d have to recompile via MATLAB with the gmex script, so it was a bit more painful.
I actually just re-tested this method and it also still works – no need to even compile it under Visual Studio as long as it’s compiled with debug symbols via the gmex method – my project is simple, so it only had one .cu file. This might not work if the mex file is divided into several .cu/.cpp files. YMMV. However, the first method should always work.