xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
80d0321e0SJeremy L Thompson #include "ceed-hip-ref.h"
90d0321e0SJeremy L Thompson 
10*2b730f8bSJeremy L Thompson #include <ceed/backend.h>
11*2b730f8bSJeremy L Thompson #include <ceed/ceed.h>
12*2b730f8bSJeremy L Thompson #include <stdlib.h>
13*2b730f8bSJeremy L Thompson #include <string.h>
14*2b730f8bSJeremy L Thompson 
150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
160d0321e0SJeremy L Thompson // HIP preferred MemType
170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
180d0321e0SJeremy L Thompson static int CeedGetPreferredMemType_Hip(CeedMemType *type) {
190d0321e0SJeremy L Thompson   *type = CEED_MEM_DEVICE;
200d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
210d0321e0SJeremy L Thompson }
220d0321e0SJeremy L Thompson 
230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
240d0321e0SJeremy L Thompson // Get hipBLAS handle
250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
260d0321e0SJeremy L Thompson int CeedHipGetHipblasHandle(Ceed ceed, hipblasHandle_t *handle) {
270d0321e0SJeremy L Thompson   Ceed_Hip *data;
28*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
290d0321e0SJeremy L Thompson 
30*2b730f8bSJeremy L Thompson   if (!data->hipblas_handle) CeedCallHipblas(ceed, hipblasCreate(&data->hipblas_handle));
310d0321e0SJeremy L Thompson   *handle = data->hipblas_handle;
320d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
330d0321e0SJeremy L Thompson }
340d0321e0SJeremy L Thompson 
350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
360d0321e0SJeremy L Thompson // Backend Init
370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
380d0321e0SJeremy L Thompson static int CeedInit_Hip(const char *resource, Ceed ceed) {
39b11824b3SJeremy L Thompson   char *resource_root;
40*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedHipGetResourceRoot(ceed, resource, &resource_root));
41*2b730f8bSJeremy L Thompson   if (strcmp(resource_root, "/gpu/hip/ref")) {
420d0321e0SJeremy L Thompson     // LCOV_EXCL_START
43*2b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Hip backend cannot use resource: %s", resource);
440d0321e0SJeremy L Thompson     // LCOV_EXCL_STOP
45*2b730f8bSJeremy L Thompson   }
46*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&resource_root));
47*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetDeterministic(ceed, true));
480d0321e0SJeremy L Thompson 
490d0321e0SJeremy L Thompson   Ceed_Hip *data;
50*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
51*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetData(ceed, data));
52*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedHipInit(ceed, resource));
530d0321e0SJeremy L Thompson 
54*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "GetPreferredMemType", CeedGetPreferredMemType_Hip));
55*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", CeedVectorCreate_Hip));
56*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Hip));
57*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", CeedBasisCreateH1_Hip));
58*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", CeedElemRestrictionCreate_Hip));
59*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreateBlocked", CeedElemRestrictionCreateBlocked_Hip));
60*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate", CeedQFunctionCreate_Hip));
61*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionContextCreate", CeedQFunctionContextCreate_Hip));
62*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate", CeedOperatorCreate_Hip));
63*2b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Hip));
640d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
650d0321e0SJeremy L Thompson }
660d0321e0SJeremy L Thompson 
670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
680d0321e0SJeremy L Thompson // Backend Register
690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
70*2b730f8bSJeremy L Thompson CEED_INTERN int CeedRegister_Hip(void) { return CeedRegister("/gpu/hip/ref", CeedInit_Hip, 40); }
710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
72