Main Content

createTask

Create new task in job

Syntax

t = createTask(j, F, N, {inputargs})
t = createTask(j, F, N, {C1,...,Cm})
t = createTask(..., 'p1',v1,'p2',v2,...)
t = createTask(...,'Profile', 'ProfileName',...)

Arguments

t

Task object or vector of task objects.

j

的工作任务中创建的对象。

F

A handle to the function that is called when the task is evaluated, or an array of function handles.

N

The number of output arguments to be returned from execution of the task function. This is a double or array of doubles.

{inputargs}

A row cell array specifying the input arguments to be passed to the functionF. Each element in the cell array will be passed as a separate input argument. If this is a cell array of cell arrays, a task is created for each cell array.

{C1,...,Cm}

Cell array of cell arrays defining input arguments to each ofmtasks.

p1,p2

Task object properties configured at object creation.

v1,v2

Initial values for corresponding task object properties.

Description

t = createTask(j, F, N, {inputargs})creates a new task object in jobj, and returns a reference,t, to the added task object. This task evaluates the function specified by a function handle or function nameF, with the given input arguments{inputargs}, returningNoutput arguments.

t = createTask(j, F, N, {C1,...,Cm})uses a cell array ofmcell arrays to createmtask objects in jobj, and returns a vector,t, of references to the new task objects. Each task evaluates the function specified by a function handle or function nameF. The cell arrayC1provides the input arguments to the first task,C2to the second task, and so on, so that there is one task per cell array. Each task returnsNoutput arguments. IfFis a cell array, each element ofFspecifies a function for each task in the vector; it must havemelements. IfNis an array of doubles, each element specifies the number of output arguments for each task in the vector. Multidimensional matrices of inputsF,Nand{C1,...,Cm}are supported; if a cell array is used forF, or a double array forN, its dimensions must match those of the input arguments cell array of cell arrays. The outputtwill be a vector with the same number of elements as{C1,...,Cm}. Note that because a communicating job has only one task, this form of vectorized task creation is not appropriate for such jobs.

t = createTask(..., 'p1',v1,'p2',v2,...)adds a task object with the specified property values. For a listing of the valid properties of the created object, see theparallel.Taskobject reference page. The property name must be a character vector, with the value being the appropriate type for that property. The values specified in these property-value pairs override the values in the profile. If an invalid property name or property value is specified, the object will not be created.

t = createTask(...,'Profile', 'ProfileName',...)creates a task object with the property values specified in the cluster profileProfileName. For details about defining and applying cluster profiles, seeDiscover Clusters and Use Cluster Profiles.

Examples

collapse all

Create a job object.

c = parcluster();% Use default profilej = createJob(c);

Add a task object which generates a 10-by-10 random matrix.

t = createTask(j, @rand, 1, {10,10});

Run the job.

submit(j);

Wait for the job to finish running, and get the output from the task evaluation.

wait(j); taskoutput = fetchOutputs(j);

Show the 10-by-10 random matrix.

disp(taskoutput{1});

This example creates a job with three tasks, each of which generates a 10-by-10 random matrix.

c = parcluster();% Use default profilej = createJob(c); t = createTask(j, @rand, 1, {{10,10} {10,10} {10,10}});

This example creates a task that captures the worker diary, regardless of the setting in the profile.

c = parcluster();% Use default profilej = createJob(c); t = createTask(j,@rand,1,{10,10},'CaptureDiary',true);

Create a job object on the default cluster.

c = parcluster; job = createJob(c);

To create a single task with all cell arrays as its input arguments, use the{C1}syntax ofcreateTask. For example, to create a task that runsstrjoin({'1','1','2'},{'+','='}), use the following code.

task = createTask(job,@strjoin,1,{{{'1','1','2'},{'+','='}}}); task.InputArguments{:}
ans =1×3 cell{'1'} {'1'} {'2'}
ans =1×2 cell{'+'} {'='}

Submit and wait for the job.

submit(job); wait(job);

Retrieve the outputs and display them.

outputs = fetchOutputs(job); disp(outputs{1});
1+1=2

If you attempt to use the{inputargs}syntax with{inputargs} = {{'1','1','2'},{'+','='}}, thencreateTaskuses the{C1,...,Cm}syntax and creates multiple tasks. For example, the following code incorrectly creates two tasks, one forstrjoin('1','1','2')and one forstrjoin('+','=').

task = createTask(job,@strjoin,1,{{'1','1','2'},{'+','='}});
Introduced before R2006a