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