xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-common.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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-hip-common.h"
97fcac036SJeremy L Thompson 
10*2b730f8bSJeremy L Thompson #include <ceed/backend.h>
11*2b730f8bSJeremy L Thompson #include <ceed/ceed.h>
12*2b730f8bSJeremy L Thompson #include <stdlib.h>
13*2b730f8bSJeremy L Thompson #include <string.h>
14*2b730f8bSJeremy L Thompson 
157fcac036SJeremy L Thompson //------------------------------------------------------------------------------
16b11824b3SJeremy L Thompson // Get root resource without device spec
17b11824b3SJeremy L Thompson //------------------------------------------------------------------------------
18*2b730f8bSJeremy L Thompson int CeedHipGetResourceRoot(Ceed ceed, const char *resource, char **resource_root) {
19b11824b3SJeremy L Thompson   char  *device_spec       = strstr(resource, ":device_id=");
20*2b730f8bSJeremy L Thompson   size_t resource_root_len = device_spec ? (size_t)(device_spec - resource) + 1 : strlen(resource) + 1;
21*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(resource_root_len, resource_root));
22b11824b3SJeremy L Thompson   memcpy(*resource_root, resource, resource_root_len - 1);
23b11824b3SJeremy L Thompson 
24b11824b3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
25b11824b3SJeremy L Thompson }
26b11824b3SJeremy L Thompson 
27b11824b3SJeremy L Thompson //------------------------------------------------------------------------------
287fcac036SJeremy L Thompson // Device information backend init
297fcac036SJeremy L Thompson //------------------------------------------------------------------------------
30f87d896cSJeremy L Thompson int CeedHipInit(Ceed ceed, const char *resource) {
317fcac036SJeremy L Thompson   const char *device_spec = strstr(resource, ":device_id=");
320d0321e0SJeremy L Thompson   const int   device_id   = (device_spec) ? atoi(device_spec + 11) : -1;
337fcac036SJeremy L Thompson 
340d0321e0SJeremy L Thompson   int current_device_id;
35*2b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipGetDevice(&current_device_id));
360d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
37*2b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipSetDevice(device_id));
380d0321e0SJeremy L Thompson     current_device_id = device_id;
397fcac036SJeremy L Thompson   }
407fcac036SJeremy L Thompson 
410d0321e0SJeremy L Thompson   struct hipDeviceProp_t device_prop;
42*2b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipGetDeviceProperties(&device_prop, current_device_id));
437fcac036SJeremy L Thompson 
447fcac036SJeremy L Thompson   Ceed_Hip *data;
45*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
460d0321e0SJeremy L Thompson   data->device_id      = current_device_id;
470d0321e0SJeremy L Thompson   data->opt_block_size = 256;
487fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
497fcac036SJeremy L Thompson }
507fcac036SJeremy L Thompson 
517fcac036SJeremy L Thompson //------------------------------------------------------------------------------
527fcac036SJeremy L Thompson // Backend Destroy
537fcac036SJeremy L Thompson //------------------------------------------------------------------------------
547fcac036SJeremy L Thompson int CeedDestroy_Hip(Ceed ceed) {
557fcac036SJeremy L Thompson   Ceed_Hip *data;
56*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
570d0321e0SJeremy L Thompson   if (data->hipblas_handle) {
58*2b730f8bSJeremy L Thompson     CeedCallHipblas(ceed, hipblasDestroy(data->hipblas_handle));
597fcac036SJeremy L Thompson   }
60*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
617fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
627fcac036SJeremy L Thompson }
637fcac036SJeremy L Thompson 
647fcac036SJeremy L Thompson //------------------------------------------------------------------------------
65