Can I make use of OpenMP in my MATLAB MEX-files?

조회 수: 38 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2015년 8월 28일
편집: MathWorks Support Team 2025년 2월 4일
I would like to make use of the OpenMP parallel programming features in my MATLAB MEX-files; is this supported?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2025년 3월 11일
편집: MathWorks Support Team 2025년 2월 4일
MathWorks does not specifically test or support using OpenMP in customer-written MATLAB MEX-files. The article below discusses several points that should help you in successfully using OpenMP in MEX-files.
*Compiler Support*
To utilize OpenMP, you will first need a C/C++ compiler that supports OpenMP.
- On Windows: not all supported MEX compilers support OpenMP. For example, Microsoft Windows SDK 7.1 does not, nor do many of the Microsoft Visual C++ Express editions, which are supported MEX compilers in older MATLAB versions. Typically, the Microsoft Visual C++ Professional Editions do support OpenMP.
- On Mac: the default MEX Compiler in recent MATLAB for Mac versions is clang as included with Xcode 5.x and 6.x. The clang compilers included in Xcode 5 and 6 do not support OpenMP. MathWorks does not offer support for using compilers other than the officially supported MEX compilers; however, it should theoretically be possible to use gcc/g++ (compiled by yourself or obtained elsewhere) on Mac to create MATLAB MEX-files.
- On Linux: the supported MEX compilers gcc/g++ typically support OpenMP without problems.
*Incompatibilities between OpenMP implementations*
Microsoft Visual C++ will by default link your objects to Microsoft's OpenMP implementation, while gcc/g++ will typically link against libgomp (GNU OpenMP). MATLAB itself uses Intel's OpenMP implementation, which can lead to incompatibilities when executing your MEX-file linked against Microsoft OpenMP or GOMP in MATLAB. To avoid these incompatibilities, we recommend using Intel's OpenMP implementation in your MEX-files. The following page on the Intel website explains how various non-Intel compilers and linkers (like gcc/g++ and Microsoft Visual C++) can be used to compile code for and link against Intel's OpenMP:
To learn more about how to override the compiler and linker flags when calling "mex" so that you can apply the correct flags, refer to the documentation page for "mex". You can open this page by typing "doc mex" in your MATLAB Command Window.
For release-specific documentation, please run the following command in the MATLAB R2020b command window:
>> web(fullfile(docroot, 'matlab/ref/mex.html'))
Furthermore, the Intel OpenMP import libraries (libiomp5.lib/so/dylib) can be found in your bin\win32 or bin\win64 directories for 32- and 64-bit MATLAB on Windows, respectively, and in the sys/os/glnxa64 directory of your MATLAB installation on Linux and sys/os/maci64 on Mac.
*Version Compatibility*
The MATLAB documentation states that "for best results, your version of MATLAB must be the same version that was used to create the MEX-file" but also that "MEX-files are usually backward compatible." For MEX-files that use OpenMP and are linked according to the instructions above, the former holds true, but the latter does not. You should rebuild and relink your OpenMP MEX-files for each MATLAB release you are working with.
Please use the below link to search for the required information in the current release: 

추가 답변 (1개)

James Tursa
James Tursa 2015년 8월 28일
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
James Tursa
James Tursa 2020년 12월 17일
Can you create a minimum working example that crashes and post it?
Bruno Luong
Bruno Luong 2020년 12월 17일
I have written Mex with omp_set_num_threads to a number and pragma omp parallel for and it works just fine.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by