% Whenever you run a MATLAB PCT job on SCC's cluster, MATLAB will create a
% folder within the .matlab folder under the user's home dir. You need not
% know anything about it. If this job is an interactive job (i.e., on the 
% login node), such folder and files generation present no problem. When
% this PCT job is submitted to the batch queue, it will be executed on a
% compute node (not the login node). The aforementioned folder creation
% will be taking place on the user's home dir as decribed. This process
% is far less efficient from a communication view point, though it would 
% not be a serious issue for the system. However, if you submit multiple
% PCT jobs (likely through the use of qsub's -t Array Jobs feature), the 
% issue would be compounded as requests to create a folder to the same user's
% .matlab directory come from multiple nodes (one for each batch job) which
% would create a "traffic jam" for the system to handle. To circumvent this 
% issue, a simple solution is to redirect the generation of this folder to 
% the runtime assigned compute node's TMPDIR folder. This is achieved by 
% runBatch.m. It makes use of MATLAB's batch utility which enables the 
% redirection. 
%
% Included below is the procedure required:
% 1) Make a copy of runBatch.m and modify as necessary for your application.
% 2) Customize the run_matlab_pct_job matlab batch script if need be. The
%    key difference between this script and your existing one (if any) would 
%    be that it calls runBatch instead of your application. In this case,
%    runBatch will be calling your application.
%
% Run Instructions:
% scc1% qsub run_matlab_pct_job   <== runs 1 MATLAB PCT job
% scc1% qsub -t 3-5 run_matlab_pct_job   <== runs 3 MATLAB PCT jobs
%
% Demonstration code package consists of:
% 1) myApp.m demonstrates the application that is a function m-file
% 2) myWave.m demonstrates the application that is a script m-file
% 3) Several RCS (Research Computing Services) common batch utilities helpful
%    for batch processing, such as getbev.m and isBatch.m
% 4) runBatch.m is the code that you need to customize for your application
% 5) run_matlab_pct_job is the batch script that invokes matlab and runBatch.m
%

Behavior of using the matlab "batch" command:
1) Running batch with MATLAB batch automatically suppresses output.
   Upon returning, retrieve data with fetchOutputs (for functions) or
   load (for scripts).
2) iIf you use SPMD for your parallel processing, you will need to gather 
   output from workers before returning from myApp.m. For example,
   delta = n/nslots;
   x=distributed.zeros(1,n);
   spmd
     for k=1+(labindex-1)*delta:labindex*delta
       x(k) = k;
     end
   enddo
% A workaround; not necessary if myApp not invoked thru the matlab batch utility
   x = gather(x);  
