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)?