IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Mojo function

task_id_for_device

def task_id_for_device(device_id: Int) -> Int

Maps a device ID to a preferred AsyncRT worker thread ID for CPU affinity.

Delegates to the shared C++ implementation in DeviceAffinity.cpp which handles explicit MODULAR_RUNTIME_DEVICE_TASK_CPU_IDS config, NUMA-inferred GPU-to-CPU core mapping, and round-robin fallback.

Intended for use by affinity-aware task launchers such as _create_task and _launch_device_collective. Pass -1 when no affinity hint is needed.

Args:

  • device_id (Int): The integer device ID (e.g. the ordinal of a GPU device).

Returns:

Int: An AsyncRT worker thread index to use as desired_worker_id, or -1 if no affinity mapping is configured.