Simultaneously send and receive data on worker in
B = spmdSendReceive(
A from the current worker in an
spmd block or communicating job to the
receives data from the
source. The array
A is sent
from the current worker to the worker whose index is equal to
destination. The current worker receives data
sent to the current worker from the worker whose index is equal to
When you use this syntax, the computation is equivalent to the worker sending and receiving data by running these lines of code simultaneously:
spmdSend(A,destination); B = spmdReceive(source);
sends and receives data with the tag
B = spmdSendReceive(___,
tag. When you use
spmdSendReceive to send data between workers, multiple items of data
can wait to be collected. When you send multiple items of data to a worker, add a tag to
each item to distinguish between the items.
Send Data Between Workers in
spmd Block Using
This example shows how to use
send data between workers in an
spmd block or communicating job.
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. Use
spmdSendReceive to send and receive data in a
chain of workers.
spmdSendReceive to send data to the worker whose index is
greater by one than the index of the current worker, modulo the number of workers
running the current spmd block. Receive data from the worker with an index that is less
by one than the index of the current worker, modulo the number of workers running the
current spmd block.
When you use modulo division, the worker whose index is equal to
1 receives data from the worker whose index is equal to the number
of workers running the current spmd block. The worker whose index is equal to
1 receives data from the worker whose index is equal to
spmd A = 2*spmdIndex; destination = 1 + mod((spmdIndex+1)-1, spmdSize); source = 1 + mod((spmdIndex-1)-1, spmdSize); A = spmdSendReceive(source,destination,A) end
Worker 1: A = 4 Worker 2: A = 6 Worker 3: A = 8 Worker 4: A = 2
destination — Index of destination worker
positive integer | empty array
Index of the destination worker, specified as a positive integer or an empty array.
The destination worker receives data from the current worker. This input must be less
than or equal to the number of workers running the current
or communicating job.
If this argument is an empty array, the function does not send any data.
source — Index of source worker
positive integer | empty array
Index of the source worker, specified as a positive integer or an empty array. The
current worker waits until it receives data from the source worker. This value must be
less than the number of workers running the current
spmd block or
If this argument is an empty array, the function does not receive any data.
A — Data to send
Data to send from the current worker, specified as a scalar, vector, matrix, multidimensional array, table, timetable, or any other MATLAB variable.
tag — Message tag
0 (default) | nonnegative integer
Message tag, specified as a nonnegative integer. When you specify this input,
spmdSendReceive sends data with the tag
from the current worker, and returns data sent with the tag
the current worker.
Tags have many uses, for example:
Use tags to save memory by only loading arrays on workers when you need the data.
Use tags to create code that does not depend on the index of the sending worker.
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