nested loops in parfor, indexing

2 views (last 30 days)
dominik
dominik on 9 Dec 2013
Commented: dominik on 9 Dec 2013
Hey
im trying to solove an equation system over a 4 dimensional grid of parameter values. Since the difference between neigbouring parameter-grid points is small, using the solution of a neigbouring parameter-grid point as a starting value speeds up computation a lot. See the code below.
Now i would like to use parallel computiong to further speed up computation. But Matlab doesnt allow the indexing i need (i3-1, i4-1). Now i understand this makes sense for the parfor index (i1), but for the nested loop indexes it seems an unneccessary restriction.
Is there a way around?
Many Thanks
Dominik
parfor i1=1:n1 % works with for, not with parfor
for i2=1:n2
for i3=1:n3
for i4=1:n4
if solution(i1,i2,max(1,i3-1),i4)~=0
start=solution(i1,i2,max(1,i3-1),i4);
else
start=solution(i1,i2,i3,max(1,i4-1));
end
myparametrizedfun=@(x)myfun(x,parametergeneratingfun(i1,i2,i3,i4));
solution(i1,i2,i3,i4)=fsolve(myparametrizedfun,start);
end
end
end
end

Accepted Answer

Edric Ellis
Edric Ellis on 9 Dec 2013
Edited: Edric Ellis on 9 Dec 2013
Unfortunately, PARFOR's static analysis cannot tell that you are accessing 'solution' in an order-independent "sliced" manner. You can help it out by making a temporary partial solution, filling it out, and then sticking that pack into 'solution':
parfor i1=1:n1
% Local temporary 'solution' for this value of i1.
tmp_solution = zeros(n2, n3, n4);
for i2=1:n2
for i3=1:n3
for i4=1:n4
if tmp_solution(i2,max(1,i3-1),i4)~=0
start=tmp_solution(i2,max(1,i3-1),i4);
else
start=tmp_solution(i2,i3,max(1,i4-1));
end
myparametrizedfun=@(x)myfun(x,parametergeneratingfun(i1,i2,i3,i4));
tmp_solution(i2,i3,i4)=fsolve(myparametrizedfun,start);
end
end
end
% tmp_solution is now complete, paste back into solution.
solution(i1, :, :, :) = tmp_solution;
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!