Converting this 3-nested for loop for parfor

The context is a finite element assembly routine where I need to loop over a bunch of mesh elements. In principle, each iteration computes a small local matrix (3x3 in this case) and inserts it into the much larger global matrix according to the indices ie and je.
The iterations are completely independent of each other as I'm pretty sure as my code stands now, it could run through the outer loop in any order without any problems. But of course, this version isn't accepted by MATLAB for parfor format. Can anyone help me out here?
NLoc = 3;
Klocal = zeros(NLoc);
blocal = zeros(NLoc,1);
Aglobal = zeros(NLoc*nElem);
bglobal = zeros(NLoc*nElem,1);
for k=1:nElem
kk = NLoc*(k-1);
Klocal = function1(nElem,k);
blocal = function2(nElem,k);
for i=1:NLoc
ie = i+kk;
bglobal(ie) = bglobal(ie) + blocal(i);
for j=1:NLoc
je = j+kk;
Aglobal(ie,je) = Aglobal(ie,je) + Klocal(i,j);
end
end
end

 Respuesta aceptada

Matt J
Matt J el 9 de Nov. de 2013
Editada: Matt J el 9 de Nov. de 2013
If I'm interpreting it right, it looks like
Aglobal = cell(nElem,1);
bglobal = cell(nElem,1);
parfor k=1:nElem
Aglobal{k}=function1(nElem,k);
bglobal{k}=function2(nElem,k);
end
Aglobal=blkdiag(Aglobal{:});
bglobal=cell2mat(bglobal);

3 comentarios

Justin
Justin el 9 de Nov. de 2013
Editada: Justin el 9 de Nov. de 2013
This works for part of my code, so thanks a ton! I might post a follow up as another question that's similar but I think might have to be handled differently.
What happens if we have, say
Klocal1 = function1(nElem,k);
Klocal2 = function2(nELem,k);
Klocal3 = function3(nElem,k);
but these local matrices get inserted into different places in the global matrix? i.e. not just Aglobal{k} = function1(...) + function2(...) + function3(...)
As well, some of the local matrices I've got won't fit a block diagonal structure for blkdiag
Matt J
Matt J el 9 de Nov. de 2013
You can create an MxN cell array to hold the blocks and fill them as you wish. Essentially, the same was done above with bglobal, which was not block diagonal.
Justin
Justin el 10 de Nov. de 2013
Do you think you could take a look? I'm still not sure I'm understanding how t do it when the matrices need to be inserted in a non-diagonal manner. I understand how to do so for bglobal since essentially each member of the cell array was just inserted right after one another.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 9 de Nov. de 2013

Comentada:

el 10 de Nov. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by