Loading and saving variables when using parallel toolbox
Show older comments
I am solving a systme of equations using fsolve, where in order to compute those equations I need to solve a recursive problem. The recursive problem depends on the inputs and takes a long time to compute. However, one way to speed it up is to use a better guess. Unfortunately, it is not easy to come up with this guess. So what I am doing now is to come up with some "reasonable" guess the first time and then each time the solver calls the function I save the final result of the recursive problem and load it as the guess for the next time the function is called by fsolve. Basicaly, the code I am using has the following structure
fsolve(@my_fun,x0,options)
output = my_fun(param)
if exist('guess_Amat.mat','file')==2
load guess_Amat
end
Amat = my_fun_2(param,guess_Amat) % Compute Amat (a recursive problem where guess_Amat is the initial guess)
% Compute other things using Amat including output variable
% saving Amat
guess_Amat = Amat;
save guess_Amat guess_Amat
end
This works well when I am using a single core. However, when I set in the options
optimoptions('fsolve','UseParallel',true)
the code breaks since (as I understand) different workers are trying to save/load the Amat.mat file at the same time.
My question: Is there a way to adjust the code so that I can "save" A_mat each time and then "load" it for the next evaluation of my_fun to speed up the recursive protion of the code. When using the above code with a single worker loading/saving Amat saves me about 2/3 of the time so I am wondering if there is a way to adapt this appraoch when using the optimoptions('fsolve','UseParallel',true).
Accepted Answer
More Answers (1)
This might work. Basically, I am saying that you must repeatedly call fsolve in a loop having it do 1 iteration at a time.You generate guess_Amat once before the loop and at the end of each loop iteration.
function main()
guess_Amat=___________; %supply initial guess_Amat
options.UseParallel=true;
options.MaxIterations=1;
for i=1:N
[x0,fval]=fsolve( @(param) my_fun(param, guess_Amat) , x0,options);
guess_Amat = my_fun_2(x0,guess_Amat);
end
end
function fval = my_fun(param, Amat)
fval=...
end
2 Comments
Matt J
on 1 Sep 2020
You could also experiment with options.MaxIterations settings larger than one. This may diminish overhead from making a fresh call to fsolve, but you will be updating guess_Amat less frequently.
Michal Szkup
on 1 Sep 2020
Categories
Find more on Startup and Shutdown 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!