for loops nested inside parfor loops.

3 ビュー (過去 30 日間)
Alexantrou Serb
Alexantrou Serb 2015 年 4 月 16 日
編集済み: BHUSHAN MUTHIYAN 2017 年 9 月 5 日
Hallo everyone, I have a bit of a problem with some parfor coding. The concept is this:
parfor j = 1:1:10;
for i = 1:1:10;
Q(i) = i^2;
end
R = Q(j);
end
Of course this code is non-sensicle, but it illustrates the problem. I cannot create an array of values inside a parfor loop that I can then use throughout the parfor loop. In a practical application the expression for Q(i) will depend on j, so taking the sub-loop outside the parfor in not an option. Therefore, for now I wish to know why such a simple example can't be parallelised. As far as the algorithm is concerned it has to individually loop through i and then compute R in parallel with no obvious interdependency between the loops. Many thanks.

採用された回答

Titus Edelhofer
Titus Edelhofer 2015 年 4 月 16 日
Hi,
if you add the following line before the inner loop, it should work:
Q = zeros(10, 1);
Titus

その他の回答 (1 件)

Alexantrou Serb
Alexantrou Serb 2015 年 4 月 16 日
It did indeed. Thanks!
Does this happen because the parfor needs to allocate space for Q for each independent thread it generates?
  2 件のコメント
Edric Ellis
Edric Ellis 2015 年 4 月 16 日
No, it's because parfor cannot immediately deduce that you're not re-using values from Q in subsequent iterations of the loop. By assigning to the whole of Q, then it can correctly deduce that Q is a temporary variable. More details about parfor variable classification here.
BHUSHAN MUTHIYAN
BHUSHAN MUTHIYAN 2017 年 9 月 5 日
編集済み: BHUSHAN MUTHIYAN 2017 年 9 月 5 日
Hello Edric,
If I have to change the global variable which is declared outside the parfor loop, I am getting an error. Any reasoning for this ?
My code looks something like this :
out = zeros(dimfox, dimfoy, fis(4), ins(4), 'like', sumModel);
H = vision.Convolver('OutputSize', 'Valid', 'CustomProductDataType',numerictype([],32,frac), 'CustomOutputDataType', numerictype([],32,frac));
parfor im = 1:ins(4)
carve = zeros(dimfox, dimfoy, 'like', sumModel);
for f = 1:fis(4)
for ch = 1:fis(3)
carve(:) = carve + step(H,input(:,:,ch,im),filter(:,:,ch,f));
end
carve(:) = carve + bias(1, f);
out(:,:,f,im) = carve;
end
end
I am getting error as :
The variable out in a parfor cannot be classified.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by