MATLAB Answers

How to use a parfor loop inside a for loop?

14 views (last 30 days)
I have a for loop with variables that have dependency. Within that loop I would like to use parfor to perform a costly operation on a variable that is independent across iterations. Below I made an example; say that I want to perform myFun on each entry in the row (in parallel) corresponding to the iteration of the for-loop.
N = 10;
inputVar = [randi(100,N,1),zeros(N,1)];
out = cell(N,2)
for idx = 1:N
inputVars(idx,2) = idx;
parfor jdx = 1:2
out{jdx,kdx} = myFun(inputVars(jdx,kdx))
end
end
I'm new to parallel computing. Not sure if this example is a good one. I can provide a better example if asked.

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 9 Mar 2018
Generally speaking you can do that. However, parfor will have an easier job if you write to an output that is indexed only by the loop index and possibly the : (colon) operator. For example,
temp_input = inputVars(:,kdx);
parfor jdx = 1 : 2
temp_out{jdx} = myfun(temp_input(jdx));
end
out(:,kdx) = temp_out;

  3 Comments

Dominik Mattioli
Dominik Mattioli on 9 Mar 2018
Ah ok cool. So I've applied that and it still seems to be taking longer than a conventional for-loop. In fact, it doesn't run to completion at all while using parfor and the command window just says "Starting parallel pool (parpool) using the 'local' profile ... connected to 2 workers." for a long time. Am I misusing parfor? Maybe I should do a for-loop nested within the parfor-loop instead?
Stephen Cobeldick
Stephen Cobeldick on 9 Mar 2018
@Dominik Mattioli: parfor is not guaranteed to be faster than for in all cases: if it was, then parfor would be the default! It is only faster if the overhead of initializing and handling the parallel processes is less than time gained running them. This is explained in the documentation:
and many threads on this forum, e.g.:
etc.
Your question states: "I would like to use parfor to perform a costly operation on a variable that is independent across iterations", so it really depends on how "costly" that operation really is.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!