Reputation: 133
I have a main loop that contains a parfor loop.
I want to make sure that I am benefiting from using parfor.
The following explains conceptually what I am doing:
% parameters
iterate = [1e0 1e2 1e3 1e4 1e5];
% main loop
for k=1:5
% parfor
parfor j=1:iterate(k)
*calculations*
m(j)= *calculations*
end
%using the result of parfor in the main loop
vector(k)= m/iterate(k);
end
I am assuming that matlab will use a number of workers to fill the array doing_smth
separately which will result in filling that array faster than usual.
The array would then be used to act on something in the main loop.
Would this arrangement produce results faster?
Upvotes: 0
Views: 221
Reputation: 65460
I would probably make your outer loop be the parfor
so that each worker has a relatively long job to run and also you'll spend less time waiting for other workers to finish.
Having it on the inside you have to wait for all workers to finish every time through the loop.
for main = 1:N
doing_smth = zeros(N1,1);
parfor pal = 1:2N
%// This task may not be very long-running
doing_smth(pal) = whatever;
end
%// Have to wait for all runners to complete before we can continue
%// Obviously we have to do this waiting N times (each time through the loop)
act(main)= doing_smth/2
end
If you move it to the outer loop, the job sent to the worker will take longer to execute (more efficient due to lower back-and-forth traffic with worker) and you aren't waiting for all workers to finish within the loop.
parfor main = 1:N
doing_smth = zeros(N1, 1);
for pal = 1:2N
doing_smth(pal) = whatever;
end
%// No waiting anymore!
act(main) = doing_smth / 2;
end
%// Now wait here for all workers to finish
Update
Based upon the update you have provided to your code, I actually think that it would be better to have the parfor
on the inner loop assuming that that calculation inside of that loop takes sufficiently long. This will better utilize the workers because if you have the outer loop be a parfor
, since you only have 4 workers and 5 iterations of the outer loop, then it will take 2x longer than if you had 5 workers because the 4 workers will finish 4 of the iterations and then 3 workers will sit idle while 1 worker completes the fifth iteration. Using parfor
on the inside, you will utilize all workers fully. You do want to be sure that the inside of that inner loop takes a decent amount of time to reduce communication overhead. Otherwise you will want to manually carve your data into blocks and process those within a parfor
.
Upvotes: 1