Program Independent Jobs on a Local Cluster

Create and Run Jobs with a Local Cluster

Some jobs require more control than the functionality offered by high-level constructs like spmd and parfor. In such cases, 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 do not offer any performance enhancement. Therefore 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 and 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:

The objects used by the client session to interact with the cluster are only references to data in the cluster 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 job storage location and creates the job object job1in the client session. If you omit the semicolon at the end of the command, it displays some information about the job.

job1 = createJob(c)
 Job

    Properties:

                   ID: 2
                 Type: Independent
             Username: eng864
                State: pending
           SubmitTime: 
            StartTime: 
     Running Duration: 0 days 0h 0m 0s

      AutoAttachFiles: true
  Auto Attached Files: List files
        AttachedFiles: {}
      AdditionalPaths: {}

    Associated Tasks:

       Number Pending: 0
       Number Running: 0
      Number Finished: 0
    Task ID of Errors: []

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

The scheduler 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 each generate a 3-by-3 matrix of random numbers.

createTask(job1, @rand, 1, {{3,3} {3,3} {3,3} {3,3} {3,3}});

The Tasks property of job1 is now a 5-by-1 matrix of task objects.

job1.Tasks
         ID      State    FinishTime  Function  Error
 -----------------------------------------------------
    1     1    pending                   @rand       
    2     2    pending                   @rand       
    3     3    pending                   @rand       
    4     4    pending                   @rand       
    5     5    pending                   @rand        

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 Results

The results of each task evaluation are stored in the task object 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.

wait(job1)
results = fetchOutputs(job1);

Display the results from each task.

results{1:5}
    0.9501    0.4860    0.4565
    0.2311    0.8913    0.0185
    0.6068    0.7621    0.8214

    0.4447    0.9218    0.4057
    0.6154    0.7382    0.9355
    0.7919    0.1763    0.9169

    0.4103    0.3529    0.1389
    0.8936    0.8132    0.2028
    0.0579    0.0099    0.1987

    0.6038    0.0153    0.9318
    0.2722    0.7468    0.4660
    0.1988    0.4451    0.4186

    0.8462    0.6721    0.6813
    0.5252    0.8381    0.3795
    0.2026    0.0196    0.8318

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 to the local cluster, the scheduler starts a MATLAB worker for each task in the job. You can do this for 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 can also be running on your client machine under the mdcs 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 also stop immediately.

Was this topic helpful?