overhead communication in Parfor

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

sorry, I did not notice this when I asked the question.
Actually, in my code, it is: y = function(x((ii-1)*128)+1:ii*128); in this case, I think there is no overlap anymore and the same number of elements is taken each time. However, it is still not slicing.
In fact, for each core, it is required to handle non-overlapped part of X, of which length is 128.
XA = reshape(X, 128, :);
Then
parfor ii = 1 : 64
y(ii) = function(XA(:,ii));
end
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

Thank you very much for your answer. I will test on it. BY the way, could you point out what kind of matrix or cell is sliced and what is not? Now, I think I am confused by this definition.
What's more, since X is not a matrix but cell. I am just wondering maybe X{:,ii} does not work.
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.
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 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!