parfevalOnAll - Run function asynchronously on all workers in parallel pool - MATLAB (original) (raw)

Run function asynchronously on all workers in parallel pool

Syntax

Description

[F](#mw%5F5638ebf6-e8b7-4584-a739-0f886a9e4d5a) = parfevalOnAll([p](#mw%5F1f5a33d6-5819-4e88-889d-055c0f356b09),[fcn](#mw%5F9159e1e9-3717-4d09-a433-f0067be57ffc),[numFcnOut](#mw%5Fe265880c-38c8-4770-b364-69b2b4cf52b7),[X1,...,Xm](#mw%5F644eb230-a461-411d-940f-3df9f9dc5017)) 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 argumentsX1,...,Xm and returns numFcnOut output arguments. You can obtain the results from the Future objectF when all workers have completed runningfcn.

[F](#mw%5F5638ebf6-e8b7-4584-a739-0f886a9e4d5a) = parfevalOnAll([fcn](#mw%5F9159e1e9-3717-4d09-a433-f0067be57ffc),[numFcnOut](#mw%5Fe265880c-38c8-4770-b364-69b2b4cf52b7),[X1,...,Xm](#mw%5F644eb230-a461-411d-940f-3df9f9dc5017)) 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.

example

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 usingparfevalOnAll. Because clear has0 output arguments, specify 0 to thenumFcnOut input argument of parfevalOnAll.

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

To close all Simulink model windows on all the workers, run thebdclose 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 thefetchOutputs function. Even when you do not request output arguments, you can use fetchOutputs on the future to check for errors from the workers.

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 functionfcn.

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

Starting in R2021b, you can now run parfevalOnAll in serial with no pool. This behavior allows you to share parallel code that you write with users who do not have Parallel Computing Toolbox.

When you use the syntax parfevalOnAll(fcn,numFcnOut,X1,...,Xm), MATLAB tries to use an open parallel pool if you have Parallel Computing Toolbox. If a parallel pool is not open, MATLAB will create one if automatic pool creation is enabled.

If parallel pool creation is disabled or if you do not have Parallel Computing Toolbox, the function is evaluated in serial. In previous releases, MATLAB threw an error instead.