xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-common.c (revision b11824b355ec5db8d1d0662d2c2bd260606aac4b)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, 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/ceed.h>
97fcac036SJeremy L Thompson #include <ceed/backend.h>
107fcac036SJeremy L Thompson #include <string.h>
117fcac036SJeremy L Thompson #include <stdlib.h>
127fcac036SJeremy L Thompson #include "ceed-hip-common.h"
137fcac036SJeremy L Thompson 
147fcac036SJeremy L Thompson //------------------------------------------------------------------------------
15*b11824b3SJeremy L Thompson // Get root resource without device spec
16*b11824b3SJeremy L Thompson //------------------------------------------------------------------------------
17*b11824b3SJeremy L Thompson int CeedHipGetResourceRoot(Ceed ceed, const char *resource,
18*b11824b3SJeremy L Thompson                            char **resource_root) {
19*b11824b3SJeremy L Thompson   int ierr;
20*b11824b3SJeremy L Thompson 
21*b11824b3SJeremy L Thompson   char *device_spec = strstr(resource, ":device_id=");
22*b11824b3SJeremy L Thompson   size_t resource_root_len = device_spec
23*b11824b3SJeremy L Thompson                              ? (size_t)(device_spec - resource) + 1
24*b11824b3SJeremy L Thompson                              : strlen(resource) + 1;
25*b11824b3SJeremy L Thompson   ierr = CeedCalloc(resource_root_len, resource_root); CeedChkBackend(ierr);
26*b11824b3SJeremy L Thompson   memcpy(*resource_root, resource, resource_root_len - 1);
27*b11824b3SJeremy L Thompson 
28*b11824b3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
29*b11824b3SJeremy L Thompson }
30*b11824b3SJeremy L Thompson 
31*b11824b3SJeremy L Thompson //------------------------------------------------------------------------------
327fcac036SJeremy L Thompson // Device information backend init
337fcac036SJeremy L Thompson //------------------------------------------------------------------------------
34f87d896cSJeremy L Thompson int CeedHipInit(Ceed ceed, const char *resource) {
357fcac036SJeremy L Thompson   int ierr;
367fcac036SJeremy L Thompson   const char *device_spec = strstr(resource, ":device_id=");
370d0321e0SJeremy L Thompson   const int device_id = (device_spec) ? atoi(device_spec + 11) : -1;
387fcac036SJeremy L Thompson 
390d0321e0SJeremy L Thompson   int current_device_id;
400d0321e0SJeremy L Thompson   ierr = hipGetDevice(&current_device_id); CeedChk_Hip(ceed, ierr);
410d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
420d0321e0SJeremy L Thompson     ierr = hipSetDevice(device_id); CeedChk_Hip(ceed, ierr);
430d0321e0SJeremy L Thompson     current_device_id = device_id;
447fcac036SJeremy L Thompson   }
457fcac036SJeremy L Thompson 
460d0321e0SJeremy L Thompson   struct hipDeviceProp_t device_prop;
470d0321e0SJeremy L Thompson   ierr = hipGetDeviceProperties(&device_prop, current_device_id);
487fcac036SJeremy L Thompson   CeedChk_Hip(ceed, ierr);
497fcac036SJeremy L Thompson 
507fcac036SJeremy L Thompson   Ceed_Hip *data;
517fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
520d0321e0SJeremy L Thompson   data->device_id = current_device_id;
530d0321e0SJeremy L Thompson   data->opt_block_size = 256;
547fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
557fcac036SJeremy L Thompson }
567fcac036SJeremy L Thompson 
577fcac036SJeremy L Thompson //------------------------------------------------------------------------------
587fcac036SJeremy L Thompson // Backend Destroy
597fcac036SJeremy L Thompson //------------------------------------------------------------------------------
607fcac036SJeremy L Thompson int CeedDestroy_Hip(Ceed ceed) {
617fcac036SJeremy L Thompson   int ierr;
627fcac036SJeremy L Thompson   Ceed_Hip *data;
637fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
640d0321e0SJeremy L Thompson   if (data->hipblas_handle) {
650d0321e0SJeremy L Thompson     ierr = hipblasDestroy(data->hipblas_handle); CeedChk_Hipblas(ceed, ierr);
667fcac036SJeremy L Thompson   }
677fcac036SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
687fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
697fcac036SJeremy L Thompson }
707fcac036SJeremy L Thompson 
717fcac036SJeremy L Thompson //------------------------------------------------------------------------------
72