Can I make use of OpenMP in my MATLAB MEX-files?
Show older comments
I would like to make use of the OpenMP parallel programming features in my MATLAB MEX-files; is this supported?
Accepted Answer
More Answers (1)
James Tursa
on 28 Aug 2015
You left out one extremely important detail. The API functions that use the MATLAB Memory Manager are not thread-safe! That is, you should never use any API functions that allocate memory inside of a parallel thread ... doing so will typically crash MATLAB.
Examples of API function that do not allocate memory (and hence are thread-safe):
mxGetPr
mxGetPi
mxGetIr
mxGetJc
mxGetNumberOfDimensions
etc.
Examples of API functions that do allocate memory (and hence are not thread-safe):
mxArrayToString
mxCreateDoubleMatrix
mxCreateNumericArray
etc.
Questionable:
mxGetDimensions if mwSize does not match size_t
For the last one, if mwSize matches size_t (i.e., both 32-bit or both 64-bit), then the mxGetDimensions call returns a pointer to the actual dimensions array that is part of the mxArray. In that case the call is probably thread-safe. But if they don't match, then the mxGetDimensions call will return a pointer to a copy of the dimensions array. This is not discussed in the documentation, so it is unclear to me if this copy was already pre-allocated at the time of the mxArray creation (in which case the call would be thread-safe) or if the mxGetDimensions call itself does an allocation (in which case the call would not be thread-safe). I haven't tried tests cases to try and determine this, so caveat emptor.
BOTTOM LINE: If you need to use API functions that allocate memory, do it outside of your parallel threads!
4 Comments
Peter Cook
on 8 Apr 2019
@Tim Davis,
One technique I use to circumvent mxMalloc/mxCalloc is to put my heavy lifter functions in a header file (separate from wherever mexFunction sits) and send pointers to MATLAB input data sources (via mxGetData) to these functions, I can then dynamically manage memory normally with malloc/calloc/realloc/free over there. Basically anything I write in pure C/C++ without mex.h header functions live there.
As for output data, I haven't run into trouble (yet?) with OpenMP by using plhs[k] = mxCreateNumericArray(...) in mexFunction and shipping a pointer to that array over to get filled in (except when trying to use strided/pitched/aligned pointers).
Also, in general when working with OpenMP (which, disclaimer, I have not been doing that long, likely much less time than you) I try to preallocate whatever is coming out of the parallel loop and just shift pointers around via omp_get_thread_num(). I peeked at SuiteSparse on GitHub and couldn't really assess if this was an option in your application.
James Tursa
on 17 Dec 2020
Can you create a minimum working example that crashes and post it?
Bruno Luong
on 17 Dec 2020
I have written Mex with omp_set_num_threads to a number and pragma omp parallel for and it works just fine.
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!