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

1 view (last 30 days)
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

Accepted Answer

Matt J
Matt J on 1 Jul 2015
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));

More Answers (1)

Brendan Hamm
Brendan Hamm on 1 Jul 2015
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.

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!