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*49aac155SJeremy L Thompson #include <ceed.h> 112b730f8bSJeremy L Thompson #include <ceed/backend.h> 12*49aac155SJeremy L Thompson #include <stdbool.h> 132b730f8bSJeremy L Thompson #include <string.h> 142b730f8bSJeremy L Thompson 15*49aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h" 16*49aac155SJeremy L Thompson 170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 180d0321e0SJeremy L Thompson // HIP preferred MemType 190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 200d0321e0SJeremy L Thompson static int CeedGetPreferredMemType_Hip(CeedMemType *type) { 210d0321e0SJeremy L Thompson *type = CEED_MEM_DEVICE; 220d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 230d0321e0SJeremy L Thompson } 240d0321e0SJeremy L Thompson 250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 260d0321e0SJeremy L Thompson // Get hipBLAS handle 270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 280d0321e0SJeremy L Thompson int CeedHipGetHipblasHandle(Ceed ceed, hipblasHandle_t *handle) { 290d0321e0SJeremy L Thompson Ceed_Hip *data; 302b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 310d0321e0SJeremy L Thompson 322b730f8bSJeremy L Thompson if (!data->hipblas_handle) CeedCallHipblas(ceed, hipblasCreate(&data->hipblas_handle)); 330d0321e0SJeremy L Thompson *handle = data->hipblas_handle; 340d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 350d0321e0SJeremy L Thompson } 360d0321e0SJeremy L Thompson 370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 380d0321e0SJeremy L Thompson // Backend Init 390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 400d0321e0SJeremy L Thompson static int CeedInit_Hip(const char *resource, Ceed ceed) { 41b11824b3SJeremy L Thompson char *resource_root; 422b730f8bSJeremy L Thompson CeedCallBackend(CeedHipGetResourceRoot(ceed, resource, &resource_root)); 432b730f8bSJeremy L Thompson if (strcmp(resource_root, "/gpu/hip/ref")) { 440d0321e0SJeremy L Thompson // LCOV_EXCL_START 452b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Hip backend cannot use resource: %s", resource); 460d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 472b730f8bSJeremy L Thompson } 482b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&resource_root)); 492b730f8bSJeremy L Thompson CeedCallBackend(CeedSetDeterministic(ceed, true)); 500d0321e0SJeremy L Thompson 510d0321e0SJeremy L Thompson Ceed_Hip *data; 522b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &data)); 532b730f8bSJeremy L Thompson CeedCallBackend(CeedSetData(ceed, data)); 542b730f8bSJeremy L Thompson CeedCallBackend(CeedHipInit(ceed, resource)); 550d0321e0SJeremy L Thompson 562b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "GetPreferredMemType", CeedGetPreferredMemType_Hip)); 572b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", CeedVectorCreate_Hip)); 582b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Hip)); 592b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", CeedBasisCreateH1_Hip)); 602b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", CeedElemRestrictionCreate_Hip)); 612b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreateBlocked", CeedElemRestrictionCreateBlocked_Hip)); 622b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate", CeedQFunctionCreate_Hip)); 632b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionContextCreate", CeedQFunctionContextCreate_Hip)); 642b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate", CeedOperatorCreate_Hip)); 652b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Hip)); 660d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 670d0321e0SJeremy L Thompson } 680d0321e0SJeremy L Thompson 690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 700d0321e0SJeremy L Thompson // Backend Register 710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 722b730f8bSJeremy L Thompson CEED_INTERN int CeedRegister_Hip(void) { return CeedRegister("/gpu/hip/ref", CeedInit_Hip, 40); } 730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 74