Main Content

labindex

Index of the current worker in an spmd block

Description

example

id = labindex returns the index of the worker currently executing the function in an spmd block. When workers run an spmd block or a communicating job begins execution, a unique index is assigned to each worker. The value of labindex is an integer between 1 and numlabs.

A worker gets the same id inside every spmd block for the duration of a given parallel pool.

Examples

collapse all

View labindex in spmd blocks and parfor-loops.

p = parpool('local',2);
spmd
    labindex
end
Worker 1: 
         1
  
Worker 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 =
     4     1

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.

Version History

Introduced before R2006a