MATLAB Answers

parallel loop in matlab

1 ビュー (過去 30 日間)
Abhishek Saini
Abhishek Saini 2020 年 7 月 26 日
回答済み: Edric Ellis 2020 年 7 月 28 日
Hi ,
I am new to use parfor in MATLAB. I am trying to convert my for loop in to parfor, but getting problem in varaible definition. Here is the a section of the code where I want to implement parfor. Kindly advice how to run this correctly.
%% matrices initialisation
KB=zeros(sdof,sdof);
KS=zeros(sdof,sdof);
KK=zeros(sdof,sdof);
FF=zeros(sdof,1);
KG=zeros(sdof,sdof);
[DB,DS]=material_mat(E,nu,G,shcof);
parfor i=1:nelem
elecon=elemconn(i,:);
nodes =cor(elecon,1:2);
index1=nodedof(elecon,:).';
index=reshape(index1,1,size(index1,1)*size(index1,2));
[KB_final]=elementstiffness_bending(nodes,DB);
[KS_final,F_final]=elementstiffness_shear(nodes,DS,P);
[KG_final]=geometricstiff(sigma_i,nodes);
KK1=KB_final+KS_final;
[KK,FF,KG] = paral(KK1,F_final,KG_final,index,KK,FF,KG,i);
KB(index,index) = KB(index,index) + KB_final;
end
I am getting problem in defining Kb variable. How to rectify this?

  0 件のコメント

サインインしてコメントする。

回答 (1 件)

Edric Ellis
Edric Ellis 2020 年 7 月 28 日
Output variables in parfor loops must be either sliced outputs or reduction outputs. More info here in the doc. In this case, you need KB to be a "reduction" variable since each iteration of your loop computes an increment to that matrix. To fit in with the parfor rules, you cannot index a "reduction" variable, so you need to modify things a little to have each iteration compute a full-sized increment. Something a bit like this
parfor i = 1:nelem
% compute KB_final and index
KB_increment = zeros(sdof);
KB_increment(index,index) = KB_final;
% Use a reduction assignment into KB:
KB = KB + KB_increment;
end

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by