Home > matlab > pm3.m

pm3

PURPOSE ^

PARALLEL CONTEXT

SYNOPSIS ^

function pm3(n1,n2,ifil,B,tit1,tit2,tit3,tit_tex,names1,names2,name3,DirectoryName,var_type)

DESCRIPTION ^

 PARALLEL CONTEXT
 See also the comment in random_walk_metropolis_hastings.m funtion.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function pm3(n1,n2,ifil,B,tit1,tit2,tit3,tit_tex,names1,names2,name3,DirectoryName,var_type)
0002 
0003 % PARALLEL CONTEXT
0004 % See also the comment in random_walk_metropolis_hastings.m funtion.
0005 
0006 
0007 % Copyright (C) 2007-2011 Dynare Team
0008 %
0009 % This file is part of Dynare.
0010 %
0011 % Dynare is free software: you can redistribute it and/or modify
0012 % it under the terms of the GNU General Public License as published by
0013 % the Free Software Foundation, either version 3 of the License, or
0014 % (at your option) any later version.
0015 %
0016 % Dynare is distributed in the hope that it will be useful,
0017 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0018 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0019 % GNU General Public License for more details.
0020 %
0021 % You should have received a copy of the GNU General Public License
0022 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0023 
0024 global options_ M_ oo_
0025 
0026 nn = 3;
0027 MaxNumberOfPlotsPerFigure = nn^2; % must be square
0028 varlist = names2;
0029 if isempty(varlist)
0030     varlist = names1;
0031     SelecVariables = (1:M_.endo_nbr)';
0032     nvar = M_.endo_nbr;
0033 else
0034     nvar = size(varlist,1);
0035     SelecVariables = [];
0036     for i=1:nvar
0037         if ~isempty(strmatch(varlist(i,:),names1,'exact'))
0038             SelecVariables = [SelecVariables;strmatch(varlist(i,:),names1,'exact')];
0039         end
0040     end
0041 end
0042 if options_.TeX
0043     % needs to be fixed
0044     varlist_TeX = [];
0045     for i=1:nvar
0046         if i==1
0047             varlist_TeX = M_.endo_names_tex(SelecVariables(i),:);
0048         else
0049             varlist_TeX = char(varlist_TeX,M_.endo_names_tex(SelecVariables(i),:));
0050         end
0051     end
0052 end
0053 Mean = zeros(n2,nvar);
0054 Median = zeros(n2,nvar);
0055 Std = zeros(n2,nvar);
0056 Distrib = zeros(9,n2,nvar);
0057 HPD = zeros(2,n2,nvar);
0058 fprintf(['MH: ' tit1 '\n']);
0059 stock1 = zeros(n1,n2,B);
0060 k = 0;
0061 for file = 1:ifil
0062     load([DirectoryName '/' M_.fname var_type int2str(file)]);
0063     if size(size(stock),2) == 4
0064         stock = squeeze(stock(1,:,1:n2,:));
0065     end
0066     k = k(end)+(1:size(stock,3));
0067     stock1(:,:,k) = stock;
0068 end
0069 clear stock
0070 tmp =zeros(B,1);
0071 for i = 1:nvar
0072     for j = 1:n2
0073         [Mean(j,i),Median(j,i),Var(j,i),HPD(:,j,i),Distrib(:,j,i)] = ...
0074             posterior_moments(squeeze(stock1(SelecVariables(i),j,:)),0,options_.mh_conf_sig);
0075     end
0076 end
0077 clear stock1
0078 for i = 1:nvar
0079     name = deblank(names1(SelecVariables(i),:));
0080     eval(['oo_.' name3 '.Mean.' name ' = Mean(:,i);']);
0081     eval(['oo_.' name3 '.Median.' name ' = Median(:,i);']);
0082     eval(['oo_.' name3 '.Var.' name ' = Var(:,i);']);
0083     eval(['oo_.' name3 '.Distribution.' name ' = Distrib(:,:,i);']);
0084     eval(['oo_.' name3 '.HPDinf.' name ' = HPD(1,:,i);']);
0085     eval(['oo_.' name3 '.HPDsup.' name ' = HPD(2,:,i);']);
0086 end
0087 %%
0088 %%     Finally I build the plots.
0089 %%
0090 
0091 % Block of code executed in parallel, with the exception of file
0092 % .tex generation always run sequentially. This portion of code is execute in parallel by
0093 % pm3_core1.m function.
0094 
0095 % %%%%%%%%%   PARALLEL BLOCK % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0096 %
0097 % %%% The file .TeX! are not saved in parallel.
0098 
0099 
0100 subplotnum = 0;
0101 
0102 if options_.TeX
0103     fidTeX = fopen([M_.dname '/Output/' M_.fname '_' name3 '.TeX'],'w');
0104     fprintf(fidTeX,'%% TeX eps-loader file generated by Dynare.\n');
0105     fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
0106     fprintf(fidTeX,' \n');
0107     
0108     for i=1:nvar
0109         NAMES = [];
0110         TEXNAMES = [];
0111         if max(abs(Mean(:,i))) > 10^(-6)
0112             subplotnum = subplotnum+1;
0113             name = deblank(varlist(i,:));
0114             NAMES = name;
0115             texname = deblank(varlist_TeX(i,:));
0116             TEXNAMES = ['$' texname '$'];
0117         end
0118         if subplotnum == MaxNumberOfPlotsPerFigure || i == nvar
0119             fprintf(fidTeX,'\\begin{figure}[H]\n');
0120             for jj = 1:size(TEXNAMES,1)
0121                 fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{%s}\n'],deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:)));
0122             end
0123             fprintf(fidTeX,'\\centering \n');
0124             fprintf(fidTeX,['\\includegraphics[scale=0.5]{%s_' name3 '_%s}\n'],M_.fname,deblank(tit3(i,:)));
0125             fprintf(fidTeX,'\\label{Fig:%s:%s}\n',name3,deblank(tit3(i,:)));
0126             fprintf(fidTeX,'\\end{figure}\n');
0127             fprintf(fidTeX,' \n');
0128             subplotnum = 0;
0129         end
0130     end
0131     fprintf(fidTeX,'%% End of TeX file.\n');
0132     fclose(fidTeX);
0133 end
0134 
0135 % Store the variable mandatory for local/remote parallel computing.
0136 
0137 localVars=[];
0138 
0139 localVars.tit1=tit1;
0140 localVars.nn=nn;
0141 localVars.n2=n2;
0142 localVars.Distrib=Distrib;
0143 localVars.varlist=varlist;
0144 localVars.MaxNumberOfPlotsPerFigure=MaxNumberOfPlotsPerFigure;
0145 localVars.name3=name3;
0146 localVars.tit3=tit3;
0147 localVars.Mean=Mean;
0148 % Like sequential execution!
0149 
0150 
0151 if ~exist('OCTAVE_VERSION')
0152     % Commenting for testing!
0153     if isnumeric(options_.parallel) || ceil(size(varlist,1)/MaxNumberOfPlotsPerFigure)<4,
0154         fout = pm3_core(localVars,1,nvar,0);
0155         
0156         % Parallel execution!
0157     else
0158         isRemoteOctave = 0;
0159         for indPC=1:length(options_.parallel),
0160             isRemoteOctave = isRemoteOctave + (findstr(options_.parallel(indPC).MatlabOctavePath, 'octave'));
0161         end
0162         if isRemoteOctave
0163             fout = pm3_core(localVars,1,nvar,0);
0164         else
0165             globalVars = struct('M_',M_, ...
0166                 'options_', options_, ...
0167                 'oo_', oo_);
0168             [fout, nBlockPerCPU, totCPU] = masterParallel(options_.parallel, 1, nvar, [],'pm3_core', localVars,globalVars, options_.parallel_info);
0169         end
0170     end
0171 else
0172     % For the time being in Octave enviroment the pm3.m is executed only in
0173     % serial modality, to avoid problem with the plots.
0174     
0175     fout = pm3_core(localVars,1,nvar,0);
0176 end
0177 
0178 
0179 fprintf(['MH: ' tit1 ', done!\n']);
0180 
0181 
0182 
0183 
0184 
0185

Generated on Tue 22-May-2012 02:40:23 by m2html © 2005