|
| 1 | +.. SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +.. |
| 3 | +.. SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +.. currentmodule:: cuda.lang |
| 6 | + |
| 7 | +.. _execution-execution-model: |
| 8 | + |
| 9 | +Execution Model |
| 10 | +=============== |
| 11 | + |
| 12 | +Abstract Machine |
| 13 | +---------------- |
| 14 | + |
| 15 | +.. _thread: |
| 16 | +.. _block: |
| 17 | +.. _grid: |
| 18 | + |
| 19 | +A *SIMT kernel* is executed using the following thread hierarchy: |
| 20 | + |
| 21 | +- A *grid* is a 1D, 2D, or 3D collection of thread blocks. |
| 22 | +- A *thread-block cluster* is an optional 1D, 2D, or 3D group of blocks within |
| 23 | + the grid. |
| 24 | +- A *thread block* is a 1D, 2D, or 3D collection of threads. |
| 25 | +- A *warp* groups threads within a block for SIMT execution. |
| 26 | +- A *thread* executes the body of the |kernel|. |
| 27 | + |
| 28 | +A thread can query its position in the thread, block, warp, and cluster |
| 29 | +hierarchy using the functions listed under |
| 30 | +:ref:`SIMT Model <operations-simt-model>`. |
| 31 | + |
| 32 | +Threads in a block can communicate through shared memory and synchronize |
| 33 | +explicitly. Blocks in the same cluster can map each other's shared memory and |
| 34 | +synchronize at cluster scope. Global memory is accessible to all blocks, |
| 35 | +subject to CUDA synchronization and memory-ordering rules. See the |
| 36 | +:ref:`Data Model <data-data-model>` for the corresponding memory spaces. |
| 37 | + |
| 38 | + |
| 39 | +.. _execution-execution-spaces: |
| 40 | + |
| 41 | +Execution Spaces |
| 42 | +---------------- |
| 43 | + |
| 44 | +|cuda lang| code runs on one or more *targets*. A target is an execution |
| 45 | +environment defined by its hardware resources and programming model. |
| 46 | + |
| 47 | +.. _host code: |
| 48 | +.. _SIMT code: |
| 49 | + |
| 50 | +The set of targets where a construct can be used is called its *execution |
| 51 | +space*. |cuda lang| defines two execution spaces: |
| 52 | + |
| 53 | +- *Host code* --- Python code that prepares data and launches kernels on a CPU. |
| 54 | +- *SIMT code* --- code compiled for CUDA SIMT targets and executed by GPU |
| 55 | + threads. |
| 56 | + |
| 57 | +Some constructs span multiple execution spaces. For example, :func:`cdiv` is |
| 58 | +usable in both host code and SIMT code. |
| 59 | + |
| 60 | +A function whose decorator explicitly specifies its execution space is called |
| 61 | +an *annotated function*. |
| 62 | + |
| 63 | + |
| 64 | +.. _execution-simt-functions: |
| 65 | + |
| 66 | +SIMT Functions |
| 67 | +-------------- |
| 68 | + |
| 69 | +A *SIMT function* is a helper function usable from SIMT code. The |
| 70 | +``@cl.function`` decorator explicitly marks such a function. An undecorated |
| 71 | +Python function called from a kernel or SIMT function is compiled recursively |
| 72 | +as SIMT code, so an explicit decorator is not required. |
| 73 | + |
| 74 | + |
| 75 | +.. _execution-simt-kernels: |
| 76 | + |
| 77 | +SIMT Kernels |
| 78 | +------------ |
| 79 | + |
| 80 | +A *SIMT kernel* is an entry point executed by each thread in each block in a |
| 81 | +grid. Kernels cannot be called directly. Use :func:`launch` to queue a kernel |
| 82 | +for execution with explicit grid and block dimensions. |
| 83 | + |
| 84 | + |
| 85 | +Python Subset |
| 86 | +------------- |
| 87 | + |
| 88 | +|SIMT code| supports a subset of the Python language. There is no Python |
| 89 | +runtime within SIMT code. Features such as exceptions and coroutines are not |
| 90 | +supported today. |
| 91 | + |
| 92 | +Object Model & Lifetimes |
| 93 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 94 | + |
| 95 | +Arrays and pointers are views of memory and may alias or update the same |
| 96 | +storage. Their visibility and lifetime depend on their CUDA memory space. |
| 97 | +Because a launch is asynchronous with respect to the host, kernel arguments |
| 98 | +must remain valid until the kernel completes. |
| 99 | + |
| 100 | +Control Flow |
| 101 | +~~~~~~~~~~~~ |
| 102 | + |
| 103 | +Supported Python control flow statements include ``if``, ``while``, and |
| 104 | +range-based ``for`` loops. |
| 105 | + |
| 106 | +Current limitations |
| 107 | +^^^^^^^^^^^^^^^^^^^ |
| 108 | + |
| 109 | +The ``step`` of a ``range`` must be strictly positive. Negative-step ranges |
| 110 | +such as ``range(10, 0, -1)`` are not supported today. |
0 commit comments