labindex — Return the current SPMD worker's one-based lab index.
labindex is the compatible lab-oriented name for the rank returned by spmdIndex.
Syntax
index = labindexReturns
index- Type
NumericScalar- Description
- One-based index of the current SPMD worker.
Errors
RunMat:labindex:TooManyOutputs- When
- More than one output is requested.
- Message
labindex: too many output arguments
Behavior
Each admitted worker in an spmd region has a stable one-based rank and observes the same gang size. spmdIndex and its labindex alias return the rank; spmdSize and its numlabs alias return the size.
The values are ordinary scalar doubles inside the worker. When assigned in an spmd region, the driver receives one value per worker in a Composite, like other worker-local outputs.
Outside an active spmd gang, the serial context is rank 1 of size 1. This makes helper functions that inspect the context usable in both serial and SPMD execution.
Each name accepts no inputs and produces one scalar double. An unrequested result may be discarded; requesting more than one output is an error.
Execution placement
The values come from the active typed collective context. Reading them does not communicate with another worker, move array data, or invoke an accelerator provider.
Example
Use the compatible lab-rank name
pool = parpool(2);
spmd
rank = labindex;
endExpected output:
rank contains 1 and 2 in worker orderUsing labindex with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how labindex changes the result.
Run a small labindex example, explain the result, then change one input and compare the output.
FAQ
Are worker indices zero-based or one-based?⌄
They are one-based and range from 1 through the active gang size.
Can the rank change during one SPMD region?⌄
No. Admission fixes each worker's rank for the lifetime of that region.
Do these functions synchronize workers?⌄
No. They only read the current worker's collective context.
What do they return outside SPMD?⌄
The serial context reports rank 1 and size 1.
Related Parallel functions
codistributed · codistributed.build · codistributor · codistributor1d · codistributor2dbc · distributed · fetchNext · fetchOutputs · gcat · gcp · getCodistributor · getCurrentJob · getCurrentTask · getCurrentWorker · getLocalPart · globalIndices · gop · gplus · iscodistributed · isComplete · labBarrier · labBroadcast · labProbe · labReceive · labSend · labSendReceive · numlabs · parfeval · parfevalOnAll · parpool · redistribute · spmdBarrier · spmdBroadcast · spmdCat · spmdIndex · spmdPlus · spmdProbe · spmdReceive · spmdReduce · spmdSend · spmdSendReceive · spmdSize
Related guides
Source you can read
Every RunMat function's source is public. Read exactly how labindex is executed, line by line, in Rust.
- View the source for labindex in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.