Borrar filtros
Borrar filtros

plot from inside of function in parallel worker

2 visualizaciones (últimos 30 días)
Torsten Reh
Torsten Reh el 25 de Dic. de 2021
Respondida: Walter Roberson el 26 de Dic. de 2021
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Dic. de 2021
Enhanced to support plotting multiple points at the same time.
In the current version, the x axis location becomes the same as the order that the values happen to be returned; you could certainly change that, such as having fun1 return two columns with x and y values
% 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 b:
send(D, fun1());
end
function out = fun1
out = labindex * 10 + randi(9);
end
function PlotFun(y, UseMode)
persistent AL AX i
if UseMode == "INIT"
f = figure;
AX = axes('Parent', f);
AL = animatedline('Parent', AX, 'color', 'b', 'Marker', 'o');
i = 0;
return
end
if UseMode == "ADD"
xvals = i + 1: i + length(y);
addpoints(AL, xvals, y);
i = xvals(end);
end
end

Más respuestas (0)

Categorías

Más información sobre Graphics Objects en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by