xref: /libCEED/rust/libceed-sys/c-src/backends/magma/ceed-magma.c (revision 58549094d8a305d0f4b066b44680cf34cff212e7)
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   const int nrc = 14;  // number of characters in resource
25   CeedCheck(!strncmp(resource, "/gpu/cuda/magma", nrc) || !strncmp(resource, "/gpu/hip/magma", nrc), ceed, CEED_ERROR_BACKEND,
26             "Magma backend cannot use resource: %s", resource);
27 
28   Ceed_Magma *data;
29   CeedCallBackend(CeedCalloc(1, &data));
30   CeedCallBackend(CeedSetData(ceed, data));
31 
32   // Get/set device ID
33   const char *device_spec = strstr(resource, ":device_id=");
34   const int   device_id   = (device_spec) ? atoi(device_spec + 11) : -1;
35   int         current_device_id;
36   CeedCallBackend(magma_init());
37   magma_getdevice(&current_device_id);
38   if (device_id >= 0 && current_device_id != device_id) {
39     magma_setdevice(device_id);
40     current_device_id = device_id;
41   }
42   data->device_id = current_device_id;
43 
44   // Create a queue that uses the null stream
45 #ifdef CEED_MAGMA_USE_HIP
46   magma_queue_create_from_hip(data->device_id, NULL, NULL, NULL, &(data->queue));
47 #else
48   magma_queue_create_from_cuda(data->device_id, NULL, NULL, NULL, &(data->queue));
49 #endif
50 
51   // Create reference Ceed that implementation will be dispatched through unless overridden
52   Ceed ceed_ref;
53 #ifdef CEED_MAGMA_USE_HIP
54   CeedCallBackend(CeedInit("/gpu/hip/ref", &ceed_ref));
55 #else
56   CeedCallBackend(CeedInit("/gpu/cuda/ref", &ceed_ref));
57 #endif
58   CeedCallBackend(CeedSetDelegate(ceed, ceed_ref));
59 
60   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Magma));
61   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", CeedBasisCreateH1_Magma));
62   CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Magma));
63 
64   return CEED_ERROR_SUCCESS;
65 }
66 
67 CEED_INTERN int CeedRegister_Magma(void) {
68 #ifdef CEED_MAGMA_USE_HIP
69   return CeedRegister("/gpu/hip/magma", CeedInit_Magma, 120);
70 #else
71   return CeedRegister("/gpu/cuda/magma", CeedInit_Magma, 120);
72 #endif
73 }
74