Effacer les filtres
Effacer les filtres

nested loops in parfor, indexing

2 vues (au cours des 30 derniers jours)
dominik
dominik le 9 Déc 2013
Commenté : dominik le 9 Déc 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

Réponse acceptée

Edric Ellis
Edric Ellis le 9 Déc 2013
Modifié(e) : Edric Ellis le 9 Déc 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
  1 commentaire
dominik
dominik le 9 Déc 2013
Thats very helpful! Thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by