Page 1 of 1

Parfor sucessfully implemented in Dynare

PostPosted: Fri Jul 17, 2015 8:48 pm
by eastnile
Dear Community,

I've recently implemented a parallelized version of the stoch_simul.m in order to speed up my computations. As mentioned in this thread, the main obstacle was the use of global variables; I've modified many of the original Dynare functions (such as set_random_seed) slightly so that they are passed local versions of M_, oo_, etc. My initial runs show very significant speedups if I run with the 4 cores on my CPU. Are any of you guys interested in this code? If so, I will clean up the files post them.

Best,
Zhao

Re: Parfor sucessfully implemented in Dynare

PostPosted: Thu Jul 30, 2015 1:24 am
by hakeru7
Hi Zhao,

I've been struggling with this precise problem. I'm simply trying to parallelize stoch_simul on a cluster for different parameterization.

Any help/hint would be greatly appreicated!

Re: Parfor sucessfully implemented in Dynare

PostPosted: Thu Jul 30, 2015 10:51 am
by jpfeifer
Example codes please.

Re: Parfor sucessfully implemented in Dynare

PostPosted: Fri Jul 31, 2015 6:04 pm
by hakeru7
Here it is. Any tips would be helpful! Thank you!

Re: Parfor sucessfully implemented in Dynare

PostPosted: Wed Aug 05, 2015 6:09 am
by jpfeifer
Ok. I will try to walk you through this, but I need you to do some changes for me.

1. The function allcomb is missing
2. I need a clean mod-file without all the loop part. Please provide a mod-file that has the correct starting values for all parameters hard-coded and runs the desired simulations.
3. Please provide a description of the targets. That is, when you write
Code: Select all
X(5,ii) = oo_.mean(10);
, please provide me with a comment showing the name of the variable you are looking for. Variable 10 for example seems to be welfare.
4. Please replace
Code: Select all
    ppsipi = X(1,ii);
    ppsiy = X(2,ii);
    rrhor = X(3,ii);
    ppibar = X(4,ii);

by set_param_value statements like
Code: Select all
set_param_value('ppsiy',start_value)

where start_value is the value for the first loop step.
5. Please get rid of all parameter dependencies defined before the model block. If you have an analytic steady state, please use a steady_state_model block.

Re: Parfor sucessfully implemented in Dynare

PostPosted: Thu Aug 06, 2015 8:44 pm
by hakeru7
Thank you!

Here are the fixes....As you see, I'm looping over 4 coefficients and would like to run some big grids on parallel.

Re: Parfor sucessfully implemented in Dynare

PostPosted: Mon Aug 17, 2015 2:01 am
by hakeru7
I'm still struggling with this. Any tips or hints on how to implement this...?

Re: Parfor sucessfully implemented in Dynare

PostPosted: Mon Aug 17, 2015 2:51 pm
by jpfeifer
This is somewhat more involved. First, you have to alter the dynare functions stoch_simul.m and disp_th_moments.m. For the latter, change the header to
Code: Select all
function oo_=disp_th_moments(dr,var_list,M_,options_,oo_)

(note that for some reasons the underscores are not displayed; just follow the already existing statements and variable definitions) and delete the
Code: Select all
global M_ oo_ options_

In stoch_simul.m change the call to
Code: Select all
            disp_th_moments(oo_.dr,var_list);

to
Code: Select all
    oo_=disp_th_moments(oo_.dr,var_list_,M_,options_,oo_);

Upon doing this, you should be able to use the attached code.

Re: Parfor sucessfully implemented in Dynare

PostPosted: Tue Aug 18, 2015 2:22 pm
by hakeru7
Thank you for your help! Made a couple of fixes and it seems to be working.

Changed the parfor loop to
Code: Select all
[a,b]=wrapper_function(X_in(:,ii),M_,options_,oo_,var_list_);
X_out(:,ii)=[a;b];


Two questions though,

1) It doesn't seem like stoch_simul needs to be altered. Since the loop calls resol.m directly and stoch_simul is only called in the first run, the original stoch_simul file seems to work.
2) the moments that return from the exercise are slightly off in decimal points compared to running a standalone mod-file once. Is this expected?

Thanks!

Re: Parfor sucessfully implemented in Dynare

PostPosted: Tue Aug 18, 2015 2:34 pm
by jpfeifer
1) The change to stoch_simul is needed to not break Dynare when executing it standalone. If you alter disp_th_moments in the describe way, you need to adapt Dynare to the new syntax.
2) No, this is not to be expected and may indicate there is something wrong. If you did not fix 1), this may be the reason.

Re: Parfor sucessfully implemented in Dynare

PostPosted: Tue Aug 18, 2015 7:59 pm
by hakeru7
I've changed the stoch_simul as you suggested and it's giving the same results that are slightly off.

What's weird is that the non-parallel loop I initially had is giving the same results as this parallelized version. It's the stand-alone that I run to check that is slightly off of these two....

Re: Parfor sucessfully implemented in Dynare

PostPosted: Wed Aug 19, 2015 8:17 am
by jpfeifer
Please provide code and instructions to replicate the issue.