Main Content

spmdSize

Number of workers running current spmd block

    Description

    example

    n = spmdSize returns the number of workers running the current spmd block or communicating job. This value is the maximum value that you can use as the destination input to spmdSend and source input to spmdReceive.

    Examples

    collapse all

    This example shows how to run spmdSize inside and outside 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.

    Run spmdSize inside and outside an spmd block. In the block, the returned value is equal to the number of workers. Outside the block, the returned value is 1.

    spmd
        disp(spmdSize)
    end
    disp(spmdSize)
    Worker 1: 
           4
      
    Worker 2: 
           4
      
    Worker 3: 
           4
      
    Worker 4: 
           4
      
         1

    Output Arguments

    collapse all

    Number of workers running the current spmd block or communicating job, returned as a positive integer. In an spmd block, n is the number of workers running the spmd block. Outside an spmd block, n is 1.

    When you create a communicating job using createCommunicatingJob, you can run a function or script on n workers. When one of the workers runs spmdSize, the returned value is n.

    Extended Capabilities

    Version History

    Introduced in R2022b