deterministic array filling in parfor loops

2 ビュー (過去 30 日間)
Raphael Maenle
Raphael Maenle 2018 年 9 月 17 日
編集済み: Matt J 2018 年 9 月 17 日
this code:
fit = [];
parfor i = 1:10
pause(3*rand());
fit = [fit; i];
disp(i);
end
gives every parallel worker a different end time. disp(i) shows an expectedly nondeterministic number output. eg.:
2, 1, 7, 6, 4, 3, 5, 8, 9,10
now, if we look at the array 'fit', which I would have expeced to be filled with a series of numbers equal to the output, it shows as:
1,2,3,4,5,6,7,8,9,10
Is this expected behavior? How do the workers know what position of the array they should write to?
best wishes!

回答 (1 件)

Matt J
Matt J 2018 年 9 月 17 日
編集済み: Matt J 2018 年 9 月 17 日
PARFOR pre-categorizes 'fit' as a reduction variable before the loop is run. Essentially, it knows in advance that
fit = [fit; i];
can be processed like,
parfor i=1:10
fit{i}= whatever
end
fit = cell2mat(fit)

カテゴリ

Help Center および File ExchangeParallel Computing Toolbox についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by