xref: /libCEED/backends/hip/ceed-hip-common.c (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
37fcac036SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
57fcac036SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
77fcac036SJeremy L Thompson 
87fcac036SJeremy L Thompson #include "ceed-hip-common.h"
97fcac036SJeremy L Thompson 
1049aac155SJeremy L Thompson #include <ceed.h>
112b730f8bSJeremy L Thompson #include <ceed/backend.h>
122b730f8bSJeremy L Thompson #include <stdlib.h>
132b730f8bSJeremy L Thompson #include <string.h>
142b730f8bSJeremy L Thompson 
157fcac036SJeremy L Thompson //------------------------------------------------------------------------------
167fcac036SJeremy L Thompson // Device information backend init
177fcac036SJeremy L Thompson //------------------------------------------------------------------------------
CeedInit_Hip(Ceed ceed,const char * resource)18eb7e6cafSJeremy L Thompson int CeedInit_Hip(Ceed ceed, const char *resource) {
19b7453713SJeremy L Thompson   Ceed_Hip   *data;
207fcac036SJeremy L Thompson   const char *device_spec = strstr(resource, ":device_id=");
210d0321e0SJeremy L Thompson   const int   device_id   = (device_spec) ? atoi(device_spec + 11) : -1;
22a3b195efSJeremy L Thompson   int         current_device_id, xnack_value;
23a3b195efSJeremy L Thompson   const char *xnack;
24b7453713SJeremy L Thompson 
252b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipGetDevice(&current_device_id));
260d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
272b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipSetDevice(device_id));
280d0321e0SJeremy L Thompson     current_device_id = device_id;
297fcac036SJeremy L Thompson   }
30b7453713SJeremy L Thompson 
312b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
320d0321e0SJeremy L Thompson   data->device_id = current_device_id;
33b2165e7aSSebastian Grimberg   CeedCallHip(ceed, hipGetDeviceProperties(&data->device_prop, current_device_id));
34a3b195efSJeremy L Thompson   xnack                        = getenv("HSA_XNACK");
35a3b195efSJeremy L Thompson   xnack_value                  = !!xnack ? atol(xnack) : 0;
36a3b195efSJeremy L Thompson   data->has_unified_addressing = xnack_value > 0 ? data->device_prop.unifiedAddressing : 0;
37a3b195efSJeremy L Thompson   if (data->has_unified_addressing) {
38a3b195efSJeremy L Thompson     CeedDebug(ceed, "Using unified memory addressing");
39a3b195efSJeremy L Thompson   }
400d0321e0SJeremy L Thompson   data->opt_block_size = 256;
417fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
427fcac036SJeremy L Thompson }
437fcac036SJeremy L Thompson 
447fcac036SJeremy L Thompson //------------------------------------------------------------------------------
457fcac036SJeremy L Thompson // Backend Destroy
467fcac036SJeremy L Thompson //------------------------------------------------------------------------------
CeedDestroy_Hip(Ceed ceed)477fcac036SJeremy L Thompson int CeedDestroy_Hip(Ceed ceed) {
487fcac036SJeremy L Thompson   Ceed_Hip *data;
49b7453713SJeremy L Thompson 
502b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
51b2165e7aSSebastian Grimberg   if (data->hipblas_handle) CeedCallHipblas(ceed, hipblasDestroy(data->hipblas_handle));
522b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
537fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
547fcac036SJeremy L Thompson }
557fcac036SJeremy L Thompson 
567fcac036SJeremy L Thompson //------------------------------------------------------------------------------
57f5d1e504SJeremy L Thompson // Memory transfer utilities
58f5d1e504SJeremy L Thompson //------------------------------------------------------------------------------
CeedSetDeviceGenericArray_Hip(Ceed ceed,const void * source_array,CeedCopyMode copy_mode,size_t size_unit,CeedSize num_values,void * target_array_owned,void * target_array_borrowed,void * target_array)59f5d1e504SJeremy L Thompson static inline int CeedSetDeviceGenericArray_Hip(Ceed ceed, const void *source_array, CeedCopyMode copy_mode, size_t size_unit, CeedSize num_values,
60f5d1e504SJeremy L Thompson                                                 void *target_array_owned, void *target_array_borrowed, void *target_array) {
61f5d1e504SJeremy L Thompson   switch (copy_mode) {
62f5d1e504SJeremy L Thompson     case CEED_COPY_VALUES:
63cc3bdf8cSJeremy L Thompson       if (!*(void **)target_array) {
64cc3bdf8cSJeremy L Thompson         if (*(void **)target_array_borrowed) {
65cc3bdf8cSJeremy L Thompson           *(void **)target_array = *(void **)target_array_borrowed;
66cc3bdf8cSJeremy L Thompson         } else {
67f5d1e504SJeremy L Thompson           if (!*(void **)target_array_owned) CeedCallHip(ceed, hipMalloc(target_array_owned, size_unit * num_values));
68f5d1e504SJeremy L Thompson           *(void **)target_array = *(void **)target_array_owned;
69cc3bdf8cSJeremy L Thompson         }
70cc3bdf8cSJeremy L Thompson       }
71cc3bdf8cSJeremy L Thompson       if (source_array) CeedCallHip(ceed, hipMemcpy(*(void **)target_array, source_array, size_unit * num_values, hipMemcpyDeviceToDevice));
72f5d1e504SJeremy L Thompson       break;
73f5d1e504SJeremy L Thompson     case CEED_OWN_POINTER:
74081aa29dSJeremy L Thompson       CeedCallHip(ceed, hipFree(*(void **)target_array_owned));
75f5d1e504SJeremy L Thompson       *(void **)target_array_owned    = (void *)source_array;
76f5d1e504SJeremy L Thompson       *(void **)target_array_borrowed = NULL;
77f5d1e504SJeremy L Thompson       *(void **)target_array          = *(void **)target_array_owned;
78f5d1e504SJeremy L Thompson       break;
79f5d1e504SJeremy L Thompson     case CEED_USE_POINTER:
80081aa29dSJeremy L Thompson       CeedCallHip(ceed, hipFree(*(void **)target_array_owned));
81f5d1e504SJeremy L Thompson       *(void **)target_array_owned    = NULL;
82f5d1e504SJeremy L Thompson       *(void **)target_array_borrowed = (void *)source_array;
83f5d1e504SJeremy L Thompson       *(void **)target_array          = *(void **)target_array_borrowed;
84f5d1e504SJeremy L Thompson   }
85f5d1e504SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86f5d1e504SJeremy L Thompson }
87f5d1e504SJeremy L Thompson 
CeedSetDeviceBoolArray_Hip(Ceed ceed,const bool * source_array,CeedCopyMode copy_mode,CeedSize num_values,const bool ** target_array_owned,const bool ** target_array_borrowed,const bool ** target_array)88f5d1e504SJeremy L Thompson int CeedSetDeviceBoolArray_Hip(Ceed ceed, const bool *source_array, CeedCopyMode copy_mode, CeedSize num_values, const bool **target_array_owned,
89f5d1e504SJeremy L Thompson                                const bool **target_array_borrowed, const bool **target_array) {
90f5d1e504SJeremy L Thompson   CeedCallBackend(CeedSetDeviceGenericArray_Hip(ceed, source_array, copy_mode, sizeof(bool), num_values, target_array_owned, target_array_borrowed,
91f5d1e504SJeremy L Thompson                                                 target_array));
92f5d1e504SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93f5d1e504SJeremy L Thompson }
94f5d1e504SJeremy L Thompson 
CeedSetDeviceCeedInt8Array_Hip(Ceed ceed,const CeedInt8 * source_array,CeedCopyMode copy_mode,CeedSize num_values,const CeedInt8 ** target_array_owned,const CeedInt8 ** target_array_borrowed,const CeedInt8 ** target_array)95f5d1e504SJeremy L Thompson int CeedSetDeviceCeedInt8Array_Hip(Ceed ceed, const CeedInt8 *source_array, CeedCopyMode copy_mode, CeedSize num_values,
96f5d1e504SJeremy L Thompson                                    const CeedInt8 **target_array_owned, const CeedInt8 **target_array_borrowed, const CeedInt8 **target_array) {
97f5d1e504SJeremy L Thompson   CeedCallBackend(CeedSetDeviceGenericArray_Hip(ceed, source_array, copy_mode, sizeof(CeedInt8), num_values, target_array_owned,
98f5d1e504SJeremy L Thompson                                                 target_array_borrowed, target_array));
99f5d1e504SJeremy L Thompson   return CEED_ERROR_SUCCESS;
100f5d1e504SJeremy L Thompson }
101f5d1e504SJeremy L Thompson 
CeedSetDeviceCeedIntArray_Hip(Ceed ceed,const CeedInt * source_array,CeedCopyMode copy_mode,CeedSize num_values,const CeedInt ** target_array_owned,const CeedInt ** target_array_borrowed,const CeedInt ** target_array)102f5d1e504SJeremy L Thompson int CeedSetDeviceCeedIntArray_Hip(Ceed ceed, const CeedInt *source_array, CeedCopyMode copy_mode, CeedSize num_values,
103f5d1e504SJeremy L Thompson                                   const CeedInt **target_array_owned, const CeedInt **target_array_borrowed, const CeedInt **target_array) {
104f5d1e504SJeremy L Thompson   CeedCallBackend(CeedSetDeviceGenericArray_Hip(ceed, source_array, copy_mode, sizeof(CeedInt), num_values, target_array_owned, target_array_borrowed,
105f5d1e504SJeremy L Thompson                                                 target_array));
106f5d1e504SJeremy L Thompson   return CEED_ERROR_SUCCESS;
107f5d1e504SJeremy L Thompson }
108f5d1e504SJeremy L Thompson 
CeedSetDeviceCeedScalarArray_Hip(Ceed ceed,const CeedScalar * source_array,CeedCopyMode copy_mode,CeedSize num_values,const CeedScalar ** target_array_owned,const CeedScalar ** target_array_borrowed,const CeedScalar ** target_array)109f5d1e504SJeremy L Thompson int CeedSetDeviceCeedScalarArray_Hip(Ceed ceed, const CeedScalar *source_array, CeedCopyMode copy_mode, CeedSize num_values,
110f5d1e504SJeremy L Thompson                                      const CeedScalar **target_array_owned, const CeedScalar **target_array_borrowed,
111f5d1e504SJeremy L Thompson                                      const CeedScalar **target_array) {
112f5d1e504SJeremy L Thompson   CeedCallBackend(CeedSetDeviceGenericArray_Hip(ceed, source_array, copy_mode, sizeof(CeedScalar), num_values, target_array_owned,
113f5d1e504SJeremy L Thompson                                                 target_array_borrowed, target_array));
114f5d1e504SJeremy L Thompson   return CEED_ERROR_SUCCESS;
115f5d1e504SJeremy L Thompson }
116f5d1e504SJeremy L Thompson 
117f5d1e504SJeremy L Thompson //------------------------------------------------------------------------------
118