Mex files with OpenMP

12 views (last 30 days)
chrisath
chrisath on 29 Mar 2023
Commented: chrisath on 6 Apr 2023
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
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!

Answers (1)

Meet
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.
  1 Comment
chrisath
chrisath on 6 Apr 2023
But shouldn't the part of OpenMP code dictate how many threads to specifically use? Does that mean that the command to run the code in 2 threads for example is ignored? Would it make any difference if the OpenMP was directly written in matlab instead of a Mex file?
Thank you for your help!

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!