Main Content

labSendReceive

(Not recommended) Simultaneously send and receive data on a worker in an spmd block

    labSendReceive is not recommended. Use spmdSendReceive instead. For more information, see Version History.

    Description

    B = labSendReceive(destination,source,A) sends data from the current worker in an spmd block or communicating job to a destination, and receives data from a source. The array A is sent from the current worker to the worker with labindex is equal to destination. The current worker receives data from the worker with labindex equal to source.

    When a worker runs labSendReceive(destination,source,A) the computation is equivalent to the worker running the following code, but the send and receive can proceed simultaneously:

    labSend(A,destination);
    B = labReceive(source);
    

    example

    B = labSendReceive(___,tag) sends and receives data with the tag tag. When you use labSendReceive 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

    collapse all

    This example shows how to use labSendReceive to send data between workers in an spmd block or communicating job.

    Create a parallel pool with 4 workers. By default, spmd is supported on all process-backed pools.

    parpool(4);

    When you execute an spmd block after creating a parallel pool, by default all available workers in the pool will run the code inside the spmd block.

    Create an spmd block. You can use mod and labSendReceive to send and receive data in a chain of workers. Use labSendReceive to send data to the worker with labindex one higher than the current worker's index, mod numlabs. Receive data from the worker with labindex one lower than the current worker's index, mod numlabs. When you use modulo division, the worker with labindex equal to 1 receives from the worker with labindex equal to numlabs. The worker with labindex equal to 1 receives from the worker with labindex equal to numlabs.

    spmd
        A = 2*labindex;
        
        destination = 1 + mod((labindex + 1) - 1, numlabs);
        source = 1 + mod((labindex - 1) - 1, numlabs);
        
        A = labSendReceive(source, destination, A)
    end
    Worker 1: 
      
      A =
      
           4
      
    Worker 2: 
      
      A =
      
           6
      
    Worker 3: 
      
      A =
      
           8
      
    Worker 4: 
      
      A =
      
           2
      

    Input Arguments

    collapse all

    Index of the destination worker, specified as a positive integer scalar or empty array. The destination worker receives data from the current worker. destination must be less than or equal to the value given by numlabs, the number of workers running the current spmd block or communicating job.

    If this argument is empty, the function does not send any data.

    Example: 2

    Index of the source worker, specified as a positive integer scalar or empty array. The current worker waits until it receives data from the source worker. destination must be less than the value given by numlabs, the number of workers running the current spmd block or communicating job.

    If this argument is empty, the function does not receive any data.

    Example: 1

    Data to send from the current worker, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.

    Example: magic(3)

    Tag attached to data, specified as 0 or a positive integer scalar. When specified, labSendReceive sends data with the tag tag from the current worker, and returns data sent with the tag tag to the current worker.

    Example: 314159

    Version History

    Introduced in R2006b

    collapse all

    R2022b: labSendReceive function is not recommended

    To indicate their intended use within spmd blocks, labSendReceive is renamed to spmdSendReceive. labSendReceive will continue to work but is no longer recommended. To update your code, replace any instance of labSendReceive with spmdSendReceive. There are no plans to remove labSendReceive.