0

I'm trying to speed up my code using parfor. The purpose of the code is to slide a 3D square window on a 3D image and for each block of mxmxm apply a function.

I wrote this code:

function [ o_image ] = SlidingWindow( i_image, i_padSize, i_fun, i_options )
%SLIDINGWINDOW Summary of this function goes here
%   Detailed explanation goes here

o_image = zeros(size(i_image,1),size(i_image,2),size(i_image,3));
i_image = padarray(i_image,i_padSize,'symmetric');
i_padSize = num2cell(i_padSize);
[m,n,p] = deal(i_padSize{:});
[row,col,depth] = size(i_image);
windowShape = i_options.windowShape;
mask = i_options.mask;
parfor (i = m+1:row-m,i_options.cores)
    temp = i_image(i-m:i+m,:,:);
    for j = n+1:col-n
        for h = p+1:depth-p
            ii = i-m;
            jj = j-n;
            hh = h-p;
            temp = temp(:,j-n:j+n, h-p:h+p);
            o_image(ii,jj,hh) = parfeval(i_fun, temp, windowShape, mask);
        end
    end
end

end

I get one warning and one error that I don't understand how to solve. The warning says:

the entire array or structure 'i_image' is a broadcast variable.

The error says:

the PARFOR loop can not run due to the way variable 'o_image' is used.

I don't understand how to fix these two things. Any help is greatly appreciated!

BugsFree
  • 540
  • 1
  • 6
  • 24

1 Answers1

0

As far as I understand, parfeval takes care of running your function on the available number of workers, which is why it doesn't need to be surrounded by parfor. Assuming you already have an active parpool, changing the external parfor into for eliminates both problems.

Unfortunately, I can't support my answer with a benchmark or suggest a more fitting solution because your inputs are unknown.

It seems to me that the code can be optimized in other ways, mainly by . I would suggest you looked into the following resources:

P.S.: The 2nd part of (i = m+1:row-m,i_options.cores) seems out of place...

Community
  • 1
  • 1
Dev-iL
  • 23,742
  • 7
  • 57
  • 99