Main Content

labBroadcast

(Not recommended) Send data to all workers in an spmd block

    labBroadcast is not recommended. Use spmdBroadcast instead. For more information, see Version History.

    Description

    B = labBroadcast(source,A) sends the data A to every 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 labBroadcast on these workers, the function has no effect.

    The data is broadcast from the worker with labindex equal to source.

    If source is equal to labindex, B is equal to A. numlabs is equal to 1 outside of an spmd block or communicating job.

    example

    B = labBroadcast(source) receives the data B on each worker running the current spmd block or communicating job. The data B is equal to the data A sent from the worker with labindex equal to source.

    example

    Examples

    collapse all

    This example shows how to broadcast an array from one worker to other workers in an spmd block.

    Create a parallel pool with 4 workers. By default, labBroadcast 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 labBroadcast to send the array to all workers. On each other worker, use labBroadcast to receive the array.

    spmd
        source = 1;
        if labindex == source
            A = magic(3);
            B = labBroadcast(source, A);
        else
            B = labBroadcast(source);
        end
        B
    end
    Worker 1: 
      
      B =
      
           8     1     6
           3     5     7
           4     9     2
      
    Worker 2: 
      
      B =
      
           8     1     6
           3     5     7
           4     9     2
      
    Worker 3: 
      
      B =
      
           8     1     6
           3     5     7
           4     9     2
      
    Worker 4: 
      
      B =
      
           8     1     6
           3     5     7
           4     9     2
      

    On the client, inspect the Composite A. The array A is only defined on the worker with labindex equal to 1.

    A
     
    A =
     
       Worker 1: class = double, size = [3  3]
       Worker 2: No data
       Worker 3: No data
       Worker 4: No data
     

    Input Arguments

    collapse all

    Index of the worker that is sending data, specified as a positive integer scalar. This value must be greater than or equal to 1 and less than or equal to numlabs.

    Example: 1

    Data sent from the worker with labindex equal to source, specified as any MATLAB variable that can be saved and loaded.

    Example: magic(3)

    Output Arguments

    collapse all

    Data received on the worker, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.

    Tips

    A worker that calls labBroadcast may return before other workers. When you need synchronized workers in an spmd block or communicating job, such as when you close a shared resource, use labBarrier after calling labBroadcast.

    Extended Capabilities

    Version History

    Introduced before R2006a

    collapse all

    R2022b: labBroadcast function is not recommended

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