Main Content

spmdBroadcast

Send data to all workers in spmd block

Since R2022b

    Description

    B = spmdBroadcast(source,A) sends the data A to every worker in the current spmd block or communicating job.

    When you offload computations using parfor and parfeval, only one worker at a time runs each computation. These workers are independent and do not communicate with each other. If you apply spmdBroadcast to these workers, the function has no effect.

    The worker whose index in the current spmd block is equal to source broadcasts the data. To get the worker index, use the spmdIndex function.

    If source is equal to the worker index, B is equal to A.

    example

    B = spmdBroadcast(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 an index 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 four workers.

    parpool(4);

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

    Create an spmd block. On the worker whose index is equal to 1, create an array. Use spmdBroadcast to send the array to all workers. On each other worker, use spmdBroadcast to receive the array.

    spmd
        source = 1;
        if spmdIndex == source
            A = magic(3);
            B = spmdBroadcast(source, A);
        else
            B = spmdBroadcast(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. Only the worker whose index is 1 contains a definition of A.

    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. This value must be greater than or equal to 1 and less than or equal to the number of workers running the current spmd block. To determine the number of workers running the current spmd block, use the spmdSize function. The spmdSize function returns a value of 1 outside of an spmd block or communicating job.

    Example: 1

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

    Example: magic(3)

    Output Arguments

    collapse all

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

    Tips

    A worker that calls spmdBroadcast might finish execution before other workers. When you need synchronized workers in an spmd block or communicating job, such as when you close a shared resource, use spmdBarrier after calling spmdBroadcast.

    Extended Capabilities

    Version History

    Introduced in R2022b