nested loops in parfor, indexing
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Edric Ellis
2013-12-9
编辑:Edric Ellis
2013-12-9
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!