plot from inside of function in parallel worker
Show older comments
hi, i am running some function (fun1) parallel using spmd and i want to plot values that are calculated inside of fun1. I want to display the values in a single plot, using a custom plot function (PlotFun). The problem is that i can not use "send" directly inside the spmd environment. This has to happen from within fun1.
The problem is that fun1 does not recognise the DataQueue that i have set up earlier. And if i refer to it using "gcp", it still has problems but then regarding the send function.
This would not be a problem if i could use the "send" function directly inside spmd. The reason for this quite weird circumstance is that i want to plot some values inside an objective function that is being evaluated by a solver running in parallel mode. This is resembled by the following simplified example:
clear, clc;
% initialize plot function and create figure
PlotFun([], "INIT")
D = parallel.pool.DataQueue;
D.afterEach(@(x) PlotFun(x, "ADD")); % add value after each evaluation
spmd
% option a:
fun1;
% % option b:
%send(D, fun1);
end
function out = fun1
% option a:
send(gcp, 1);
% % option b:
%out = 1;
end
function PlotFun(x, UseMode)
persistent f;
persistent i;
if UseMode == "INIT"
f = figure;
i = 1;
return
end
if UseMode == "ADD"
set(0, 'CurrentFigure', f);
hold on
plot(i, x, 'bo')
hold off
i = i+1;
return
end
end
i out-commented option b. It does work, but i can not have the "send" there since that is hidden from me inside the solver file.
Accepted Answer
More Answers (0)
Categories
Find more on Parallel Computing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!