Error using Parfor "Unable to classify the variable 'Sort'"
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Raymond Norris
2021년 7월 23일
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
Raymond Norris
2021년 7월 23일
I would suggest two things:
- Make Solver_V a function and not a script. This will scope what variables are in the script's workspace.
- 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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!