parfor issue: reference to a cleared variable
조회 수: 1 (최근 30일)
이전 댓글 표시
I have some code that was written with R2011b that has been working fine for a while. It made calls to matlabpool, and R2014 gave me a warning every time I ran it that I should be using parpool. I sometime have to run the code on a computer with R2011b though, so I modified the code to run verLessThan and then use either matlabpool or parpool depending on the version. Below is the code in its entirety
function DPSimDriver( )
ticID = tic;
seed_value = randi(200000);
rand('seed', seed_value);
NineWeights = [.085 .064 .128 .085 .128 .064 .085 .170 .191];
EightWeights = [.091 0 .137 .091 .137 .068 .091 .182 .204];
SevenWeights = [.097 0 .147 .097 .147 0 .097 .195 .219];
SixWeights = [.108 0 .163 0 .163 0 .108 .216 .243];
FiveWeights = [.121 0 .182 0 .182 0 0 .242 .272];
FourWeights = [0 0 .207 0 .207 0 0 .276 .310];
ThreeWeights = [0 0 0 0 .262 0 0 .348 .391];
m = csvread('design.txt');
p = ProgressBar(size(m,1));
cores = getenv('NUMBER_OF_PROCESSORS');
if verLessThan('matlab', '8.3.0.532')
%old version, use matlabpool
if matlabpool('size') == 0
matlabpool('open', cores)
end
else
%newer version, use parpool
poolObj = gcp('nocreate'); %if no pool, do not create new one
if isempty(poolObj)
parpool('local')
end
end
parfor i = 1:size(m,1)
RunNumber = m(i,1);
ModelSize = m(i,2);
AlphaLevel = m(i,3);
Sensitivity = m(i,4);
Arrival = m(i,5);
Departure = m(i,6);
switch ModelSize
case 3
Weights = ThreeWeights;
case 4
Weights = FourWeights;
case 5
Weights = FiveWeights;
case 6
Weights = SixWeights;
case 7
Weights = SevenWeights;
case 8
Weights = EightWeights;
case 9
Weights = NineWeights;
end
DPSim(RunNumber, Weights, AlphaLevel, Sensitivity, Arrival, Departure);
p.progress;
end
p.stop;
fileID2 = -1;
while fileID2 == -1
fileID2 = fopen('projects_results.txt', 'a');
end
fprintf(fileID2, 'Total run time: %s', datestr(toc(ticID)/24/3600, 'DD-HH:MM:SS'));
fclose(fileID2);
if verLessThan('matlab', '8.3.0.532')
matlabpool('close');
else
poolObj = parpool;
delete(poolObj);
end
end
The only thing that has changed between the above code and the previously running version is the addition of the if verLessThan block to change between matlabpool and parpool.
Now when I run this, I get the error message
Error using DPSimDriver>(parfor body) (line 61) Reference to a cleared variable Weights.
Line 61 is the call to
DPSim(RunNumber, Weights, AlphaLevel, Sensitivity, Arrival, Departure);
The variable Weights is only used within the body of the parfor loop, so I don't understand what the issue is. Can someone enlighten me?
Thanks in advance!
댓글 수: 2
José-Luis
2014년 7월 9일
편집: José-Luis
2014년 7월 9일
Sanity check. Since you are running on different computers, are you sure that "design.txt" contains what you think it does. If it contains a value other than 3:9 for ModelSize, then Weights will not be defined and that could explain your error.
You could put
Weights = something;
before the switch statement to see if that might be the case.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!