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