spmdSend - Send data to another worker in spmd block - MATLAB (original) (raw)
Send data to another worker in spmd
block
Since R2022b
Syntax
Description
spmdSend([A](#mw%5F70187395-0826-455a-b601-c08140ae0e55),[destination](#mw%5F7f5bf5de-9fb8-40e6-b9c2-2ec2120cef81))
sends data A
from the current worker in an spmd block or communicating job to the workers specified bydestination
.
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 applyspmdSend
to these workers, the function has no effect.
To use spmdSend
, the number of workers running the current spmd block must be greater than 1
. To get the number of workers running the current spmd block, use the spmdSize function.
spmdSend(___,[tag](#mw%5F9ba00917-db85-4d60-a309-844ae8a88833))
sends data with the tag tag
. When you use spmdSend
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.
Examples
This example shows how to send data between workers in anspmd
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 thespmd
block.
Create an spmd
block. On the worker whose index is equal to1
, create an array. Use spmdSend
to send the array to the worker whose index is equal to 2 and use spmdReceive
to collect the data.
spmd switch spmdIndex case 1 A = magic(3) spmdSend(A,2); case 2 B = spmdReceive end end
Worker 1:
A =
8 1 6
3 5 7
4 9 2
Worker 2:
B =
8 1 6
3 5 7
4 9 2
This example shows how to tag and send data between workers in anspmd
block or communicating job.
Create a parallel pool with four workers.
Create an spmd
block. On the worker whose index is equal to1
, create two arrays A1
andA2
. Before and between creating arrays, pause the execution using the pause function to simulate some work. Then, use spmdSend
to send the matrices to the worker whose index is equal to 2
.
Tag each matrix with an integer. On the worker whose index is equal to2
, use spmdReceive
to collect the data.
tic spmd switch spmdIndex case 1 pause(5); A1 = magic(1) pause(5); A2 = magic(2) spmdSend(A1,2,1); spmdSend(A2,2,2); case 2 B1 = spmdReceive('any',1) B2 = spmdReceive('any',2)
end
end toc
Worker 1:
A1 =
1
A2 =
1 3
4 2
Worker 2:
B1 =
1
B2 =
1 3
4 2
Elapsed time is 10.061523 seconds.
In some cases, you can improve the performance of your code by moving some work from one worker to another. Move some work from the worker whose index is equal to1
to the worker whose index is equal to 2
. When you use tags, you can easily move calculations from one worker to another without updating code on the receiving worker.
tic spmd switch spmdIndex case 1 pause(5); A1 = magic(1) spmdSend(A1,2,1); case 2 B2 = spmdReceive('any',2) B1 = spmdReceive('any',1) case 3 pause(5); A2 = magic(2) spmdSend(A2,2,2); end end toc
Worker 1:
A1 =
1
Worker 2:
B2 =
1 3
4 2
B1 =
1
Worker 3:
A2 =
1 3
4 2
Elapsed time is 5.117787 seconds.
Input Arguments
Data to send from the current worker, specified as a scalar, vector, matrix, multidimensional array, table, timetable, or any other MATLAB variable.
Example: magic(3)
Indices of the workers receiving data, specified as a positive integer or vector of positive integers. The indices must be less than or equal to the number of workers running the current spmd
block or communicating job.
Example: [2 3 4]
Message tag, specified as nonnegative integer. When you specify this input,spmdSend
sends data with this tag. If you receive the data on another worker using spmdReceive, that function returns the tag in itstagOut
output.
Example: 314159
Tips
- 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.
- A worker that sends data using
spmdSend
might finish execution before the receiving worker receives the data. When you need synchronized workers in anspmd
block or communicating job, such as when you close a shared resource, use spmdBarrier after callingspmdSend
andspmdReceive
.
Extended Capabilities
The spmdSend
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray. For more information, see Run MATLAB Functions on a GPU.
If data
is a gpuArray
, data transfer between multiple GPUs in a parallel pool uses fast peer-to-peer communication, including NVLink, if available.
Version History
Introduced in R2022b