labindex

Index of this worker

Syntax

id = labindex

Description

id = labindex returns the index of the worker currently executing the function. labindex is assigned to each worker when a job begins execution, and applies only for the duration of that job. The value of labindex spans from 1 to n, where n is the number of workers running the current job, defined by numlabs.

Examples

View labindex in spmd blocks and parfor-loops.

p = parpool('local',2);
spmd
    labindex
end
Lab 1: 
         1
  
Lab 2: 
        2

Using the same two-worker pool, p:

parfor a=1:4
    [a,labindex]
end
ans =
     3     1
ans =
     2     1
ans =
     1     1
ans =1
     4     1

More About

expand all

Tips

In an spmd block, because you have access to all workers individually and control what gets executed on them, each worker has a unique labindex.

However, inside a parfor-loop, labindex always returns a value of 1 on all workers in all iterations.

Was this topic helpful?