How to run matlab coder generated code in serial / How to tell matlab coder not to use tbb libraries

I'm using Matlab coder for generating c++ code.
I see that Matlab uses an internal Matlab libraries (dlls and libs) like:
libmwmorphop_binary_tbb.lib
libmwbwlookup_tbb.lib
Which is any library in the following format:
libmw*_tbb.lib
Due to an overall system performance, I'd like not to use the tbb libraries of Matlab - How can I generate code that does that ? (Please consider that package command shall pack all the non tbb libraries)
I also searched on Matlab installation folder and found that for every tbb library there is a matching non tbb library.
Ashish Uthama suggestion:
start matlab with -singleCompThread doesn't chagne the generated code or dependent dlls from matlab coder.
First try:
I tried to replace the libraries between the tbb version to the non tbb version - however matlab have different functions signature to the tbb functions - therefor this endeavor didn't work.
First try continues:
I manually changed the code and used the non tbb functions, I linked against the non tbb libraries.
matlab code still runs in parallel.
Second try:
From the thread that calls my matlab generated function in C++ , I executed the following commands:
Unfortunately the code still runs in parallel. It’s most likely that these code doesn’t change the execution for sealed matlab dll.
omp_set_num_threads(1);
tbb::task_scheduler_init tbbInit(1);
Third try:
I set the coder configuration: “enable openmp flag” to false.
I run the coder one more time. Matlab doesn’t change the code, the code still uses matlab’s tbb functions.
Forth try
I run the visual studio profiler, there was one line with a large portion of execution time:
kmp implicit barrier
I called off kmp by setting the environment variable
KMP_DEVICE_THREAD_LIMIT to 1
Now my matlab function doesn’t create extra superfluous threads
I don’t know what matlab are doing under the hood however setting this environment variable now makes my application run single threaded
Maybe someone from matlab would give a more detail explanation
Setting the environment variable changes the whole system behavior. If there are other processes that uses openMP I’d limit their number of threads too. How to apply this change only for single thread or a single process.
Any help would be highly appreciated

Answers (1)

Would you please try starting MATLAB with -singleCompThread on the session that is generating the code and check if that works for you? This ought to force MATLAB Coder to use the non-tbb versions of the libraries.
I understand this was not easily discoverable.
Could you elaborate more on why you want to do this (generate single thread code for the host)? Knowing that will help us figure out how to address this pain better in the future.

5 Comments

I think that your advice will not work, because singleCompThread works for matlab runtime, in my case I'm not using matlab runtime.
The system I work on is working with 8 different processes running on the same physical computer - i7 6th generation.
My applications is currently suitable to single threaded application only - I can't let matlab code start another 8 working threads- this will cause an high cpu utilization and thread starvation.
As I mentioned before, I have already changed (by hand) the code from its tbb version to its non tbb version, unfortunately it didn't change the number of working threads
Most (if not all) image processing functions use the number of threads on the host compile session to decide wheather to use TBB or not. Please give it a try.
Dear Ashish Uthama
Thank you for your replay, It took me a while to get back to this project.
Your solution didn't work. It did not change even a single bit from the package command output at the end of the coder process.
For the first time, I used the coder after I opened matlab with the -singleCompThread flag. I also verified that maxNumCompThreads is equal to 1.
I used the coder without the flag (regulary), the output code after package command is identical, also the dll files are identical.
Any further suggestion ?
Best
S
At this time, it might be best to create a formal service request for us to help you out better:
https://www.mathworks.com/support/contact_us.html

Sign in to comment.

Products

Release

R2018b

Asked:

on 31 Mar 2020

Commented:

on 23 Apr 2020

Community Treasure Hunt

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

Start Hunting!