%% Batch processing demo utilizing the ODE System example
% Copyright 2015 The MathWorks, Inc.

%% Close any open pool of workers (if it is on the cluster, an open pool might cause your job the be queued instead of running)

delete(gcp('nocreate'));

%% Submit job (1 worker)

fprintf('Submitting batch job on 1 worker... ');

% batch(function, number of outputs, inputs, profile, pool size)
job1 = batch('paramSweepSerial', 4, {10, 10});  

fprintf('Submitted Job 1!\n');

%% Submit job (30 workers)

fprintf('Submitting batch job on 30 workers... ');

job2 = batch('paramSweepParallel', 4, {50, 50}, 'Pool', 30);  

fprintf('Submitted Job 2!\n');

%% Plot data when job is finished
% See the function definition for paramSweepParallel.m for the definition
%   of the four output variables 

% Make sure job2 is finished and fetch results
wait(job2);  
jobOutput2=fetchOutputs(job2);

% Visualize results
visualizeParamSweep(jobOutput2);

%% Compare Timings

% Make sure job1 is finished and fetch results
wait(job1);
jobOutput1=fetchOutputs(job1);

fprintf('Computation time on 1 worker  : %0.2f sec\n', jobOutput1{4});
fprintf('Computation time on 30 workers: %0.2f sec\n', jobOutput2{4});

%% Clean Up
% These jobs are using physical resources.  It's a best practice to delete
% them when done

delete(job1);
delete(job2);