..
    MIT License

    Copyright (c) 2024-2026 Advanced Micro Devices, Inc.

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.

.. meta::
  :description: API reference for cuda::atomic, an extended form of std::atomic with thread scope support for system-wide, device-wide, and block-wide atomic operations in libhipcxx.
  :keywords: libhipcxx, ROCm, HIP, C++, atomic, thread scope, fetch_min, fetch_max, atomic_thread_fence, memory order

.. _libcudacxx-extended-api-synchronization-atomic:

``cuda::atomic``
================

This page documents ``cuda::atomic``, an extended form of ``std::atomic`` with thread scope support for system-wide, device-wide, and block-wide atomic operations.

.. toctree::
   :hidden:
   :maxdepth: 1

   atomic/atomic_thread_fence
   atomic/fetch_max
   atomic/fetch_min

Defined in header ``<cuda/atomic>``:

.. code:: cpp

   template <typename T, cuda::thread_scope Scope = cuda::thread_scope_system>
   class cuda::atomic;

The class template ``cuda::atomic`` is an extended form of `cuda::std::atomic <https://en.cppreference.com/w/cpp/atomic/atomic>`_
that takes an additional :ref:`cuda::thread_scope <libcudacxx-extended-api-memory-model-thread-scopes>` argument,
defaulted to ``cuda::std::thread_scope_system``.

It has the same interface and semantics as `cuda::std::atomic <https://en.cppreference.com/w/cpp/atomic/atomic>`_,
with the following additional operations.

.. list-table::
   :widths: 25 75
   :header-rows: 0

   * - :ref:`cuda::atomic_thread_fence <libcudacxx-extended-api-synchronization-atomic-atomic-thread-fence>`
     - Memory order and scope dependent fence synchronization primitive.
   * - :ref:`cuda::atomic::fetch_min <libcudacxx-extended-api-synchronization-atomic-atomic-fetch-min>`
     - Atomically find the minimum of the stored value and a provided value.
   * - :ref:`cuda::atomic::fetch_max <libcudacxx-extended-api-synchronization-atomic-atomic-fetch-max>`
     - Atomically find the maximum of the stored value and a provided value.

Concurrency Restrictions
------------------------

An object of type ``cuda::atomic`` or `cuda::std::atomic <https://en.cppreference.com/w/cpp/atomic/atomic>`_
shall not be accessed concurrently by CPU and GPU threads unless:

- it is in unified memory and the `concurrentManagedAccess property <https://rocm.docs.amd.com/projects/HIP/en/latest/doxygen/html/group___global_defs.html#structhip_device_prop__t>`_
  is 1, or
- it is in CPU memory and the `hostNativeAtomicSupported property <https://rocm.docs.amd.com/projects/HIP/en/latest/doxygen/html/group___global_defs.html#structhip_device_prop__t>`_
  is 1.

Note, for objects of scopes other than ``cuda::thread_scope_system`` this is a data-race, and therefore also
prohibited regardless of memory characteristics.

..
   Under CUDA Compute Capability 6 (Pascal), an object of type ``atomic`` may not be used:

   - with automatic storage duration, or
   - if ``is_always_lock_free()`` is ``false``.

   Under CUDA Compute Capability prior to 6 (Pascal), objects of type ``cuda::atomic`` or
   `cuda::std::atomic <https://en.cppreference.com/w/cpp/atomic/atomic>`_ may not be used.

Implementation-Defined Behavior
-------------------------------

For each type ``T`` and :ref:`cuda::thread_scope <libcudacxx-extended-api-memory-model-thread-scopes>` ``S``,
the value of ``cuda::atomic<T, S>::is_always_lock_free()`` is as follows:

.. list-table::
   :widths: 25 25 50
   :header-rows: 0

   * - Type ``T``
     - :ref:`cuda::thread_scope <libcudacxx-extended-api-memory-model-thread-scopes>` ``S``
     - ``cuda::atomic<T, S>::is_always_lock_free()``
   * - Any valid type
     - Any thread scope
     - ``sizeof(T) <= 8``

Example
-------

.. code:: cpp

   #include <cuda/atomic>

   __global__ void example_kernel() {
     // This atomic is suitable for all threads in the system.
     cuda::atomic<int, cuda::thread_scope_system> a;

     // This atomic has the same type as the previous one (`a`).
     cuda::atomic<int> b;

     // This atomic is suitable for all threads on the current processor (e.g. GPU).
     cuda::atomic<int, cuda::thread_scope_device> c;

     // This atomic is suitable for threads in the same thread block.
     cuda::atomic<int, cuda::thread_scope_block> d;
   }

..
   `See it on Godbolt <https://godbolt.org/z/avo3Evbee>`_
