SWDEV-514685 - Update documentation 2025-02-11 (#25)

Co-authored-by: Istvan Kiss <neon60@gmail.com>

[ROCm/hip commit: 69becfc7a8]
Dieser Commit ist enthalten in:
Kiss, Istvan
2025-03-19 22:04:47 +01:00
committet von GitHub
Ursprung bfd7b0dd3f
Commit e60263a55a
41 geänderte Dateien mit 3318 neuen und 1879 gelöschten Zeilen
@@ -0,0 +1,176 @@
.. meta::
:description: Maps CUDA API syntax to HIP API syntax with an example
:keywords: AMD, ROCm, HIP, CUDA, syntax, HIP syntax
********************************************************************************
CUDA to HIP API Function Comparison
********************************************************************************
This page introduces key syntax differences between CUDA and HIP APIs with a focused code
example and comparison table. For a complete list of mappings, visit :ref:`HIPIFY <HIPIFY:index>`.
The following CUDA code example illustrates several CUDA API syntaxes.
.. code-block:: cpp
#include <iostream>
#include <vector>
#include <cuda_runtime.h>
__global__ void block_reduction(const float* input, float* output, int num_elements)
{
extern __shared__ float s_data[];
int tid = threadIdx.x;
int global_id = blockDim.x * blockIdx.x + tid;
if (global_id < num_elements)
{
s_data[tid] = input[global_id];
}
else
{
s_data[tid] = 0.0f;
}
__syncthreads();
for (int stride = blockDim.x / 2; stride > 0; stride >>= 1)
{
if (tid < stride)
{
s_data[tid] += s_data[tid + stride];
}
__syncthreads();
}
if (tid == 0)
{
output[blockIdx.x] = s_data[0];
}
}
int main()
{
int threads = 256;
const int num_elements = 50000;
std::vector<float> h_a(num_elements);
std::vector<float> h_b((num_elements + threads - 1) / threads);
for (int i = 0; i < num_elements; ++i)
{
h_a[i] = rand() / static_cast<float>(RAND_MAX);
}
float *d_a, *d_b;
cudaMalloc(&d_a, h_a.size() * sizeof(float));
cudaMalloc(&d_b, h_b.size() * sizeof(float));
cudaStream_t stream;
cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
cudaEvent_t start_event, stop_event;
cudaEventCreate(&start_event);
cudaEventCreate(&stop_event);
cudaMemcpyAsync(d_a, h_a.data(), h_a.size() * sizeof(float), cudaMemcpyHostToDevice, stream);
cudaEventRecord(start_event, stream);
int blocks = (num_elements + threads - 1) / threads;
block_reduction<<<blocks, threads, threads * sizeof(float), stream>>>(d_a, d_b, num_elements);
cudaMemcpyAsync(h_b.data(), d_b, h_b.size() * sizeof(float), cudaMemcpyDeviceToHost, stream);
cudaEventRecord(stop_event, stream);
cudaEventSynchronize(stop_event);
cudaEventElapsedTime(&milliseconds, start_event, stop_event);
std::cout << "Kernel execution time: " << milliseconds << " ms\n";
cudaFree(d_a);
cudaFree(d_b);
cudaEventDestroy(start_event);
cudaEventDestroy(stop_event);
cudaStreamDestroy(stream);
return 0;
}
The following table maps CUDA API functions to corresponding HIP API functions, as demonstrated in the
preceding code examples.
.. list-table::
:header-rows: 1
:name: syntax-mapping-table
*
- CUDA
- HIP
*
- ``#include <cuda_runtime.h>``
- ``#include <hip/hip_runtime.h>``
*
- ``cudaError_t``
- ``hipError_t``
*
- ``cudaEvent_t``
- ``hipEvent_t``
*
- ``cudaStream_t``
- ``hipStream_t``
*
- ``cudaMalloc``
- ``hipMalloc``
*
- ``cudaStreamCreateWithFlags``
- ``hipStreamCreateWithFlags``
*
- ``cudaStreamNonBlocking``
- ``hipStreamNonBlocking``
*
- ``cudaEventCreate``
- ``hipEventCreate``
*
- ``cudaMemcpyAsync``
- ``hipMemcpyAsync``
*
- ``cudaMemcpyHostToDevice``
- ``hipMemcpyHostToDevice``
*
- ``cudaEventRecord``
- ``hipEventRecord``
*
- ``cudaEventSynchronize``
- ``hipEventSynchronize``
*
- ``cudaEventElapsedTime``
- ``hipEventElapsedTime``
*
- ``cudaFree``
- ``hipFree``
*
- ``cudaEventDestroy``
- ``hipEventDestroy``
*
- ``cudaStreamDestroy``
- ``hipStreamDestroy``
In summary, this comparison highlights the primary differences between CUDA and HIP APIs.
Datei-Diff unterdrückt, da er zu groß ist Diff laden
@@ -1,171 +0,0 @@
.. meta::
:description: This chapter describes the C++ support of the HIP ecosystem
ROCm software.
:keywords: AMD, ROCm, HIP, C++
*******************************************************************************
C++ language support
*******************************************************************************
The ROCm platform enables the power of combined C++ and HIP (Heterogeneous-computing
Interface for Portability) code. This code is compiled with a ``clang`` or ``clang++``
compiler. The official compilers support the HIP platform, or you can use the
``amdclang`` or ``amdclang++`` included in the ROCm installation, which are a wrapper for
the official versions.
The source code is compiled according to the ``C++03``, ``C++11``, ``C++14``, ``C++17``,
and ``C++20`` standards, along with HIP-specific extensions, but is subject to
restrictions. The key restriction is the reduced support of standard library in device
code. This is due to the fact that by default a function is considered to run on host,
except for ``constexpr`` functions, which can run on host and device as well.
.. _language_modern_cpp_support:
Modern C++ support
===============================================================================
C++ is considered a modern programming language as of C++11. This section describes how
HIP supports these new C++ features.
C++11 support
-------------------------------------------------------------------------------
The C++11 standard introduced many new features. These features are supported in HIP host
code, with some notable omissions on the device side. The rule of thumb here is that
``constexpr`` functions work on device, the rest doesn't. This means that some important
functionality like ``std::function`` is missing on the device, but unfortunately the
standard library wasn't designed with HIP in mind, which means that the support is in a
state of "works as-is".
Certain features have restrictions and clarifications. For example, any functions using
the ``constexpr`` qualifier or the new ``initializer lists``, ``std::move`` or
``std::forward`` features are implicitly considered to have the ``__host__`` and
``__device__`` execution space specifier. Also, ``constexpr`` variables that are static
members or namespace scoped can be used from both host and device, but only for read
access. Dereferencing a static ``constexpr`` outside its specified execution space causes
an error.
Lambdas are supported, but there are some extensions and restrictions on their usage. For
more information, see the `Extended lambdas`_ section below.
C++14 support
-------------------------------------------------------------------------------
The C++14 language features are supported.
C++17 support
-------------------------------------------------------------------------------
All C++17 language features are supported.
C++20 support
-------------------------------------------------------------------------------
All C++20 language features are supported, but extensions and restrictions apply. C++20
introduced coroutines and modules, which fundamentally changed how programs are written.
HIP doesn't support these features. However, ``consteval`` functions can be called from
host and device, even if specified for host use only.
The three-way comparison operator (spaceship operator ``<=>``) works with host and device
code.
.. _language_restrictions:
Extensions and restrictions
===============================================================================
In addition to the deviations from the standard, there are some general extensions and
restrictions to consider.
Global functions
-------------------------------------------------------------------------------
Functions that serve as an entry point for device execution are called kernels and are
specified with the ``__global__`` qualifier. To call a kernel function, use the triple
chevron operator: ``<<< >>>``. Kernel functions must have a ``void`` return type. These
functions can't:
* have a ``constexpr`` specifier
* have a parameter of type ``std::initializer_list`` or ``va_list``
* use an rvalue reference as a parameter.
* use parameters having different sizes in host and device code, e.g. long double arguments, or structs containing long double members.
* use struct-type arguments which have different layout in host and device code.
Kernels can have variadic template parameters, but only one parameter pack, which must be
the last item in the template parameter list.
Device space memory specifiers
-------------------------------------------------------------------------------
HIP includes device space memory specifiers to indicate whether a variable is allocated
in host or device memory and how its memory should be allocated. HIP supports the
``__device__``, ``__shared__``, ``__managed__``, and ``__constant__`` specifiers.
The ``__device__`` and ``__constant__`` specifiers define global variables, which are
allocated within global memory on the HIP devices. The only difference is that
``__constant__`` variables can't be changed after allocation. The ``__shared__``
specifier allocates the variable within shared memory, which is available for all threads
in a block.
The ``__managed__`` variable specifier creates global variables that are initially
undefined and unaddressed within the global symbol table. The HIP runtime allocates
managed memory and defines the symbol when it loads the device binary. A managed variable
can be accessed in both device and host code.
It's important to know where a variable is stored because it is only available from
certain locations. Generally, variables allocated in the host memory are not accessible
from the device code, while variables allocated in the device memory are not directly
accessible from the host code. Dereferencing a pointer to device memory on the host
results in a segmentation fault. Accessing device variables in host code should be done
through kernel execution or HIP functions like ``hipMemCpyToSymbol``.
Exception handling
-------------------------------------------------------------------------------
An important difference between the host and device code is exception handling. In device
code, this control flow isn't available due to the hardware architecture. The device
code must use return codes to handle errors.
Kernel parameters
-------------------------------------------------------------------------------
There are some restrictions on kernel function parameters. They cannot be passed by
reference, because these functions are called from the host but run on the device. Also,
a variable number of arguments is not allowed.
Classes
-------------------------------------------------------------------------------
Classes work on both the host and device side, but there are some constraints. The
``static`` member functions can't be ``__global__``. ``Virtual`` member functions work,
but a ``virtual`` function must not be called from the host if the parent object was
created on the device, or the other way around, because this behavior is undefined.
Another minor restriction is that ``__device__`` variables, that are global scoped must
have trivial constructors.
Polymorphic function wrappers
-------------------------------------------------------------------------------
HIP doesn't support the polymorphic function wrapper ``std::function``, which was
introduced in C++11.
Extended lambdas
-------------------------------------------------------------------------------
HIP supports Lambdas, which by default work as expected.
Lambdas have implicit host device attributes. This means that they can be executed by
both host and device code, and works the way you would expect. To make a lambda callable
only by host or device code, users can add ``__host__`` or ``__device__`` attribute. The
only restriction is that host variables can only be accessed through copy on the device.
Accessing through reference will cause undefined behavior.
Inline namespaces
-------------------------------------------------------------------------------
Inline namespaces are supported, but with a few exceptions. The following entities can't
be declared in namespace scope within an inline unnamed namespace:
* ``__managed__``, ``__device__``, ``__shared__`` and ``__constant__`` variables
* ``__global__`` function and function templates
* variables with surface or texture type
+14 -148
Datei anzeigen
@@ -12,162 +12,38 @@ on AMD platform, which are grouped by functionality.
GPU isolation variables
================================================================================
The GPU isolation environment variables in HIP are collected in the next table.
The GPU isolation environment variables in HIP are collected in the following table.
For more information, check :doc:`GPU isolation page <rocm:conceptual/gpu-isolation>`.
.. list-table::
:header-rows: 1
:widths: 70,30
* - **Environment variable**
- **Value**
* - | ``ROCR_VISIBLE_DEVICES``
| A list of device indices or UUIDs that will be exposed to applications.
- Example: ``0,GPU-DEADBEEFDEADBEEF``
* - | ``GPU_DEVICE_ORDINAL``
| Devices indices exposed to OpenCL and HIP applications.
- Example: ``0,2``
* - | ``HIP_VISIBLE_DEVICES`` or ``CUDA_VISIBLE_DEVICES``
| Device indices exposed to HIP applications.
- Example: ``0,2``
.. include-table:: data/env_variables_hip.rst
:table: hip-env-isolation
Profiling variables
================================================================================
The profiling environment variables in HIP are collected in the next table. For
The profiling environment variables in HIP are collected in the following table. For
more information, check :doc:`setting the number of CUs page <rocm:how-to/setting-cus>`.
.. list-table::
:header-rows: 1
:widths: 70,30
* - **Environment variable**
- **Value**
* - | ``HSA_CU_MASK``
| Sets the mask on a lower level of queue creation in the driver,
| this mask will also be set for queues being profiled.
- Example: ``1:0-8``
* - | ``ROC_GLOBAL_CU_MASK``
| Sets the mask on queues created by the HIP or the OpenCL runtimes,
| this mask will also be set for queues being profiled.
- Example: ``0xf``, enables only 4 CUs
* - | ``HIP_FORCE_QUEUE_PROFILING``
| Used to run the app as if it were run in rocprof. Forces command queue
| profiling on by default.
- | 0: Disable
| 1: Enable
.. include-table:: data/env_variables_hip.rst
:table: hip-env-prof
Debug variables
================================================================================
The debugging environment variables in HIP are collected in the next table. For
The debugging environment variables in HIP are collected in the following table. For
more information, check :ref:`debugging_with_hip`.
.. include:: ../how-to/debugging_env.rst
.. include-table:: data/env_variables_hip.rst
:table: hip-env-debug
Memory management related variables
================================================================================
The memory management related environment variables in HIP are collected in the
next table.
following table.
.. list-table::
:header-rows: 1
:widths: 35,14,51
* - **Environment variable**
- **Default value**
- **Value**
* - | ``HIP_HIDDEN_FREE_MEM``
| Amount of memory to hide from the free memory reported by hipMemGetInfo.
- ``0``
- | 0: Disable
| Unit: megabyte (MB)
* - | ``HIP_HOST_COHERENT``
| Specifies if the memory is coherent between the host and GPU in ``hipHostMalloc``.
- ``0``
- | 0: Memory is not coherent.
| 1: Memory is coherent.
| Environment variable has effect, if the following conditions are statisfied:
| - One of the ``hipHostMallocDefault``, ``hipHostMallocPortable``, ``hipHostMallocWriteCombined`` or ``hipHostMallocNumaUser`` flag set to 1.
| - ``hipHostMallocCoherent``, ``hipHostMallocNonCoherent`` and ``hipHostMallocMapped`` flags set to 0.
* - | ``HIP_INITIAL_DM_SIZE``
| Set initial heap size for device malloc.
- ``8388608``
- | Unit: Byte
| The default value corresponds to 8 MB.
* - | ``HIP_MEM_POOL_SUPPORT``
| Enables memory pool support in HIP.
- ``0``
- | 0: Disable
| 1: Enable
* - | ``HIP_MEM_POOL_USE_VM``
| Enables memory pool support in HIP.
- | ``0``: other OS
| ``1``: Windows
- | 0: Disable
| 1: Enable
* - | ``HIP_VMEM_MANAGE_SUPPORT``
| Virtual Memory Management Support.
- ``1``
- | 0: Disable
| 1: Enable
* - | ``GPU_MAX_HEAP_SIZE``
| Set maximum size of the GPU heap to % of board memory.
- ``100``
- | Unit: Percentage
* - | ``GPU_MAX_REMOTE_MEM_SIZE``
| Maximum size that allows device memory substitution with system.
- ``2``
- | Unit: kilobyte (KB)
* - | ``GPU_NUM_MEM_DEPENDENCY``
| Number of memory objects for dependency tracking.
- ``256``
-
* - | ``GPU_STREAMOPS_CP_WAIT``
| Force the stream memory operation to wait on CP.
- ``0``
- | 0: Disable
| 1: Enable
* - | ``HSA_LOCAL_MEMORY_ENABLE``
| Enable HSA device local memory usage.
- ``1``
- | 0: Disable
| 1: Enable
* - | ``PAL_ALWAYS_RESIDENT``
| Force memory resources to become resident at allocation time.
- ``0``
- | 0: Disable
| 1: Enable
* - | ``PAL_PREPINNED_MEMORY_SIZE``
| Size of prepinned memory.
- ``64``
- | Unit: kilobyte (KB)
* - | ``REMOTE_ALLOC``
| Use remote memory for the global heap allocation.
- ``0``
- | 0: Disable
| 1: Enable
.. include-table:: data/env_variables_hip.rst
:table: hip-env-memory
Other useful variables
================================================================================
@@ -175,15 +51,5 @@ Other useful variables
The following table lists environment variables that are useful but relate to
different features.
.. list-table::
:header-rows: 1
:widths: 35,14,51
* - **Environment variable**
- **Default value**
- **Value**
* - | ``HIPRTC_COMPILE_OPTIONS_APPEND``
| Sets compile options needed for ``hiprtc`` compilation.
- None
- ``--gpu-architecture=gfx906:sramecc+:xnack``, ``-fgpu-rdc``
.. include-table:: data/env_variables_hip.rst
:table: hip-env-other
@@ -1,6 +1,6 @@
.. meta::
:description: This chapter lists types and device API wrappers related to the
Cooperative Group feature. Programmers can directly use these
:description: This chapter lists types and device API wrappers related to the
Cooperative Group feature. Programmers can directly use these
API features in their kernels.
:keywords: AMD, ROCm, HIP, cooperative groups
@@ -42,7 +42,7 @@ The following cooperative groups classes can be used on the device side.
.. doxygenclass:: cooperative_groups::multi_grid_group
:members:
.. _thread_block_tile_ref:
.. doxygenclass:: cooperative_groups::thread_block_tile
-38
Datei anzeigen
@@ -1,38 +0,0 @@
# Table comparing syntax for different compute APIs
|Term|CUDA|HIP|OpenCL|
|---|---|---|---|
|Device|`int deviceId`|`int deviceId`|`cl_device`|
|Queue|`cudaStream_t`|`hipStream_t`|`cl_command_queue`|
|Event|`cudaEvent_t`|`hipEvent_t`|`cl_event`|
|Memory|`void *`|`void *`|`cl_mem`|
|||||
| |grid|grid|NDRange|
| |block|block|work-group|
| |thread|thread|work-item|
| |warp|warp|sub-group|
|||||
|Thread-<br>index | `threadIdx.x` | `threadIdx.x` | `get_local_id(0)` |
|Block-<br>index | `blockIdx.x` | `blockIdx.x` | `get_group_id(0)` |
|Block-<br>dim | `blockDim.x` | `blockDim.x` | `get_local_size(0)` |
|Grid-dim | `gridDim.x` | `gridDim.x` | `get_num_groups(0)` |
|||||
|Device Kernel|`__global__`|`__global__`|`__kernel`|
|Device Function|`__device__`|`__device__`|Implied in device compilation|
|Host Function|`__host_` (default)|`__host_` (default)|Implied in host compilation|
|Host + Device Function|`__host__` `__device__`|`__host__` `__device__`| No equivalent|
|Kernel Launch|`<<< >>>`|`hipLaunchKernel`/`hipLaunchKernelGGL`/`<<< >>>`|`clEnqueueNDRangeKernel`|
||||||
|Global Memory|`__global__`|`__global__`|`__global`|
|Group Memory|`__shared__`|`__shared__`|`__local`|
|Constant|`__constant__`|`__constant__`|`__constant`|
||||||
||`__syncthreads`|`__syncthreads`|`barrier(CLK_LOCAL_MEMFENCE)`|
|Atomic Builtins|`atomicAdd`|`atomicAdd`|`atomic_add`|
|Precise Math|`cos(f)`|`cos(f)`|`cos(f)`|
|Fast Math|`__cos(f)`|`__cos(f)`|`native_cos(f)`|
|Vector|`float4`|`float4`|`float4`|
## Notes
The indexing functions (starting with `thread-index`) show the terminology for a 1D grid. Some APIs use reverse order of `xyz` / 012 indexing for 3D grids.