Asynchronous Parallel Programming
parfeval
You can evaluate a function in the background without waiting for it to complete, using
parfeval
. In many cases, it can be
convenient to break out of a for loop early. For example, in an optimization
procedure, you can stop the loop early when the result is good enough. You can do
this on one or all parallel pool workers, using parfeval
or parfevalOnAll
. This can be useful
if you want to be able to plot intermediate results. Using parfeval
or parfevalOnAll
is different from
using parfor
, where you have to wait for
the loop to complete.
Use send
and poll
together to send and poll for messages or data from different workers using a data queue. You can use afterEach
to add a function to call when new data is received from a data queue.
Use afterEach
and afterAll
to automatically invoke functions after each or after all elements of a Future
array complete. This array can contain futures returned by parfeval
, parfevalOnAll
, afterEach
or afterAll
.
Functions
Topics
Get Started with Asynchronous Parallel Programming
- Evaluate Functions in the Background Using parfeval
Break out of an optimizing loop early and collect results as they become available. - Use afterEach and afterAll to Run Callback Functions
Automatically run functions afterFuture
objects finish running on parallel pools. - Query and Cancel parfeval Futures
This example shows how to query the state ofparfeval
futures and cancel them. - Update User Interface Asynchronously Using afterEach and afterAll
This example shows how to update a user interface as computations complete. - Choose Between spmd, parfor, and parfeval
Compare and contrastspmd
against other parallel computing functionality such asparfor
andparfeval
.
Applications
- Plot During Parameter Sweep with parfeval
This example shows how to perform a parallel parameter sweep withparfeval
and send results back during computations with aDataQueue
object. - Perform Webcam Image Acquisition in Parallel with Postprocessing
This example shows how to perform frame acquisition from a webcam in parallel with data postprocessing. - Perform Image Acquisition and Parallel Image Processing
This example shows how to perform image acquisition from a webcam and postprocess data in parallel. - Send Messages to Workers Using Pollable Data Queues
This example shows how to use pollable data queues to send data or instructions to workers during asynchronous function evaluations withparfeval
. (Since R2025a) - Transfer Data Between Workers Using Pollable Data Queues
This example shows how to use a pollable data queue to transfer data between workers during asynchronous function evaluations withparfeval
. (Since R2025a) - Train Deep Learning Networks in Parallel (Deep Learning Toolbox)
This example shows how to run multiple deep learning experiments on your local machine. - Use parfeval to Train Multiple Deep Learning Networks (Deep Learning Toolbox)
This example shows how to useparfeval
to perform a parameter sweep on the depth of the network architecture for a deep learning network and retrieve data during training. - Receive Communication on Workers
This example shows how to set up a data queue on the workers to receive data. (Since R2023b) - Interactively Import and Process Data in Parallel
This example shows how to import and process data simultaneously in an interactive parallel pool. (Since R2023b) - Perform Data Acquisition and Processing on Pool Workers
This example shows how to implement a parallel data acquisition and processing pipeline on an interactive parallel pool. (Since R2025a) - Control Hardware and Acquire Data in Parallel
This example shows how to simultaneously control hardware and perform data acquisition on parallel workers. (Since R2025a)