Parfor simple issue: "uses a value set outside the PARFOR loop"

11 views (last 30 days)
Dearest Matlab users,
I am dealing for the first time with the parfor command. I simply have a for loop that runs lots of times so I would like to run it faster through multithreading.
The variable "data" is a struct containing several information. After the for loop, the "data" variable will be used in the code outside the parfor loop. The main problem I have encountered is a simple error in the "data" variable inside the parfor loop:
The temporary variable 'data' uses a value set OUTSIDE the parfor loop
Here is the code (is part of the shear-warp algorithm in volume processing). My question is whether it is not possible to use parfor in this case because the 'data' struct variable is comming from another function and then it will go to another one. Thanks a lot in advance!
parfor z=0:(data.Iin_sizex-1);
% Offset calculation
xd=(-data.Ibuffer_sizex/2)+data.Mshearinv(1,3)*(z-data.Iin_sizex/2)+data.Iin_sizey/2;
yd=(-data.Ibuffer_sizey/2)+data.Mshearinv(2,3)*(z-data.Iin_sizex/2)+data.Iin_sizez/2;
xdfloor=floor(xd); ydfloor=floor(yd);
%Calculate the coordinates on which a image slice starts and
%ends in the temporary shear image (buffer)
pystart=-ydfloor;
if(pystart<0), pystart=0; end
pyend=data.Iin_sizez-ydfloor;
if(pyend>data.Ibuffer_sizey), pyend=data.Ibuffer_sizey; end
pxstart=-xdfloor;
if(pxstart<0), pxstart=0; end
pxend=data.Iin_sizey-xdfloor;
if(pxend>data.Ibuffer_sizex), pxend=data.Ibuffer_sizex; end
data.py=(pystart+1:pyend-1); data.px=(pxstart+1:pxend-1);
if(isempty(data.px)), data.px=pxstart+1; end
if(isempty(data.py)), data.py=pystart+1; end
% Determine x and y coordinates of pixel(s) which will be come current pixel
yBas=data.py+ydfloor; xBas=data.px+xdfloor;
switch (data.ShearInterp)
case {'bilinear'}
xBas1=xBas+1; xBas1(end)=xBas1(end)-1;
yBas1=yBas+1; yBas1(end)=yBas1(end)-1;
% Linear interpolation constants (percentages)
xCom=xd-floor(xd); yCom=yd-floor(yd);
perc=[(1-xCom)*(1-yCom) (1-xCom)*yCom xCom*(1-yCom) xCom*yCom];
if(isempty(data.VolumeX))
% Get the intensities
if(data.Iin_sizez>1)
slice=double(squeeze(data.Volume(z+1,:,:)));
% size(data.Volume)
else
slice=double(data.Volume(z+1,:))';
end
intensity_xyz1=slice(xBas, yBas);
intensity_xyz2=slice(xBas, yBas1);
intensity_xyz3=slice(xBas1, yBas);
intensity_xyz4=slice(xBas1, yBas1);
else
slice=double(data.VolumeX(:, :,z+1));
% size(slice)
intensity_xyz1=slice(xBas, yBas);
intensity_xyz2=slice(xBas, yBas1);
intensity_xyz3=slice(xBas1, yBas);
intensity_xyz4=slice(xBas1, yBas1);
end
% Calculate the interpolated intensity
data.intensity_loc=(intensity_xyz1*perc(1)+intensity_xyz2*perc(2)+intensity_xyz3*perc(3)+intensity_xyz4*perc(4));
% size(data.intensity_loc)
data.xBas = xBas; data.yBas = yBas;
otherwise
if(isempty(data.VolumeX))
data.intensity_loc=double(squeeze(data.Volume(z+1,xBas, yBas)));
else
data.intensity_loc=double(data.VolumeX(xBas, yBas,z+1));
end
data.xBas = xBas; data.yBas = yBas;
end
% Update the shear image buffer
switch (data.RenderType)
case {'mip'}
data=updatebuffer_MIP(data,maskv,z);
case {'color'}
data=updatebuffer_COLOR(data);
case {'bw'}
data=updatebuffer_BW(data);
case {'shaded'}
data=returnnormal(z+1,xBas, yBas,data);
data=updatebuffer_SHADED(data);
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 5 Nov 2012
You have
data.py=(pystart+1:pyend-1); data.px=(pxstart+1:pxend-1);
That attempts to update "data" inside the parfor loop. With all the conditionals, the parser cannot prove that the result would be independent of the order that the parfor loops were executed in. Likewise for other components of "data" that you update.
You can update a non-indexed variable inside a parfor loop only if the variable's value is provably written inside the loop before it is used within that iteration, so that the value used will not be affected by the order of execution of the loops. You cannot access such a variable afterwards.
You can update variables in an indexed manner, such as data(z+1).py = (something), within certain indexing constraints that ensure that different iterations do not update the same location.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!