Commit c3b5b65f authored by Marco Ratto's avatar Marco Ratto
Browse files

Further generalization of dyn_waitbar for parallel execution and...

Further generalization of dyn_waitbar for parallel execution and simplification of _core functions accordingly.
parent b23c5086
......@@ -98,38 +98,29 @@ end
RemoteFlag = 0;
if whoiam
waitbarString = ['Please wait... Bayesian (posterior) IRFs computing. (' int2str(fpar) 'of' int2str(npar) ')...'];
if Parallel(ThisMatlab).Local,
waitbarTitle=['Local '];
else
waitbarTitle=[Parallel(ThisMatlab).ComputerName];
if Parallel(ThisMatlab).Local==0,
RemoteFlag =1;
end
fMessageStatus(0,whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
prct0={0,whoiam,Parallel(ThisMatlab)};
else
if exist('OCTAVE_VERSION')
diary off;
printf('\n')
else
if strcmpi(type,'posterior')
h = waitbar(0,'Bayesian (posterior) IRFs...');
set(h,'Name','Bayesian (posterior) IRFs.');
elseif strcmpi(type,'gsa')
h = waitbar(0,'GSA (prior) IRFs...');
else
h = waitbar(0,'Bayesian (prior) IRFs...');
end
end
prct0=0;
end
if strcmpi(type,'posterior')
h = dyn_waitbar(prct0,'Bayesian (posterior) IRFs...');
elseif strcmpi(type,'gsa')
h = dyn_waitbar(prct0,'GSA (prior) IRFs...');
else
h = dyn_waitbar(prct0,'Bayesian (prior) IRFs...');
end
OutputFileName_bvardsge = {};
OutputFileName_dsge = {};
OutputFileName_param = {};
fpar0=fpar;
fpar = fpar-1;
fpar0=fpar;
if whoiam
ifil2=ifil2(whoiam);
......@@ -279,33 +270,29 @@ while fpar<npar
ifil2 = ifil2 + 1;
irun2 = 0;
end
if exist('OCTAVE_VERSION'),
if (whoiam==0),
printf(['Posterior IRF %3.f%% done\r'],(fpar/npar*100));
end
elseif ~whoiam,
waitbar(fpar/npar,h);
end
if whoiam,
if ~exist('OCTAVE_VERSION')
fprintf('Done! \n');
end
waitbarString = [ 'Subdraw ' int2str(fpar) '/' int2str(npar) ' done.'];
fMessageStatus((fpar-fpar0)/(npar-fpar0),whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
end
% if exist('OCTAVE_VERSION'),
% if (whoiam==0),
% printf(['Posterior IRF %3.f%% done\r'],(fpar/npar*100));
% end
% elseif ~whoiam,
% waitbar(fpar/npar,h);
% end
% if whoiam,
% if ~exist('OCTAVE_VERSION')
% fprintf('Done! \n');
% end
% waitbarString = [ 'Subdraw ' int2str(fpar) '/' int2str(npar) ' done.'];
% fMessageStatus((fpar-fpar0)/(npar-fpar0),whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
% end
dyn_waitbar((fpar-fpar0)/(npar-fpar0),h);
end
dyn_waitbar_close(h);
if whoiam==0
if nosaddle
disp(['PosteriorIRF :: Percentage of discarded posterior draws = ' num2str(nosaddle/(npar+nosaddle))])
end
if exist('h')
close(h);
end
if exist('OCTAVE_VERSION')
printf('\n');
diary on;
end
end
% Copy the rusults of computation on the call machine (specifically in the
......
......@@ -73,14 +73,11 @@ DirectoryName = CheckPath('Output');
RemoteFlag = 0;
if whoiam,
waitbarString = ['Please wait... PosteriorIRF Plots (exog. shocks ' int2str(fpar) 'of' int2str(npar) ')...'];
if Parallel(ThisMatlab).Local,
waitbarTitle=['Local '];
else
waitbarTitle=[Parallel(ThisMatlab).ComputerName];
RemoteFlag = 1;
if Parallel(ThisMatlab).Local==0,
RemoteFlag =1;
end
fMessageStatus(0,whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
prct0={0,whoiam,Parallel(ThisMatlab)};
dyn_waitbar(prct0,'PosteriorIRF Plots ...');
end
OutputFileName={};
......@@ -162,7 +159,8 @@ for i=fpar:npar,
if whoiam,
fprintf('Done! \n');
waitbarString = [ 'Exog. shocks ' int2str(i) '/' int2str(npar) ' done.'];
fMessageStatus((i-fpar+1)/(npar-fpar+1),whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
% fMessageStatus((i-fpar+1)/(npar-fpar+1),whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
dyn_waitbar((i-fpar+1)/(npar-fpar+1),[],waitbarString);
end
end% loop over exo_var
......
function h = dyn_waitbar(prctdone, varargin)
% h = dyn_waitbar(prctdone, varargin)
% adaptive waitbar, producing console mode waitbars with
% adaptive waitbar, producing console mode waitbars with
% octave and when console_mode=1
%
......@@ -22,41 +22,66 @@ function h = dyn_waitbar(prctdone, varargin)
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global options_
persistent running_text newString
persistent whoiam Parallel
if iscell(prctdone)
whoiam = prctdone{2};
Parallel = prctdone{3};
prctdone=prctdone{1};
end
if prctdone==0,
init=1;
if isempty(whoiam)
whoiam=0;
end
else
init=0;
end
if nargout,
h=[];
end
if exist('OCTAVE_VERSION') || options_.console_mode,
if ~whoiam
if nargout,
h=[];
if exist('OCTAVE_VERSION') || options_.console_mode,
if init,
diary off;
running_text = varargin{1};
newString='';
return;
elseif nargin>2,
running_text = varargin{2};
end
if exist('OCTAVE_VERSION'),
printf([running_text,' %3.f%% done\r'], prctdone*100);
else
s0=repmat('\b',1,length(newString));
newString=sprintf([running_text,' %3.f%% done'], prctdone*100);
fprintf([s0,'%s'],newString);
end
else
if nargout,
h = waitbar(prctdone,varargin{:});
else
waitbar(prctdone,varargin{:});
end
end
else
if init,
diary off;
running_text = varargin{1};
newString='';
return;
elseif nargin>2,
running_text = varargin{2};
elseif nargin>2
running_text = varargin{2};
end
if exist('OCTAVE_VERSION'),
printf([running_text,' %3.f%% done\r'], prctdone*100);
else
s0=repmat('\b',1,length(newString));
newString=sprintf([running_text,' %3.f%% done'], prctdone*100);
fprintf([s0,'%s'],newString);
end
else
if nargout,
h = waitbar(prctdone,varargin{:});
if Parallel.Local,
waitbarTitle=['Local '];
else
waitbar(prctdone,varargin{:});
waitbarTitle=[Parallel.ComputerName];
end
fMessageStatus(prctdone,whoiam,running_text, waitbarTitle, Parallel);
end
......@@ -29,3 +29,7 @@ if exist('OCTAVE_VERSION') || options_.console_mode,
else
close(h),
end
clear dyn_waitbar;
......@@ -237,17 +237,7 @@ if fload==0,
end
end
%
if exist('OCTAVE_VERSION') || (options_.console_mode == 1),
diary off;
if exist('OCTAVE_VERSION'),
printf('Please wait ... \r')
else
newString = 'Please wait ...';
fprintf('%s',newString)
end
else
h = waitbar(0,'Please wait...');
end
h = dyn_waitbar(0,'Please wait...');
istable=[1:Nsam];
jstab=0;
iunstable=[1:Nsam];
......@@ -318,24 +308,9 @@ if fload==0,
ys_=real(dr_.ys);
yys(:,j) = ys_;
ys_=yys(:,1);
if exist('OCTAVE_VERSION') || (options_.console_mode == 1),
if exist('OCTAVE_VERSION')
printf(['MC iteration %3.i / %3.i \r'], j,Nsam);
else
s0=repmat('\b',1,length(newString));
newString=sprintf('MC iteration %3.i / %3.i', j,Nsam);
fprintf([s0,'%s'],newString);
end
else
waitbar(j/Nsam,h,['MC iteration ',int2str(j),'/',int2str(Nsam)])
end
end
if (exist('OCTAVE_VERSION') || (options_.console_mode == 1)),
fprintf('\n'),
diary on,
else
close(h)
dyn_waitbar(j/Nsam,h,['MC iteration ',int2str(j),'/',int2str(Nsam)])
end
dyn_waitbar_close(h);
if prepSA,
T=T(:,:,1:jstab);
end
......@@ -416,17 +391,7 @@ else
if prepSA & isempty(strmatch('T',who('-file', filetoload),'exact')),
if exist('OCTAVE_VERSION') || (options_.console_mode == 1),
diary off;
if exist('OCTAVE_VERSION'),
printf('Please wait ... \r')
else
newString = 'Please wait ...';
fprintf('%s',newString)
end
else
h = waitbar(0,'Please wait...');
end
h = dyn_waitbar(0,'Please wait...');
options_.periods=0;
options_.nomoments=1;
options_.irf=0;
......@@ -455,24 +420,9 @@ else
ys_=real(dr_.ys);
yys(:,j) = ys_;
ys_=yys(:,1);
if exist('OCTAVE_VERSION') || (options_.console_mode == 1),
if exist('OCTAVE_VERSION')
printf(['MC iteration %3.i / %3.i \r'], j,ntrans);
else
s0=repmat('\b',1,length(newString));
newString=sprintf('MC iteration %3.i / %3.i', j,ntrans);
fprintf([s0,'%s'],newString);
end
else
waitbar(j/ntrans,h,['MC iteration ',int2str(j),'/',int2str(ntrans)])
end
end
if exist('OCTAVE_VERSION') || (options_.console_mode == 1),
fprintf('\n'),
diary on,
else
close(h);
dyn_waitbar(j/ntrans,h,['MC iteration ',int2str(j),'/',int2str(ntrans)])
end
dyn_waitbar_close(h);
save(filetoload,'T','-append')
elseif prepSA
load(filetoload,'T')
......
......@@ -59,13 +59,8 @@ global options_ M_ oo_
if whoiam
waitbarString = ['Parallel plots pm3 ...'];
if Parallel(ThisMatlab).Local,
waitbarTitle=['Local '];
else
waitbarTitle=[Parallel(ThisMatlab).ComputerName];
end
fMessageStatus(0,whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
prct0={0,whoiam,Parallel(ThisMatlab)};
h = dyn_waitbar(prct0,'Parallel plots pm3 ...');
end
......@@ -123,12 +118,15 @@ for i=fpar:nvar
end
if whoiam,
waitbarString = [ 'Variable ' int2str(i) '/' int2str(nvar) ' done.'];
fMessageStatus((i-fpar+1)/(nvar-fpar+1),whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
% waitbarString = [ 'Variable ' int2str(i) '/' int2str(nvar) ' done.'];
% fMessageStatus((i-fpar+1)/(nvar-fpar+1),whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
dyn_waitbar((i-fpar+1)/(nvar-fpar+1),h);
end
end
if whoiam,
dyn_waitbar_close(h);
end
myoutput.OutputFileName=OutputFileName;
......@@ -110,28 +110,15 @@ end
RemoteFlag = 0;
if whoiam
for j=1:length(Parallel),
if Parallel(j).Local==0,
RemoteFlag = 1;
end
if Parallel(ThisMatlab).Local==0,
RemoteFlag =1;
end
ifil=ifil(:,whoiam);
waitbarString = ['Please wait... ',type,' subdraws (' int2str(fpar) 'of' int2str(B) ')...'];
if Parallel(ThisMatlab).Local,
waitbarTitle=['Local '];
else
waitbarTitle=[Parallel(ThisMatlab).ComputerName];
end
fMessageStatus(0,whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
prct0={0,whoiam,Parallel(ThisMatlab)};
else
if exist('OCTAVE_VERSION') || options_.console_mode,
diary off;
newString = '';
else
h = waitbar(0,['Taking ',type,' subdraws...']);
end
prct0=0;
end
h = dyn_waitbar(prct0,['Taking ',type,' subdraws...']);
if RemoteFlag==1,
OutputFileName_smooth = {};
......@@ -313,27 +300,8 @@ for b=fpar:B
% DirectoryName=TempPath;
if exist('OCTAVE_VERSION') || options_.console_mode,
if (whoiam==0),
if exist('OCTAVE_VERSION'),
printf('Taking subdraws: %3.f%% done\r', b/B*100);
else
s0=repmat('\b',1,length(newString));
newString = sprintf('Taking subdraws: %3.f%% done', b/B*100);
fprintf([s0,'%s'],newString);
end
end
elseif ~whoiam,
waitbar(b/B,h);
end
dyn_waitbar((b-fpar+1)/(B-fpar+1),h);
if whoiam,
if ~exist('OCTAVE_VERSION')
fprintf('Done! \n');
end
waitbarString = [ 'Subdraw ' int2str(b) '/' int2str(B) ' done.'];
fMessageStatus((b-fpar+1)/(B-fpar+1),whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
end
end
myoutput.ifil=ifil;
......@@ -349,14 +317,5 @@ if RemoteFlag==1,
% OutputFileName_moments];
end
if exist('OCTAVE_VERSION') || options_.console_mode,
if (whoiam==0),
fprintf('\n');
diary on;
end
else
if exist('h')
close(h)
end
end
dyn_waitbar_close(h);
......@@ -153,30 +153,12 @@ for b = fblck:nblck,
x2 = zeros(InitSizeArray(b),npar);
logpo2 = zeros(InitSizeArray(b),1);
end
if exist('OCTAVE_VERSION') || options_.console_mode
diary off
disp(' ')
newString='';
if whoiam
waitbarString = ['Wait... MH (' int2str(b) '/' int2str(options_.mh_nblck) ')...'];
end
else,
if whoiam
waitbarString = ['Please wait... Metropolis-Hastings (' int2str(b) '/' int2str(options_.mh_nblck) ')...'];
else
hh = waitbar(0,['Please wait... Metropolis-Hastings (' int2str(b) '/' int2str(options_.mh_nblck) ')...']);
set(hh,'Name','Metropolis-Hastings');
end
end
if whoiam
if options_.parallel(ThisMatlab).Local,
waitbarTitle=['Local '];
else
waitbarTitle=[options_.parallel(ThisMatlab).ComputerName];
end
prc0=(b-fblck)/(nblck-fblck+1)*(exist('OCTAVE_VERSION') || options_.console_mode);
fMessageStatus(prc0,whoiam,waitbarString, waitbarTitle, options_.parallel(ThisMatlab));
hh = dyn_waitbar({prc0,whoiam,options_.parallel(ThisMatlab)},['MH (' int2str(b) '/' int2str(options_.mh_nblck) ')...']);
else
hh = dyn_waitbar(0,['Metropolis-Hastings (' int2str(b) '/' int2str(options_.mh_nblck) ')...']);
set(hh,'Name','Metropolis-Hastings');
end
isux = 0;
jsux = 0;
......@@ -205,38 +187,40 @@ for b = fblck:nblck,
logpo2(irun) = ilogpo2(b);
end
prtfrc = j/nruns(b);
if exist('OCTAVE_VERSION') || options_.console_mode
if mod(j, 10) == 0
if exist('OCTAVE_VERSION')
if (whoiam==0)
printf('MH: Computing Metropolis-Hastings (chain %d/%d): %3.f%% done, acception rate: %3.f%%\r', b, nblck, 100 * prtfrc, 100 * isux / j);
end
else
s0=repmat('\b',1,length(newString));
newString=sprintf('MH: Computing Metropolis-Hastings (chain %d/%d): %3.f%% done, acceptance rate: %3.f%%', b, nblck, 100 * prtfrc, 100 * isux / j);
fprintf([s0,'%s'],newString);
end
end
if mod(j,50)==0 && whoiam
% keyboard;
if (strcmp([options_.parallel(ThisMatlab).MatlabOctavePath], 'octave'))
waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) '), ' sprintf('accept. %3.f%%',100 * isux / j)];
fMessageStatus(prtfrc,whoiam,waitbarString, waitbarTitle, options_.parallel(ThisMatlab));
else
waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) '), ' sprintf('accept. %3.f%%', 100 * isux/j)];
fMessageStatus((b-fblck)/(nblck-fblck+1)+prtfrc/(nblck-fblck+1),whoiam,waitbarString, '', options_.parallel(ThisMatlab));
end
end
else
if mod(j, 3)==0 && ~whoiam
waitbar(prtfrc,hh,[ '(' int2str(b) '/' int2str(options_.mh_nblck) ') ' sprintf('%f done, acceptation rate %f',prtfrc,isux/j)]);
elseif mod(j,50)==0 && whoiam,
% keyboard;
waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) ') ' sprintf('%f done, acceptation rate %f',prtfrc,isux/j)];
fMessageStatus(prtfrc,whoiam,waitbarString, waitbarTitle, options_.parallel(ThisMatlab));
end
% if exist('OCTAVE_VERSION') || options_.console_mode
% if mod(j, 10) == 0
% if exist('OCTAVE_VERSION')
% if (whoiam==0)
% printf('MH: Computing Metropolis-Hastings (chain %d/%d): %3.f%% done, acception rate: %3.f%%\r', b, nblck, 100 * prtfrc, 100 * isux / j);
% end
% else
% s0=repmat('\b',1,length(newString));
% newString=sprintf('MH: Computing Metropolis-Hastings (chain %d/%d): %3.f%% done, acceptance rate: %3.f%%', b, nblck, 100 * prtfrc, 100 * isux / j);
% fprintf([s0,'%s'],newString);
% end
% end
% if mod(j,50)==0 && whoiam
% % keyboard;
% if (strcmp([options_.parallel(ThisMatlab).MatlabOctavePath], 'octave'))
% waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) '), ' sprintf('accept. %3.f%%',100 * isux / j)];
% fMessageStatus(prtfrc,whoiam,waitbarString, waitbarTitle, options_.parallel(ThisMatlab));
% else
% waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) '), ' sprintf('accept. %3.f%%', 100 * isux/j)];
% fMessageStatus((b-fblck)/(nblck-fblck+1)+prtfrc/(nblck-fblck+1),whoiam,waitbarString, '', options_.parallel(ThisMatlab));
% end
% end
% else
% if mod(j, 3)==0 && ~whoiam
% waitbar(prtfrc,hh,[ '(' int2str(b) '/' int2str(options_.mh_nblck) ') ' sprintf('%f done, acceptation rate %f',prtfrc,isux/j)]);
% elseif mod(j,50)==0 && whoiam,
% % keyboard;
% waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) ') ' sprintf('%f done, acceptation rate %f',prtfrc,isux/j)];
% fMessageStatus(prtfrc,whoiam,waitbarString, waitbarTitle, options_.parallel(ThisMatlab));
% end
% end
if (mod(j, 3)==0 && ~whoiam) || (mod(j,50)==0 && whoiam)
dyn_waitbar(prtfrc,hh,[ 'MH (' int2str(b) '/' int2str(options_.mh_nblck) ') ' sprintf('acceptation rate %4.3f', isux/j)]);
end
if (irun == InitSizeArray(b)) || (j == nruns(b)) % Now I save the simulations
save([MhDirectoryName '/' ModelName '_mh' int2str(NewFile(b)) '_blck' int2str(b) '.mat'],'x2','logpo2');
fidlog = fopen([MhDirectoryName '/metropolis.log'],'a');
......@@ -281,16 +265,17 @@ for b = fblck:nblck,
irun = irun + 1;
end% End of the simulations for one mh-block.
record.AcceptationRates(b) = isux/j;
if exist('OCTAVE_VERSION') || options_.console_mode || whoiam
if exist('OCTAVE_VERSION')
printf('\n');
else
fprintf('\n');
end
diary on;
else %if ~whoiam
close(hh);
end
% if exist('OCTAVE_VERSION') || options_.console_mode || whoiam
% if exist('OCTAVE_VERSION')
% printf('\n');
% else
% fprintf('\n');
% end
% diary on;
% else %if ~whoiam
% close(hh);
% end
dyn_waitbar_close(hh);
record.Seeds(b).Normal = randn('state');
record.Seeds(b).Unifor = rand('state');
OutputFileName(jloop,:) = {[MhDirectoryName,filesep], [ModelName '_mh*_blck' int2str(b) '.mat']};
......
Supports Markdown
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