xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref.c (revision 0d0321e0e600f17fbb9528732fcb5c1d5c63fc0f)
1*0d0321e0SJeremy L Thompson // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2*0d0321e0SJeremy L Thompson // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3*0d0321e0SJeremy L Thompson // All Rights reserved. See files LICENSE and NOTICE for details.
4*0d0321e0SJeremy L Thompson //
5*0d0321e0SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software
6*0d0321e0SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral
7*0d0321e0SJeremy L Thompson // element discretizations for exascale applications. For more information and
8*0d0321e0SJeremy L Thompson // source code availability see http://github.com/ceed.
9*0d0321e0SJeremy L Thompson //
10*0d0321e0SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11*0d0321e0SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office
12*0d0321e0SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for
13*0d0321e0SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including
14*0d0321e0SJeremy L Thompson // software, applications, hardware, advanced system engineering and early
15*0d0321e0SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative.
16*0d0321e0SJeremy L Thompson 
17*0d0321e0SJeremy L Thompson #include <ceed/ceed.h>
18*0d0321e0SJeremy L Thompson #include <ceed/backend.h>
19*0d0321e0SJeremy L Thompson #include <string.h>
20*0d0321e0SJeremy L Thompson #include <stdlib.h>
21*0d0321e0SJeremy L Thompson #include "ceed-hip-ref.h"
22*0d0321e0SJeremy L Thompson 
23*0d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
24*0d0321e0SJeremy L Thompson // HIP preferred MemType
25*0d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
26*0d0321e0SJeremy L Thompson static int CeedGetPreferredMemType_Hip(CeedMemType *type) {
27*0d0321e0SJeremy L Thompson   *type = CEED_MEM_DEVICE;
28*0d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
29*0d0321e0SJeremy L Thompson }
30*0d0321e0SJeremy L Thompson 
31*0d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
32*0d0321e0SJeremy L Thompson // Get hipBLAS handle
33*0d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
34*0d0321e0SJeremy L Thompson int CeedHipGetHipblasHandle(Ceed ceed, hipblasHandle_t *handle) {
35*0d0321e0SJeremy L Thompson   int ierr;
36*0d0321e0SJeremy L Thompson   Ceed_Hip *data;
37*0d0321e0SJeremy L Thompson   ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr);
38*0d0321e0SJeremy L Thompson 
39*0d0321e0SJeremy L Thompson   if (!data->hipblas_handle) {
40*0d0321e0SJeremy L Thompson     ierr = hipblasCreate(&data->hipblas_handle); CeedChk_Hipblas(ceed, ierr);
41*0d0321e0SJeremy L Thompson   }
42*0d0321e0SJeremy L Thompson   *handle = data->hipblas_handle;
43*0d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
44*0d0321e0SJeremy L Thompson }
45*0d0321e0SJeremy L Thompson 
46*0d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
47*0d0321e0SJeremy L Thompson // Backend Init
48*0d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
49*0d0321e0SJeremy L Thompson static int CeedInit_Hip(const char *resource, Ceed ceed) {
50*0d0321e0SJeremy L Thompson   int ierr;
51*0d0321e0SJeremy L Thompson 
52*0d0321e0SJeremy L Thompson   if (strcmp(resource, "/gpu/hip/ref"))
53*0d0321e0SJeremy L Thompson     // LCOV_EXCL_START
54*0d0321e0SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
55*0d0321e0SJeremy L Thompson                      "Hip backend cannot use resource: %s", resource);
56*0d0321e0SJeremy L Thompson   // LCOV_EXCL_STOP
57*0d0321e0SJeremy L Thompson   ierr = CeedSetDeterministic(ceed, true); CeedChk(ierr);
58*0d0321e0SJeremy L Thompson 
59*0d0321e0SJeremy L Thompson   Ceed_Hip *data;
60*0d0321e0SJeremy L Thompson   ierr = CeedCalloc(1, &data); CeedChkBackend(ierr);
61*0d0321e0SJeremy L Thompson   ierr = CeedSetData(ceed, data); CeedChkBackend(ierr);
62*0d0321e0SJeremy L Thompson   ierr = CeedHipInit(ceed, resource); CeedChkBackend(ierr);
63*0d0321e0SJeremy L Thompson 
64*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "GetPreferredMemType",
65*0d0321e0SJeremy L Thompson                                 CeedGetPreferredMemType_Hip); CeedChkBackend(ierr);
66*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate",
67*0d0321e0SJeremy L Thompson                                 CeedVectorCreate_Hip); CeedChkBackend(ierr);
68*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1",
69*0d0321e0SJeremy L Thompson                                 CeedBasisCreateTensorH1_Hip); CeedChkBackend(ierr);
70*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1",
71*0d0321e0SJeremy L Thompson                                 CeedBasisCreateH1_Hip); CeedChkBackend(ierr);
72*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate",
73*0d0321e0SJeremy L Thompson                                 CeedElemRestrictionCreate_Hip); CeedChkBackend(ierr);
74*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed,
75*0d0321e0SJeremy L Thompson                                 "ElemRestrictionCreateBlocked",
76*0d0321e0SJeremy L Thompson                                 CeedElemRestrictionCreateBlocked_Hip);
77*0d0321e0SJeremy L Thompson   CeedChkBackend(ierr);
78*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate",
79*0d0321e0SJeremy L Thompson                                 CeedQFunctionCreate_Hip); CeedChkBackend(ierr);
80*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionContextCreate",
81*0d0321e0SJeremy L Thompson                                 CeedQFunctionContextCreate_Hip); CeedChkBackend(ierr);
82*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate",
83*0d0321e0SJeremy L Thompson                                 CeedOperatorCreate_Hip); CeedChkBackend(ierr);
84*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "CompositeOperatorCreate",
85*0d0321e0SJeremy L Thompson                                 CeedCompositeOperatorCreate_Hip); CeedChkBackend(ierr);
86*0d0321e0SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy",
87*0d0321e0SJeremy L Thompson                                 CeedDestroy_Hip); CeedChkBackend(ierr);
88*0d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89*0d0321e0SJeremy L Thompson }
90*0d0321e0SJeremy L Thompson 
91*0d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
92*0d0321e0SJeremy L Thompson // Backend Register
93*0d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
94*0d0321e0SJeremy L Thompson CEED_INTERN int CeedRegister_Hip(void) {
95*0d0321e0SJeremy L Thompson   return CeedRegister("/gpu/hip/ref", CeedInit_Hip, 40);
96*0d0321e0SJeremy L Thompson }
97*0d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
98