3

I want to compute a fractal image in two nested loops over the pixel indices (ix,iy). The sample code just assigns a random number to RGB values instead of the real calculations.

x = 0:.2:4;
y = 0:.2:3;
nX = length(x);
nY = length(y);
RenderRed = zeros(nX,nY); RenderGreen = zeros(nX,nY); RenderBlue = zeros(nX,nY);

parfor ix = 1:nX
    % for iy = 1:length(y)  % error
    for iy = 1:nY
        % "compute" pixel (ix,iy)
        RenderRed(ix, iy) = rand; RenderGreen(ix, iy) = rand; RenderBlue(ix, iy) = rand;
    end
end

Pctr = [];
Pctr(:,:,1)=RenderRed; Pctr(:,:,2)=RenderGreen; Pctr(:,:,3)=RenderBlue;
handle = image(Pctr);
shg

The code works as shown but if the end value of the iy loop is changed from nY to length(y) - see the commented line - an error is issued:

Error: The variable RenderRed in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".

Why? In my understanding of sliced variables no version should work: one has to use an auxiliary variable collecting the results of the inner loop, and assign that to a slice of the matrix. But length(y) instead of nY should have no effect at all on the classification of variables because y is never assigned in the loops.

mlint finds no error, in neither version. Same behaviour with MATLAB versions 2016b, 2017b.

1
  • I guess that matlab don't know if the nY variable is modified or not in the parfoor loop, so to avoid that one pool modify this variable matlab simply do not give you the right to set the for loop limit using a function call. I imagine that if this code was compiled and not interpreted both solutions could work. Commented Oct 25, 2018 at 11:50

1 Answer 1

3

According to the Matlab documentation, this actually can work! When you want to use nested for loops inside parfor loops:

For proper variable classification, you must define the range of a for-loop nested in a parfor-loop by constant numbers or variables.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.