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

 採用された回答

Walter Roberson
Walter Roberson 2015 年 6 月 29 日

0 投票

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 件のコメント

Chao Song
Chao Song 2015 年 6 月 29 日
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.
Chao Song
Chao Song 2015 年 6 月 29 日
In fact, for each core, it is required to handle non-overlapped part of X, of which length is 128.
Walter Roberson
Walter Roberson 2015 年 6 月 29 日
XA = reshape(X, 128, :);
Then
parfor ii = 1 : 64
y(ii) = function(XA(:,ii));
end
Chao Song
Chao Song 2015 年 6 月 30 日
編集済み: Walter Roberson 2015 年 6 月 30 日
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!

サインインしてコメントする。

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2015 年 6 月 29 日

0 投票

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 件のコメント

Chao Song
Chao Song 2015 年 6 月 29 日
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.
Chao Song
Chao Song 2015 年 6 月 29 日
What's more, since X is not a matrix but cell. I am just wondering maybe X{:,ii} does not work.
Sean de Wolski
Sean de Wolski 2015 年 6 月 30 日
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 2015 年 6 月 30 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeParallel for-Loops (parfor) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by