Error using Parfor "Unable to classify the variable 'Sort'"

2 views (last 30 days)
Please help, pulling my hair out;
The following code is meant to sift through arrays Sort and Sort2 looking for intersections, and then if it finds one it reassigns that line (k) to all zeros. Sort is 16.777E6 x 24, and Sort2 is 24x24 matrix. k runs from 1 to Np = 16.777E6. This segment of the whole program takes considerable time with very, very large versions of Sort, so I am trying to use parfor to speed things up. I assumed this could be done because the results of any line Sort(k) do not affect any other line of Sort(k).
The error specifically says "Error using Solver_V (line 647)
Error: Unable to classify the variable 'Sort' in the body of the parfor-loop. For more information, see Parallel for Loops in MATLAB, "Solve Variable Classification Issues in
parfor-Loops".
The offending portion of code is below, and I included the full files for context.
Thank you so much!
parfor k=1:Np
for i=1:(Nu-1)
for j=(i+1):Nu
if Sort(k,i)*Sort(k,j)==1 && Sort2(i,j)==0
Sort(k,:)=zeros;
flag=1;
break
end
end
if flag==1
flag=0;
break
end
end
if mod(count2,10E6)==0
count2-count1
count2
end
count2=count2+1;
end

Accepted Answer

Raymond Norris
Raymond Norris on 23 Jul 2021
Try this refactoring the for-loop a bit
parfor k=1:Np
flag = 0; %#ok<PFTUSW>
for i=1:(Nu-1)
for j=(i+1):Nu
% if Sort(k,i)*Sort(k,j)==1 && Sort2(i,j)==0
% Sort(k,:)=zeros;
% flag=1;
% break
% end
tmp_S = Sort(k,:);
if lFoundIntersection(tmp_S,i,j,Sort2)
Sort(k,:) = zeros;
flag = 1;
end
end
if flag==1
flag=0;
break
end
end
if mod(count2,10E6)==0
count2-count1
count2
end
count2=count2+1;
end
At the end of your script, add
function tf = lFoundIntersection(Sort,i,j,Sort2)
tf = Sort(:,i)*Sort(:,j)==1 && Sort2(i,j)==0;
end
I think you'll also need to comment out this
if mod(count2,10E6)==0
count2-count1
count2
end
  3 Comments
Raymond Norris
Raymond Norris on 23 Jul 2021
I would suggest two things:
  1. Make Solver_V a function and not a script. This will scope what variables are in the script's workspace.
  2. Change the parfor back to a for-loop with a 26 node project and see if it works. If not, resolve the issue and then go back to a parfor for a final test.

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!