Date: 2026-06-01T23:13:43+00:00

Actual source code: cupmcontext.hip.cxx

      1: #include "../cupmcontext.hpp" /*I "petscdevice.h" I*/

      3: using namespace Petsc::device::cupm;

      5: PetscErrorCode PetscDeviceContextCreate_HIP(PetscDeviceContext dctx)
      6: {
      7:   static constexpr auto hip_context = CUPMContextHip();

      9:   PetscFunctionBegin;
     10:   PetscCall(hip_context.initialize(dctx->device));
     11:   dctx->data = new PetscDeviceContext_(HIP);
     12:   *dctx->ops = hip_context.ops;
     13:   PetscFunctionReturn(PETSC_SUCCESS);
     14: }

     16: /*
     17:  Management of HIPBLAS and HIPSOLVER handles

     19:  Unlike CUDA, hipSOLVER is just for dense matrices so there is
     20:  no distinguishing being dense and sparse.  Also, hipSOLVER is
     21:  very immature so we often have to do the mapping between roc and
     22:  cuda manually.
     23:  */

     25: /*@C
     26:   PetscHIPBLASGetHandle - Get the hipBLAS handle associated with PETSc's current `PetscDeviceContext`

     28:   Not Collective; No Fortran Support

     30:   Output Parameter:
     31: . handle - the `hipblasHandle_t` for the current context

     33:   Level: developer

     35:   Note:
     36:   The current device context must be of type `PETSC_DEVICE_HIP`. The returned handle is owned by
     37:   PETSc and must not be destroyed by the caller.

     39: .seealso: `PetscDeviceContext`, `PetscHIPSOLVERGetHandle()`, `PetscGetCurrentHIPStream()`
     40: @*/
     41: PetscErrorCode PetscHIPBLASGetHandle(hipblasHandle_t *handle)
     42: {
     43:   PetscDeviceContext dctx;

     45:   PetscFunctionBegin;
     46:   PetscAssertPointer(handle, 1);
     47:   PetscCall(PetscDeviceContextGetCurrentContextAssertType_Internal(&dctx, PETSC_DEVICE_HIP));
     48:   PetscCall(PetscDeviceContextGetBLASHandle_Internal(dctx, handle));
     49:   PetscFunctionReturn(PETSC_SUCCESS);
     50: }

     52: /*@C
     53:   PetscHIPSOLVERGetHandle - Get the hipSOLVER handle associated with PETSc's current `PetscDeviceContext`

     55:   Not Collective; No Fortran Support

     57:   Output Parameter:
     58: . handle - the `hipsolverHandle_t` for the current context

     60:   Level: developer

     62:   Note:
     63:   The current device context must be of type `PETSC_DEVICE_HIP`. The returned handle is owned by
     64:   PETSc and must not be destroyed by the caller.

     66: .seealso: `PetscDeviceContext`, `PetscHIPBLASGetHandle()`, `PetscGetCurrentHIPStream()`
     67: @*/
     68: PetscErrorCode PetscHIPSOLVERGetHandle(hipsolverHandle_t *handle)
     69: {
     70:   PetscDeviceContext dctx;

     72:   PetscFunctionBegin;
     73:   PetscAssertPointer(handle, 1);
     74:   PetscCall(PetscDeviceContextGetCurrentContextAssertType_Internal(&dctx, PETSC_DEVICE_HIP));
     75:   PetscCall(PetscDeviceContextGetSOLVERHandle_Internal(dctx, handle));
     76:   PetscFunctionReturn(PETSC_SUCCESS);
     77: }

     79: /*@C
     80:   PetscGetCurrentHIPStream - Get the HIP stream associated with PETSc's current `PetscDeviceContext`

     82:   Not Collective; No Fortran Support

     84:   Output Parameter:
     85: . stream - the `hipStream_t` for the current context

     87:   Level: developer

     89:   Note:
     90:   The current device context must be of type `PETSC_DEVICE_HIP`. The returned stream is owned by
     91:   PETSc and must not be destroyed by the caller.

     93: .seealso: `PetscDeviceContext`, `PetscHIPBLASGetHandle()`, `PetscHIPSOLVERGetHandle()`
     94: @*/
     95: PetscErrorCode PetscGetCurrentHIPStream(hipStream_t *stream)
     96: {
     97:   PetscDeviceContext dctx;
     98:   void              *handle;

    100:   PetscFunctionBegin;
    101:   PetscAssertPointer(stream, 1);
    102:   PetscCall(PetscDeviceContextGetCurrentContextAssertType_Internal(&dctx, PETSC_DEVICE_HIP));
    103:   PetscCall(PetscDeviceContextGetStreamHandle(dctx, &handle));
    104:   *stream = *(hipStream_t *)handle;
    105:   PetscFunctionReturn(PETSC_SUCCESS);
    106: }