xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-common.c (revision b2165e7a2e371018feedcb47974a027ed47e0487)
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) {
197fcac036SJeremy L Thompson   const char *device_spec = strstr(resource, ":device_id=");
200d0321e0SJeremy L Thompson   const int   device_id   = (device_spec) ? atoi(device_spec + 11) : -1;
217fcac036SJeremy L Thompson 
220d0321e0SJeremy L Thompson   int current_device_id;
232b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipGetDevice(&current_device_id));
240d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
252b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipSetDevice(device_id));
260d0321e0SJeremy L Thompson     current_device_id = device_id;
277fcac036SJeremy L Thompson   }
287fcac036SJeremy L Thompson   Ceed_Hip *data;
292b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
300d0321e0SJeremy L Thompson   data->device_id = current_device_id;
31*b2165e7aSSebastian Grimberg   CeedCallHip(ceed, hipGetDeviceProperties(&data->device_prop, current_device_id));
320d0321e0SJeremy L Thompson   data->opt_block_size = 256;
337fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
347fcac036SJeremy L Thompson }
357fcac036SJeremy L Thompson 
367fcac036SJeremy L Thompson //------------------------------------------------------------------------------
377fcac036SJeremy L Thompson // Backend Destroy
387fcac036SJeremy L Thompson //------------------------------------------------------------------------------
397fcac036SJeremy L Thompson int CeedDestroy_Hip(Ceed ceed) {
407fcac036SJeremy L Thompson   Ceed_Hip *data;
412b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
42*b2165e7aSSebastian Grimberg   if (data->hipblas_handle) CeedCallHipblas(ceed, hipblasDestroy(data->hipblas_handle));
432b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
447fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
457fcac036SJeremy L Thompson }
467fcac036SJeremy L Thompson 
477fcac036SJeremy L Thompson //------------------------------------------------------------------------------
48