Index of current worker in
returns the index of the worker
currently executing the function in an
id = spmdIndex
spmd block or a communicating job. When workers run an
block or a communicating job begins execution, a unique index is assigned to each worker.
The value of
spmdIndex is an integer between
the number of workers running in the current
spmd block or
communicating job. To get the number of workers running the current spmd block, use the
A worker gets the same value
id inside every
spmd block for the duration of a parallel pool.
Get Worker Index with
View the indices of workers in an
p = parpool('Threads',2); spmd spmdIndex end
Worker 1: 1 Worker 2: 2
View the indices of workers in a
parpool loop run in the same two worker pool,
parfor a=1:4 [a,spmdIndex] end
ans = 3 1 ans = 2 1 ans = 1 1 ans = 4 1
id — Index of current worker
Index of current worker, specified as a positive integer. The value of this input
must be less than or equal to the output of the
in the current
spmd block or communicating job.
spmd block, because you have access to all workers individually
and control what the workers execute, each worker has a unique index.
However, inside a
returns a value of 1 on all workers in all iterations.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Introduced in R2022b