
function pooljob_submit(task_function, task_arguments, minimum_workers, maximum_workers, wall_time, path_dependencies, data_location, clustername)

% Do not modify these lines
sched = findResource('scheduler', 'type', 'generic');
is_della = false;
is_aurora = false;
if ~isempty(strfind(lower(clustername),'della'))
    matlab_root = '/opt/export/matlabR2011a';
    is_della=true;
elseif ~isempty(strfind(lower(clustername),'aurora'))
    matlab_root = '/usr/local/matlab';
    is_aurora = true;
else
    disp('Which Cluster??');
    return;
end

set(sched,'ClusterMatlabRoot',matlab_root); % This will be different on the different clusters.
set(sched,'ClusterOsType','unix');
set(sched,'ClusterSize',16);
set(sched,'DestroyJobFcn',@destroyJobFcn);
set(sched,'GetJobStateFcn',@getJobStateFcn);
set(sched,'HasSharedFilesystem',true);

% Modify these lines to suit your job requirements.
% This is where the many "Job" diagnostic files go .. the dir must exist! 
set(sched,'DataLocation',data_location);
% Some special variables to set for the queue on Aurora at IAS
exclusive = 'false';

% Do not modify this line (second one is used for pool jobs)
%set(sched,'SubmitFcn',{@distributedSubmitFcn, wall_time, exclusive});
if is_della
    path('/opt/export/matlabR2011a/toolbox/distcomp/examples/integration/pbs/shared/',path)
    setenv('PATH', [getenv('PATH') ':/opt/export/matlabR2011a/toolbox/distcomp/examples/integration/pbs/shared']);
    set(sched,'ParallelSubmitFcn',{@della_parallelSubmitFcn, wall_time});
elseif is_aurora
    set(sched,'ParallelSubmitFcn',{@aurora_parallelSubmitFcn, wall_time, exclusive});
end

% Create parallel job AND tell it the absolute path to executables and data 
job = createMatlabPoolJob(sched,'PathDependencies',path_dependencies);  
% Tell it where to look for other necessary files (I think) 
%set(job, 'FileDependencies', {'matlab_test.m'});

set(job, 'MinimumNumberOfWorkers', minimum_workers);
set(job, 'MaximumNumberOfWorkers', maximum_workers);

% Create a specific task
%task = createTask(job, @test_parfor , 1, {});
%task = createTask(job, @matlab_test, 1, {'mem1_noncoding', 'junk'});
% note the structure of the args in next line: { double, cell array }
task = createTask(job, task_function, 0, task_arguments);

% Capture command window output
alltasks = get(job, 'Tasks');
set(alltasks, 'CaptureCommandWindowOutput', true)


% Run the job
submit(job);

% Returns to console, executing job.state gives running/finished/queued
