Routine Name MPI_Run( m_file, n_proc, machines )
Routine Location MatlabMPI\MPI_Run.m
Routine Purpose ---
Routine Arguments { defscommands, m_file, machines, n_proc }
Routine Outputs { --- }
Routine Size 145 line(s)
Routine Comment
Click here
Parent Routines
Child Routines
Routine Body

001  function defscommands = MPI_Run( m_file , n_proc , machines )
002  % MPI_Run  -  Run m_file on multiple processors.
003  %
004  %  defscommands = MPI_Run( m_file, n_proc, machines )
005  %
006  %    Runs n_proc copies of m_file on machines, where
007  %
008  %    machines = {};
009  %      Run on a local processor.
010  %
011  %    machines = {'machine1' 'machine2'}) );
012  %      Run on a multi processors.
013  %
014  %    machines = {'machine1:dir1' 'machine2:dir2'}) );
015  %      Run on a multi processors and communicate using via dir1 and dir2,
016  %      which must be visible to both machines.
017  %
018  %    If machine1 is the local cpu, then defscommands will contain
019  %    the commands that need to be run locally, via eval(defscommands).
020  %
021  
022  % Check if the directory 'MatMPI' exists
023  if exist('MatMPI' , 'dir') ~ = 0
024  %error('MatMPI directory already exists: rename or remove with MatMPI_Delete_all');
025  else
026      mkdir('MatMPI') ;
027  end
028  
029  % Create working directory.
030  % mkdir('MatMPI');
031  
032  % Get host.
033  host = getenv('HOST') ;
034  
035  % Get number of machines to launch on.
036  n_machines = size(machines ,2) ;
037  
038  % Create generic comm.
039  MPI_COMM_WORLD = MatMPI_Comm_init(n_proc ,machines) ;
040  
041  % Set newline string.
042  nl = sprintf('\n') ;
043  % Get single quote character.
044  q = strrep(' '' ' ,' ' ,'') ;
045  
046  % Initialize unix command launch on all the different machines.
047  unix_launch = '' ;
048  
049  % Get number of machines.
050  n_m = MPI_COMM_WORLD.machine_db.n_machine ;
051  
052  % Loop backwards over each machine.
053  for i_m = n_m: - 1:1
054      
055      % Get number of processes to launch on this machine.
056      n_proc_i_m = MPI_COMM_WORLD.machine_db.n_proc(1 ,i_m) ;
057      
058      if (n_proc_i_m >=  1)
059          
060          % Get machine info.
061          machine = MPI_COMM_WORLD.machine_db.machine{1 ,i_m} ;
062          remote_launch = MPI_COMM_WORLD.machine_db.remote_launch{1 ,i_m} ;
063          remote_flags = MPI_COMM_WORLD.machine_db.remote_flags{1 ,i_m} ;
064          
065          % Get starting and stopping rank.
066          i_rank_start = MPI_COMM_WORLD.machine_db.id_start(1 ,i_m) - 1 ;
067          i_rank_stop = MPI_COMM_WORLD.machine_db.id_stop(1 ,i_m) - 1 ;
068          
069          % Initialize unix command that will be run on each node.
070          unix_matlab = '' ;
071          
072          % Loop backwards over number of processes.
073          for i_rank = i_rank_stop: - 1:i_rank_start
074              
075              % Build commands
076              [defscommands , unix_matlab_i_rank] = ...
077              MatMPI_Commands(m_file ,i_rank ,MPI_COMM_WORLD) ;
078              unix_matlab = [unix_matlab unix_matlab_i_rank] ;
079              
080          end
081          
082          % Create a file name.
083          %      unix_matlab_file = ['MatMPI/Unix_Commands.' machine '.sh'];
084          unix_matlab_file = ['MatMPI / Unix_Commands.' machine '.' num2str(i_rank_start) '.sh'] ;
085          
086          % Append delete command.
087          unix_matlab = [unix_matlab ' rm ' unix_matlab_file ' ;' nl] ;
088          
089          % Put commands in a file.
090          fid = fopen(unix_matlab_file ,'wt') ;
091          fwrite(fid ,unix_matlab) ;
092          fclose(fid) ;
093          
094          % Create unix commands to launch this file.
095          if (strcmp(machine ,host))
096              unix_launch_i_m = [' / bin / sh . / ' unix_matlab_file ' &' nl] ;
097          else
098              unix_launch_i_m = [remote_launch machine remote_flags ...
099              q 'cd ' pwd ' ; / bin / sh . / ' unix_matlab_file ' &' q ' &' nl] ;
100          end
101          
102          unix_launch = [unix_launch unix_launch_i_m] ;
103      end
104  end
105  
106  % Execute all launches in a single unix call.
107  unix_launch
108  %  unix(unix_launch);
109  
110  
111  % Write commands unix commands to .sh text file
112  % to fix Matlab's problem with very long commands sent to unix().
113  unix_launch_file = 'MatMPI / Unix_Commands.sh' ;
114  fid = fopen(unix_launch_file ,'wt') ;
115  fwrite(fid ,unix_launch) ;
116  fclose(fid) ;
117  unix([' / bin / sh ' unix_launch_file]) ;
118  delete(unix_launch_file) ;
119  
120  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121  % MatlabMPI
122  % Dr. Jeremy Kepner
123  % MIT Lincoln Laboratory
124  % kepner@ll.mit.edu
125  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126  % Copyright 2002 Massachusetts Institute of Technology
127  %
128  % Permission is herby granted, without payment, to copy, modify, display
129  % and distribute this software and its documentation, if any, for any
130  % purpose, provided that the above copyright notices and the following
131  % three paragraphs appear in all copies of this software.  Use of this
132  % software constitutes acceptance of these terms and conditions.
133  %
134  % IN NO EVENT SHALL MIT BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
135  % SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
136  % THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF MIT HAS BEEN ADVISED OF THE
137  % POSSIBILITY OF SUCH DAMAGE.
138  %
139  % MIT SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTIES INCLUDING,
140  % BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
141  % FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
142  %
143  % THIS SOFTWARE IS PROVIDED "AS IS," MIT HAS NO OBLIGATION TO PROVIDE
144  % MAINTENANCE, SUPPORT, UPDATE, ENHANCEMENTS, OR MODIFICATIONS.
145