parallel computation for a for loop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Maryam
el 12 de Abr. de 2019
Comentada: Maryam
el 12 de Abr. de 2019
Below is the part of the code I have written, in which I am trying to use parallel computation. But it does give me an error as below:
"Error: The variable k_1 in a parfor cannot be classified."
In each parfor iteration some specific rows of "k_1" matrix will be updated irrelavant to the rest, so I cannot see why I get this message. Any help in this regard will be highly appreciated. Please find the parallel portion of my code below:
k_1 = k;
M_1 = M;
% pi = 0;
parfor ppi=1:NP
for pii=1:kkk
% pi= pi + 1;
pi = (ppi-1)*kkk+pii;
Tempo1 = zeros(1,1);
Tempo1_M = zeros(1,1);
Tempo2 = zeros(1,1);
Tempo2_M = zeros(1,1);
for irow = 1:p(pi)
[xx,inside_angle] = find(irow==[p;q]);
Tempo1(irow) = [(xx-1)*alpha(inside_angle)+(2-xx)]* ...
[k(p(inside_angle),p(pi))+lambda(pi)*k(p(inside_angle),q(pi))] + ...
[(2-xx)*lambda(inside_angle)+(xx-1)]* ...
[k(q(inside_angle),p(pi))+lambda(pi)*k(q(inside_angle),q(pi))];
Tempo1_M(irow) = [(xx-1)*alpha(inside_angle)+(2-xx)]* ...
[M(p(inside_angle),p(pi))+lambda(pi)*M(p(inside_angle),q(pi))] + ...
[(2-xx)*lambda(inside_angle)+(xx-1)]* ...
[M(q(inside_angle),p(pi))+lambda(pi)*M(q(inside_angle),q(pi))];
end
for irow = 1:q(pi)
[xx,inside_angle] = find(irow==[p;q]);
Tempo2(irow) = [(xx-1)*alpha(inside_angle)+(2-xx)]* ...
[alpha(pi)*k(p(inside_angle),p(pi))+k(p(inside_angle),q(pi))] + ...
[(2-xx)*lambda(inside_angle)+(xx-1)]* ...
[alpha(pi)*k(q(inside_angle),p(pi))+k(q(inside_angle),q(pi))];
Tempo2_M(irow) = [(xx-1)*alpha(inside_angle)+(2-xx)]* ...
[alpha(pi)*M(p(inside_angle),p(pi))+M(p(inside_angle),q(pi))] + ...
[(2-xx)*lambda(inside_angle)+(xx-1)]* ...
[alpha(pi)*M(q(inside_angle),p(pi))+M(q(inside_angle),q(pi))];
end
%Assign tempos to k
for irow = 1:p(pi)
k_1(irow,p(pi)) = Tempo1(irow);
k_1(p(pi),irow) = Tempo1(irow);
M_1(irow,p(pi)) = Tempo1_M(irow);
M_1(p(pi),irow) = Tempo1_M(irow);
end
for irow = 1:q(pi)
k_1(irow,q(pi)) = Tempo2(irow);
k_1(q(pi),irow) = Tempo2(irow);
M_1(irow,q(pi)) = Tempo2_M(irow);
M_1(q(pi),irow) = Tempo2_M(irow);
end
end
end
k=k_1;
M=M_1;
Please note that k and M matrices are defined by me at the start of the code!
5 comentarios
Matt J
el 12 de Abr. de 2019
Editada: Matt J
el 12 de Abr. de 2019
No, you need to explain to us why you think it is parallelizable. Parallelizable means the operations done by your loop iterations (over ppi) are independent of one another and could just as well be done on separate computers. Since all loop iterations in the code you've shown appear to be writing into the same matrix, it is hard to see what kind of independence from each other you think the loop iterations can have.
Respuesta aceptada
Catalytic
el 12 de Abr. de 2019
parfor pi=1:NP*kkk
Tempo1 = zeros( p(pi) ,1 );
Tempo1_M = zeros( p(pi) ,1);
Tempo2 = zeros(q(pi) ,1);
Tempo2_M = zeros(q(pi) ,1);
pSubs = zeros( p(pi) ,2 ); %new
qSubs = zeros(q(pi) ,2);
for irow = 1:p(pi)
[xx,inside_angle] = find(irow==[p;q]);
Tempo1(irow) = [(xx-1)*alpha(inside_angle)+(2-xx)]* ...
[k(p(inside_angle),p(pi))+lambda(pi)*k(p(inside_angle),q(pi))] + ...
[(2-xx)*lambda(inside_angle)+(xx-1)]* ...
[k(q(inside_angle),p(pi))+lambda(pi)*k(q(inside_angle),q(pi))];
Tempo1_M(irow) = [(xx-1)*alpha(inside_angle)+(2-xx)]* ...
[M(p(inside_angle),p(pi))+lambda(pi)*M(p(inside_angle),q(pi))] + ...
[(2-xx)*lambda(inside_angle)+(xx-1)]* ...
[M(q(inside_angle),p(pi))+lambda(pi)*M(q(inside_angle),q(pi))];
pSubs(irow,:)=[irow,p(pi)]; %new
end
for irow = 1:q(pi)
[xx,inside_angle] = find(irow==[p;q]);
Tempo2(irow) = [(xx-1)*alpha(inside_angle)+(2-xx)]* ...
[alpha(pi)*k(p(inside_angle),p(pi))+k(p(inside_angle),q(pi))] + ...
[(2-xx)*lambda(inside_angle)+(xx-1)]* ...
[alpha(pi)*k(q(inside_angle),p(pi))+k(q(inside_angle),q(pi))];
Tempo2_M(irow) = [(xx-1)*alpha(inside_angle)+(2-xx)]* ...
[alpha(pi)*M(p(inside_angle),p(pi))+M(p(inside_angle),q(pi))] + ...
[(2-xx)*lambda(inside_angle)+(xx-1)]* ...
[alpha(pi)*M(q(inside_angle),p(pi))+M(q(inside_angle),q(pi))];
qSubs(irow,:)=[irow,q(pi)]; %new
end
subsCell{pi}=[pSubs;qSubs]; %new
kValCell{pi}=[Tempo1;Tempo2];
MValCell{pi}=[Tempo1_M;Tempo2_M];
end
subs=cell2mat(subsCell);
kVal=cell2mat(kValCell);
MVal=cell2mat(MValCell);
k_1=accumarray(subs,kVal,size(k));
M_1=accumarray(subs,MVal,size(M));
k=k_1 + tril(k_1.',-1); %make symmetric
M=M_1 + tril(M_1.',-1);
6 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!