Home > database >  How do you increase CPU usage to 100 percent in matlab?
How do you increase CPU usage to 100 percent in matlab?

Time:06-08

I have used a parfor loop, but the CPU usage is around 50%. The configuration of the computer is shown in the picture. Are there only 4 cores that I can use? Is there a command to open all the cores? Does it matter how I write parfor?

The simplified codes are as follows:

n = 5;
d = 2^n;
r0 = [2 3];
m = d^2;
delta0 = 0:0.05:0.5;

ave = 50;
tic;
for j = 1:length(r0)
  for k1 = 1:length(delta0)
    delta = delta0(k1);
    r = r0(j);

    parfor i = 1:ave

    % Getdataz and Solve_CC_rhoE_zGau are function file
    [Pauli,y,rhoT,noiseT] = Getdataz(d,n,r,m,s,delta);
    [rhoE,noiseE] = Solve_CC_rhoE_zGau(n,r,m,s,Pauli,y,noiseT,delta);

    rhoE = rhoE/trace(rhoE);

   FdRho(i) = fullfidelity(rhoT,rhoE);
   end
   out.delta(k1,1) = delta;
   out.FdRho(k1,:) = FdRho;

   end  
 end
toc;

enter image description here

CodePudding user response:

One of the bigger problems in your code, is that you've got three nested for or parfor loops and parallelised the innermost one. However, it's recommended to parallelise the outermost one whenever possible. Given you haven't provided us with either Getdataz or Solve_CC_rhoE_zGau, I'm going to assume those are rather light functions, making MATLAB spend more time on shifting data back and forth between the workers, rather than the actual computation.

You only have 1000 iterations, which isn't a lot for parfor. Either spmd() or parfeval might be better suited to your case. If you want to keep using parfor, rearrange your loops as such:

parfor k1 = 1:length(delta0)
    for jj = 1:length(r0)
        for ii = 1:ave
            (...)
        end
    end
end

Since r0 only contains two values, don't use that as outer loop, as you'd only have two parallel running threads that way. Also, given i and j are built-in variables, I usually caution against their use as loop variables.

You might want to read Decide when to use parfor and Convert for-loops into parfor-loops. Usually, parfor is recommended when you have a lot of iterations (magnitudes above your 1000), or each separate iteration is very heavy (in which case spmd or parfeval are recommended). See this answer of mine for a short summary.

CodePudding user response:

Multi threading is a complicated subject, especially in MATLAB where you have little control over how it is done.

Performance

First off, parallelization is not the only way to increase performance and should not be your only got-to method. Here are what MATLAB suggests. And @Adriaan suggests a few improvements that would probably improve performances more than using additional CPU resources.

Why not 100 %

The reason you're not getting 100% CPU usage is that MATLAB uses as much workers as you have physical cores. Your CPU has 4 physical cores, 8 logical ones, that's why it has about 50% usage.

Full CPU usage does not mean shorter execution time

Getting 100 % does not guarantee that your code will execute faster. There are multiple reasons why it might or might not work for you. If you are interested look at the comments under this pose and at this post from MATLAB answers. The ultimate answer it that you have to try and time you execution to see if using more resource actually improve your execution time.

Getting to 100 %

There are two ways you can force MATLAB to use 100% of your CPU.

  1. You can increase the number of workers that MATLAB uses to match the number of logical cores you have
  2. You can increase the number of threads each worker uses

To do that:

  • In the home toolbar
  • Under ENVIRONMENT
  • Go to the drop-down menu Parallel
  • Select Create and Manage Clusters...
  • On the left list, select the cluster you want to use (in general local (default))
  • Click edit on the bottom right
  • Increase NumWorkers (for option 1) or increase NumThreads (for option 2).
  • If you increased NumWorkers you might need to also increase the preferred number of workers in parallel preferences to actually have all of them starting in your parallel pool.

Final notes

  • Be careful when increasing NumWorkers. In my personal experience it can crash MATLAB on Ubuntu 22.04
  • Do not expect too much improvement. In general MATLAB is all about doing floating point operations. However each physical core usually only has one floating point unit (FPU). Hence you might not get as much improvement as you'd hope
  • The MATLAB preset for the number of workers is usually a good rule of thumb. I would recommend working on other aspect of optimization and only meddling with that in last resort.
  • Related