%% Submitting Serial and Parallel Scripts
% This schedules a parameter sweep study of a 2nd order ODE system.
%
%   m*X'' + b*X' + k*X = 0
%
% This example will schedule both a serial (parfor w/out a parpool) and
% parallel (parfor w/a parpool) version. 
%
% The batch command sends your serial script to run on one worker in your
% cluster. batch with the 'pool' option sends scripts containing
% parfor to run on workers via a parpool.  In this process, one worker
% behaves like a MATLAB client process that facilitates the distribution of
% the job amongst the workers in the pool and runs the serial portion of
% the script. 
%
% All of the variables in your client workspace (e.g. the MATLAB process
% you are submitting from) are sent to the worker by default. You can
% alternatively pass a subset of these variables by defining the Workspace
% argument and passing the desired variables in a structure. 
%
% After your job has finished, you can use the load command to retrieve the
% results from the worker-workspace back into your client-workspace.  In
% this and all other scheduling examples, we use a wait to ensure the job
% is done before we load back in worker-workspace. This is optional, but
% you can not load the data from a task or job until that task or job is
% finished. So, we use wait to block the MATLAB command line until that
% occurs. 
%
%
% Copyright 2013 The MathWorks, Inc.
clear
close all
delete(gcp)
%% Submitting the Job
% paramSweep uses parfor
c=parcluster;
job=c.batch('paramSweep','AttachedFiles',{'odesystem.m'},'pool',1)
%job=c.batch('paramSweep','AttachedFiles',{'odesystem.m'},'pool',23)
%%
job.State

%%
wait(job);
%%
load(job);
results=fetchOutputs(job);
%%
delete(job)
%{
>> time
time =
    2.2307
%}

%% Visualize using Output Data from Workers

figure;
surf(bVals, kVals, peakVals);
xlabel('Damping'); ylabel('Stiffness'); zlabel('Peak Displacement');
view(50, 30)