xref: /libCEED/backends/magma/ceed-magma-det.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
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.h>
9 #include <ceed/backend.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include "ceed-magma.h"
14 
15 CEED_INTERN int CeedInit_Magma_Det(const char *resource, Ceed ceed) {
16   const int nrc = 18;  // number of characters in resource
17   if (strncmp(resource, "/gpu/cuda/magma/det", nrc) && strncmp(resource, "/gpu/hip/magma/det", nrc)) {
18     // LCOV_EXCL_START
19     return CeedError(ceed, CEED_ERROR_BACKEND, "Magma backend cannot use resource: %s", resource);
20     // LCOV_EXCL_STOP
21   }
22   CeedCallBackend(CeedSetDeterministic(ceed, true));
23 
24   Ceed_Magma *data;
25   CeedCallBackend(CeedCalloc(sizeof(Ceed_Magma), &data));
26   CeedCallBackend(CeedSetData(ceed, data));
27 
28   // get/set device ID
29   const char *device_spec = strstr(resource, ":device_id=");
30   const int   deviceID    = (device_spec) ? atoi(device_spec + 11) : -1;
31 
32   int currentDeviceID;
33   magma_getdevice(&currentDeviceID);
34   if (deviceID >= 0 && currentDeviceID != deviceID) {
35     magma_setdevice(deviceID);
36     currentDeviceID = deviceID;
37   }
38   // create a queue that uses the null stream
39   data->device = currentDeviceID;
40 
41   // Create reference CEED that implementation will be dispatched
42   //   through unless overridden
43   Ceed ceedref;
44 #ifdef CEED_MAGMA_USE_HIP
45   CeedCallBackend(CeedInit("/gpu/hip/magma", &ceedref));
46 #else
47   CeedCallBackend(CeedInit("/gpu/cuda/magma", &ceedref));
48 #endif
49   CeedCallBackend(CeedSetDelegate(ceed, ceedref));
50 
51   // Create reference CEED for restriction
52   Ceed restrictionceedref;
53 #ifdef CEED_MAGMA_USE_HIP
54   CeedInit("/gpu/hip/ref", &restrictionceedref);
55 #else
56   CeedInit("/gpu/cuda/ref", &restrictionceedref);
57 #endif
58   CeedCallBackend(CeedSetObjectDelegate(ceed, restrictionceedref, "ElemRestriction"));
59 
60   return CEED_ERROR_SUCCESS;
61 }
62 
63 CEED_INTERN int CeedRegister_Magma_Det(void) {
64 #ifdef CEED_MAGMA_USE_HIP
65   return CeedRegister("/gpu/hip/magma/det", CeedInit_Magma_Det, 125);
66 #else
67   return CeedRegister("/gpu/cuda/magma/det", CeedInit_Magma_Det, 125);
68 #endif
69 }
70