How can I use fmincon into the SPMD when the parameters of objective function differs from on workers to another?

Hello everyone,
the overall structure of my code is here:
%some parameters are defined before
%exclusive values for parameters for each workers are defined
%for exapmle, "u" is a parameter in cell array that each cell belongs to a worke: u{1} for worker 1
spmd
for
fun=@(x) norm(x-u)
fmincon
%other calculations based on the output of fmincon that provides "u" for the next itteration
end
end
when the fun is in the spmd block, I face the error of anonymous funtion that says I can not define the function into the spmd.
if the dunction goes out of the spmd, there is no more access to the workers data (calculated "u" in the example).
can anyone say how can handle this problem?

Respuestas (2)

You can use spmd to create the u data on the workers, but then use a parfor loop to do the optimizations.
spmd
u=labindex; %create composite
end
c=parallel.pool.Constant(u);
parfor i=1:numel(u)
uValue=c.Value
fun=@(x) (x-uValue).^2;
w{i}=fmincon(fun,0);
end

6 comentarios

thanks for reply. there is a for loop in the spmd and all calculations must be into the loop because it has stopping criteria. to create u data, there is a need to do all the statements in that afformentioned for loop just as the structure written in the question. in other words, all calculations must be itterative in the spmd block. so that, in this for loop the first step in fmincon optimization, the second step is calculating "u" for the next itteration based on the result of fmincon, and the third step is to check stopping criteria, and if it's not met, the next itteration begins.
I think you're going to have to restructure things. Perhaps as follows:
for i=1:numIterations
spmd
u=...
stop_flag=...
end
if all([stop_flag{:}]), break; end
U=parallel.pool.Constant(u);
Stop=parallel.pool.Constant(stop_flag);
parfor i=1:numel(U)
if ~Stop.Value
uValue=U.Value;
fun=@(x) (x-uValue).^2;
out=fmincon(fun,0);
u{i}=...
end
end
end
Thank you for your reply and sorry for my late reply due to working on the problem. the problem is that parfor loop does not meet the goal, because I need each worker seperately solve their own problem with their own paramters. so the optimization step must be done in the spmd.if not, in the parfor loop there is no clue for what the workers are doing and which problem they are solving. for more information, assume that a problem is decomposed to some subproblems, each worker should solve their own problem, the solution of them will be gathered and checked to see if those could be the solution of the main problem. in summary, the results of workers must be reachable and changable distinctly.
I don't see how the "parfor loop does not meet the goal". The parfor loop I outlined is solving a different problem with its own parameters on each worker by virtue of the fact that uValue is different on the different workers...
Regardless, what aboud Sean's proposal to use parFeval?
let me make my last comment clear by a question: In the parfor loop, can I tell to a worker to stop if some conditions are met? while the others continue to computations.
about the parfval; I have not used it so far, I'll give it a try to see how it works and if it can be helpful. thanks.
In the parfor loop, can I tell to a worker to stop if some conditions are met?
That's what stop_flag is for.

Iniciar sesión para comentar.

Categorías

Más información sobre Parallel Computing Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 18 de En. de 2021

Comentada:

el 24 de En. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by