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;

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.
To do that:
ENVIRONMENTParallelCreate and Manage Clusters...local (default))NumWorkers(for option 1) or increaseNumThreads(for option 2).NumWorkersyou might need to also increase thepreferred number of workersinparallel preferencesto actually have all of them starting in your parallel pool.Final notes
NumWorkers. In my personal experience it can crash MATLAB on Ubuntu 22.04