perfect_foresight_problem MEX: number of threads is now configurable

parent 0eab36e8
......@@ -71,6 +71,7 @@ options_.huge_number = 1e7;
% Default number of threads for parallelized mex files.
options_.threads.kronecker.sparse_hessian_times_B_kronecker_C = num_procs;
options_.threads.local_state_space_iteration_2 = 1;
options_.threads.perfect_foresight_problem = num_procs;
% steady state
options_.jacobian_flag = true;
......
......@@ -191,7 +191,7 @@ if ~isreal(oo_.endo_simul(:)) % cannot happen with bytecode or the perfect_fores
model_dynamic_g1_nz = str2func([M_.fname,'.dynamic_g1_nz']);
[nzij_pred, nzij_current, nzij_fwrd] = model_dynamic_g1_nz();
residuals = perfect_foresight_problem(yy(:), M_.fname, sum(M_.dynamic_tmp_nbr(1:2)), y0, yT, oo_.exo_simul, M_.params, oo_.steady_state, periods, M_.endo_nbr, M_.maximum_lag, M_.maximum_endo_lag, M_.lead_lag_incidence, nzij_pred, nzij_current, nzij_fwrd, M_.has_external_function, options_.use_dll);
residuals = perfect_foresight_problem(yy(:), M_.fname, sum(M_.dynamic_tmp_nbr(1:2)), y0, yT, oo_.exo_simul, M_.params, oo_.steady_state, periods, M_.endo_nbr, M_.maximum_lag, M_.maximum_endo_lag, M_.lead_lag_incidence, nzij_pred, nzij_current, nzij_fwrd, M_.has_external_function, options_.use_dll, options_.threads.perfect_foresight_problem);
if max(abs(residuals))< options_.dynatol.f
oo_.deterministic_simulation.status = 1;
......
......@@ -140,7 +140,7 @@ if nargout>1
model_dynamic_g1_nz = str2func([M_.fname,'.dynamic_g1_nz']);
[nzij_pred, nzij_current, nzij_fwrd] = model_dynamic_g1_nz();
residuals = perfect_foresight_problem(yy(:), M_.fname, sum(M_.dynamic_tmp_nbr(1:2)), y0, yT, oo_.exo_simul, M_.params, oo_.steady_state, periods, M_.endo_nbr, M_.maximum_lag, M_.maximum_endo_lag, M_.lead_lag_incidence, nzij_pred, nzij_current, nzij_fwrd, M_.has_external_function, options_.use_dll);
residuals = perfect_foresight_problem(yy(:), M_.fname, sum(M_.dynamic_tmp_nbr(1:2)), y0, yT, oo_.exo_simul, M_.params, oo_.steady_state, periods, M_.endo_nbr, M_.maximum_lag, M_.maximum_endo_lag, M_.lead_lag_incidence, nzij_pred, nzij_current, nzij_fwrd, M_.has_external_function, options_.use_dll, options_.threads.perfect_foresight_problem);
end
maxerror = max(max(abs(residuals)));
end
......
......@@ -67,7 +67,7 @@ h1 = clock;
for iter = 1:options.simul.maxit
h2 = clock;
[res, A] = perfect_foresight_problem(y, M.fname, sum(M.dynamic_tmp_nbr(1:2)), y0, yT, exogenousvariables, M.params, steadystate, periods, ny, M.maximum_lag, M.maximum_endo_lag, M.lead_lag_incidence, nzij_pred, nzij_current, nzij_fwrd, M.has_external_function, options.use_dll);
[res, A] = perfect_foresight_problem(y, M.fname, sum(M.dynamic_tmp_nbr(1:2)), y0, yT, exogenousvariables, M.params, steadystate, periods, ny, M.maximum_lag, M.maximum_endo_lag, M.lead_lag_incidence, nzij_pred, nzij_current, nzij_fwrd, M.has_external_function, options.use_dll, options.threads.perfect_foresight_problem);
if options.endogenous_terminal_period && iter > 1
for it = 1:periods
......
......@@ -56,7 +56,7 @@ else
model_dynamic_g1_nz = str2func([M.fname,'.dynamic_g1_nz']);
[nzij_pred, nzij_current, nzij_fwrd] = model_dynamic_g1_nz();
[y, check] = dynare_solve(@perfect_foresight_problem,z(:), options, M.fname, sum(M.dynamic_tmp_nbr(1:2)), y0, yT, exogenousvariables, M.params, steadystate, options.periods, M.endo_nbr, M.maximum_lag, M.maximum_endo_lag, M.lead_lag_incidence, nzij_pred, nzij_current, nzij_fwrd, M.has_external_function, options.use_dll);
[y, check] = dynare_solve(@perfect_foresight_problem,z(:), options, M.fname, sum(M.dynamic_tmp_nbr(1:2)), y0, yT, exogenousvariables, M.params, steadystate, options.periods, M.endo_nbr, M.maximum_lag, M.maximum_endo_lag, M.lead_lag_incidence, nzij_pred, nzij_current, nzij_fwrd, M.has_external_function, options.use_dll, options.threads.perfect_foresight_problem);
end
if all(imag(y)<.1*options.dynatol.x)
......
......@@ -40,6 +40,8 @@ switch mexname
options_.threads.kronecker.sparse_hessian_times_B_kronecker_C = n;
case 'local_state_space_iteration_2'
options_.threads.local_state_space_iteration_2 = n;
case 'perfect_foresight_problem'
options_.threads.perfect_foresight_problem = n;
otherwise
message = [ mexname ' is not a known parallel mex file.' ];
message_id = 'Dynare:Threads:UnknownParallelMex';
......
......@@ -28,8 +28,8 @@
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nlhs < 1 || nlhs > 2 || nrhs != 18)
mexErrMsgTxt("Must have 18 input arguments and 1 or 2 output arguments");
if (nlhs < 1 || nlhs > 2 || nrhs != 19)
mexErrMsgTxt("Must have 19 input arguments and 1 or 2 output arguments");
bool compute_jacobian = nlhs == 2;
// Give explicit names to input arguments
......@@ -51,6 +51,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
const mxArray *nzij_fwrd_mx = prhs[15];
const mxArray *has_external_function_mx = prhs[16];
const mxArray *use_dll_mx = prhs[17];
const mxArray *num_threads_mx = prhs[18];
// Check input and map it to local variables
if (!(mxIsChar(basename_mx) && mxGetM(basename_mx) == 1))
......@@ -131,6 +132,10 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
mexErrMsgTxt("use_dll should be a logical scalar");
bool use_dll = static_cast<bool>(mxGetScalar(use_dll_mx));
if (!(mxIsScalar(num_threads_mx) && mxIsNumeric(num_threads_mx)))
mexErrMsgTxt("num_threads should be a numeric scalar");
int num_threads = static_cast<int>(mxGetScalar(num_threads_mx));
// Allocate output matrices
plhs[0] = mxCreateDoubleMatrix(periods*ny, 1, mxREAL);
double *stacked_residual = mxGetPr(plhs[0]);
......@@ -178,7 +183,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
/* Parallelize the main loop, if use_dll and no external function (to avoid
parallel calls to MATLAB) */
#pragma omp parallel if (use_dll && !has_external_function)
#pragma omp parallel num_threads(num_threads) if (use_dll && !has_external_function)
{
// Allocate (thread-private) model evaluator (which allocates space for temporaries)
std::unique_ptr<DynamicModelCaller> m;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment