Keep a temporary variable after parfor
Show older comments
I am just wondering how do I keep my value z after the parfor has run?
% x (real(c))
tic;
ticBytes(gcp);
dx=0.01; % step
x1=-2;
x2=2;
% y (imag(c))
dy=0.01; % step
y1=-1.5;
y2=1.5;
x=x1:dx:x2;
y=y1:dy:y2;
[X,Y] = meshgrid(x,y); % for vectorized calculation
c=X+1i*Y;
R=1000; % if abs(z)>R then z is considered as infinity
n=1000; % maximal number of iterations, 100 is close to infinity
parfor nc=1:n
z=zeros(size(c)); % starts from zeros
z=z.^2+c; % vectorized
end
I=abs(z)<R; % logical image
imagesc(x,y,I);
colormap((jet));
set(gca,'XColor', 'none','YColor','none');
toc;
tocBytes(gcp);
7 Comments
Rik
on 7 Apr 2021
The content of the loop doesn't depend on the loop variable and is overwritten every iteration. Since parfor cannot guarantee an order, z is not clearly defined (even though it actually is).
You should first make your code work with for before you swap it with parfor.
Oisin Kelly
on 7 Apr 2021
Rik
on 7 Apr 2021
The problem with your code is that this:
for nc=1:n
z=zeros(size(c)); % starts from zeros
z=z.^2+c; % vectorized
end
has the same result as this:
z=c;
Why do you want to use a parfor loop? One main requirement of a parallel loop is that every iteration is independent.
Raymond Norris
on 7 Apr 2021
If I understand you correctly, your code use to look like this
z=zeros(size(c)); % starts from zeros
for nc=1:n
z=z.^2+c; % vectorized
end
In order to speed it up, you rewrote the for-loop to be parfor. However, z is a temporary value (in the loop) and must be defined on the left hand size before referencing it on the right hand side. You then pushed the zeros call into the parfor, but z (a temporary variable) doesn't get passed back and therefore can't be plotted. Do I have that right?
We could probably go into the intracacies of sliced and reducation variables, but I want to make sure this is the actual code we're working with. If so, you ought not need to the loop to beging with, no? Won't this do:
% x (real(c))
tic;
dx=0.01; % step
x1=-2;
x2=2;
% y (imag(c))
dy=0.01; % step
y1=-1.5;
y2=1.5;
x=x1:dx:x2;
y=y1:dy:y2;
[X,Y] = meshgrid(x,y); % for vectorized calculation
c=X+1i*Y;
R=1000; % if abs(z)>R then z is considered as infinity
n=1000; % maximal number of iterations, 100 is close to infinity
z=z.^2+c; % vectorized
I=abs(z)<R; % logical image
imagesc(x,y,I);
colormap((jet));
set(gca,'XColor', 'none','YColor','none');
toc
Oisin Kelly
on 7 Apr 2021
Paul Hoffrichter
on 7 Apr 2021
>> go into the intracacies of sliced and reducation variables
I have an idea where you are going with this, and would appreciate whether you think it would work in the parfor loop. Given the square of s within the parfor loop, I did not think that s could be broken into chunks for each of the parfor workers since the workers would not know how to initialize s properly in each of the workers' processes. I try to explain why I cam to this conclusion in my posts below.
Accepted Answer
More Answers (0)
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!