Skip to content

Commit

Permalink
PR feedbacks initalize
Browse files Browse the repository at this point in the history
  • Loading branch information
neon60 committed Oct 10, 2024
1 parent fcabc3f commit 8bb5416
Showing 1 changed file with 27 additions and 54 deletions.
81 changes: 27 additions & 54 deletions docs/how-to/hip_runtime_api/initialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,64 +29,46 @@ Initialize the HIP runtime

The HIP runtime is initialized automatically when the first HIP API call is
made. However, you can explicitly initialize it using :cpp:func:`hipInit`,
to be able to control the timing of the initialization. The explicit
initialization example code:

.. code-block:: cpp
#include <hip/hip_runtime.h>
hipError_t err = hipInit(0);
if (err != hipSuccess)
{
// Handle error
}
.. note::

Include the HIP runtime header in your source file, if you want access HIP
functions.

to be able to control the timing of the initialization.

Querying and setting GPUs
================================================================================

If multiple GPUs are available, it's worth to query and select the desired GPU(s)
based on properties.

Querying GPU devices
--------------------------------------------------------------------------------

Before using a GPU device, you might want to query its properties:
The properties of a gpu can be queried using :cpp:func:`hipGetDeviceProperties`,
which returns a struct of :cpp:struct:`hipDeviceProp_t`. This struct can be
used to identify a device or give an overview of hardware characteristics, that
might make one gpu better suited for the task at hand than others.

.. code-block:: cpp
#include <hip/hip_runtime.h>
#include <iostream>
int deviceCount;
if (hipGetDeviceCount(&deviceCount) == hipSuccess){
for (int i = 0; i < deviceCount; ++i){
hipDeviceProp_t prop;
if ( hipGetDeviceProperties(&prop, i) == hipSuccess)
std::cout << "Device" << i << prop.name << std::endl;
int main() {
int deviceCount;
if (hipGetDeviceCount(&deviceCount) == hipSuccess){
for (int i = 0; i < deviceCount; ++i){
hipDeviceProp_t prop;
if ( hipGetDeviceProperties(&prop, i) == hipSuccess)
std::cout << "Device" << i << prop.name << std::endl;
}
}
return 0;
}
Setting the GPU
--------------------------------------------------------------------------------

Select the GPU to be used for subsequent HIP operations:

.. code-block:: cpp
#include <hip/hip_runtime.h>
int deviceId = 0; // Example: selecting the first device
hipError_t err = hipSetDevice(deviceId);
if (err != hipSuccess)
{
// Handle error
}
This function performs several key tasks:
:cpp:func:`hipSetDevice` function select the GPU to be used for subsequent HIP
operations. This function performs several key tasks:

- Context Binding

Expand All @@ -100,24 +82,15 @@ This function performs several key tasks:

- Check device availablity

Checks for errors in device selection and ensures that the specified device is
available and capable of executing HIP operations.
Checks for errors in device selection and returns error if the specified
device is not available or not capable of executing HIP operations.

Finalize
================================================================================

:cpp:func:`hipDeviceReset()` deletes all streams created, memory allocated,
kernels running and events created (by this process?). Make sure that no other thread is using the
device or streams, memory, kernels or events associated with the current device.
:cpp:func:`hipDeviceReset()` deletes all streams created, memory allocated,
kernels running and events created by the current process. Make sure that no
other thread is using the device or streams, memory, kernels or events
associated with the current device.

Any new HIP API call initializes the HIP runtime again.

.. code-block:: cpp
#include <hip/hip_runtime.h>
hipError_t err = hipDeviceReset();
if (err != hipSuccess)
{
// Handle error
}

0 comments on commit 8bb5416

Please sign in to comment.