MATLAB: PARFOR: Broadcast variable

communicationimage processingImage Processing ToolboxMATLABParallel Computing Toolboxparfor

Hi, I'm starting to use "parfor" in my MATLAB scripts.
In this simple example, for each iteration, I get a 2D 'sub_matrix' from a 2D big matrix (image).
But I got this warning message: "the entire array is a broadcast variable. This might result in unnecessary communication overhead.
% For each pixel in the 'image':
parfor row = 1:N
for col = 1:M
% Crop a sub matrix from the original image:
sub_image = image(row:row+H-1, col:col+W-1); % WARNING HERE!
% Do other stuffs...
end
end
How can I modify this code to avoid this communication overhead?

Best Answer

parfor row = 1:N
for col = 1:M
% Crop a sub matrix from the original image:
sub_image = image(row:row+H-1, col:col+W-1); % WARNING HERE!
In order for it to be possible to slice the variable, one of the dimensions of the variable would have to depend only on the parfor index (possibly plus a constant.) You can do slicing by forming separate variables:
image_slices = cell(N,1);
for row = 1 : N
image_slices{row} = image(row:row+H-1, 1:M+W-1);
end
parfor row = 1 : N
image_slice = image_slices{row};
for col = 1 : M
sub_image = image_slice(:, col:col+W-1);
...
end
end
This would of course end up using a heck of a lot of memory.
I would suggest that you might be better off rewriting everything in terms of a 2D filter operation or possibly nonlinear filtering.