Program Independent Jobs on a Local Cluster

Create and Run Jobs with a Local Cluster

For jobs that require more control than the functionality offered
by such high level constructs as spmd and parfor,
you have to program all the steps for creating and running the job.
Using the local cluster (or local scheduler) on your machine lets
you create and test your jobs without using the resources of your
network cluster. Distributing tasks to workers that are all running
on your client machine might not offer any performance enhancement,
so this feature is provided primarily for code development, testing,
and debugging.

Note:
Workers running in a local cluster on a Microsoft® Windows® operating
system can display Simulink® graphics as well as the output from
certain functions such as uigetfile and uigetdir. (With other platforms or schedulers,
workers cannot display any graphical output.) This behavior is subject
to removal in a future release.

This section details the steps of a typical programming session
with Parallel Computing Toolbox™ software using a local cluster:

Note that the objects that the client session uses to interact
with the cluster are only references to data that is actually contained
in the cluster's job storage location, not in the client session.
After jobs and tasks are created, you can close your client session
and restart it, and your job still resides in the storage location.
You can find existing jobs using the findJob function
or the Jobs property of the cluster object.

Create a Cluster Object

You use the parcluster function
to create an object in your local MATLAB® session representing
the local scheduler.

parallel.defaultClusterProfile('local');
c = parcluster();

Create a Job

You create a job with the createJob function.
This statement creates a job in the cluster's job storage location,
creates the job object job1 in the client session,
and if you omit the semicolon at the end of the command, displays
some information about the job.

Note that the job's State property
is pending. This means the job has not yet been
submitted (queued) for running, so you can now add tasks to it.

The scheduler's display now indicates the existence of
your job, which is the pending one, as appears in this partial listing:

c

Local Cluster
Associated Jobs
Number Pending: 1
Number Queued: 0
Number Running: 0
Number Finished: 0

Create Tasks

After you have created your job, you can create tasks for the
job using the createTask function.
Tasks define the functions to be evaluated by the workers during the
running of the job. Often, the tasks of a job are all identical. In
this example, five tasks will each generate a 3-by-3 matrix of random
numbers.

Submit a Job to the Cluster

To run your job and have its tasks evaluated, you submit the
job to the cluster with the submit function.

submit(job1)

The local scheduler starts the workers on your machine, and
distributes the tasks of job1 to these workers
for evaluation.

Fetch the Job's Results

The results of each task's evaluation are stored in the
task object's OutputArguments property
as a cell array. After waiting for the job to complete, use the function fetchOutputs to retrieve the results
from all the tasks in the job.

After the job is complete, you can repeat the commands to examine
the updated status of the cluster, job, and task objects:

c
job1
job1.Tasks

Local Cluster Behavior

The local scheduler runs in the MATLAB client session,
so you do not have to start any separate scheduler or MJS process
for the local scheduler. When you submit a job for evaluation to the
local cluster, the scheduler starts a MATLAB worker for each
task in the job, but only up to as many workers as allowed by the
local profile. If your job has more tasks than allowed workers, the
scheduler waits for one of the current tasks to complete before starting
another MATLAB worker to evaluate the next task. You can modify
the number of allowed workers in the local cluster
profile. If not specified, the default is to run only as many workers
as computational cores on the machine.

The local cluster has no interaction with any other scheduler
or MJS, nor with any other workers that might also be running on your
client machine under the mdce service. Multiple MATLAB sessions
on your computer can each start its own local scheduler with its own
workers, but these groups do not interact with each other.

When you end your MATLAB client session, its local scheduler
and any workers that happen to be running at that time also stop immediately.