フィルターのクリア

nested loops in parfor, indexing

2 ビュー (過去 30 日間)
dominik
dominik 2013 年 12 月 9 日
コメント済み: dominik 2013 年 12 月 9 日
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

採用された回答

Edric Ellis
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
  1 件のコメント
dominik
dominik 2013 年 12 月 9 日
Thats very helpful! Thanks a lot!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by