How to make Matlab use all CPU cores?

419 views (last 30 days)
Benjamin
Benjamin on 4 Sep 2019
Hello
I have a computer with the configuration of CPU = 2.2 GHz and RAM = 64 GB with 24 cores!
however, since Matlab uses just one core, I have long runtime. I have a "for" loop in my code, but when I use parfor, or parpool my runtime doesn't change or I even get errors. I need to know how can I make my code and Matlab use more cores to accelerate the runs?
Here is the Matlab code

Answers (2)

Rik
Rik on 4 Sep 2019
The value of n is only 63, so the main constriction in your code is the while loop, which can't be parallelized.
Matlab is fundamentally linear, which can only under some circumstances be mitigated. parfor has an overhead, because it needs to duplicate data in memory for each worker. Fewer iterations means less chance to have your investment work out. Mathworks engineers do spend a lot of time speeding up calculations, which is easier for matrix manipulations.
Your main problem is not with the loop itself, it's with 14000 calls to fsolve (see profiler screenshot below). It is probably more efficient to attempt to change your algorithm instead of trying to make your code run in parallel.
Capture.PNG
You should be aware that 2.2GHz isn't that fast in the context of a single-threaded calculation. Higher end CPUs are currently almost double that. Your computer configuration is optimized for multi-threaded workloads, which Matlab is not.

MathWorks Support Team
MathWorks Support Team on 7 Mar 2023
If you have a similar question about using all your CPU cores in MATLAB, try this more generic approach:
First, you can convert for-loops to run in parallel by using a parfor-loop instead. Often, this is as simple as replacing for with parfor. If your code doesn’t easily convert to parfor, see this documentation.
Next, you can compare the number of workers (NumWorkers) you can start on your ‘Processes’ (or if using MATLAB version R2022a or earlier, ‘local’) cluster profile to the number of physical cores on your machine. From the MATLAB desktop Parallel menu, select Create and Manage Clusters (or if you are using MATLAB version R2018a or earlier, select Manage Cluster Profiles). This opens the Cluster Profile Manager window. Select the Processes (or local if using MATLAB version R2022a or earlier) cluster profile and view the value of the NumWorkers property. By default, the Processes/local cluster profile will use the number of physical cores on the machine.
If you would like MATLAB to start more workers than the default NumWorkers value, you can increase the value of the NumWorkers property in the Cluster Profile Manager window. After selecting the Processes/local cluster profile, click the Edit button, change the value of the NumWorkers property and save the cluster profile. Raising the NumWorkers value higher than the default value increases the risk of resource contention and may lead to poor performance or instability.
If your code is running in parallel and you are not seeing the speedup you expect, you can profile your code to see where MATLAB workers spend most of their time. Here is an example showing how to profile parallel code. To find out how code execution speed scales with the number of parallel workers, see this example about resource contention.

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!