|
| 1 | +.. _cccl-runtime-device: |
| 2 | + |
| 3 | +Devices |
| 4 | +======= |
| 5 | + |
| 6 | +``cuda::device_ref`` |
| 7 | +--------------------- |
| 8 | +.. _cccl-runtime-device-device-ref: |
| 9 | + |
| 10 | +``cuda::device_ref`` is a lightweight, non-owning handle to a CUDA device ordinal. |
| 11 | +It offers: |
| 12 | + |
| 13 | +- ``get()``: native device ordinal |
| 14 | +- ``name()``: device name |
| 15 | +- ``init()``: initialize the device context |
| 16 | +- ``peers()``: list peers for which peer access can be enabled |
| 17 | +- ``has_peer_access_to(device_ref)``: query if peer access can be enabled to the given device |
| 18 | +- ``attribute(attr)`` / ``attribute<::cudaDeviceAttr>()``: attribute queries |
| 19 | + |
| 20 | +Availability: CCCL 3.1.0 / CUDA 13.1 |
| 21 | + |
| 22 | +``cuda::devices`` |
| 23 | +------------------ |
| 24 | +.. _cccl-runtime-device-devices: |
| 25 | + |
| 26 | +``cuda::devices`` is a random-access view of all available CUDA devices in form of ``cuda::device_ref`` objects`. It provides indexing, size, and iteration for use |
| 27 | +in range-based loops. |
| 28 | + |
| 29 | +Availability: CCCL 3.1.0 / CUDA 13.1 |
| 30 | + |
| 31 | +Example: |
| 32 | + |
| 33 | +.. code:: cpp |
| 34 | +
|
| 35 | + #include <cuda/devices> |
| 36 | + #include <iostream> |
| 37 | +
|
| 38 | + void print_devices() { |
| 39 | + for (auto& dev : cuda::devices) { |
| 40 | + std::cout << "Device " << dev.get() << ": " << dev.name() << std::endl; |
| 41 | + } |
| 42 | + } |
| 43 | +
|
| 44 | +Device attributes |
| 45 | +----------------- |
| 46 | +.. _cccl-runtime-device-attributes: |
| 47 | + |
| 48 | +``cuda::device_attributes`` provides strongly-typed attribute query objects usable with |
| 49 | +``device_ref::attribute``. Selected examples: |
| 50 | + |
| 51 | +- ``compute_capability`` |
| 52 | +- ``multiprocessor_count`` |
| 53 | +- ``concurrent_managed_access`` |
| 54 | +- ``clock_rate`` |
| 55 | +- ``numa_id`` |
| 56 | + |
| 57 | +Availability: CCCL 3.1.0 / CUDA 13.1 |
| 58 | + |
| 59 | +Example: |
| 60 | + |
| 61 | +.. code:: cpp |
| 62 | +
|
| 63 | + #include <cuda/devices> |
| 64 | +
|
| 65 | + int get_max_blocks_on_device(cuda::device_ref dev) { |
| 66 | + return cuda::device_attributes::multiprocessor_count(dev) * cuda::device_attributes::blocks_per_multiprocessor(dev); |
| 67 | + } |
| 68 | +
|
| 69 | +``cuda::arch_traits`` |
| 70 | +--------------------- |
| 71 | +.. _cccl-runtime-device-arch-traits: |
| 72 | + |
| 73 | +Per-architecture trait accessors providing limits and capabilities common to all devices of an architecture. |
| 74 | +Compared to ``device_attributes``, ``cuda::arch_traits`` provide a compile-time accessible structure that describes common characteristics of all devices of an architecture, while attributes are run-time queries of a single characteristic of a specific device. |
| 75 | + |
| 76 | +- ``cuda::arch_traits<cuda::arch_id::sm_80>()`` (compile-time) or |
| 77 | + ``cuda::arch_traits_for(cuda::arch_id)`` / ``cuda::arch_traits_for(cuda::compute_capability)`` (run-time). |
| 78 | +- Returns a ``cuda::arch_traits_t`` with fields like |
| 79 | + ``max_threads_per_block``, ``max_shared_memory_per_block``, ``cluster_supported`` and other capability flags. |
| 80 | +- Traits for the current architecture can be accessed with ``cuda::device::current_arch_traits()`` |
| 81 | + |
| 82 | +Availability: CCCL 3.1.0 / CUDA 13.1 |
| 83 | + |
| 84 | +Example: |
| 85 | + |
| 86 | +.. code:: cpp |
| 87 | +
|
| 88 | + #include <cuda/devices> |
| 89 | +
|
| 90 | + template <cuda::arch_id Arch> |
| 91 | + __device__ void fn() { |
| 92 | + auto traits = cuda::arch_traits<Arch>(); |
| 93 | + if constexpr (traits.cluster_supported) { |
| 94 | + // cluster specific code |
| 95 | + } |
| 96 | + else { |
| 97 | + // non-cluster code |
| 98 | + } |
| 99 | +
|
| 100 | + } |
| 101 | +
|
| 102 | + __global__ void kernel() { |
| 103 | + fn<cuda::arch_id::sm_90>(); |
| 104 | + } |
0 commit comments