Skip to content

Commit

Permalink
Fix the titles of the driver API docs
Browse files Browse the repository at this point in the history
  • Loading branch information
neon60 committed Sep 30, 2024
1 parent 3d90efa commit ce89adc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
51 changes: 29 additions & 22 deletions docs/how-to/hip_porting_driver_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ NVIDIA provides separate CUDA driver and runtime APIs. The two APIs have signifi
The driver API offers two additional functionalities not provided by the runtime API: ``cuModule`` and ``cuCtx`` APIs.

cuModule API
------------
============

The Module section of the driver API provides additional control over how and when accelerator code objects are loaded. For example, the driver API enables code objects to load from files or memory pointers. Symbols for kernels or global data are extracted from the loaded code objects. In contrast, the runtime API loads automatically and, if necessary, compiles all the kernels from an executable binary when it runs. In this mode, kernel code must be compiled using NVCC so that automatic loading can function correctly.

The Module features are useful in an environment that generates the code objects directly, such as a new accelerator language front end. NVCC is not used here. Instead, the environment might have a different kernel language or compilation flow. Other environments have many kernels and don't want all of them to be loaded automatically. The Module functions load the generated code objects and launch kernels. Similar to the cuModule API, HIP defines a hipModule API that provides similar explicit control over code object management.

cuCtx API
---------
=========

The driver API defines "Context" and "Devices" as separate entities.
Contexts contain a single device, and a device can theoretically have multiple contexts.
Expand Down Expand Up @@ -73,14 +73,18 @@ Notably, there is no fat binary format that can contain code for both NVCC and H
The ``hipModule`` API can be used to load additional code objects. When used this way, it extends the capability of the automatically loaded code objects.
HIP-Clang enables both of these capabilities to be used together. Of course, it is possible to create a program with no kernels and no automatic loading.

For module API reference, visit :ref:`driver_api_module_reference`.

hipCtx API
----------

HIP provides a ``Ctx`` API as a thin layer over the existing device functions. The ``Ctx`` API can be used to set the current context or to query properties of the device associated with the context.
The current context is implicitly used by other APIs, such as ``hipStreamCreate``.

For context reference, visit :ref:`driver_api_context_reference`.

HIPIFY translation of CUDA driver API
-------------------------------------
=====================================

The HIPIFY tools convert CUDA driver APIs for streams, events, modules, devices, memory management, context, and the profiler to the equivalent HIP calls. For example, ``cuEventCreate`` is translated to ``hipEventCreate``.
HIPIFY tools also convert error codes from the driver namespace and coding conventions to the equivalent HIP error code. HIP unifies the APIs for these common functions.
Expand All @@ -93,35 +97,41 @@ HIP defines a single error space and uses camel case for all errors (i.e. ``hipE

For further information, visit the :doc:`hipify:index`.

**Address spaces**
Address spaces
--------------

HIP-Clang defines a process-wide address space where the CPU and all devices allocate addresses from a single unified pool.
This means addresses can be shared between contexts. Unlike the original CUDA implementation, a new context does not create a new address space for the device.

**Using hipModuleLaunchKernel**
Using hipModuleLaunchKernel
---------------------------

Both CUDA driver and runtime APIs define a function for launching kernels, called ``cuLaunchKernel`` or ``cudaLaunchKernel``. The equivalent API in HIP is ``hipModuleLaunchKernel``.
The kernel arguments and the execution configuration (grid dimensions, group dimensions, dynamic shared memory, and stream) are passed as arguments to the launch function.
The runtime API additionally provides the ``<<< >>>`` syntax for launching kernels, which resembles a special function call and is easier to use than the explicit launch API, especially when handling kernel arguments.
However, this syntax is not standard C++ and is available only when NVCC is used to compile the host code.

**Additional information**
Additional information
----------------------

HIP-Clang creates a primary context when the HIP API is called. So, in pure driver API code, HIP-Clang creates a primary context while HIP/NVCC has an empty context stack. HIP-Clang pushes the primary context to the context stack when it is empty. This can lead to subtle differences in applications which mix the runtime and driver APIs.

hip-clang implementation notes
------------------------------
HIP-Clang implementation notes
==============================

**.hip_fatbin**
.hip_fatbin
-----------

HIP-Clang links device code from different translation units together. For each device target, it generates a code object. ``clang-offload-bundler`` bundles code objects for different device targets into one fat binary, which is embedded as the global symbol ``__hip_fatbin`` in the ``.hip_fatbin`` section of the ELF file of the executable or shared object.

**Initialization and termination functions**
Initialization and termination functions
-----------------------------------------

HIP-Clang generates initialization and termination functions for each translation unit for host code compilation. The initialization functions call ``__hipRegisterFatBinary`` to register the fat binary embedded in the ELF file. They also call ``__hipRegisterFunction`` and ``__hipRegisterVar`` to register kernel functions and device-side global variables. The termination functions call ``__hipUnregisterFatBinary``.
HIP-Clang emits a global variable ``__hip_gpubin_handle`` of type ``void**`` with ``linkonce`` linkage and an initial value of 0 for each host translation unit. Each initialization function checks ``__hip_gpubin_handle`` and registers the fat binary only if ``__hip_gpubin_handle`` is 0. It saves the return value of ``__hip_gpubin_handle`` to ``__hip_gpubin_handle``. This is to guarantee that the fat binary is only registered once. A similar check is performed in the termination functions.

**Kernel launching**
Kernel launching
----------------

HIP-Clang supports kernel launching using either the CUDA ``<<<>>>`` syntax, ``hipLaunchKernel``, or ``hipLaunchKernelGGL``. The last option is a macro which expands to the CUDA ``<<<>>>`` syntax by default. It can also be turned into a template by defining ``HIP_TEMPLATE_KERNEL_LAUNCH``.

Expand All @@ -131,9 +141,10 @@ HIP-Clang implements two sets of APIs for launching kernels.
By default, when HIP-Clang encounters the ``<<<>>>`` statement in the host code, it first calls ``hipConfigureCall`` to set up the threads and grids. It then calls the stub function with the given arguments. The stub function calls ``hipSetupArgument`` for each kernel argument, then calls ``hipLaunchByPtr`` with a function pointer to the stub function. In ``hipLaunchByPtr``, the real kernel associated with the stub function is launched.

NVCC implementation notes
-------------------------
=========================

**Interoperation between HIP and CUDA driver**
Interoperation between HIP and CUDA driver
------------------------------------------

CUDA applications might want to mix CUDA driver code with HIP code (see the example below). This table shows the equivalence between CUDA and HIP types required to implement this interaction.

Expand Down Expand Up @@ -165,7 +176,8 @@ CUDA applications might want to mix CUDA driver code with HIP code (see the exam
- ``CUarray``
- ``cudaArray``

**Compilation options**
Compilation options
-------------------

The ``hipModule_t`` interface does not support the ``cuModuleLoadDataEx`` function, which is used to control PTX compilation options.
HIP-Clang does not use PTX, so it does not support these compilation options.
Expand Down Expand Up @@ -338,7 +350,7 @@ HIP supports texture driver APIs. However, texture references must be declared w
// ...
}
Driver Entry Point Access
Driver entry point access
=========================

Starting from HIP version 6.2.0, support for Driver Entry Point Access is available when using CUDA 12.0 or newer. This feature allows developers to directly interact with the CUDA driver API, providing more control over GPU operations.
Expand All @@ -349,6 +361,8 @@ Driver Entry Point Access provides several features:
* Requesting the default stream version on a per-thread basis
* Accessing new HIP features on older toolkits with a newer driver

For driver entry point access reference, visit :ref:`driver_api_entry_point_reference`.

Address retrieval
-----------------

Expand Down Expand Up @@ -471,10 +485,3 @@ The HIP version number is defined as an integer:
.. code-block:: cpp
HIP_VERSION=HIP_VERSION_MAJOR * 10000000 + HIP_VERSION_MINOR * 100000 + HIP_VERSION_PATCH
For further details, see :doc:`../how-to/faq`.

Reference
=========

For driver API reference, visit :ref:`driver_api_reference`.
8 changes: 7 additions & 1 deletion docs/reference/driver_api_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@ For comparison, compatibility, and version information with the CUDA driver API,
HIP driver API
==============

.. _driver_api_context_reference:

Context
-------

.. doxygengroup:: Context
:content-only:

.. _driver_api_module_reference:

Module
------

.. doxygengroup:: Module
:content-only:

Driver Entry Point Access
.. _driver_api_entry_point_reference:

Driver entry point access
-------------------------

.. doxygenfunction:: hipGetProcAddress

0 comments on commit ce89adc

Please sign in to comment.