Borrar filtros
Borrar filtros

nested loops in parfor, indexing

2 visualizaciones (últimos 30 días)
dominik
dominik el 9 de Dic. de 2013
Comentada: dominik el 9 de Dic. de 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

Respuesta aceptada

Edric Ellis
Edric Ellis el 9 de Dic. de 2013
Editada: Edric Ellis el 9 de Dic. de 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 comentario
dominik
dominik el 9 de Dic. de 2013
Thats very helpful! Thanks a lot!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by