Synchronize workers in
spmdBarrier stops all workers in the current
spmd block or communicating job from executing code until every worker calls
When you offload computations using
parfeval, only one worker at a time runs each computation. These
workers are independent and do not communicate with each other. If you apply
spmdBarrier to these workers, the function has no effect.
spmdBarrier to synchronize workers, for example, when workers
use shared resources such as a file handle.
If only one worker is running on the current spmd block, execution continues
immediately. To determine the number of workers running the current spmd block, use the
spmdSize function returns a value of
outside of an
spmd block or communicating job.
Synchronize Workers in
This example shows how to use
synchronize workers in an
Create a parallel pool with four workers.
When you execute an
spmd block after creating a parallel pool, by
default all available workers in the pool run the code inside the
spmd block. Pause each worker for a random amount of
time to simulate some computationally expensive work. Use
toc to time the execution on each worker.
spmd tic pause(5*rand); toc end
Worker 2: Elapsed time is 0.702969 seconds. Worker 3: Elapsed time is 1.807292 seconds. Worker 1: Elapsed time is 4.651690 seconds. Worker 4: Elapsed time is 4.694443 seconds.
To synchronize the workers after each worker runs
spmdBarrier. All the workers wait for the
slowest worker to finish its computation. The elapsed time on each worker is now the
same, except for small numerical noise.
spmd tic pause(5*rand); spmdBarrier; toc end
Worker 1: Elapsed time is 4.758529 seconds. Worker 2: Elapsed time is 4.758529 seconds. Worker 3: Elapsed time is 4.743785 seconds. Worker 4: Elapsed time is 4.743739 seconds.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Introduced in R2022b