How to stop Matlab (erroneous) execution of mex file with CUDA functionality

I am currently developing a mex file with CUDA functionality that will be used in MATLAB. When I do something wrong (for example, incorrect pointers or something like that), MATLAB always crashes (windows tell me to complete, send a report to mathworks or try to continue). Is there any way to prevent this? It is very annoying to develop in such a way, but as you probably know yourself: hardly anyone can write the perfect code without trial and error ... Thanks, still!

+7
source share
3 answers

On the Matlab MEX page,

mex -g yourmexfile.c

if you don’t do it already.

+3
source

As far as I know, there is no way to prevent the loss of Matlab by mex error. But you can connect the debugger to the Matlab process and execute the code.

I know this works if your code is in an external dll that you upload to Matlab. I'm not sure if this works with mex files.

+4
source

You can debug mexfiles Matlab, including CUDA codes, using Visual Studio and NVIDIA Nsight for Visual Studio by following the procedure below .

  • Define the system environment variable NSIGHT_CUDA_DEBUGGER and set it to 1 .
  • Launch Matlab .
  • Launch NVIDIA Nsight . Right-click the Nsight Monitor icon on the taskbar and select Options . Select the CUDA tab. For Use this monitor to attach CUDA , click the drop-down menu and select True .
  • Open the project in Visual Studio, set breakpoints and compile it.
  • Go to Tools β†’ Bind to Process .
  • Click the drop-down menu next to the Transport field and select Nsight GPU Debugger .
  • Verify that the host name is specified in the Qualifier field. Note that this field is empty by default; you will have to manually select the name of your computer the first time you open this dialog box.
  • When you enter the computer name in the Qualifier field, a list of available processes appears in the dialog box. Processes that may be associated with the use of CUDA will appear along with the CUDA indicated in the Type column. If a process is inactive and CUDA is listed in the Type column, it is already debugging, so it cannot be attached. Processes that are grayed out without CUDA in the Type column indicate that the process is not using CUDA . Processes that can be attached will usually be displayed, and the Attach button will be enabled.
  • Make sure Matlab has CUDA in the Type column and select it.
  • At the Matlab command line, call the function defined in mexfile CUDA. Then execution stops at the first breakpoint, and debugging can begin.
+1
source

All Articles