MATLAB Answers

0

Parfor, reference to a cleared variable

Asked by yong xingwang on 3 Apr 2019
Latest activity Commented on by yong xingwang on 4 Apr 2019
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 Comments

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?
Thank you. I've updated my code, please check it. Thanks!

Sign in to comment.

Tags

Products


Release

R2018a

1 Answer

Answer by Walter Roberson
on 3 Apr 2019

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 Comments

Show 1 older comment
Thank you. I'm very sure that my do_something() doesn't involve clear. I've updated my code, please check it.
On the one hand, in do_something(), there's an fmincon, the rest are just some normal assignments. On the other hand, after avoiding 'continue', I can disp, so do_something doesn't clear any variable.
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.
Actually, before continue to next iteration, I assign zeros(1,4) to fitted_params. Besides, fitted_params is a sliced output variable here, I didn't initilize it before the loop. According to matlab documentation, you don't have to initilize a sliced output variable before the loop.

Sign in to comment.