Parfor, reference to a cleared variable

8 views (last 30 days)
Xingwang Yong
Xingwang Yong on 3 Apr 2019
Commented: Xingwang Yong 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
Edric Ellis
Edric Ellis on 3 Apr 2019
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?
Xingwang Yong
Xingwang Yong on 3 Apr 2019
Thank you. I've updated my code, please check it. Thanks!

Sign in to comment.

Answers (1)

Walter Roberson
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
Walter Roberson
Walter Roberson on 3 Apr 2019
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.
Xingwang Yong
Xingwang Yong on 4 Apr 2019
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.

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!