sgeSubmitFcn.m

function sgeSubmitFcn(scheduler, job, props, varargin) 
%SUBMITFCN Submit a Matlab job to a SGE scheduler
%
% See also workerDecodeFunc.
%
% Assign the relevant values to environment variables, starting 
% with identifying the decode function to be run by the worker:

% Copyright 2006 The MathWorks, Inc.


setenv('MATLAB_ENV_SETTING', '/exports/work/is_iti_ug/ywan/my_matlab_env.sh');

system(' if [ -r ${MATLAB_ENV_SETTING} ]; then rm -f ${MATLAB_ENV_SETTING}; fi '
);
system(' touch ${MATLAB_ENV_SETTING}; chmod +x ${MATLAB_ENV_SETTING}');


setenv('MDCE_DECODE_FUNCTION', 'sgeDecodeFunc'); 
system('echo "export MDCE_DECODE_FUNCTION=${MDCE_DECODE_FUNCTION}" >> ${MATLAB_E
NV_SETTING}');

% 
% Set the other job-related environment variables:
setenv('MDCE_STORAGE_LOCATION', props.StorageLocation); 
system('echo "export MDCE_STORAGE_LOCATION=${MDCE_STORAGE_LOCATION}" >> ${MATLAB_ENV_SETTING}');

setenv('MDCE_STORAGE_CONSTRUCTOR', props.StorageConstructor);
system('echo "export MDCE_STORAGE_CONSTRUCTOR=${MDCE_STORAGE_CONSTRUCTOR}" >> ${MATLAB_ENV_SETTING}');

setenv('MDCE_JOB_LOCATION', props.JobLocation); 
system('echo "export MDCE_JOB_LOCATION=${MDCE_JOB_LOCATION}" >> ${MATLAB_ENV_SETTING}');

% Ask the workers to print debug messages by default:
setenv('MDCE_DEBUG', 'true');
system('echo "export MDCE_DEBUG=${MDCE_DEBUG}" >> ${MATLAB_ENV_SETTING}');

% Tell the script what it needs to run. These two properties will
% incorporate ClusterMatlabRoot if it is set.
setenv( 'MDCE_MATLAB_EXE', props.MatlabExecutable );
system('echo "export MDCE_MATLAB_EXE=${MDCE_MATLAB_EXE}" >> ${MATLAB_ENV_SETTING}');

setenv( 'MDCE_MATLAB_ARGS', props.MatlabArguments );
system('echo "export MDCE_MATLAB_ARGS=${MDCE_MATLAB_ARGS}" >> ${MATLAB_ENV_SETTING}');

[dirpart] = fileparts( mfilename( 'fullpath' ) );
% scriptName = fullfile( dirpart, 'sgeWrapper.sh' );
scriptName = '/exports/work/is_iti_ug/ywan/sgeWrapper.sh'

% Submit the wrapper script to SGE once for each task, supplying a different
% environment each time.
for i = 1:props.NumberOfTasks
    fprintf('Submitting task %i\n', i);
    setenv('MDCE_TASK_LOCATION', props.TaskLocations{i});
    system('echo "export MDCE_TASK_LOCATION=${MDCE_TASK_LOCATION}" >> ${MATLAB_ENV_SETTING}');

    % Choose a file for the output. Please note that currently, DataLocation refers
    % to a directory on disk, but this may change in the future.
    logFile = fullfile( scheduler.DataLocation, ...
                        sprintf( 'Job%d_Task%d.out', job.ID, job.Tasks(i).ID ) );
    % Finally, submit to SGE. note the following:
    % "-N Job#" - specifies the job name
    % "-j yes" joins together output and error streams
    % "-o ..." specifies where standard output goes to
   
    % add -cwd and display the submit command    

    cmdLine = sprintf( '. /exports/work/is_iti_ug/ywan/my_matlab_env.sh; qsub -cwd -N Job%d.%d -j yes -o "%s" "%s"', ...
                       job.ID, job.Tasks(i).ID, logFile, scriptName );
    
    full_cmd = sprintf('ssh eddie.ecdf.ed.ac.uk "%s"', cmdLine);    

%    disp(cmdLine);
     disp(full_cmd);   

%    [s, w] = system( cmdLine );
     [s, w] = system( full_cmd );   

    if s ~= 0
        warning( 'distcompexamples:generic:SGE', ...
                 'Submit failed with the following message:\n%s', w);
    else
        % The output of successful submissions shows the SGE job identifier%
        fprintf( 1, 'Job output will be written to: %s\nQSUB output: %s\n', logFile, w );
    end
end

Note:

1. This m file could be placed in any matlab path. I suggest $matlab-root-dir/work/

2. You need replace all /exports/work/is_iti_ug/ywan/ by your shared directory in this function file

sgeSubmitFcn.m (last edited 2007-09-25 13:38:22 by Yuan WAN)