Why the data in the workers cannot be plotted in graph in PCT?

2 views (last 30 days)
In parpool i have two workers. I cannot use the output data in the workers for plotting the graph. How it can be accessed?
delete(gcp('nocreate'))
parpool(2)
delete(gcp);
parpool('AttachedFiles',{'parallel1.m','parallel2.m'})
poolobj = gcp('nocreate');
spmd
first_run = true;
for i=1:5
if labindex == 1
if first_run
A_parameters = 1;
disp('hello');
first_run = false;
else
A_parameters = labReceive()
end
A_output = parallel1(A_parameters);
else
B_output = parallel2(1);
labSend(B_output, 1);
end
end
end
parallel1.m code
function[str1]= parallel1(y1)
filename = fullfile(tempdir, 'talkk.dat');
% Memory map the file.
m = memmapfile(filename, 'Writable', true, 'Format','double' );
m.Data(1) = 0;
for i=1
y11=y1;
% Set first byte to zero, indicating a message is not yet ready.
a=[250000.1594+i 26000+i 27000+i];
str=a ;
len = length(str);
if (m.Data(1)==0)
% Update the file via the memory map.
m.Data(2:len+1) = str;
m.Data(1)=len;
disp('sending message')
end
str1=str;
% Wait until the first byte is set back to zero,indicating that a response is available.
while (m.Data(1) ~= 0)
pause(.25);
end
end
parallel2.m code
function[output]= parallel2(y)
yy=y;
k=1
% Respond to SEND using memmapfile class.
disp('ANSWER server is awaiting message');
filename = fullfile(tempdir, 'talkk.dat');
% Memory map the file.
m = memmapfile(filename, 'Writable', true, 'Format', 'double');
m.Data(1) = 0;
for i=1:5
% Wait until the first byte is not zero.
while (m.Data(1) == 0)
pause(.25);
end
if (m.Data(1)~=0)
textdat = double(m.Data(2:1+double(m.Data(1))))';
output=textdat+1;
%Display the message.
disp('Received message from SEND:')
disp(textdat)
% Signal to SEND that the response is ready.
m.Data(1) = 0;
k=k+1;
end
Data=1;
end
I want this textdat in the worker to be plotted for the whole iteration or any other way to plotting is also appreciated.Kindly give answers.Thanks in advance!!

Answers (2)

Jason Ross
Jason Ross on 20 Mar 2018
Edited: Jason Ross on 20 Mar 2018
You could look at using a DataQueue. There is an example of using one here.

William Smith
William Smith on 26 Mar 2018
Edited: William Smith on 26 Mar 2018
I have multiple parfor workers plotting 'invisible' graphs then saving to PNG. Seems to work fine.
Pseudocode:
fig = figure('Position', [ 100 100 1000 500 ], 'Visible', 'off' );
ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
plot(ax1, ...);
plot(ax2, ...);
print(fig, imageFilename, '-dpng');
close(fig);

Community Treasure Hunt

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

Start Hunting!

Translated by