Parfor, reference to a cleared variable
5 次查看(过去 30 天)
显示 更早的评论
I have a script like the pseudo-code below. When I run my script, MATLAB says "Reference to a cleared variable xxx". After removing "disp()", my script can run.
parfor n=1:300
tmp = do_something(n); %tmp is a 1-by-M vector
a(n,:) = tmp; % a is a sliced output
disp(a(n,:));
end
%this script can run in MATLAB R2018a
But the pseudo-code above can run even without removing "disp()"!!!
Where the problem is? So strange, isn't it?
========================================================================================================================================================
Well, I realized that the pseudo-code I gave above is too simple to descript this problem. I rewrite a new one.
parfor iter = 1:length(img(:))
if img(iter)==0 %do nothing to background
tmp = zeros(1,4);
fitted_param(iter,:) = tmp;%fitted_param is a sliced output
continue;
end
fitted_param(iter,:) = fmincon(some_params);
disp(fitted_param(iter,:));%if don't comment this, there will be an error, reference to a cleared varible
end
some_analysis(fitted_param);
The disp() above is useless for my work. Remove disp(), I can finish my work at once. I just want to figure out why this causes an error.
Actually, my teacher helped me solve this problem, like the pseudo-code below. Just avoid using continue. It works!
parfor iter = 1:length(img(:))
if img(iter)==0 %do nothing to background
tmp = zeros(1,4);
fitted_param(iter,:) = tmp;
else
fitted_param(iter,:) = fmincon(some_params);
disp(fitted_param(iter,:)); %no error
end
end
some_analysis(fitted_param);
It seems that 'continue' caused this problem. But the documentaion didn't say anything about using continue in parfor.
The code below is runable(maybe error if not remove disp()), you can use this to find out where the problem is. What is the most weird is that the code below sometimes can run, sometimes can not! There is one time that I run it successfully on my PC, using MATLAB R2016a. After that, error always happens. I think this may be related to the parallel pool itself, not the code.
img = 128*ones(256,256);
img(1:10,:)=0;
parfor iter = 1:length(img(:))
if img(iter)==0 %do nothing to background
tmp = zeros(1,4);
fitted_param(iter,:) = tmp;%fitted_param is a sliced output
continue;
end
fitted_param(iter,:) = ones(1,4); %originally, here is a fmincon(), I simplify it
disp(fitted_param(iter,:));%sometimes there will be an error, reference to a cleared varible
end
2 个评论
Edric Ellis
2019-4-3
Very strange - that error generally only occurs if a variable has actually been cleared using clear. Do you have any simple code that actually demonstrates the problem?
回答(1 个)
Walter Roberson
2019-4-3
I suspect your "do_something" contains a clear, perhaps a "clear all".
Your variable a is perhaps not being used after the parfor . That would make a into a local variable, and with the disp() not being there, optimization would find that a did not need to be assigned into, and therefore that tmp did not need to be assigned into. do_something would still need to be called for the purpose of side effects so the clear all inside it would still be executed.
But with the disp() in place, a needs to exist, and tmp needs to be assigned into, but it would stop existing because of the clear all that I suspect is in your code.
4 个评论
Walter Roberson
2019-4-3
With the continue in there, you are effectively not assigning anything for some iterations. If the variable had not been initialized to full size before the loop, that could potentially cause problems.
另请参阅
类别
在 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!