From: Fahim Chandurwala on
Greetings,
I am converting a normal for loop into a parfor loop. When I run the code w/ for loop, there are warnings, but the program seems to run as expected and w/o errors. Now, I tried to run the program with parfor, and then the error: "reference to a cleared variable" occurs. Is it due to the fact that whenever I run the code I do: 'clear all;'? If so why is it the case that the error occurs only for one particular variable? Some Details are below including the concept code.
Error Message begins:
??? Reference to a cleared variable still.

Error in ==> parallel_function at 468
consume(base, limit, F(base, limit, supply(base, limit)));

Error in ==> testautomation_2 at 39
tic;
End of Error.
Here's the concept program:
clear all;
close all;
clc;
parfor kk=1:Number_of_Images
if(kk==1)
still=zeros(1,nE1(2));
edge=zeros(1,nE1(2));
end

if(kk>=2)
if(still(1,ii)-edge(1,ii)<=0.235 && still(1,ii)-edge(1,ii)>=-0.235)

D(ii)=0;
else

D(ii)=still(1,ii)-edge(1,ii);
end
end
end
From: Edric M Ellis on
"Fahim Chandurwala" <fchandur(a)gmail.com> writes:

> I am converting a normal for loop into a parfor loop. When I run the
> code w/ for loop, there are warnings, but the program seems to run as
> expected and w/o errors. Now, I tried to run the program with parfor,
> and then the error: "reference to a cleared variable" occurs. Is it
> due to the fact that whenever I run the code I do: 'clear all;'?

I don't think 'clear all' is the problem; I think in this case it's
because your code does something like this:

parfor kk = 1:N
if kk == 1
still = zeros(...);
end
if kk >= 2
% access "still"
end
end

The problem here is that the iterations of your loop are not
independent. PARFOR requires that your loop is order-independent - in
other words, there must be no dependency between the individual
iterations of the loop. In your case, you're using the creation of
"still" in iteration 1 in all other iterations.

Can you move the creation of "still" and "edge" outside the body of the
PARFOR loop? That should help.

Cheers,

Edric.
From: Fahim Chandurwala on
Edric M Ellis <eellis(a)mathworks.com> wrote in message <ytwr5iyskgp.fsf(a)uk-eellis-deb5-64.mathworks.co.uk>...
> I don't think 'clear all' is the problem; I think in this case it's
> because your code does something like this:
>
> parfor kk = 1:N
> if kk == 1
> still = zeros(...);
> end
> if kk >= 2
> % access "still"
> end
> end
>
> The problem here is that the iterations of your loop are not
> independent. PARFOR requires that your loop is order-independent - in
> other words, there must be no dependency between the individual
> iterations of the loop. In your case, you're using the creation of
> "still" in iteration 1 in all other iterations.
> Can you move the creation of "still" and "edge" outside the body of the
> PARFOR loop? That should help.
>
> Cheers,
>
> Edric.

Edric, thank you for that bit of clarification. The problem isn't with 'edge', because 'edge' is calculated for each value of kk. And, according to your explanation of PARFOR, it makes sense that it should not give me an error. Unfortunately, 'still' must be created inside the PARFOR loop for kk=1.'still'='edge' for kk=1. However, once 'still' is created it need not be calculated again, so to overcome that error should I store the variable 'still' it in a file and then access it?
Once again, thank you for explaining the cause of the error. it helps a lot.
--Fahim