Products & Services Solutions Academia Support User Community Company

Learn more about Parallel Computing Toolbox   

Using Parallel Computing Toolbox Software

Example: Evaluating a Basic Function

The dfeval function allows you to evaluate a function in a cluster of workers without having to individually define jobs and tasks yourself. When you can divide your job into similar tasks, using dfeval might be an appropriate way to run your job. The following code uses a local scheduler on your client computer for dfeval.

results = dfeval(@sum, {[1 1] [2 2] [3 3]}, 'Configuration', 'local')
results = 
    [2]
    [4]
    [6]

This example runs the job as three tasks in three separate MATLAB worker sessions, reporting the results back to the session from which you ran dfeval.

For more information about dfeval and in what circumstances you can use it, see Evaluating Functions in a Cluster.

Example: Programming a Basic Job with a Local Scheduler

In some situations, you might need to define the individual tasks of a job, perhaps because they might evaluate different functions or have uniquely structured arguments. To program a job like this, the typical Parallel Computing Toolbox client session includes the steps shown in the following example.

This example illustrates the basic steps in creating and running a job that contains a few simple tasks. Each task evaluates the sum function for an input array.

  1. Identify a scheduler. Use findResource to indicate that you are using the local scheduler and create the object sched, which represents the scheduler. (For more information, see Find a Job Manager or Creating and Running Jobs.)

    sched = findResource('scheduler', 'type', 'local')
  2. Create a job. Create job j on the scheduler. (For more information, see Create a Job.)

    j = createJob(sched)
  3. Create three tasks within the job j. Each task evaluates the sum of the array that is passed as an input argument. (For more information, see Create Tasks.)

    createTask(j, @sum, 1, {[1 1]})
    createTask(j, @sum, 1, {[2 2]})
    createTask(j, @sum, 1, {[3 3]})
  4. Submit the job to the scheduler queue for evaluation. The scheduler then distributes the job's tasks to MATLAB workers that are available for evaluating. The local scheduler actually starts a MATLAB worker session for each task, up to eight at one time. (For more information, see Submit a Job to the Job Queue.)

    submit(j);
  5. Wait for the job to complete, then get the results from all the tasks of the job. (For more information, see Retrieve the Job's Results.)

    waitForState(j)
    results = getAllOutputArguments(j)
    results = 
        [2]
        [4]
        [6]
  6. Destroy the job. When you have the results, you can permanently remove the job from the scheduler's data location.

    destroy(j)

Getting Help

Command-Line Help

You can get command-line help on the toolbox object functions by using the syntax

help distcomp.objectType/functionName

For example, to get command-line help on the createTask function, type

help distcomp.job/createTask

The available choices for objectType are listed in the Object Reference.

Listing Available Functions.   To find the functions available for each type of object, type

methods(obj)

where obj is an object of one of the available types.

For example, to see the functions available for job manager objects, type

jm = findResource('scheduler','type','jobmanager');
methods(jm)

To see the functions available for job objects, type

job1 = createJob(jm)
methods(job1)

To see the functions available for task objects, type

task1 = createTask(job1,1,@rand,{3})
methods(task1)

Help Browser

You can open the Help browser with the doc command. To open the browser on a specific reference page for a function or property, type

doc distcomp/RefName

where RefName is the name of the function or property whose reference page you want to read.

For example, to open the Help browser on the reference page for the createJob function, type

doc distcomp/createJob

To open the Help browser on the reference page for the UserData property, type

doc distcomp/UserData
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS