MATLAB crashes when uploading a mex file that used CUDA memory

I have been trying to figure this out for some time.

I am using a MEX file in Matlab (Linux 64bit) which uses CUDA. The code compiles and runs fine, but when I want to unload mex (for example, to recompile it or when Matlab comes out), Matlab will immediately work without any message and with an empty dump.

I was able to shorten it to a minimal working example:

MEX cpp file:

#include <stdint.h> #include "mex.h" extern "C" void cudaTest(); void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { cudaTest(); } 

CUDA file compiled using NVCC:

 void cudaTest() { float* d_test = NULL; cudaMalloc((void**) &d_test, 10000 * sizeof(float)); cudaFree(d_test); } 

As long as my real program always crashes, this minimal example does not always play. Sometimes it works sometimes.

+4
source share
2 answers
+1
source

Hmm, Maybe a memory problem that you forgot to free.

Some suggestions may be helpful:

  • Do not use the MATLAB memory management function: mxalloc ..., outside of mexfunction or matlab wrap, your mex function may start some background process and may cause MATLAB to crash when the mex memory management function is working with matlab at the same time.

  • register the mexAtExit (clearfunction) function (see MATLAB help: mexAtExit) clear your mex memory and the stream that is not controlled by MATLAB, namely cudaMalloc. when you unload the mex function or exit matlab, MATLAB will automatically clear the mexfunction. therefore, if your momery control function is not a MATLAB memory management function, MATLAB will not know how to deal with your mex program.

  • debug your function below

mileage:

 clear your_mex_function 

MATLAB will call clearfunction (this function is a mexatexit registration function, see step up) your_mex_function, and you will find out what the problem is with your mex function.

0
source

Source: https://habr.com/ru/post/1446430/


All Articles