xref: /libCEED/backends/magma/ceed-magma.c (revision db002c03923317a1c3814dcd861330002c00a8ea)
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.h>
11 #include <ceed/backend.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   CeedCheck(!strncmp(resource, "/gpu/cuda/magma", nrc) || !strncmp(resource, "/gpu/hip/magma", nrc), ceed, CEED_ERROR_BACKEND,
27             "Magma backend cannot use resource: %s", resource);
28 
29   ierr = magma_init();
30   CeedCheck(ierr == 0, ceed, CEED_ERROR_BACKEND, "error in magma_init(): %d\n", ierr);
31 
32   Ceed_Magma *data;
33   CeedCallBackend(CeedCalloc(sizeof(Ceed_Magma), &data));
34   CeedCallBackend(CeedSetData(ceed, data));
35 
36   // kernel selection
37   data->basis_kernel_mode = MAGMA_KERNEL_DIM_SPECIFIC;
38 
39   // get/set device ID
40   const char *device_spec = strstr(resource, ":device_id=");
41   const int   deviceID    = (device_spec) ? atoi(device_spec + 11) : -1;
42 
43   int currentDeviceID;
44   magma_getdevice(&currentDeviceID);
45   if (deviceID >= 0 && currentDeviceID != deviceID) {
46     magma_setdevice(deviceID);
47     currentDeviceID = deviceID;
48   }
49   // create a queue that uses the null stream
50   data->device = currentDeviceID;
51 #ifdef CEED_MAGMA_USE_HIP
52   magma_queue_create_from_hip(data->device, NULL, NULL, NULL, &(data->queue));
53 #else
54   magma_queue_create_from_cuda(data->device, NULL, NULL, NULL, &(data->queue));
55 #endif
56 
57   // Create reference CEED that implementation will be dispatched
58   //   through unless overridden
59   Ceed ceedref;
60 #ifdef CEED_MAGMA_USE_HIP
61   CeedCallBackend(CeedInit("/gpu/hip/ref", &ceedref));
62 #else
63   CeedCallBackend(CeedInit("/gpu/cuda/ref", &ceedref));
64 #endif
65   CeedCallBackend(CeedSetDelegate(ceed, ceedref));
66 
67   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", CeedElemRestrictionCreate_Magma));
68   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Magma));
69   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", CeedBasisCreateH1_Magma));
70   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Magma));
71   return CEED_ERROR_SUCCESS;
72 }
73 
74 CEED_INTERN int CeedRegister_Magma(void) {
75 #ifdef CEED_MAGMA_USE_HIP
76   return CeedRegister("/gpu/hip/magma", CeedInit_Magma, 120);
77 #else
78   return CeedRegister("/gpu/cuda/magma", CeedInit_Magma, 120);
79 #endif
80 }
81