| Parallel Computing Toolbox™ | ![]() |
Simultaneously send data to and receive data from another lab
received = labSendReceive(labTo, labFrom, data)
received = labSendReceive(labTo, labFrom, data, tag)
data | Data on the sending lab that is sent to the receiving lab; any MATLAB® data type. |
received | Data accepted on the receiving lab. |
labTo | labindex of the lab to which data is sent. |
labFrom | labindex of the lab from which data is received. |
tag | Nonnegative integer to identify data. |
received = labSendReceive(labTo, labFrom, data) sends data to the lab whose labindex is labTo, and receives received from the lab whose labindex is labFrom. labTo and labFrom must be scalars. This function is conceptually equivalent to the following sequence of calls:
labSend(data, labTo); received = labReceive(labFrom);
with the important exception that both the sending and receiving of data happens concurrently. This can eliminate deadlocks that might otherwise occur if the equivalent call to labSend would block.
If labTo is an empty array, labSendReceive does not send data, but only receives. If labFrom is an empty array, labSendReceive does not receive data, but only sends.
received = labSendReceive(labTo, labFrom, data, tag) uses the specified tag for the communication. tag can be any integer from 0 to 32767.
Create a unique set of data on each lab, and transfer each lab's data one lab to the right (to the next higher labindex).
First use magic to create a unique value for the variant array mydata on each lab.
mydata = magic(labindex) 1: mydata = 1: 1 2: mydata = 2: 1 3 2: 4 2 3: mydata = 3: 8 1 6 3: 3 5 7 3: 4 9 2
Define the lab on either side, so that each lab will receive data from the lab on the "left" while sending data to the lab on the "right," cycling data from the end lab back to the beginning lab.
labTo = mod(labindex, numlabs) + 1; % one lab to the right labFrom = mod(labindex - 2, numlabs) + 1; % one lab to the left
Transfer the data, sending each lab's mydata into the next lab's otherdata variable, wrapping the third lab's data back to the first lab.
otherdata = labSendReceive(labTo, labFrom, mydata) 1: otherdata = 1: 8 1 6 1: 3 5 7 1: 4 9 2 2: otherdata = 2: 1 3: otherdata = 3: 1 3 3: 4 2
Transfer data to the next lab without wrapping data from the last lab to the first lab.
if labindex < numlabs; labTo = labindex + 1; else labTo = []; end; if labindex > 1; labFrom = labindex - 1; else labFrom = []; end; otherdata = labSendReceive(labTo, labFrom, mydata) 1: otherdata = 1: [] 2: otherdata = 2: 1 3: otherdata = 3: 1 3 3: 4 2
labBarrier, labindex, labProbe, labReceive, labSend numlabs
![]() | labSend | length | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |