xref: /libCEED/backends/cuda-gen/ceed-cuda-gen.c (revision 6c5df90db8677641a04ff505ccfa313a57dff4e5)
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-backend.h>
18 #include <string.h>
19 #include <stdarg.h>
20 #include <nvrtc.h>
21 #include <cuda.h>
22 #include <cuda_runtime.h>
23 #include "ceed-cuda-gen.h"
24 
25 static int CeedGetPreferredMemType_Cuda_gen(CeedMemType *type) {
26   *type = CEED_MEM_DEVICE;
27   return 0;
28 }
29 
30 static int CeedInit_Cuda_gen(const char *resource, Ceed ceed) {
31   int ierr;
32   const int nrc = 9; // number of characters in resource
33   if (strncmp(resource, "/gpu/cuda/gen", nrc))
34     return CeedError(ceed, 1, "Cuda backend cannot use resource: %s", resource);
35 
36   Ceed ceedshared;
37   CeedInit("/gpu/cuda/shared", &ceedshared);
38   ierr = CeedSetDelegate(ceed, ceedshared); CeedChk(ierr);
39 
40   const int rlen = strlen(resource);
41   const bool slash = (rlen>nrc) ? (resource[nrc] == '/') : false;
42   const int deviceID = (slash && rlen > nrc + 1) ? atoi(&resource[nrc + 1]) : 0;
43 
44   ierr = cudaSetDevice(deviceID); CeedChk(ierr);
45 
46   Ceed_Cuda_gen *data;
47   ierr = CeedCalloc(1,&data); CeedChk(ierr);
48 
49   ierr = CeedSetData(ceed,(void *)&data); CeedChk(ierr);
50   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "GetPreferredMemType",
51                                 CeedGetPreferredMemType_Cuda_gen); CeedChk(ierr);
52   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate",
53                                 CeedQFunctionCreate_Cuda_gen); CeedChk(ierr);
54   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate",
55                                 CeedOperatorCreate_Cuda_gen); CeedChk(ierr);
56   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "CompositeOperatorCreate",
57                                 CeedCompositeOperatorCreate_Cuda_gen);
58   CeedChk(ierr);
59   return 0;
60 }
61 
62 __attribute__((constructor))
63 static void Register(void) {
64   CeedRegister("/gpu/cuda/gen", CeedInit_Cuda_gen, 40);
65 }
66