xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-gen/ceed-cuda-gen.c (revision 288c044332e33f37503f09b6484fec9d0a55fba1)
1241a4b83SYohann // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2241a4b83SYohann // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3241a4b83SYohann // All Rights reserved. See files LICENSE and NOTICE for details.
4241a4b83SYohann //
5241a4b83SYohann // This file is part of CEED, a collection of benchmarks, miniapps, software
6241a4b83SYohann // libraries and APIs for efficient high-order finite element and spectral
7241a4b83SYohann // element discretizations for exascale applications. For more information and
8241a4b83SYohann // source code availability see http://github.com/ceed.
9241a4b83SYohann //
10241a4b83SYohann // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11241a4b83SYohann // a collaborative effort of two U.S. Department of Energy organizations (Office
12241a4b83SYohann // of Science and the National Nuclear Security Administration) responsible for
13241a4b83SYohann // the planning and preparation of a capable exascale ecosystem, including
14241a4b83SYohann // software, applications, hardware, advanced system engineering and early
15241a4b83SYohann // testbed platforms, in support of the nation's exascale computing imperative.
16241a4b83SYohann 
17241a4b83SYohann #include <ceed-backend.h>
18241a4b83SYohann #include <string.h>
19241a4b83SYohann #include <stdarg.h>
20241a4b83SYohann #include <nvrtc.h>
21241a4b83SYohann #include <cuda.h>
22241a4b83SYohann #include <cuda_runtime.h>
23241a4b83SYohann #include "ceed-cuda-gen.h"
24241a4b83SYohann 
25241a4b83SYohann static int CeedGetPreferredMemType_Cuda_gen(CeedMemType *type) {
26241a4b83SYohann   *type = CEED_MEM_DEVICE;
27241a4b83SYohann   return 0;
28241a4b83SYohann }
29241a4b83SYohann 
30241a4b83SYohann static int CeedInit_Cuda_gen(const char *resource, Ceed ceed) {
31241a4b83SYohann   int ierr;
32241a4b83SYohann   const int nrc = 9; // number of characters in resource
33241a4b83SYohann   if (strncmp(resource, "/gpu/cuda/gen", nrc))
34241a4b83SYohann     return CeedError(ceed, 1, "Cuda backend cannot use resource: %s", resource);
35241a4b83SYohann 
36241a4b83SYohann   Ceed ceedshared;
37241a4b83SYohann   CeedInit("/gpu/cuda/shared", &ceedshared);
38241a4b83SYohann   ierr = CeedSetDelegate(ceed, ceedshared); CeedChk(ierr);
39241a4b83SYohann 
40241a4b83SYohann   const int rlen = strlen(resource);
41241a4b83SYohann   const bool slash = (rlen>nrc) ? (resource[nrc] == '/') : false;
42241a4b83SYohann   const int deviceID = (slash && rlen > nrc + 1) ? atoi(&resource[nrc + 1]) : 0;
43241a4b83SYohann 
44241a4b83SYohann   ierr = cudaSetDevice(deviceID); CeedChk(ierr);
45241a4b83SYohann 
46241a4b83SYohann   Ceed_Cuda_gen *data;
47241a4b83SYohann   ierr = CeedCalloc(1,&data); CeedChk(ierr);
48241a4b83SYohann 
49241a4b83SYohann   ierr = CeedSetData(ceed,(void *)&data); CeedChk(ierr);
50241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "GetPreferredMemType",
51241a4b83SYohann                                 CeedGetPreferredMemType_Cuda_gen); CeedChk(ierr);
52241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "QFunctionCreate",
53241a4b83SYohann                                 CeedQFunctionCreate_Cuda_gen); CeedChk(ierr);
54241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate",
55241a4b83SYohann                                 CeedOperatorCreate_Cuda_gen); CeedChk(ierr);
56241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Ceed", ceed, "CompositeOperatorCreate",
57*288c0443SJeremy L Thompson                                 CeedCompositeOperatorCreate_Cuda_gen);
58*288c0443SJeremy L Thompson   CeedChk(ierr);
59241a4b83SYohann   return 0;
60241a4b83SYohann }
61241a4b83SYohann 
62241a4b83SYohann __attribute__((constructor))
63241a4b83SYohann static void Register(void) {
64241a4b83SYohann   CeedRegister("/gpu/cuda/gen", CeedInit_Cuda_gen, 40);
65241a4b83SYohann }
66