Main Content

parfevalOnAll

Run function asynchronously on all workers in parallel pool

Description

F = parfevalOnAll(p,fcn,numFcnOut,X1,...,Xm) requests the asynchronous execution of the function fcn on all workers in the parallel pool p. The parfevalOnAll function evaluates fcn on each worker with input arguments X1,...,Xm and returns numFcnOut output arguments. You can obtain the results from the Future object F when all workers have completed running fcn.

example

F = parfevalOnAll(fcn,numFcnOut,X1,...,Xm) requests asynchronous execution on all workers in the current parallel pool. If no pool exists, and automatic pool creation is enabled, MATLAB® starts a new parallel pool.

Note

Use parfevalOnAll instead of parfor or spmd if you want to use clear. This preserves workspace transparency. See Ensure Transparency in parfor-Loops or spmd Statements.

Examples

collapse all

You can use the parfevalOnAll function to run a clean up function on all the workers while preserving workspace transparency.

For example. to unload a mex file before deleting temporary folders for distributing simulations, run the clear function on all the workers using parfevalOnAll. Because clear has 0 output arguments, specify 0 to the numFcnOut input argument of parfevalOnAll.

parfevalOnAll(@clear,0,"mex");

To close all Simulink model windows on all the workers, run the bdclose using the parfevalOnAll function.

p = gcp; % Get the current parallel pool
f = parfevalOnAll(p,@bdclose,0,"all");

In both cases, wait for completion and verify success by using the fetchOutputs function. Even when you do not request output arguments, you can use fetchOutputs on the future to check for errors from the workers.

fetchOutputs(f)

Input Arguments

collapse all

Parallel pool of workers, specified as a parallel.Pool object. You can create a parallel pool by using the parpool function.

Function to run on the workers, specified as a function handle.

Example: fcn = @sum

Data Types: function_handle

Number of output arguments requested from the function fcn, specified as a nonnegative integer.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Input arguments, specified as a comma-separated list of variables or expressions. The parallel pool worker inputs these arguments to the function fcn.

Output Arguments

collapse all

Future, returned as a parallel.FevalOnAllFuture object, that represents the execution of fcn on all the parallel workers and holds their results. Use fetchOutputs to collect the results.

Extended Capabilities

Version History

Introduced in R2013b

expand all