Main Content

labReceive

(Not recommended) Receive data from another worker in an spmd block

    labReceive is not recommended. Use spmdReceive instead. For more information, see Version History.

    Description

    example

    B = labReceive receives data sent from any worker in the current spmd block or communicating job.

    Tip

    When you offload computations using parfor and parfeval, each computation is run by only one worker at a time. These workers are independent and do not communicate with each other. If you use labReceive on these workers, the function has no effect.

    When a worker runs labReceive, execution of other commands is blocked until the worker receives the data.

    To use labReceive, numlabs must be greater than 1.

    B = labReceive(source) receives data sent from the worker with labindex equal to source.

    B = labReceive('any') receives data from any worker.

    B = labReceive('any',tag) receives data sent with the tag tag from any worker.

    B = labReceive(source,tag) receives data sent with the tag tag from the worker with labindex equal to source.

    [B,source,tag] = labReceive(___) receives data sent from another worker, returns the index source of the source worker, and returns the tag tagwith the data.

    Examples

    collapse all

    This example shows how 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. On the worker with labindex equal to 1, create an array. Use labSend to send the array to the worker with labindex equal to 2.

    Use labReceive to collect the data.

    spmd
        switch labindex
            case 1
                A = magic(3)
                labSend(A,2);
            case 2
                B = labReceive
        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
      

    Input Arguments

    collapse all

    Index of the worker sending data, specified as a positive integer scalar. The value must be less than or equal to the value given by numlabs, the number of workers running the current spmd block or communicating job. When specified, labReceive returns data sent from the worker with labindex equal to source. When not specified, labReceive receives data sent from any worker.

    Example: 1

    Tag attached to data, specified as 0 or a positive integer scalar. When specified, labReceive returns data with sent to the current worker using labSend with the tag argument equal to tag.

    Example: 314159

    Output Arguments

    collapse all

    Index of the worker sending data, specified as a positive integer scalar or the character vector 'any'. The value is equal to labindex on the worker that sent the received data.

    Tag attached to data received by the current worker, specified as a positive integer scalar.

    Extended Capabilities

    Version History

    Introduced before R2006a

    collapse all

    R2022b: labReceive function is not recommended

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

    See Also