overhead communication in Parfor

3 views (last 30 days)
I would like to use parfor to speed up my code but it does not work well. I think it may be due to a warning: Variable "x" is indexed, but not sliced, in a parlor loop. This might result in unnecessary communication.
My code looks similar to the following,
X = cell{128*64,1};
parfor ii = 1:64
y = function(X(ii:ii*128))
end

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jun 2015
The number of elements of X that you are using is changing in each iteration, and the elements overlap between different iterations. slicing requires that the same number of elements be taken each time and requires that the ranges do not overlap, so each element is sent for exactly one call.
  4 Comments
Walter Roberson
Walter Roberson on 29 Jun 2015
XA = reshape(X, 128, :);
Then
parfor ii = 1 : 64
y(ii) = function(XA(:,ii));
end
Chao Song
Chao Song on 30 Jun 2015
Edited: Walter Roberson on 30 Jun 2015
This answer is quite helpful for me. Thank you very much!
By the way, may I ask you another question?
for gg = 1:64
parfor ii = 1:64
for kk = 1:64
yy(aa) = function(XA(:,aa));%%aa is related to gg and kk
end
end
end
In this case, system show that XA is not sliced balabala again. Why does it happen, I mean that must index aa be ii? I try to set aa be anything except ii, it does not work. and is there any solution to it? Thank you a lot!

Sign in to comment.

More Answers (1)

Sean de Wolski
Sean de Wolski on 29 Jun 2015
Since your matrix is not that big, one approach would be to replicate it to be a 64x128 where each column is the values you want transmitted to that worker. Then you could have the parfor loop run over columns of this array passing off one column at a time x(:,ii) and this will result in a sliced matrix.
If you have access to the R2015b Prerelease, there might also be something in there you find useful.
  4 Comments
Sean de Wolski
Sean de Wolski on 30 Jun 2015
This document helps explain classification of variables, basically, you can only have the loop iterator and a constant, not a dynamic range (like ii:ii*128).
My same thought above should work on a cell rather than an mxn matrix since cells can be sliced by the same rules as a numeric matrix. I'll try to provide a small example tomorrow.
Chao Song
Chao Song on 30 Jun 2015
Thank you a lot for your great answer. I also think basically, if the range is static, it should be sliced. However, even if I set XA(1:10,:) (a constant range), system still shows that it's not sliced. What's more, I find that it can only be the loop iterator.

Sign in to comment.

Categories

Find more on Parallel for-Loops (parfor) 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!