parallel computation for a for loop
显示 更早的评论
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 个评论
Maryam
2019-4-12
Maryam
2019-4-12
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.
Maryam
2019-4-12
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!