function runBatchJob()
% function runBatchJob()
% Purpose: 
% When computing with the Parallel Computing Toolbox (PCT), MATLAB --
% for internal administrative purposes -- for each job will generate 
% folders and files in user's home dir (i.e., ~/.matlab). When a PCT
% job is submitted to SCC's batch queue, it will run on a compute node.
% The folders and files mentioned above would still be created in
% ~/.matlab, by default. I/O between the compute node and ~/.matlab
% is non-local and inefficient. Running multiple PCT jobs concurrently
% will compound the data flow bottleneck for the system. 
% To prevent this from happening, a PCT job should redirect data that
% goes to ~/.matlab to the runtime assigned compute node's TMPDIR.
% This runBatchJob.m example demonstrates the procedure for redirection.
% In a nutshell:
% 1) scc1$ qsub -t 100-500:200 run_matlab_pct_job
% 2) run_matlab_pct_job batch script
%    #!/bin/sh
%    #$ -pe omp 4
%    matlab ... -r "runBatchJob, exit"
%  
% runBatchJob is a template. As an example, it computes an arithmetic
% sequence sum (s=1+2+...+n) via myFCT.m (a function) and mySCR.m
% (a script). *** Modify runBatchJob for your own applications ***
% 3) For this example, the SCC provides 4 cores for the job, which forms 
%    your "private cluster" through PCT's "parcluster" command.
% 4) You can now customize the private cluster to redirect ~/.matlab
%    to the compute node's TMPDIR. Your application should then be
%    submitted internally to the private cluster via MATLAB's "batch."
% 5) Important to note that output from "batch" must be retrieved. Note
%    that at this point, the job is still running within the SCC batch 
%    mode. Use of "exit" will enable you to get out of MATLAB and
%    henceforth the SCC batch.
% For many batch jobs, a user runs multiple serial or parallel batch jobs 
% using qsub's Array Job option, for which the number of cores as well as 
% an index (SGE_TASK_ID) are required. RCS has available a getbev.m file 
% to return these parameters for your convenience.
% (see /project/scv/examples/matlab/batch)
% Kadin Tseng, RCS, IS&T, Boston University, February, 2015

[nslots, task] = getbev;  % RCS util to get batch-generated env vars

if task == 'undefined'
  n = 100;   % default if Array Job (qsub -t) not used
else
  n = task;  % for Array Jobs, each job has its only n
end

myCluster = parcluster('local')  % treat compute node as "local"
if isBatch    % if this job submitted by qsub, then
  myCluster.JobStorageLocation = getenv('TMPDIR');  % for batch job
end

% Example 1: myFCT.m is a function; takes 2 input & 3 output args.
% It computes s = 1+2+3+ . . . +n with multiprocessors
job1 = batch(myCluster, 'myFCT', 3, {n, nslots}, 'Pool', nslots)
wait(job1);   % wait for job to finish
% A function needs fetchOutputs to recover output (cell array)
data=fetchOutputs(job1);  % retrieve output for function m-file
data{1:end}   % prints myFCT output; see myFCT.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example 2: mySCR.m is a script
% It computes s = 1+2+3+ . . . +n with multiprocessors
job2 = batch(myCluster, 'mySCR', 'Pool', nslots)  % submit batch
wait(job2);      % wait for job to finish
load(job2, 'x2', 's2', 'txt2'); % retrieve output for script m-file with load
x2  % prints mySCR output
s2
txt2

end
