28 views (last 30 days)

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?

Walter Roberson
on 14 Jan 2020

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.

Walter Roberson
on 15 Jan 2020

You are using all of template in every worker. MATLAB must send all of it over: there is no getting around making it a broadcast variable.

Note: for increased efficiency you can pre-calculate template(logical_idx) and store it in a variable instead of calculating it in your inner loop.

Walter Roberson
on 15 Jan 2020

image_slices{row} = image(row:row+template_height-1, 1:template_width-1);

The 1:template_width-1 is wrong. The upper bound needs to be the such that col:col+template_width-1 fits for the largest col value -- so the upper bound must be search_width+template_width-1

Matt J
on 14 Jan 2020

Edited: Matt J
on 14 Jan 2020

Matt J
on 14 Jan 2020

Opportunities for recent engineering grads.

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

Start Hunting!
## 5 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/500124-parfor-broadcast-variable#comment_785741

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/500124-parfor-broadcast-variable#comment_785741

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/500124-parfor-broadcast-variable#comment_785821

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/500124-parfor-broadcast-variable#comment_785821

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/500124-parfor-broadcast-variable#comment_785829

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/500124-parfor-broadcast-variable#comment_785829

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/500124-parfor-broadcast-variable#comment_785867

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/500124-parfor-broadcast-variable#comment_785867

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/500124-parfor-broadcast-variable#comment_785869

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/500124-parfor-broadcast-variable#comment_785869

Sign in to comment.