admin管理员组文章数量:1221352
i am trying to visualize several parallel processes in matlab. The processes can of course have different update rates. I have implemented a small example for this:
% Clean up environment
clear;
close all;
clc;
% Stop running parallel pools
if ~isempty(gcp('nocreate'))
delete(gcp('nocreate'));
end
% Create pool with two processes
if isempty(gcp('nocreate'))
parpool(2);
end
% Define DataQueue and connect function handle to afterEach-function
Q = parallel.pool.DataQueue;
afterEach(Q, @updatePlot);
% Prepare figures
f{1} = figure;
f{2} = figure;
ax{1} = axes(f{1});
ax{2} = axes(f{2});
updatePlot(ax, cell(0));
shg;
% Let the test run for 60 s
timeSec = 60;
% Start two parallel workers
tStart = tic;
spmd(2)
if spmdIndex == 1
% Worker 1
idx = 1;
while idx < timeSec + 1
% "Calculate" some data
r = round(rand * 50) + 5;
pause(1);
% Send it to the client
send(Q, {1 r idx});
idx = idx + 1;
end
end
if spmdIndex == 2
% Worker 2
idx = 1;
while idx < 2 * timeSec + 1
% "Calculate" some data
r = round(rand * 100) + 5;
pause(0.5);
% Send it to the client
send(Q, {2 r idx});
idx = idx + 1;
end
end
end
toc(tStart)
delete(gcp('nocreate'));
% Function to update figures
function updatePlot(varargin)
persistent ax;
persistent s1;
persistent s2;
persistent t1;
persistent t2;
if nargin == 2
% Initialize
ax = varargin{1};
s1 = surfc(ax{1}, [], [], []);
t1 = title(ax{1}, '');
axis(ax{1}, 'tight');
s2 = surfc(ax{2}, [], [], []);
t2 = title(ax{2}, '');
axis(ax{2}, 'tight');
else
% Update plot depending on worker
if varargin{1}{1} == 1
values = rand(varargin{1}{2});
s1(1).XData = 1:varargin{1}{2};
s1(2).XData = 1:varargin{1}{2};
s1(1).YData = 1:varargin{1}{2};
s1(2).YData = 1:varargin{1}{2};
s1(1).ZData = values;
s1(2).ZData = values;
t1.String = num2str(varargin{1}{3});
drawnow nocallbacks;
elseif varargin{1}{1} == 2
values = rand(varargin{1}{2});
s2(1).XData = 1:varargin{1}{2};
s2(2).XData = 1:varargin{1}{2};
s2(1).YData = 1:varargin{1}{2};
s2(2).YData = 1:varargin{1}{2};
s2(1).ZData = values;
s2(2).ZData = values;
t2.String = num2str(varargin{1}{3});
drawnow nocallbacks;
end
end
end
Now the figures are not updated according to the update rates. Instead, the figures are only ever updated at the rate of the slowest worker. This means that updates in the figure of the faster worker are basically skipped.
How can I solve this problem so that the update rates of the figures correspond to the update rates of the workers (e.g. if I have a very fast worker and a very slow worker)?
本文标签: parallel processingUpdate figures from different workersStack Overflow
版权声明:本文标题:parallel processing - Update figures from different workers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739361381a2159835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论