Main Content

labSend

Send data to another worker in an spmd block

    Description

    example

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

    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 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.

    example

    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.

    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
      

    This example shows how to tag and 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 two arrays A1 and A2. Before and between creating arrays, use pause again to simulate some work. Then, use labSend to send the matrices to the worker with labindex equal to 2.

    Tag each matrix with an integer. Tags have many uses, including the following.

    • Use tags to save memory by only loading arrays on workers when you need the data.

    • Use tags to create code that doesn't depend on the index of the sending worker.

    On the worker with labindex equal to 2, use labReceive to collect the data.

    tic
    spmd
        switch labindex
            case 1
                pause(5);
                A1 = magic(1)
                pause(5);
                A2 = magic(2)
                labSend(A1,2,1);
                labSend(A2,2,2);
            case 2
                B1 = labReceive('any',1)
                B2 = labReceive('any',2)
        end
    end
    toc
    Worker 1: 
      
      A1 =
      
           1
      
      
      A2 =
      
           1     3
           4     2
      
    Worker 2: 
    
      B1 =
      
           1
      
      B2 =
      
           1     3
           4     2
      
      
      
    Elapsed time is 10.061523 seconds.

    In some cases, you can improve the performance of your code by moving some work from one worker to another. Move some work from the worker with labindex equal to 1 to the worker with labindex equal to 3. When you use tags, you can easily move calculations from one worker to another without updating code on the receiving worker.

    tic
    spmd
        switch labindex
            case 1
                pause(5);
                A1 = magic(1)
                labSend(A1,2,1);
            case 2
                B2 = labReceive('any',2)
                B1 = labReceive('any',1)
            case 3
                pause(5);
                A2 = magic(2)
                labSend(A2,2,2);
        end
    end
    toc
    Worker 1: 
      
      A1 =
      
           1
      
    Worker 2: 
      
      B2 =
      
           1     3
           4     2
      
      
      B1 =
      
           1
      
    Worker 3: 
      
      A2 =
      
           1     3
           4     2
      
    Elapsed time is 5.117787 seconds.

    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

    Tips

    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