xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-gen/ceed-cuda-gen.c (revision 241a4b83dc9c714eb4bcc90729a46be02322143a)
1*241a4b83SYohann // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2*241a4b83SYohann // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3*241a4b83SYohann // All Rights reserved. See files LICENSE and NOTICE for details.
4*241a4b83SYohann //
5*241a4b83SYohann // This file is part of CEED, a collection of benchmarks, miniapps, software
6*241a4b83SYohann // libraries and APIs for efficient high-order finite element and spectral
7*241a4b83SYohann // element discretizations for exascale applications. For more information and
8*241a4b83SYohann // source code availability see http://github.com/ceed.
9*241a4b83SYohann //
10*241a4b83SYohann // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11*241a4b83SYohann // a collaborative effort of two U.S. Department of Energy organizations (Office
12*241a4b83SYohann // of Science and the National Nuclear Security Administration) responsible for
13*241a4b83SYohann // the planning and preparation of a capable exascale ecosystem, including
14*241a4b83SYohann // software, applications, hardware, advanced system engineering and early
15*241a4b83SYohann // testbed platforms, in support of the nation's exascale computing imperative.
16*241a4b83SYohann 
17*241a4b83SYohann #include <ceed-backend.h>
18*241a4b83SYohann #include <string.h>
19*241a4b83SYohann #include <stdarg.h>
20*241a4b83SYohann #include <nvrtc.h>
21*241a4b83SYohann #include <cuda.h>
22*241a4b83SYohann #include <cuda_runtime.h>
23*241a4b83SYohann #include "ceed-cuda-gen.h"
24*241a4b83SYohann 
25*241a4b83SYohann static int CeedGetPreferredMemType_Cuda_gen(CeedMemType *type) {
26*241a4b83SYohann   *type = CEED_MEM_DEVICE;
27*241a4b83SYohann   return 0;
28*241a4b83SYohann }
29*241a4b83SYohann 
30*241a4b83SYohann static int CeedInit_Cuda_gen(const char *resource, Ceed ceed) {
31*241a4b83SYohann   int ierr;
32*241a4b83SYohann   const int nrc = 9; // number of characters in resource
33*241a4b83SYohann   if (strncmp(resource, "/gpu/cuda/gen", nrc))
34*241a4b83SYohann     return CeedError(ceed, 1, "Cuda backend cannot use resource: %s", resource);
35*241a4b83SYohann 
36*241a4b83SYohann   Ceed ceedshared;
37*241a4b83SYohann   CeedInit("/gpu/cuda/shared", &ceedshared);
38*241a4b83SYohann   ierr = CeedSetDelegate(ceed, ceedshared); CeedChk(ierr);
39*241a4b83SYohann 
40*241a4b83SYohann   const int rlen = strlen(resource);
41*241a4b83SYohann   const bool slash = (rlen>nrc) ? (resource[nrc] == '/') : false;
42*241a4b83SYohann   const int deviceID = (slash && rlen > nrc + 1) ? atoi(&resource[nrc + 1]) : 0;
43*241a4b83SYohann 
44*241a4b83SYohann   ierr = cudaSetDevice(deviceID); CeedChk(ierr);
45*241a4b83SYohann 
46*241a4b83SYohann   Ceed_Cuda_gen *data;
47*241a4b83SYohann   ierr = CeedCalloc(1,&data); CeedChk(ierr);
48*241a4b83SYohann 
49*241a4b83SYohann   ierr = CeedSetData(ceed,(void *)&data); CeedChk(ierr);
50*241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "GetPreferredMemType",
51*241a4b83SYohann                                 CeedGetPreferredMemType_Cuda_gen); CeedChk(ierr);
52*241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate",
53*241a4b83SYohann                                 CeedQFunctionCreate_Cuda_gen); CeedChk(ierr);
54*241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate",
55*241a4b83SYohann                                 CeedOperatorCreate_Cuda_gen); CeedChk(ierr);
56*241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "CompositeOperatorCreate",
57*241a4b83SYohann                                 CeedCompositeOperatorCreate_Cuda_gen); CeedChk(ierr);
58*241a4b83SYohann   return 0;
59*241a4b83SYohann }
60*241a4b83SYohann 
61*241a4b83SYohann __attribute__((constructor))
62*241a4b83SYohann static void Register(void) {
63*241a4b83SYohann   CeedRegister("/gpu/cuda/gen", CeedInit_Cuda_gen, 40);
64*241a4b83SYohann }
65