(Not recommended) Synchronize workers in an
labBarrier stops all workers in the current
block or communicating job from executing code until every worker calls
When you offload computations using
parfeval, each computation is run by only one worker at a time.
These workers are independent and do not communicate with each other. If you use
labBarrier on these workers, the function has no effect.
labBarrier when you need to synchronize workers, such as when
workers use shared resources such as a file handle.
numlabs is equal to
1, execution continues
numlabs is equal to
1 outside of an
spmd block or communicating job.
labBarrier to Synchronize Workers
This example shows how to use
synchronize workers in an
Create a parallel pool with 4 workers. By default,
supported on all process-backed pools.
When you execute an
spmd block after creating a parallel pool, by
default all available workers in the pool will run the code inside the
spmd block, and run
pause on each
worker for a random amount of time to simulate some computationally expensive work. Use
toc to time the execution on each
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
labBarrier. In the following code, all workers wait for the slowest
worker to finish its computation. The elapsed time on each worker is now the same,
except for slight numerical noise.
spmd tic pause(5*rand); labBarrier; toc end
Lab 1: Elapsed time is 4.758529 seconds. Lab 2: Elapsed time is 4.758529 seconds. Lab 3: Elapsed time is 4.743785 seconds. Lab 4: Elapsed time is 4.743739 seconds.
Version HistoryIntroduced before R2006a
labBarrier function is not recommended
To indicate their intended use within
labBarrier is renamed to
labBarrier will continue to work but is no longer recommended. To
update your code, replace any instance of
spmdBarrier. There are no plans to remove