xref: /libCEED/backends/magma/ceed-magma-det.c (revision 05a9c2bb2f62eff0bfbb15aec60b0312b25f01c2)
1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3 // All Rights reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #include <ceed/ceed.h>
18 #include <ceed/backend.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include "ceed-magma.h"
22 
23 CEED_INTERN int CeedInit_Magma_Det(const char *resource, Ceed ceed) {
24   int ierr;
25   const int nrc = 18; // number of characters in resource
26   if (strncmp(resource, "/gpu/cuda/magma/det", nrc)
27       && strncmp(resource, "/gpu/hip/magma/det", nrc))
28     // LCOV_EXCL_START
29     return CeedError(ceed, CEED_ERROR_BACKEND,
30                      "Magma backend cannot use resource: %s", resource);
31   // LCOV_EXCL_STOP
32   ierr = CeedSetDeterministic(ceed, true); CeedChkBackend(ierr);
33 
34   Ceed_Magma *data;
35   ierr = CeedCalloc(sizeof(Ceed_Magma), &data); CeedChkBackend(ierr);
36   ierr = CeedSetData(ceed, data); CeedChkBackend(ierr);
37 
38   // get/set device ID
39   const char *device_spec = strstr(resource, ":device_id=");
40   const int deviceID = (device_spec) ? atoi(device_spec+11) : -1;
41 
42   int currentDeviceID;
43   magma_getdevice(&currentDeviceID);
44   if (deviceID >= 0 && currentDeviceID != deviceID) {
45     magma_setdevice(deviceID);
46     currentDeviceID = deviceID;
47   }
48   // create a queue that uses the null stream
49   data->device = currentDeviceID;
50 
51   // Create reference CEED that implementation will be dispatched
52   //   through unless overridden
53   Ceed ceedref;
54   #ifdef HAVE_HIP
55   CeedInit("/gpu/hip/magma", &ceedref);
56   #else
57   CeedInit("/gpu/cuda/magma", &ceedref);
58   #endif
59   ierr = CeedSetDelegate(ceed, ceedref); CeedChkBackend(ierr);
60 
61   // Create reference CEED for restriction
62   Ceed restrictionceedref;
63   #ifdef HAVE_HIP
64   CeedInit("/gpu/hip/ref", &restrictionceedref);
65   #else
66   CeedInit("/gpu/cuda/ref", &restrictionceedref);
67   #endif
68   ierr = CeedSetObjectDelegate(ceed, restrictionceedref, "ElemRestriction");
69   CeedChkBackend(ierr);
70 
71   return CEED_ERROR_SUCCESS;
72 }
73 
74 CEED_INTERN int CeedRegister_Magma_Det(void) {
75   #ifdef HAVE_HIP
76   return CeedRegister("/gpu/hip/magma/det", CeedInit_Magma_Det, 125);
77   #else
78   return CeedRegister("/gpu/cuda/magma/det", CeedInit_Magma_Det, 125);
79   #endif
80 }
81