Main Content

numlabs

Number of workers running current spmd block

    Description

    example

    n = numlabs returns the number of workers running the current spmd block or communicating job. This value is the maximum value that can be used with labSend and labReceive.

    Examples

    collapse all

    This example shows how to run numlabs inside and outside an spmd block.

    Create a parallel pool with 4 workers.

    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.

    Run numlabs 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(numlabs)
    end
    disp(numlabs)
    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, specified as an integer. In an spmd block, numlabs returns the number of workers running the spmd block. Outside an spmd block, numlabs 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 numlabs, the returned value is N.

    Version History

    Introduced before R2006a