Mex files with OpenMP

12 次查看(过去 30 天)
chrisath
chrisath 2023-3-29
评论: chrisath 2023-4-6
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!

回答(1 个)

Meet
Meet 2023-4-4
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 个评论
chrisath
chrisath 2023-4-6
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!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by