Main Content


(Not recommended) Send data to another worker in an spmd block

    labSend is not recommended. Use spmdSend instead. For information on updating your code, see Version History.



    labSend(A,destination) sends data A from the current worker in an spmd block or communicating job to another worker.


    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 labSend on these workers, the function has no effect.

    The function labSend sends data to the worker or workers specified by destination.

    To use labSend, numlabs must be greater than 1.

    labSend(___,tag) sends data with the tag tag. When you use labSend 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.


    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.


    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.

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

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

    Example: magic(3)

    Indices of the worker or workers receiving data, specified as a positive integer scalar or vector. The values must be less than or equal to the value given by numlabs, the number of workers running the current spmd block or communicating job.

    Example: [2 3 4]

    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 specified as tag.

    Example: 314159


    A worker that sends data using labSend may return before the receiving worker receives the data. When you need synchronized workers in an spmd block or communicating job, such as when you close a shared resource, use labBarrier after calling labSend and labReceive.

    Extended Capabilities

    Version History

    Introduced before R2006a

    expand all

    See Also