While using parfor the cpu usage drops by 1 core every something iterations

3 views (last 30 days)
While using parfor loop for a long time everything seems to work fine loading all 8 cores of my CPU but after sometime Matlab releases cores one by one with some time in between exponentially increasing time working on the loop.
Triple for loop I use:
N = size(A,1); % around 5000
parfor i = 1:size(A,1)
for j = i+1:N
for k = j+1:N
sumMatrix = [sumMatrix; [(A(i) + A(j) + A(k)) A(i) A(j) A(k)]];
end
end
end

Answers (1)

Jan
Jan on 7 Feb 2016
Edited: Jan on 7 Feb 2016
An iterative growing of an array is a very bad idea because it wastes resources. I guess that the cores are simply waiting for the OS to deliver RAM. Look for the term "pre-allocation".
  3 Comments
Walter Roberson
Walter Roberson on 7 Feb 2016
If you cannot pre-allocate that much memory then you cannot store the result, which is just over 149 gigabytes, and your execution would eventually crash anyhow.
Your code as shown is not valid in a parfor loop as the results are dependent on the order of execution. Each output of a parfor must have a fixed output location.
Edric Ellis
Edric Ellis on 8 Feb 2016
@Walter - actually, that's not quite right - in this case, sumMatrix is treated as a parfor reduction variable. The following loop works just fine:
reduction = [];
parfor idx = 1:10
for jdx = 1:10
for kdx = 1:10
reduction = [reduction; rand];
end
end
end

Sign in to comment.

Categories

Find more on Parallel for-Loops (parfor) 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!