1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 #include "ceed-magma.h" 9 10 #include <ceed/backend.h> 11 #include <ceed/ceed.h> 12 #include <stdlib.h> 13 #include <string.h> 14 15 static int CeedDestroy_Magma(Ceed ceed) { 16 Ceed_Magma *data; 17 CeedCallBackend(CeedGetData(ceed, &data)); 18 magma_queue_destroy(data->queue); 19 CeedCallBackend(CeedFree(&data)); 20 return CEED_ERROR_SUCCESS; 21 } 22 23 static int CeedInit_Magma(const char *resource, Ceed ceed) { 24 int ierr; 25 const int nrc = 14; // number of characters in resource 26 if (strncmp(resource, "/gpu/cuda/magma", nrc) && strncmp(resource, "/gpu/hip/magma", nrc)) { 27 // LCOV_EXCL_START 28 return CeedError(ceed, CEED_ERROR_BACKEND, "Magma backend cannot use resource: %s", resource); 29 // LCOV_EXCL_STOP 30 } 31 32 ierr = magma_init(); 33 if (ierr) { 34 // LCOV_EXCL_START 35 return CeedError(ceed, CEED_ERROR_BACKEND, "error in magma_init(): %d\n", ierr); 36 // LCOV_EXCL_STOP 37 } 38 39 Ceed_Magma *data; 40 CeedCallBackend(CeedCalloc(sizeof(Ceed_Magma), &data)); 41 CeedCallBackend(CeedSetData(ceed, data)); 42 43 // kernel selection 44 data->basis_kernel_mode = MAGMA_KERNEL_DIM_SPECIFIC; 45 46 // get/set device ID 47 const char *device_spec = strstr(resource, ":device_id="); 48 const int deviceID = (device_spec) ? atoi(device_spec + 11) : -1; 49 50 int currentDeviceID; 51 magma_getdevice(¤tDeviceID); 52 if (deviceID >= 0 && currentDeviceID != deviceID) { 53 magma_setdevice(deviceID); 54 currentDeviceID = deviceID; 55 } 56 // create a queue that uses the null stream 57 data->device = currentDeviceID; 58 #ifdef CEED_MAGMA_USE_HIP 59 magma_queue_create_from_hip(data->device, NULL, NULL, NULL, &(data->queue)); 60 #else 61 magma_queue_create_from_cuda(data->device, NULL, NULL, NULL, &(data->queue)); 62 #endif 63 64 // Create reference CEED that implementation will be dispatched 65 // through unless overridden 66 Ceed ceedref; 67 #ifdef CEED_MAGMA_USE_HIP 68 CeedCallBackend(CeedInit("/gpu/hip/ref", &ceedref)); 69 #else 70 CeedCallBackend(CeedInit("/gpu/cuda/ref", &ceedref)); 71 #endif 72 CeedCallBackend(CeedSetDelegate(ceed, ceedref)); 73 74 CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", CeedElemRestrictionCreate_Magma)); 75 CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreateBlocked", CeedElemRestrictionCreateBlocked_Magma)); 76 CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Magma)); 77 CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", CeedBasisCreateH1_Magma)); 78 CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Magma)); 79 return CEED_ERROR_SUCCESS; 80 } 81 82 CEED_INTERN int CeedRegister_Magma(void) { 83 #ifdef CEED_MAGMA_USE_HIP 84 return CeedRegister("/gpu/hip/magma", CeedInit_Magma, 120); 85 #else 86 return CeedRegister("/gpu/cuda/magma", CeedInit_Magma, 120); 87 #endif 88 } 89