labSendReceive

Simultaneously send data to and receive data from another worker

Syntax

dataReceived = labSendReceive(rcvWkrIdx,srcWkrIdx,dataSent)
dataReceived = labSendReceive(rcvWkrIdx,srcWkrIdx,dataSent,tag)

Arguments

dataSent

Data on the sending worker that is sent to the receiving worker; any MATLAB® data type.

dataReceived

Data accepted on the receiving worker.

rcvWkrIdx

labindex of the receiving worker to which data is sent.

srcWkrIdx

labindex of the source worker from which data is sent.

tag

Nonnegative integer to identify data.

Description

dataReceived = labSendReceive(rcvWkrIdx,srcWkrIdx,dataSent) sends dataSent to the worker whose labindex is rcvWkrIdx, and receives dataReceived from the worker whose labindex is srcWkrIdx. The values for arguments rcvWkrIdx and srcWkrIdx must be scalars. This function is conceptually equivalent to the following sequence of calls:

labSend(dataSent,rcvWkrIdx);
dataReceived = labReceive(srcWkrIdx);

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 rcvWkrIdx is an empty array, labSendReceive does not send data, but only receives. If srcWkrIdx is an empty array, labSendReceive does not receive data, but only sends.

dataReceived = labSendReceive(rcvWkrIdx,srcWkrIdx,dataSent,tag) uses the specified tag for the communication. tag can be any integer from 0 to 32767.

Examples

Create a unique set of data on each worker, and transfer each worker's data one worker to the right (to the next higher labindex).

First use the magic function to create a unique value for the variant array mydata on each worker.

mydata = magic(labindex)
Lab 1: 
  mydata =
       1
Lab 2:
  mydata =
       1     3
       4     2
Lab 3:
  mydata =
       8     1     6
       3     5     7
       4     9     2

Define the worker on either side, so that each worker will receive data from the worker on its "left," while sending data to the worker on its "right," cycling data from the end worker back to the beginning worker.

rcvWkrIdx = mod(labindex, numlabs) + 1; % one worker to the right
srcWkrIdx = mod(labindex - 2, numlabs) + 1; % one worker to the left

Transfer the data, sending each worker's mydata into the next worker's otherdata variable, wrapping the third worker's data back to the first worker.

otherdata = labSendReceive(rcvWkrIdx,srcWkrIdx,mydata)
Lab 1:
  otherdata =
       8     1     6
       3     5     7
       4     9     2
Lab 2:
  otherdata =
       1
Lab 3: 
  otherdata =
       1     3
       4     2

Transfer data to the next worker without wrapping data from the last worker to the first worker.

if labindex < numlabs; rcvWkrIdx = labindex + 1; else rcvWkrIdx = []; end;
if labindex > 1; srcWkrIdx = labindex - 1; else srcWkrIdx = []; end;
otherdata = labSendReceive(rcvWkrIdx,srcWkrIdx,mydata)
Lab 1:
  otherdata =
       []
Lab 2:
  otherdata =
       1
Lab 3:
  otherdata =
       1     3
       4     2
Was this topic helpful?