spmdBarrier - Synchronize workers in spmd block - MATLAB (original) (raw)
Main Content
Synchronize workers in spmd
block
Since R2022b
Syntax
Description
spmdBarrier
stops all workers in the current spmd block or communicating job from executing code until every worker callsspmdBarrier
.
When you offload computations using parfor
andparfeval
, only one worker at a time runs each computation. These workers are independent and do not communicate with each other. If you applyspmdBarrier
to these workers, the function has no effect.
Use 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 thespmdSize function. The spmdSize
function returns a value of 1
outside of an spmd
block or communicating job.
Examples
This example shows how to use spmdBarrier
to synchronize workers in an spmd
block.
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 thespmd
block.
Create an spmd
block. Pause each worker for a random amount of time to simulate some computationally expensive work. Use tic
andtoc
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 pause, use 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.
Extended Capabilities
Version History
Introduced in R2022b