Send data to all workers in
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
spmdBroadcast to these workers, the function has no effect.
The worker whose index in the current spmd block is equal to source broadcasts the data.
To get the worker index, use the
source is equal to the worker index,
Broadcast Array in
This example shows how to broadcast an array from one worker to other
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. On the worker whose index is equal to
1, create an array. Use
spmdBroadcast to send
the array to all workers. On each other worker, use
to receive the array.
spmd source = 1; if spmdIndex == source A = magic(3); B = spmdBroadcast(source, A); else B = spmdBroadcast(source); end B end
Worker 1: B = 8 1 6 3 5 7 4 9 2 Worker 2: B = 8 1 6 3 5 7 4 9 2 Worker 3: B = 8 1 6 3 5 7 4 9 2 Worker 4: B = 8 1 6 3 5 7 4 9 2
On the client, inspect the
A. Only the worker whose index is 1 contains a definition of
A = Worker 1: class = double, size = [3 3] Worker 2: No data Worker 3: No data Worker 4: No data
source — Index of worker sending data
Index of the worker that is sending data, specified as a positive integer. This
value must be greater than or equal to
1 and less than or equal to
the number of workers running the current spmd block. To determine the number of workers
running the current spmd block, use the
spmdSize function returns a value of 1 outside of an
spmd block or communicating job.
B — Received data
Data returned on the worker, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.
A worker that calls
spmdBroadcast might finish execution before other
workers. When you need synchronized workers in an
spmd block or
communicating job, such as when you close a shared resource, use
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.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU.
data is a
gpuArray, data transfer between
multiple GPUs in a parallel pool uses fast peer-to-peer communication, including NVLink, if
Introduced in R2022b