Mex files with OpenMP
13 views (last 30 days)
Show older comments
Hello!
I have been trying to make use of a specific amount of threads to run my matlab code. I am using mex to run code in C that includes OpenMP
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1339329/image.png)
This is the part of the code, there are 8 more parts in the code that have that same code, but they are not executed at the same time.
Now the code compiles normaly, however when I watch it run on task manager I see that the CPU usage spreads across every thread, not just 2/4 that I put in my code.
I have tried compiling using many different compilers, Visual Studio 2015, 2017, 2022, mingw and using various flags when compiling, COMPFLAGS, CFLAGS and CXXFLAGS, on Matlab 2018b and Matlab 2022b but I get the same result every time. I installed the parallel computing toolbox and tried creating and enabling clusters from withing the Matlab interface.
Instead of num_threads I have also tried forcing the number of threads with omp_set_num_threads(2) and removing the num_threads but I also get the same result.
omp_get_num_threads returns 8 but omp_get_thread_num is always 1, doesn't change values.
I have tried every solution I found online but nothing works.
Any help would be appreciated!
0 Comments
Answers (1)
Meet
on 4 Apr 2023
By default, MATLAB makes use of the multithreading capabilities of the computer on which it is running. On Windows platforms with multiple processor groups, the default maximum number of computational threads is equal to the number of physical cores within the processor group that is running MATLAB. You can try using the maxNumCompThreads function to control the maximum number of computational threads in a MATLAB session.
Please refer to Control maximum number of computational threads - MATLAB maxNumCompThreads (mathworks.com) for more details.
See Also
Categories
Find more on MATLAB Compiler 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!