Does MATLAB use all cores by default when running a program?
Afficher commentaires plus anciens
I have been thinking about purchasing MATLAB's Parallel Computing toolbox because I will likely be responsible for some large scale computation on a single desktop computer but before that I would like to know one thing. So I guess parallel computation especially MATLAB's related toolbox can utilize all cores available in a computer to speed computation. My question is, since almost all modern PCs contain multiple cores by default, what does the normal MATLAB do when running a program? Is it only using one of those cores when parallel computation toolbox is not being used?
Réponse acceptée
Plus de réponses (2)
Jan
le 16 Déc 2016
2 votes
Many builtin functions use multithreading, e.g. sum, filter and linear algebra functions. They use multiple cores only above a certain limit of input data size. If this happens in your code, the parallel processing toolbox cannot accelerate the code sufficiently, because all cores are working already. But e.g. if the processor idles because it is waiting for data from the disk, a multi-threaded code can be much faster.
4 commentaires
Imam
le 16 Déc 2016
Greg Chance
le 3 Sep 2020
Hi, can you please elaborate on your answer @Jan?
1) Specifically does the variance function use multi-threading?
2) And can you say what the input data size needs to be before it does mulithreading?
3) Is the answer limited by input precision (similar to the numpy issue here) and if so how to control?
Thanks!
Steven Lord
le 3 Sep 2020
1) The var function is a MATLAB function file, so it doesn't use multiple threads directly. Some of the built-in functions called in that function file, like sum, are multi-threaded if the problem size is large enough. The list in this Answers post is a bit old but generally speaking each release we try to improve the performance of our functions and one of the ways we can do that is to thread the function.
2) No. Do not try to tailor your code to our internal thresholds that can change in any release without warning. If you run a piece of code and it's not performing as well as you expect, and a built-in function is the bottleneck (as shown by the profiling tools in MATLAB, report that to Technical Support and ask if there's an alternative or if they can report that to the development staff for investigation.
3) If you pass single precision data into var, the result you receive back will be in single precision. The default behavior of sum (which is the behavior used in var) is "If X is floating point, that is double or single, S has the same class as X. If X is not floating point, S has class double." That second sentence doesn't apply in this context, since var throws an error if you try to call it with integer data.
Ram Kumar Venkateswaran
le 6 Fév 2021
Hi Jan, But when I have a parallel computing toolbox, I can manually give the number of cores right? Is there any way to check if the MATLAB run is distributed among the cores by using the task manager? I believe this should make my code run faster, especially if it takes hours on a single core? Thanks, Ram
John D'Errico
le 16 Déc 2016
2 votes
MATLAB uses multiple cores only when there will be a gain. The problem is that multi-threading itself has overhead. So on simple, fast operations, there is no gain. It sometimes takes serious;y large problems before multiple cores kick in. Yes, I've gotten mine running at times, humming away, getting the fan to kick on, etc. But most of the time, on most operations, each specific call is done before you would even see multiple cores starting up. So if you are doing a lot of small operations on some problem, then no, multi-threading will never happen, and you simply won't gain from anything automatic.
If you are doing the same sets of operations on multiple problems, then yes, you can gain from using parallel processing, because you farm the whole operation out to separate processors, with one processor handling each separate problem. n processors, so it will approach an n-fold speedup. But again, there are costs in this, so you never gain a full n-fold speedup from using n cores.
Catégories
En savoir plus sur Performance and Memory dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!