function MPI_Abort()
% MPI_Abort  -  Aborts any currently running MatlabMPI sessions.
%
%   MPI_Abort()
%
%   Will abort any currently running MatlabMPI sessions.
%   by looking for leftover Matlab jobs and killing them.
%   Cannot be used after MatMPI_Deleta_all.
%

  % Get possibly user defined settings.
  machine_db_settings = MatMPI_Comm_settings;

  % Get list of pid files.
  pid_files = dir('MatMPI/pid.*.*');
  s = size(pid_files);
  n_files = s(1);

  % Create single qoute.
  q = strrep(' '' ',' ','');

  % Check if there are any files
  if (n_files < 1)
    disp('No pid files found');
  else

    % Loop over each file.
    for i_file=1:n_files

      % Get file name.
      file_name = pid_files(i_file).name;

      % Check if there is a directory appended.
      dir_sep = findstr(file_name,'.');
      if (dir_sep)
        
        % Parse file name.
        machine = file_name(1,(dir_sep(1)+1):(dir_sep(end)-1));
        pid = file_name(1,(dir_sep(end)+1):end);

        % Kill process.
        % To use ssh, change 'rsh ' to 'ssh ' in line below.
        % unix_command = ['rsh ' machine  ' ' q 'kill -9 ' pid q];
        unix_command = [ machine_db_settings.remote_launch machine  ' ' q 'kill -9 ' pid q];
        disp(unix_command);
        unix(unix_command);
      end

    end    

  end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MatlabMPI
% Dr. Jeremy Kepner
% MIT Lincoln Laboratory
% kepner@ll.mit.edu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright 2002 Massachusetts Institute of Technology
% 
% Permission is herby granted, without payment, to copy, modify, display
% and distribute this software and its documentation, if any, for any
% purpose, provided that the above copyright notices and the following
% three paragraphs appear in all copies of this software.  Use of this
% software constitutes acceptance of these terms and conditions.
%
% IN NO EVENT SHALL MIT BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
% SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
% THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF MIT HAS BEEN ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
% 
% MIT SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTIES INCLUDING,
% BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
% FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
%
% THIS SOFTWARE IS PROVIDED "AS IS," MIT HAS NO OBLIGATION TO PROVIDE
% MAINTENANCE, SUPPORT, UPDATE, ENHANCEMENTS, OR MODIFICATIONS.


