Parfor: Variable (Ind) is not sliced; Recommendations about my code?

2 次查看(过去 30 天)
I have the below lines of code and i want to make it with parfor. But i get the folloing message: Parfor cannot run due to the way Ind is used, and the it has minor corrections about the variables using the vector Ind that are not sliced. Any suggestions?
parfor iel=1:nelm
inadd=0;
Hnodes=Element_Table(iel,1:tot_Nods);
for il=1:z_nodes
for i_ind=1:tot_Nods;
hn=Hnodes(i_ind);
ielN=Element_Table(iel,i_ind+(il-1)*tot_Nods);
if (ielN>mxH && ielN<mnH);
DOFS=middofs; else DOFS=HalfDofs; end
[posf, poslf]=locF(il, z_nodes, totnodes, hn);
DoFs=1+inadd:DOFS+inadd; idofs=1:DOFS;
Ind(DoFs)=idofs+posf+poslf;
inadd=inadd+DOFS;
end
end
Unv(posel,1)=Un(Ind);
Ubv(posel,1)=Ub(Ind);
Kvn(Ind)=Kvn(Ind)+ K*Unv;
Mvn(Ind)=Mvn(Ind)+ M*Unv;
Mvb(Ind)=Mvb(Ind)+ M*Ubv;
end

采纳的回答

Matt J
Matt J 2015-7-1
Ind looks like a temporary variable and therefore needs to be created inside the parfor loop. Also, this kind of accumulation
Kvn(Ind)=Kvn(Ind)+ K*Unv;
Mvn(Ind)=Mvn(Ind)+ M*Unv;
Mvb(Ind)=Mvb(Ind)+ M*Ubv;
has no obvious parallel structure. Perhaps you meant the following
Kvn(el,Ind)= K*Unv;
Mvn(el,Ind)= M*Unv;
Mvb(el,Ind)= M*Ubv;
and then later after the parfor loop,
Kvn=squeeze(sum(Kvn,1));
Mvn=squeeze(sum(Mvn,1));
Mvb=squeeze(sum(Mvb,1));

更多回答(1 个)

Brendan Hamm
Brendan Hamm 2015-7-1
Often times when you run into an error like this, your easiest solution will be to take the inside of the parfor loop and turn it into a function.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by