parent
55764e667f
commit
839b77d679
|
@ -19,6 +19,22 @@ function random_walk_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv,mh_bou
|
||||||
% SPECIAL REQUIREMENTS
|
% SPECIAL REQUIREMENTS
|
||||||
% None.
|
% None.
|
||||||
|
|
||||||
|
% PARALLEL CONTEXT
|
||||||
|
% The most computationally intensive part of this function may be executed
|
||||||
|
% in parallel. The code sutable to be executed in
|
||||||
|
% parallel on multi core or cluster machine (in general a 'for' cycle),
|
||||||
|
% is removed from this function and placed in random_walk_metropolis_hastings_core.m funtion.
|
||||||
|
% Then the DYNARE parallel package contain a set of pairs matlab functions that can be executed in
|
||||||
|
% parallel and called name_function.m and name_function_core.m.
|
||||||
|
% In addition in parallel package we have second set of functions used
|
||||||
|
% to manage the parallel computation.
|
||||||
|
%
|
||||||
|
% This function was the first function to be parallelized, later other
|
||||||
|
% functions have been parallelized using the same methodology.
|
||||||
|
% Then the comments write here can be used for all the other pairs of
|
||||||
|
% parallel functions and also for management funtions.
|
||||||
|
|
||||||
|
|
||||||
% Copyright (C) 2006-2008,2010 Dynare Team
|
% Copyright (C) 2006-2008,2010 Dynare Team
|
||||||
%
|
%
|
||||||
% This file is part of Dynare.
|
% This file is part of Dynare.
|
||||||
|
@ -47,6 +63,10 @@ InitSizeArray = min([repmat(MAX_nruns,nblck,1) fline+nruns-1],[],2);
|
||||||
|
|
||||||
load([MhDirectoryName '/' ModelName '_mh_history.mat'],'record');
|
load([MhDirectoryName '/' ModelName '_mh_history.mat'],'record');
|
||||||
|
|
||||||
|
% Snapshot of the current state of computing. It necessary for the parallel
|
||||||
|
% execution (i.e. to execute in a corretct way portion of code remotely or
|
||||||
|
% on many core). The mandatory variables for local/remote parallel
|
||||||
|
% computing are stored in localVars struct.
|
||||||
|
|
||||||
localVars = struct('TargetFun', TargetFun, ...
|
localVars = struct('TargetFun', TargetFun, ...
|
||||||
'ProposalFun', ProposalFun, ...
|
'ProposalFun', ProposalFun, ...
|
||||||
|
@ -66,15 +86,18 @@ localVars.InitSizeArray=InitSizeArray;
|
||||||
localVars.record=record;
|
localVars.record=record;
|
||||||
localVars.varargin=varargin;
|
localVars.varargin=varargin;
|
||||||
|
|
||||||
% tic,
|
|
||||||
|
|
||||||
|
% The user don't want to use parallel computing, or want to compute a
|
||||||
|
% single chain. In this cases Random walk Metropolis-Hastings algorithm is
|
||||||
|
% computed sequentially.
|
||||||
|
|
||||||
if isnumeric(options_.parallel) || (nblck-fblck)==0,
|
if isnumeric(options_.parallel) || (nblck-fblck)==0,
|
||||||
fout = random_walk_metropolis_hastings_core(localVars, fblck, nblck, 0);
|
fout = random_walk_metropolis_hastings_core(localVars, fblck, nblck, 0);
|
||||||
record = fout.record;
|
record = fout.record;
|
||||||
|
|
||||||
else
|
% Parallel in Local or remote machine.
|
||||||
% global variables for parallel routines
|
else
|
||||||
|
% Global variables for parallel routines.
|
||||||
globalVars = struct('M_',M_, ...
|
globalVars = struct('M_',M_, ...
|
||||||
'options_', options_, ...
|
'options_', options_, ...
|
||||||
'bayestopt_', bayestopt_, ...
|
'bayestopt_', bayestopt_, ...
|
||||||
|
@ -109,8 +132,6 @@ irun = fout(1).irun;
|
||||||
NewFile = fout(1).NewFile;
|
NewFile = fout(1).NewFile;
|
||||||
|
|
||||||
|
|
||||||
% ComptationalTime=toc,
|
|
||||||
|
|
||||||
% record.Seeds.Normal = randn('state');
|
% record.Seeds.Normal = randn('state');
|
||||||
% record.Seeds.Unifor = rand('state');
|
% record.Seeds.Unifor = rand('state');
|
||||||
save([MhDirectoryName '/' ModelName '_mh_history.mat'],'record');
|
save([MhDirectoryName '/' ModelName '_mh_history.mat'],'record');
|
||||||
|
@ -119,4 +140,4 @@ disp(['MH: Total number of generated files : ' int2str(NewFile(1)*nblck) '.
|
||||||
disp(['MH: Total number of iterations : ' int2str((NewFile(1)-1)*MAX_nruns+irun-1) '.'])
|
disp(['MH: Total number of iterations : ' int2str((NewFile(1)-1)*MAX_nruns+irun-1) '.'])
|
||||||
disp('MH: average acceptation rate per chain : ')
|
disp('MH: average acceptation rate per chain : ')
|
||||||
disp(record.AcceptationRates);
|
disp(record.AcceptationRates);
|
||||||
disp(' ')
|
disp(' ')
|
||||||
|
|
|
@ -1,6 +1,53 @@
|
||||||
function myoutput = random_walk_metropolis_hastings_core(myinputs,fblck,nblck,whoiam, ThisMatlab)
|
function myoutput = random_walk_metropolis_hastings_core(myinputs,fblck,nblck,whoiam, ThisMatlab)
|
||||||
|
% PARALLEL CONTEXT
|
||||||
|
% This function contain the most computationally intensive portion of code in
|
||||||
|
% random_walk_metropolis_hastings (the 'for xxx = fblck:nblck' loop). The branches in 'for'
|
||||||
|
% cycle and are completely independent than suitable to be executed in parallel way.
|
||||||
|
%
|
||||||
|
% INPUTS
|
||||||
|
% o myimput [struc] The mandatory variables for local/remote
|
||||||
|
% parallel computing obtained from random_walk_metropolis_hastings.m
|
||||||
|
% function.
|
||||||
|
% o fblck and nblck [integer] The Metropolis-Hastings chains.
|
||||||
|
% o whoiam [integer] In concurrent programming a modality to refer to the differents thread running in parallel is needed.
|
||||||
|
% The integer whoaim is the integer that
|
||||||
|
% allows us to distinguish between them. Then it is the index number of this CPU among all CPUs in the
|
||||||
|
% cluster.
|
||||||
|
% o ThisMatlab [integer] Allows us to distinguish between the
|
||||||
|
% 'main' matlab, the slave matlab worker, local matlab, remote matlab,
|
||||||
|
% ... Then it is the index number of this slave machine in the cluster.
|
||||||
|
% OUTPUTS
|
||||||
|
% o myoutput [struc]
|
||||||
|
% If executed without parallel is the original output of 'for b =
|
||||||
|
% fblck:nblck' otherwise a portion of it computed on a specific core or
|
||||||
|
% remote machine. In this case:
|
||||||
|
% record;
|
||||||
|
% irun;
|
||||||
|
% NewFile;
|
||||||
|
% OutputFileName
|
||||||
|
%
|
||||||
|
% ALGORITHM
|
||||||
|
% Portion of Metropolis-Hastings.
|
||||||
|
%
|
||||||
|
% SPECIAL REQUIREMENTS.
|
||||||
|
% None.
|
||||||
|
|
||||||
% Copyright (C) 2006-2008 Dynare Team
|
% PARALLEL CONTEXT
|
||||||
|
% The most computationally intensive part of this function may be executed
|
||||||
|
% in parallel. The code sutable to be executed in parallel on multi core or cluster machine,
|
||||||
|
% is removed from this function and placed in random_walk_metropolis_hastings_core.m funtion.
|
||||||
|
% Then the DYNARE parallel package contain a set of pairs matlab functios that can be executed in
|
||||||
|
% parallel and called name_function.m and name_function_core.m.
|
||||||
|
% In addition in the parallel package we have second set of functions used
|
||||||
|
% to manage the parallel computation.
|
||||||
|
%
|
||||||
|
% This function was the first function to be parallelized, later other
|
||||||
|
% functions have been parallelized using the same methodology.
|
||||||
|
% Then the comments write here can be used for all the other pairs of
|
||||||
|
% parallel functions and also for management funtions.
|
||||||
|
|
||||||
|
|
||||||
|
% Copyright (C) 2006-2008,2010 Dynare Team
|
||||||
%
|
%
|
||||||
% This file is part of Dynare.
|
% This file is part of Dynare.
|
||||||
%
|
%
|
||||||
|
@ -24,7 +71,33 @@ end
|
||||||
|
|
||||||
global bayestopt_ estim_params_ options_ M_ oo_
|
global bayestopt_ estim_params_ options_ M_ oo_
|
||||||
|
|
||||||
struct2local(myinputs);
|
% reshape 'myinputs' for local computation.
|
||||||
|
% In order to avoid confusion in the name space, the instruction struct2local(myinputs) is replaced by:
|
||||||
|
|
||||||
|
TargetFun=myinputs.TargetFun;
|
||||||
|
ProposalFun=myinputs.ProposalFun;
|
||||||
|
xparam1=myinputs.xparam1;
|
||||||
|
vv=myinputs.vv;
|
||||||
|
mh_bounds=myinputs.mh_bounds;
|
||||||
|
ix2=myinputs.ix2;
|
||||||
|
ilogpo2=myinputs.ilogpo2;
|
||||||
|
ModelName=myinputs.ModelName;
|
||||||
|
fline=myinputs.fline;
|
||||||
|
npar=myinputs.npar;
|
||||||
|
nruns=myinputs.nruns;
|
||||||
|
NewFile=myinputs.NewFile;
|
||||||
|
MAX_nruns=myinputs.MAX_nruns;
|
||||||
|
d=myinputs.d;
|
||||||
|
InitSizeArray=myinputs.InitSizeArray;
|
||||||
|
record=myinputs.record;
|
||||||
|
varargin=myinputs.varargin;
|
||||||
|
|
||||||
|
% Necessary only for remote computing!
|
||||||
|
if whoiam
|
||||||
|
Parallel=myinputs.Parallel;
|
||||||
|
MasterName=myinputs.MasterName;
|
||||||
|
DyMo=myinputs.DyMo;
|
||||||
|
end
|
||||||
|
|
||||||
% (re)Set the penalty
|
% (re)Set the penalty
|
||||||
bayestopt_.penalty = Inf;
|
bayestopt_.penalty = Inf;
|
||||||
|
|
Loading…
Reference in New Issue