Do I need to initiate the parallel computing toolbox for my code to run on multiple cores?

I have a function that I minimize using fmincon. I am exploring the possibility of making my code run faster. I got rid of all for loops and replaced them with vectorised code. I further wanted to try the parallel computing toolbox. Yet before doing so, I checked how many cores MATLAB is using because if it is using already multiple cores, why would I need the parallel computing toolbox? Though I am not sure and this is a first question I would like to ask. I noticed that MATLAB actually does use all the cores when I inititate fmincon. How is it possible that multiple cores are used while I did not initiate the parallel comuting toolbox?

 Accepted Answer

MATLAB has many low level functions are multi-thread coded and potentially use all the CPU core.
In this case parallel tbx will not bring you any speed up on the same computer. You can however use GPU, that can speed up in some specific cases.

4 Comments

Thanks. Is there a list of the functions that are multi-thread coded? Is fmincon among them?
No there is no such list.
fmincon is to me a high level function. There are a bucnh of different algorithms and behavior behind the function. One can *assume* the finite difference gradient computation is multi-threaded. But then if user provide the gradient then the parallelism mechanism is not be used at this level.
Most of fmincon does not use multi-threaded core. However:
  • when executing the objective function, any mathematical calculations there are potentially executed in multi-threaded code.
  • However, automatic multi-threading is only done if MATLAB estimates that it would save time to do that, so it tends to restrict it to "expensive" calculations that can be carried out in parallel, or to operations that work on large arrays. For example adding two arrays can potentially be done with multiple threads -- for example each column could potentially be done independently.... but for small arrays it is faster to do them in a single thread.
  • iterative work can only be effectively run in parallel if it is working on sufficiently large arrays; for the most part, the automatic multi-core algorithms cannot make decisions in parallel.
  • There is a UseParallel option, when enables using the Parallel Computing Toolbox to estimate gradients in parallel.

Sign in to comment.

More Answers (0)

Asked:

on 21 Mar 2022

Commented:

on 23 Mar 2022

Community Treasure Hunt

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

Start Hunting!