Home > matlab > parallel > slaveParallel.m

slaveParallel

PURPOSE ^

PARALLEL CONTEXT

SYNOPSIS ^

function slaveParallel(whoiam,ThisMatlab)

DESCRIPTION ^

 PARALLEL CONTEXT
 In a parallelization context, this function is launched on slave
 machines, to initialize MATLAB and DYNARE environment and waits for
 instructions sent by the Master. 
 This function is invoked by masterParallel only when the strategy (1),
 i.e. always open, is actived.


 INPUTS
  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.

 OUTPUTS 
   None

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function slaveParallel(whoiam,ThisMatlab)
0002 % PARALLEL CONTEXT
0003 % In a parallelization context, this function is launched on slave
0004 % machines, to initialize MATLAB and DYNARE environment and waits for
0005 % instructions sent by the Master.
0006 % This function is invoked by masterParallel only when the strategy (1),
0007 % i.e. always open, is actived.
0008 %
0009 %
0010 % INPUTS
0011 %  o whoiam [int]       index number of this CPU among all CPUs in the
0012 %                       cluster.
0013 %  o ThisMatlab [int]   index number of this slave machine in the cluster.
0014 %
0015 % OUTPUTS
0016 %   None
0017 
0018 % Copyright (C) 2006-2011 Dynare Team
0019 %
0020 % This file is part of Dynare.
0021 %
0022 % Dynare is free software: you can redistribute it and/or modify
0023 % it under the terms of the GNU General Public License as published by
0024 % the Free Software Foundation, either version 3 of the License, or
0025 % (at your option) any later version.
0026 %
0027 % Dynare is distributed in the hope that it will be useful,
0028 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0029 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0030 % GNU General Public License for more details.
0031 %
0032 % You should have received a copy of the GNU General Public License
0033 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0034 
0035 global funcName
0036 
0037 warning off;
0038 diary off;
0039 
0040 delete( ['slaveParallel_',int2str(whoiam),'.log']);
0041 diary( ['slaveParallel_',int2str(whoiam),'.log']);
0042 
0043 
0044 % Configure dynare environment
0045 dynareroot = dynare_config();
0046 
0047 % Load input data.
0048 load( ['slaveParallel_input',int2str(whoiam)]);
0049 
0050 %Loads fGlobalVar Parallel.
0051 if exist('fGlobalVar'),
0052     globalVars = fieldnames(fGlobalVar);
0053     for j=1:length(globalVars),
0054         eval(['global ',globalVars{j},';']);
0055         evalin('base',['global ',globalVars{j},';']);
0056     end
0057     struct2local(fGlobalVar);
0058     clear fGlobalVar
0059     % create global variables in the base workspace as well
0060     evalin('base',['load( [''slaveParallel_input',int2str(whoiam),'''],''fGlobalVar'')']) ;
0061     evalin('base','struct2local(fGlobalVar)');
0062     evalin('base','clear fGlobalVar');
0063 end
0064 
0065 t0=clock;
0066 fslave = dir( ['slaveParallel_input',int2str(whoiam),'.mat']);
0067 
0068 while (etime(clock,t0)<1200 && ~isempty(fslave)) || ~isempty(dir(['stayalive',int2str(whoiam),'.txt'])),
0069     if ~isempty(dir(['stayalive',int2str(whoiam),'.txt'])),
0070         t0=clock;
0071         delete(['stayalive',int2str(whoiam),'.txt']);
0072     end
0073     % I wait for 20 min or while mater asks to exit (i.e. it cancels fslave file)
0074     pause(1);
0075     
0076     fjob = dir(['slaveJob',int2str(whoiam),'.mat']);
0077     
0078     if ~isempty(fjob),
0079         clear fGlobalVar fInputVar fblck nblck fname
0080         
0081         while(1)
0082             Go=0;
0083             
0084             Go=fopen(['slaveJob',int2str(whoiam),'.mat']);
0085             
0086             if Go>0    
0087                 fclose(Go);
0088                 pause(1);
0089                 load(['slaveJob',int2str(whoiam),'.mat']);
0090                 break
0091             else
0092                 % Only for testing, will be remouved!
0093                 
0094                 %                if isunix
0095                 %                  E1=fopen('/home/ivano/Works/Errore-slaveParallel.txt','w+');
0096                 %                  fclose(E1);
0097                 %                else
0098                 %                  E1=fopen('c:\dynare_calcs\Errore-slaveParallel.txt','w+');
0099                 %                  fclose(E1);
0100                 %                end
0101                 
0102             end
0103         end
0104         
0105         funcName=fname;  % Update global job name.
0106 
0107         if exist('fGlobalVar') && ~isempty (fGlobalVar)
0108             globalVars = fieldnames(fGlobalVar);
0109             for j=1:length(globalVars),
0110                 info_whos = whos(globalVars{j});
0111                 if isempty(info_whos) || ~info_whos.global,
0112                     eval(['global ',globalVars{j},';']);
0113                     evalin('base',['global ',globalVars{j},';']);
0114                 end
0115             end
0116             struct2local(fGlobalVar);
0117             evalin('base',['load( [''slaveJob',int2str(whoiam),'''],''fGlobalVar'')']);
0118             evalin('base','struct2local(fGlobalVar)');
0119             evalin('base','clear fGlobalVar');
0120         end
0121         delete(['slaveJob',int2str(whoiam),'.mat']);
0122         fInputVar.Parallel = Parallel;
0123         
0124         % Launch the routine to be run in parallel.
0125         try,
0126             tic,
0127             fOutputVar = feval(fname, fInputVar ,fblck, nblck, whoiam, ThisMatlab);
0128             toc,
0129             if isfield(fOutputVar,'OutputFileName'),
0130                 OutputFileName = fOutputVar.OutputFileName;
0131             else
0132                 OutputFileName = '';
0133             end
0134 
0135             if(whoiam)
0136 
0137                 % Save the output result.
0138                 save([ fname,'_output_',int2str(whoiam),'.mat'],'fOutputVar' );
0139 %                 keyboard,
0140                 if isfield(fOutputVar,'CloseAllSlaves'),
0141                     CloseAllSlaves = 1;
0142                     fOutputVar = rmfield(fOutputVar,'CloseAllSlaves');
0143                     save([ fname,'_output_',int2str(whoiam),'.mat'],'fOutputVar' )
0144                     save(['comp_status_',funcName,int2str(whoiam),'.mat'],'CloseAllSlaves');
0145                 end
0146 
0147                 % Inform the master that the job is finished, and transfer the output data
0148                 delete(['P_',fname,'_',int2str(whoiam),'End.txt']);
0149             end
0150 
0151             disp(['Job ',fname,' on CPU ',int2str(whoiam),' completed.']);
0152             t0 =clock; % Re-set waiting time of 20 mins
0153         catch,
0154             theerror = lasterror;
0155             if strfind(theerror.message,'Master asked to break the job')
0156                 disp(['Job ',fname,' on CPU ',int2str(whoiam),' broken from master.']);
0157                 fOutputVar.message = theerror;
0158                 save([ fname,'_output_',int2str(whoiam),'.mat'],'fOutputVar' )
0159                 delete(['P_',fname,'_',int2str(whoiam),'End.txt']);
0160             else
0161                 disp(['Job ',fname,' on CPU ',int2str(whoiam),' crashed.']);
0162                 fOutputVar.error = lasterror;
0163                 save([ fname,'_output_',int2str(whoiam),'.mat'],'fOutputVar' );
0164                 waitbarString = fOutputVar.error.message;
0165                 if Parallel(ThisMatlab).Local,
0166                     waitbarTitle='Local ';
0167                 else
0168                     waitbarTitle=[Parallel(ThisMatlab).ComputerName];
0169                 end
0170                 fMessageStatus(NaN,whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
0171                 delete(['P_',fname,'_',int2str(whoiam),'End.txt']);
0172                 break
0173             end
0174             
0175         end
0176     end
0177     fslave = dir( ['slaveParallel_input',int2str(whoiam),'.mat']); % Check if Master asks to exit
0178 end
0179 
0180 
0181 disp(['slaveParallel on CPU ',int2str(whoiam),' completed.']);
0182 diary off;
0183 
0184 delete(['P_slave_',int2str(whoiam),'End.txt']);
0185 
0186 
0187 exit;

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