1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC. 2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707. 3 // All Rights reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 #include <ceed/ceed.h> 18 #include <ceed/backend.h> 19 #include <string.h> 20 #include <stdlib.h> 21 #include "ceed-magma.h" 22 23 static int CeedDestroy_Magma(Ceed ceed) { 24 int ierr; 25 Ceed_Magma *data; 26 ierr = CeedGetData(ceed, &data); CeedChkBackend(ierr); 27 magma_queue_destroy( data->queue ); 28 ierr = CeedFree(&data); CeedChkBackend(ierr); 29 return CEED_ERROR_SUCCESS; 30 } 31 32 static int CeedInit_Magma(const char *resource, Ceed ceed) { 33 int ierr; 34 const int nrc = 14; // number of characters in resource 35 if (strncmp(resource, "/gpu/cuda/magma", nrc) 36 && strncmp(resource, "/gpu/hip/magma", nrc)) 37 // LCOV_EXCL_START 38 return CeedError(ceed, CEED_ERROR_BACKEND, 39 "Magma backend cannot use resource: %s", resource); 40 // LCOV_EXCL_STOP 41 42 ierr = magma_init(); 43 if (ierr) 44 // LCOV_EXCL_START 45 return CeedError(ceed, CEED_ERROR_BACKEND, "error in magma_init(): %d\n", ierr); 46 // LCOV_EXCL_STOP 47 48 Ceed_Magma *data; 49 ierr = CeedCalloc(sizeof(Ceed_Magma), &data); CeedChkBackend(ierr); 50 ierr = CeedSetData(ceed, data); CeedChkBackend(ierr); 51 52 // kernel selection 53 data->basis_kernel_mode = MAGMA_KERNEL_DIM_SPECIFIC; 54 55 // kernel max threads per thread-block 56 data->maxthreads[0] = 128; // for 1D kernels 57 data->maxthreads[1] = 128; // for 2D kernels 58 data->maxthreads[2] = 64; // for 3D kernels 59 60 // get/set device ID 61 const char *device_spec = strstr(resource, ":device_id="); 62 const int deviceID = (device_spec) ? atoi(device_spec+11) : -1; 63 64 int currentDeviceID; 65 magma_getdevice(¤tDeviceID); 66 if (deviceID >= 0 && currentDeviceID != deviceID) { 67 magma_setdevice(deviceID); 68 currentDeviceID = deviceID; 69 } 70 // create a queue that uses the null stream 71 data->device = currentDeviceID; 72 #ifdef HAVE_HIP 73 magma_queue_create_from_hip(data->device, NULL, NULL, NULL, &(data->queue)); 74 #else 75 magma_queue_create_from_cuda(data->device, NULL, NULL, NULL, &(data->queue)); 76 #endif 77 78 // Create reference CEED that implementation will be dispatched 79 // through unless overridden 80 Ceed ceedref; 81 #ifdef HAVE_HIP 82 CeedInit("/gpu/hip/ref", &ceedref); 83 #else 84 CeedInit("/gpu/cuda/ref", &ceedref); 85 #endif 86 ierr = CeedSetDelegate(ceed, ceedref); CeedChkBackend(ierr); 87 88 ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", 89 CeedElemRestrictionCreate_Magma); CeedChkBackend(ierr); 90 ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, 91 "ElemRestrictionCreateBlocked", 92 CeedElemRestrictionCreateBlocked_Magma); CeedChkBackend(ierr); 93 ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", 94 CeedBasisCreateTensorH1_Magma); CeedChkBackend(ierr); 95 ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", 96 CeedBasisCreateH1_Magma); CeedChkBackend(ierr); 97 ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", 98 CeedDestroy_Magma); CeedChkBackend(ierr); 99 return CEED_ERROR_SUCCESS; 100 } 101 102 CEED_INTERN int CeedRegister_Magma(void) { 103 #ifdef HAVE_HIP 104 return CeedRegister("/gpu/hip/magma", CeedInit_Magma, 120); 105 #else 106 return CeedRegister("/gpu/cuda/magma", CeedInit_Magma, 120); 107 #endif 108 } 109