Share two variables between workers
8 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!