Clear Filters
Clear Filters

How can I use GlobalSearch with Parallelization?

2 views (last 30 days)
Hello, I am trying the code below which uses 'parfor' and 'globalsearch' at the same time.
But I got an warning message saying 'The function RUN might make an invalid workspace access inside PARFOR loop'
I am not sure what this means. But when I run the code ignoring such message, something is running but I am a bit worried that this would give me wrong results.
So I was wondering whether this warning is something that I can just ignore or I should not use PARFOR and GLOBALSEARCH(especailly RUN) at the same time.
Thanks in advance.
parfor i_a = 1:Na %Loop over state variable a
for i_d = 1:Nd %Loop over state variable d
for i_y = 1:Ny %Loop over state variable y
for i_t = 1:Nt %Loop over state variable y
noadjamount = (1-delta)*D(i_d);
if noadjamount < D(1)
noadjamount = D(1);
problem1 = createOptimProblem('fmincon',...
'x0',[x0(i_a,i_d,i_y,i_t); x1(i_a,i_d,i_y,i_t)],'options',...
gs1 = GlobalSearch('Display','off');
rng(14,'twister') % for reproducibility
[adj_sol, adjval] = run(gs1,problem1);
problem2 = createOptimProblem('fmincon',...
gs2 = GlobalSearch('Display','off');
rng(14,'twister') % for reproducibility
[noadj_sol, noadjval] = run(gs2,problem2);
[Vnew(i_a,i_d,i_y,i_t), ind] = max([-adjval, -noadjval],[],2);
% Store values needed(Policy function, Value, Adjust or not)
pol_ap(i_a,i_d,i_y,i_t) = (ind == 1).* adj_sol(1) + (ind == 2).*noadj_sol;
pol_dp(i_a,i_d,i_y,i_t) = (ind == 1).* adj_sol(2) + (ind == 2).*noadjamount;
indadj(i_a,i_d,i_y,i_t) = (ind == 1).*1;
indnadj(i_a,i_d,i_y,i_t) = (ind == 2).*1;
x0(i_a,i_d,i_y,i_t) = adj_sol(1);
x1(i_a,i_d,i_y,i_t) = adj_sol(2);
x2(i_a,i_d,i_y,i_t) = noadj_sol;

Answers (1)

Alvaro on 19 Jan 2023
Try moving that call of run to its own function.

Community Treasure Hunt

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

Start Hunting!