Parfor does not parallelize after the first time

2 views (last 30 days)
I am using 16 workers.
My parfor loop requires 70 loops in total.
When I run the parfor it starts 16 loops.
Then as the loops finish one by one, the solver does not start new loops.
It accepts one loop at a time for the rest of load.
So the parallelization stops after the first time the load is distributed.
How can I overcome this problem? Thank you.
  2 Comments
Abdolkarim Mohammadi
Abdolkarim Mohammadi on 22 May 2020
Please provide your code or at least its outlines. parfor uses several techniques to figure out how to chop your matrices for parallel computationn, so it should be used carefully. Using for instead of parfor can be useful for debugging since debugging cannot be done inside a parfor loop.
Mehmet Selim Akay
Mehmet Selim Akay on 26 May 2020
parfor i = indexRange
x = aVector(i) ;
[A{i},B{i},C{i}] = myfunc(x,y, ...some input... );
end
Thank you for the response.
The above is what my code does.
To reiterate, when the length of "indexRange" variable is greater than the parallel workers' count, at the first time all workers are assigned a loop. But after this first distribution of work is finished, for the rest of the indexes in indexRange, it allows only one loop at a time and the CPU is underloaded.
I can only mitigate the problem with a decoration as follows:
p = 1 ; remainderIndexCount = length(aVector) ;
while remainderIndexCount > 0
indexRange = p:p+min(16,remainderIndexCount)-1 ; % 16 workers
parfor i = indexRange
x = aVector(i) ;
[A{i},B{i},C{i}] = myfunc(x,y, ...some input... );
end
remainderIndexCount = length(aVector) - indexRange(end);
p = indexRange(end) + 1 ;
end

Sign in to comment.

Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!