Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Matlab crashing on (wrong) mex-file execution with CUDA functionality

I'm currently developing a mex-file with CUDA functionality to be used in MATLAB. When I'm doing something wrong (e.g. wrong pointers or something like that), MATLAB always crashes (windows prompts me to end, send the report for mathworks or attempt to continue). Is there a way to prevent this from happening? It's really annoying to develop like that but as you probably know yourself: Hardly anybody can write perfect code without 'trial and error'... Thanks so far!

like image 508
tim Avatar asked May 12 '11 14:05

tim


3 Answers

As far as I know, there is no way to prevent Matlab from crashing on a mex bug. But you may be able to attach a debugger to the Matlab process and step through the code.

I know for a fact that this works if your code is in an external dll that you load into Matlab. I am not sure if this works with mex files.

like image 165
Dima Avatar answered Oct 16 '22 08:10

Dima


From the Matlab MEX file page,

mex -g yourmexfile.c

if you're not doing this already.

like image 3
Matt Phillips Avatar answered Oct 16 '22 07:10

Matt Phillips


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

  1. Define the system environmental variable NSIGHT_CUDA_DEBUGGER and set it to 1.
  2. Launch Matlab.
  3. Launch NVIDIA Nsight. Right click on the Nsight Monitor icon on the taskbar and select Options. Select the CUDA tab. For the option Use this Monitor for CUDA attach, click the drop-down menu and select True.
  4. Open your project in Visual Studio, set the breakpoints and compile it.
  5. Go to Tools -> Attach to Process.
  6. Click the drop-down menu next to the Transport field, and choose Nsight GPU Debugger.
  7. Ensure that your host machine name is listed in the Qualifier field. Note that this field is blank by default; you will have to manually select your machine name the first time this dialog is opened.
  8. When you enter your computer's hostname in the Qualifier field, a list of available processes will appear in the dialog box. Processes that may be attached with CUDA usage will appear with CUDA listed in the Type column. If a process is grayed out and CUDA is listed in the Type column, it is already being debugged, so it is not attachable. Processes that are grayed out without CUDA in the Type column indicates that it has no CUDA usage in the process to be debugged. Processes that may be attached will appear normally, and the Attach button will be enabled.
  9. Ensure that Matlab has CUDA in the Type column and select it.
  10. From the Matlab command line, invoke the function defined in the CUDA mexfile. The execution will then stop at the first breakpoint and debugging could start.
like image 2
Vitality Avatar answered Oct 16 '22 08:10

Vitality