xref: /libCEED/backends/hip/ceed-hip-common.c (revision 50c301a53d2cec48a2aa861bf6f38393f4831c2f)
1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3 // All Rights reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #include <ceed/ceed.h>
18 #include <ceed/backend.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include "ceed-hip-common.h"
22 
23 //------------------------------------------------------------------------------
24 // Device information backend init
25 //------------------------------------------------------------------------------
26 int CeedHipInit(Ceed ceed, const char *resource) {
27   int ierr;
28   const char *device_spec = strstr(resource, ":device_id=");
29   const int device_id = (device_spec) ? atoi(device_spec + 11) : -1;
30 
31   int current_device_id;
32   ierr = hipGetDevice(&current_device_id); CeedChk_Hip(ceed, ierr);
33   if (device_id >= 0 && current_device_id != device_id) {
34     ierr = hipSetDevice(device_id); CeedChk_Hip(ceed, ierr);
35     current_device_id = device_id;
36   }
37 
38   struct hipDeviceProp_t device_prop;
39   ierr = hipGetDeviceProperties(&device_prop, current_device_id);
40   CeedChk_Hip(ceed, ierr);
41 
42   Ceed_Hip *data;
43   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
44   data->device_id = current_device_id;
45   data->opt_block_size = 256;
46   return CEED_ERROR_SUCCESS;
47 }
48 
49 //------------------------------------------------------------------------------
50 // Backend Destroy
51 //------------------------------------------------------------------------------
52 int CeedDestroy_Hip(Ceed ceed) {
53   int ierr;
54   Ceed_Hip *data;
55   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
56   if (data->hipblas_handle) {
57     ierr = hipblasDestroy(data->hipblas_handle); CeedChk_Hipblas(ceed, ierr);
58   }
59   ierr = CeedFree(&data); CeedChkBackend(ierr);
60   return CEED_ERROR_SUCCESS;
61 }
62 
63 //------------------------------------------------------------------------------
64