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