A problem returning sliced variables from functions

Variables indexed within parfor by the loop varible need to be "sliced" (see the link or PCT docs for the definition of sliced). I ran into a problem storing values returned from a function into sliced variables. See the following:



%---Example 1, This works OK----------------------------------------------
close all; clear all;
matlabpool open 2;
%---This works OK---
parfor (i = 1:10)
  A = rand(10,1);
  [Y,I] = sort(A);
  Asort(i).Y = Y;
  Asort(i).I = I;
end
matlabpool close;

%---Example 2, this fails with the following error------------------------
close all; clear all;
matlabpool open 2;
% ??? Error: The variable Asort2 in a parfor cannot be classified.
%  See Parallel for Loops in MATLAB, "Overview".
parfor (i = 1:10)
  A = rand(10,1);
  [Asort2(i).Y, Asort2(i).I] = sort(A);
end
matlabpool close;

Following is some enlightenment from MATLAB on this situation.

% Subject: Questions @ Webinar (Parallel Computing w/ MATLAB)
% From:    "Gaurav Sharma" 
% Date:    Thu, April 10, 2008 9:27 pm
% For the variable classification problem in: "parfor (i=1:10) A =
% rand(10,1); [Asort(i).Y, Asort(i).I] = sort(A); end". It is really a
% limitation of the way we currently parse a parfor loop. I am sure you
% are aware of the several classes that variables in a parfor loop can fall into
% (http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/brdqtjj-1.html#bq_of7_-1). 
% This happens to be a case where the parser fails to
% classify Asort according to the semantic rules set up internally. We are
% working to fix these issues.

So, it appears that the only way to deal with situations like Example 2, above, is to re-code them with temporary variables inside the loop (as in Example 1). Not a big deal if you are starting a project from scratch, but kind of a hassle if you have legacy code to update. This is the situation as of R2007b. Hopefully improving the identificaiton of sliced variables is something PCT will get better at in future versions.


Return to MATLAB PCT notes

Return to Art Gleason Home

Last Updated: Mar 2008