Home > matlab > parallel > fParallel.m

fParallel

PURPOSE ^

PARALLEL CONTEXT

SYNOPSIS ^

function fParallel(fblck,nblck,whoiam,ThisMatlab,fname)

DESCRIPTION ^

 PARALLEL CONTEXT
 In a parallel context, this function is launched on slave
 machines, and acts as a wrapper around the function containing the
 computing task itself.

 INPUTS
  o fblck [int]        index number of the first thread to run in this
                       MATLAB instance
  o nblck [int]        number of threads to run in this
                       MATLAB instance
  o whoiam [int]       index number of this CPU among all CPUs in the
                       cluster
  o ThisMatlab [int]   index number of this slave machine in the cluster
                       (entry in options_.parallel)
  o fname [string]     function to be run, containing the computing task

 OUTPUTS
   None

 Copyright (C) 2006-2011 Dynare Team

 This file is part of Dynare.

 Dynare is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Dynare is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function fParallel(fblck,nblck,whoiam,ThisMatlab,fname)
0002 % PARALLEL CONTEXT
0003 % In a parallel context, this function is launched on slave
0004 % machines, and acts as a wrapper around the function containing the
0005 % computing task itself.
0006 %
0007 % INPUTS
0008 %  o fblck [int]        index number of the first thread to run in this
0009 %                       MATLAB instance
0010 %  o nblck [int]        number of threads to run in this
0011 %                       MATLAB instance
0012 %  o whoiam [int]       index number of this CPU among all CPUs in the
0013 %                       cluster
0014 %  o ThisMatlab [int]   index number of this slave machine in the cluster
0015 %                       (entry in options_.parallel)
0016 %  o fname [string]     function to be run, containing the computing task
0017 %
0018 % OUTPUTS
0019 %   None
0020 %
0021 % Copyright (C) 2006-2011 Dynare Team
0022 %
0023 % This file is part of Dynare.
0024 %
0025 % Dynare is free software: you can redistribute it and/or modify
0026 % it under the terms of the GNU General Public License as published by
0027 % the Free Software Foundation, either version 3 of the License, or
0028 % (at your option) any later version.
0029 %
0030 % Dynare is distributed in the hope that it will be useful,
0031 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0032 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0033 % GNU General Public License for more details.
0034 %
0035 % You should have received a copy of the GNU General Public License
0036 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0037 
0038 global funcName
0039 
0040 funcName=fname;
0041 
0042 warning off;
0043 diary off;
0044 
0045 delete( [fname,'_',int2str(whoiam),'.log']);
0046 diary( [fname,'_',int2str(whoiam),'.log']);
0047 
0048 % Configure dynare environment.
0049 dynareroot = dynare_config();
0050 
0051 % Load input data.
0052 load( [fname,'_input'])
0053 
0054 if exist('fGlobalVar') && ~isempty (fGlobalVar)
0055     globalVars = fieldnames(fGlobalVar);
0056     for j=1:length(globalVars),
0057         eval(['global ',globalVars{j},';'])
0058         evalin('base',['global ',globalVars{j},';'])
0059     end
0060     struct2local(fGlobalVar);
0061     % Create global variables in the base workspace as well.
0062     evalin('base',['load( [''',fname,'_input''],''fGlobalVar'')'])
0063     evalin('base','struct2local(fGlobalVar)');
0064 end
0065 
0066 fInputVar.Parallel = Parallel;
0067 
0068 
0069 % Launch the routine to be run in parallel.
0070 try,
0071     tic,
0072     
0073     fOutputVar = feval(fname, fInputVar ,fblck, nblck, whoiam, ThisMatlab);
0074     toc,
0075     if isfield(fOutputVar,'OutputFileName'),
0076         OutputFileName = fOutputVar.OutputFileName;
0077     else
0078         OutputFileName = '';
0079     end
0080     if(whoiam)
0081         % Save the output result.
0082         save([ fname,'_output_',int2str(whoiam),'.mat'],'fOutputVar' )
0083     end
0084     if isfield(fOutputVar,'CloseAllSlaves'),
0085         CloseAllSlaves = 1;
0086         fOutputVar = rmfield(fOutputVar,'CloseAllSlaves');
0087         save([ fname,'_output_',int2str(whoiam),'.mat'],'fOutputVar' )
0088         save(['comp_status_',funcName,int2str(whoiam),'.mat'],'CloseAllSlaves');
0089     end
0090     
0091     disp(['fParallel ',int2str(whoiam),' completed.'])
0092 catch,
0093     theerror = lasterror;
0094     if strfind(theerror.message,'Master asked to break the job')
0095         fOutputVar.message = theerror;
0096         save([ fname,'_output_',int2str(whoiam),'.mat'],'fOutputVar' )
0097         waitbarString = theerror.message;
0098     else
0099         disp(['fParallel ',int2str(whoiam),' crashed.'])
0100         fOutputVar.error = theerror;
0101         save([ fname,'_output_',int2str(whoiam),'.mat'],'fOutputVar' )
0102         waitbarString = theerror.message;
0103         %       waitbarTitle=['Metropolis-Hastings ',options_.parallel(ThisMatlab).ComputerName];
0104         if Parallel(ThisMatlab).Local,
0105             waitbarTitle='Local ';
0106         else
0107             waitbarTitle=[Parallel(ThisMatlab).ComputerName];
0108         end
0109         fMessageStatus(NaN,whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
0110     end
0111     
0112 end
0113 diary off;
0114 delete(['P_',fname,'_',int2str(whoiam),'End.txt']);
0115 
0116 
0117 exit;

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