Share two variables between workers

9 ビュー (過去 30 日間)
or ohev shalom
or ohev shalom 2019 年 8 月 7 日
コメント済み: or ohev shalom 2019 年 8 月 8 日
Hi all,
I'm trying to share information between workers. I want to share a counter of the loop (parfor) and the elapsed time of the iteration (both of them allow me to see the progress of the script).
I've managed to use the parallel.pool.DataQueue along with afterEach in order to share the counter between workers but I can't seem to make it work with the elapsed time.
My code:
D = parallel.pool.DataQueue;
afterEach(D, @nUpdateWaitbar);
counter = 0;
parfor ...
time = tic;
%compute something..
elapsedTime = toc(time);
send(D,counter);
end
function nUpdateWaitbar(~)
counter = counter + 1;
%do something with counter - works fine.
%do somthing with elapsedTime - doesn't work.
end
I've tried to use parallel.pool.PollableDataQueue to share the elapsedTime:
D = parallel.pool.DataQueue;
shareTime = parallel.pool.PollableDataQueue;
afterEach(D, @nUpdateWaitbar);
counter = 0;
parfor ...
time = tic;
%compute something..
elapsedTime = toc(time);
send(shareTime,elapsedTime);
send(D,counter);
end
function nUpdateWaitbar(~)
elapsedTime = poll(shareTime);
counter = counter + 1;
%do something with counter - works fine.
%do somthing with elapsedTime - doesn't work.
end
But unfortunately it didn't work.
Any ideas?
Thanks in advance!

採用された回答

Edric Ellis
Edric Ellis 2019 年 8 月 8 日
I think this should work... I tried a slight modification of your code to make it executable:
function repro
D = parallel.pool.DataQueue;
shareTime = parallel.pool.PollableDataQueue;
afterEach(D, @nUpdateWaitbar);
counter = 0;
parfor idx = 1:10
time = tic;
pause(idx/4);
elapsedTime = toc(time);
send(shareTime,elapsedTime);
send(D,counter);
end
function nUpdateWaitbar(~)
elapsedTime = poll(shareTime);
counter = counter + 1;
disp([counter, elapsedTime]);
end
end
And got the following output:
>> repro
1.0000 0.2506
2.0000 0.5008
3.0000 0.7511
4.0000 1.0013
5.0000 1.2515
6.0000 1.5018
7.0000 1.7520
8.0000 2.0022
9.0000 2.5006
10.0000 2.2525
(This was using R2019a)
  1 件のコメント
or ohev shalom
or ohev shalom 2019 年 8 月 8 日
Thanks! I've figured it out now thanks to you.
My main problem is that I received weird times. Forgot to take into account the number of workers for the remaining time calculation >.<

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by