B = spmdSendReceive( destination , source , A ) sends data A from the current worker in an spmd block or communicating job to the destination , and 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 B sent to the current worker from the worker whose index is equal to source .
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);
B = spmdSendReceive( ___ , tag ) sends and receives data with the tag 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.
This example shows how to use spmdSendReceive to send data between workers in an spmd block or communicating job.
Create a parallel pool with four workers.
parpool(4);
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.
Create an spmd block. Use mod and spmdSendReceive to send and receive data in a chain of workers.
Use 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.
spmd A = 2*spmdIndex; destination = 1 + mod((spmdIndex+1)-1, spmdSize); source = 1 + mod((spmdIndex-1)-1, spmdSize); A = spmdSendReceive(destination,source,A) end
Worker 1: A = 12 Worker 2: A = 2 Worker 3: A = 4 Worker 4: A = 6 Worker 5: A = 8 Worker 6: A = 10