Main Content

labindex

(Not recommended) Index of the current worker in an spmd block

labindex is not recommended. Use spmdIndex instead. For more information, see Version History.

Description

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.

example

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

collapse all

R2022b: labindex function is not recommended

To indicate their intended use within spmd blocks, labindex is renamed to spmdIndex. labindex will continue to work but is no longer recommended. To update your code, replace any instance of labindex with spmdIndex. There are no plans to remove labindex.

See Also