xref: /libCEED/backends/hip/ceed-hip-common.c (revision bb229da952f7e9779ba6cb3cd1ca2ebeac5feb1f)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include "ceed-hip-common.h"
9 
10 #include <ceed.h>
11 #include <ceed/backend.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 //------------------------------------------------------------------------------
16 // Device information backend init
17 //------------------------------------------------------------------------------
18 int CeedInit_Hip(Ceed ceed, const char *resource) {
19   const char *device_spec = strstr(resource, ":device_id=");
20   const int   device_id   = (device_spec) ? atoi(device_spec + 11) : -1;
21 
22   int current_device_id;
23   CeedCallHip(ceed, hipGetDevice(&current_device_id));
24   if (device_id >= 0 && current_device_id != device_id) {
25     CeedCallHip(ceed, hipSetDevice(device_id));
26     current_device_id = device_id;
27   }
28   Ceed_Hip *data;
29   CeedCallBackend(CeedGetData(ceed, &data));
30   data->device_id = current_device_id;
31   CeedCallHip(ceed, hipGetDeviceProperties(&data->device_prop, current_device_id));
32   data->opt_block_size = 256;
33   return CEED_ERROR_SUCCESS;
34 }
35 
36 //------------------------------------------------------------------------------
37 // Backend Destroy
38 //------------------------------------------------------------------------------
39 int CeedDestroy_Hip(Ceed ceed) {
40   Ceed_Hip *data;
41   CeedCallBackend(CeedGetData(ceed, &data));
42   if (data->hipblas_handle) CeedCallHipblas(ceed, hipblasDestroy(data->hipblas_handle));
43   CeedCallBackend(CeedFree(&data));
44   return CEED_ERROR_SUCCESS;
45 }
46 
47 //------------------------------------------------------------------------------
48