1

I have a MATLAB mex library that loads a problem specific cubin file at runtime. This mex function gets called a few hundred times by MATLAB. Is the kernel reloaded each time by CUDA when I call cuModuleLoad? Or is it somehow cached? If not, is there a way to persist the loaded modules in between? I'm not currently calling cuModuleUnload.

It seems like the CUDA context is created only once for the MATLAB process since only the first call to the library is slow. Subsequent matlab function calls to the mex library are fast. So I guess I can assume that the same CUDA context is being reused.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Thomas Antony
  • 544
  • 1
  • 7
  • 17
  • The question [MEX library lifecycle](http://stackoverflow.com/q/20575957/2778484) may be of interest. – chappjc Mar 28 '14 at 22:48

1 Answers1

2

The short answer is yes, you can reuse them.

Global / static variables or static members in C++ in MEX file are only created once when MEX file is dynamically loaded, as long as they do not rely on Matlab memory manager (mxMalloc, mxCreateNumericArray, etc). They are destroyed when Matlab exits or MEX files are unloaded by clear mex. Also it is possible to protect MEX file with MEX API: mexLock to prevent unexpected unloading, mexAtExit to register destructor callback, etc.

kyamagu
  • 551
  • 2
  • 5
  • But I am calling cuModuleLoad inside the function each time it is called. So, my question how CUDA manages the resources. – Thomas Antony Mar 28 '14 at 11:49
  • 1
    That is a question of what happens when you call `cuModuleLoad` multiple times. Check CUDA documentation. But you can initialize CUDA resource only once by having a flag or wrap that in a class constructor / destructor and make that instance static. – kyamagu Mar 29 '14 at 18:17
  • I can try that out. This definitely seems to be causing some issues since after a while, cuModuleLoad fails with error code 2 (out of memory). – Thomas Antony Apr 16 '14 at 22:26