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