From 839b77d6797db0334b4199b7b0b49312f18d4e6e Mon Sep 17 00:00:00 2001 From: Marco Ratto Date: Mon, 31 May 2010 11:55:25 +0200 Subject: [PATCH] 1) eliminated us of struct2local; 2) changes to header/comments. --- matlab/random_walk_metropolis_hastings.m | 35 +++++++-- matlab/random_walk_metropolis_hastings_core.m | 77 ++++++++++++++++++- 2 files changed, 103 insertions(+), 9 deletions(-) diff --git a/matlab/random_walk_metropolis_hastings.m b/matlab/random_walk_metropolis_hastings.m index 236ae898c..1c6804235 100644 --- a/matlab/random_walk_metropolis_hastings.m +++ b/matlab/random_walk_metropolis_hastings.m @@ -19,6 +19,22 @@ function random_walk_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv,mh_bou % SPECIAL REQUIREMENTS % 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 % % 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'); +% 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, ... 'ProposalFun', ProposalFun, ... @@ -66,15 +86,18 @@ localVars.InitSizeArray=InitSizeArray; localVars.record=record; 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, fout = random_walk_metropolis_hastings_core(localVars, fblck, nblck, 0); record = fout.record; - -else - % global variables for parallel routines + +% Parallel in Local or remote machine. +else + % Global variables for parallel routines. globalVars = struct('M_',M_, ... 'options_', options_, ... 'bayestopt_', bayestopt_, ... @@ -109,8 +132,6 @@ irun = fout(1).irun; NewFile = fout(1).NewFile; -% ComptationalTime=toc, - % record.Seeds.Normal = randn('state'); % record.Seeds.Unifor = rand('state'); 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: average acceptation rate per chain : ') disp(record.AcceptationRates); -disp(' ') \ No newline at end of file +disp(' ') diff --git a/matlab/random_walk_metropolis_hastings_core.m b/matlab/random_walk_metropolis_hastings_core.m index 840bbc028..a3f811fbc 100644 --- a/matlab/random_walk_metropolis_hastings_core.m +++ b/matlab/random_walk_metropolis_hastings_core.m @@ -1,6 +1,53 @@ 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. % @@ -24,7 +71,33 @@ end 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 bayestopt_.penalty = Inf;