In my code the parfor is slower than for loop when using backslah with matrices.

1 view (last 30 days)
I have following code. I am using backslash for matrices and parfor loop is slower than for loop. What kind of problems are there in following code? Thank you so much.
A = radn(3000);
[n,~] = size(A);
I = eye(n);
c = [0,-1/5, 1/5,-1/10, 1/10];
b = [128/3, 20/9, 85/3, -15, -(515/9)];
E = zeros(n);
tic
for k = 1:5
E = E + b(k)*((I-c(k)*A)\(c(k)*A));
end
toc
tic
parfor k = 1:5
E = E + b(k)*((I-c(k)*A)\(c(k)*A));
end
toc
E=E+I;
my results with 8 workers are as follows:
Elapsed time is 2.656596 seconds
Elapsed time is 3.542917 seconds

Accepted Answer

Bruno Luong
Bruno Luong on 15 Nov 2022
There is no problem.
There might be no benefit to run parfor on function that is designed with multithread and already exploit most resource of your CPU
Put parfor on top is just doing more harm than good.
  7 Comments
Raymond Norris
Raymond Norris on 15 Nov 2022
Just to be clear, the parfor loop is 2x faster than running for for-loop in single threaded mode, yes (not compared to multi-thread mode)?
DonSandalio
DonSandalio on 15 Nov 2022
I did that: maxNumCompThreads(1) for .... end maxNumCompThreads("automatic") parfor .... end It should be like this, no? Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!