xref: /libCEED/rust/libceed-sys/c-src/backends/hip/ceed-hip-common.c (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa) !
1*3d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2*3d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
37fcac036SJeremy L Thompson //
4*3d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
57fcac036SJeremy L Thompson //
6*3d8e8822SJeremy 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 //------------------------------------------------------------------------------
157fcac036SJeremy L Thompson // Device information backend init
167fcac036SJeremy L Thompson //------------------------------------------------------------------------------
17f87d896cSJeremy L Thompson int CeedHipInit(Ceed ceed, const char *resource) {
187fcac036SJeremy L Thompson   int ierr;
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;
230d0321e0SJeremy L Thompson   ierr = hipGetDevice(&current_device_id); CeedChk_Hip(ceed, ierr);
240d0321e0SJeremy L Thompson   if (device_id >= 0 && current_device_id != device_id) {
250d0321e0SJeremy L Thompson     ierr = hipSetDevice(device_id); CeedChk_Hip(ceed, ierr);
260d0321e0SJeremy L Thompson     current_device_id = device_id;
277fcac036SJeremy L Thompson   }
287fcac036SJeremy L Thompson 
290d0321e0SJeremy L Thompson   struct hipDeviceProp_t device_prop;
300d0321e0SJeremy L Thompson   ierr = hipGetDeviceProperties(&device_prop, current_device_id);
317fcac036SJeremy L Thompson   CeedChk_Hip(ceed, ierr);
327fcac036SJeremy L Thompson 
337fcac036SJeremy L Thompson   Ceed_Hip *data;
347fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
350d0321e0SJeremy L Thompson   data->device_id = current_device_id;
360d0321e0SJeremy L Thompson   data->opt_block_size = 256;
377fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
387fcac036SJeremy L Thompson }
397fcac036SJeremy L Thompson 
407fcac036SJeremy L Thompson //------------------------------------------------------------------------------
417fcac036SJeremy L Thompson // Backend Destroy
427fcac036SJeremy L Thompson //------------------------------------------------------------------------------
437fcac036SJeremy L Thompson int CeedDestroy_Hip(Ceed ceed) {
447fcac036SJeremy L Thompson   int ierr;
457fcac036SJeremy L Thompson   Ceed_Hip *data;
467fcac036SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
470d0321e0SJeremy L Thompson   if (data->hipblas_handle) {
480d0321e0SJeremy L Thompson     ierr = hipblasDestroy(data->hipblas_handle); CeedChk_Hipblas(ceed, ierr);
497fcac036SJeremy L Thompson   }
507fcac036SJeremy L Thompson   ierr = CeedFree(&data); CeedChkBackend(ierr);
517fcac036SJeremy L Thompson   return CEED_ERROR_SUCCESS;
527fcac036SJeremy L Thompson }
537fcac036SJeremy L Thompson 
547fcac036SJeremy L Thompson //------------------------------------------------------------------------------
55