File Exchange

image thumbnail

max_in_place - example of multi-threaded MEX function

version 1.0.4 (10.3 KB) by Yair Altman
Example of a multi-threaded MEX function, that can easily be adapted

2 Downloads

Updated 19 Jul 2018

View Version History

View License

Max_in_place is a sample MEX file that shows a generic framework for multi-threaded processing in MEX functions.

Syntax: max_in_place(matrix1, matrix2)

The 2 input matrices must be real non-sparse and have the same number of elements. The matrix1 input is updated in-place (without memory reallocation) with the maximal value of corresponding elements in the two matrices.

Multi-threading is controlled via Matlab's builtin maxNumCompThreads function.
The code can easily be adapted for other processing types.

A detailed description is found at: http://UndocumentedMatlab.com/blog/multi-threaded-mex

Cite As

Yair Altman (2021). max_in_place - example of multi-threaded MEX function (https://www.mathworks.com/matlabcentral/fileexchange/68237-max_in_place-example-of-multi-threaded-mex-function), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

Xingwang Yong

Maybe line 74 should be deleted, because it is somewhat misleading.
/*mexPrintf("Starting thread #%d: idx=%d:%d\n", ThreadID, startIdx, endIdx); */
If you uncommen this and run, it will crash MATLAB. I tried this in R2020a on Windows and R2018a on Linux, both crashed.
"Since the MEX API is not thread safe ... including printf...", from https://www.mathworks.com/matlabcentral/answers/101658-is-it-possible-to-start-new-threads-from-a-c-mex-file

Yunhui Zhou

MATLAB Release Compatibility
Created with R2017b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!