xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-common.c (revision b7453713e95c1c6eb59ce174cbcb87227e92884e)
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 
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 //------------------------------------------------------------------------------
18eb7e6cafSJeremy L Thompson int CeedInit_Hip(Ceed ceed, const char *resource) {
19*b7453713SJeremy 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;
220d0321e0SJeremy L Thompson   int         current_device_id;
23*b7453713SJeremy L Thompson 
242b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipGetDevice(&current_device_id));
250d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
262b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipSetDevice(device_id));
270d0321e0SJeremy L Thompson     current_device_id = device_id;
287fcac036SJeremy L Thompson   }
29*b7453713SJeremy L Thompson 
302b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
310d0321e0SJeremy L Thompson   data->device_id = current_device_id;
32b2165e7aSSebastian Grimberg   CeedCallHip(ceed, hipGetDeviceProperties(&data->device_prop, current_device_id));
330d0321e0SJeremy L Thompson   data->opt_block_size = 256;
347fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
357fcac036SJeremy L Thompson }
367fcac036SJeremy L Thompson 
377fcac036SJeremy L Thompson //------------------------------------------------------------------------------
387fcac036SJeremy L Thompson // Backend Destroy
397fcac036SJeremy L Thompson //------------------------------------------------------------------------------
407fcac036SJeremy L Thompson int CeedDestroy_Hip(Ceed ceed) {
417fcac036SJeremy L Thompson   Ceed_Hip *data;
42*b7453713SJeremy L Thompson 
432b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
44b2165e7aSSebastian Grimberg   if (data->hipblas_handle) CeedCallHipblas(ceed, hipblasDestroy(data->hipblas_handle));
452b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
467fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
477fcac036SJeremy L Thompson }
487fcac036SJeremy L Thompson 
497fcac036SJeremy L Thompson //------------------------------------------------------------------------------
50