xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-gen/ceed-cuda-gen-qfunction.c (revision 777ff853944a0dbc06f7f09486fdf4674828e728)
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 <string.h>
18241a4b83SYohann #include <stdio.h>
19241a4b83SYohann #include "ceed-cuda-gen.h"
20241a4b83SYohann 
21ab213215SJeremy L Thompson //------------------------------------------------------------------------------
22ab213215SJeremy L Thompson // Apply QFunction
23ab213215SJeremy L Thompson //------------------------------------------------------------------------------
24241a4b83SYohann static int CeedQFunctionApply_Cuda_gen(CeedQFunction qf, CeedInt Q,
25241a4b83SYohann                                        CeedVector *U, CeedVector *V) {
26241a4b83SYohann   int ierr;
27241a4b83SYohann   Ceed ceed;
28241a4b83SYohann   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
29241a4b83SYohann   return CeedError(ceed, 1, "Backend does not implement QFunctionApply");
30241a4b83SYohann }
31241a4b83SYohann 
32ab213215SJeremy L Thompson //------------------------------------------------------------------------------
33ab213215SJeremy L Thompson // Destroy QFunction
34ab213215SJeremy L Thompson //------------------------------------------------------------------------------
35241a4b83SYohann static int CeedQFunctionDestroy_Cuda_gen(CeedQFunction qf) {
36241a4b83SYohann   int ierr;
37241a4b83SYohann   CeedQFunction_Cuda_gen *data;
38*777ff853SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &data); CeedChk(ierr);
39241a4b83SYohann   Ceed ceed;
40241a4b83SYohann   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
41241a4b83SYohann   ierr = cudaFree(data->d_c); CeedChk_Cu(ceed, ierr);
42465fc175SJeremy L Thompson   ierr = CeedFree(&data->qFunctionSource); CeedChk(ierr);
43241a4b83SYohann   ierr = CeedFree(&data); CeedChk(ierr);
44241a4b83SYohann   return 0;
45241a4b83SYohann }
46241a4b83SYohann 
47ab213215SJeremy L Thompson //------------------------------------------------------------------------------
48ab213215SJeremy L Thompson // Load QFunction
49ab213215SJeremy L Thompson //------------------------------------------------------------------------------
50241a4b83SYohann static int loadCudaFunction(CeedQFunction qf, char *c_src_file) {
51241a4b83SYohann   int ierr;
52241a4b83SYohann   Ceed ceed;
53241a4b83SYohann   CeedQFunctionGetCeed(qf, &ceed);
54465fc175SJeremy L Thompson   CeedQFunction_Cuda_gen *data;
55*777ff853SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &data); CeedChk(ierr);
56ab213215SJeremy L Thompson 
57ab213215SJeremy L Thompson   // Find source file
58241a4b83SYohann   char *cuda_file;
59241a4b83SYohann   ierr = CeedCalloc(CUDA_MAX_PATH, &cuda_file); CeedChk(ierr);
60241a4b83SYohann   memcpy(cuda_file, c_src_file, strlen(c_src_file));
61241a4b83SYohann   const char *last_dot = strrchr(cuda_file, '.');
62241a4b83SYohann   if (!last_dot)
63241a4b83SYohann     return CeedError(ceed, 1, "Cannot find file's extension!");
64241a4b83SYohann   const size_t cuda_path_len = last_dot - cuda_file;
65465fc175SJeremy L Thompson   strncpy(&cuda_file[cuda_path_len], ".h", 3);
66ab213215SJeremy L Thompson 
67ab213215SJeremy L Thompson   // Open source file
68241a4b83SYohann   FILE *fp;
69241a4b83SYohann   long lSize;
70241a4b83SYohann   char *buffer;
71241a4b83SYohann   fp = fopen (cuda_file, "rb");
72ab213215SJeremy L Thompson   if (!fp)
73e9f4dca0SJeremy L Thompson     // LCOV_EXCL_START
74ab213215SJeremy L Thompson     CeedError(ceed, 1, "Couldn't open the Cuda file for the QFunction.");
75e9f4dca0SJeremy L Thompson   // LCOV_EXCL_STOP
76241a4b83SYohann 
77ab213215SJeremy L Thompson   // Compute size of source file
78241a4b83SYohann   fseek(fp, 0L, SEEK_END);
79241a4b83SYohann   lSize = ftell(fp);
80241a4b83SYohann   rewind(fp);
81241a4b83SYohann 
82ab213215SJeremy L Thompson   // Allocate memory for entire content
83241a4b83SYohann   ierr = CeedCalloc(lSize+1, &buffer); CeedChk(ierr);
84241a4b83SYohann 
85ab213215SJeremy L Thompson   // Copy the file into the buffer
86241a4b83SYohann   if (1 != fread(buffer, lSize, 1, fp)) {
876bbcfef4SJeremy L Thompson     // LCOV_EXCL_START
88241a4b83SYohann     fclose(fp);
89465fc175SJeremy L Thompson     ierr = CeedFree(&buffer); CeedChk(ierr);
90241a4b83SYohann     CeedError(ceed, 1, "Couldn't read the Cuda file for the QFunction.");
91e9f4dca0SJeremy L Thompson     // LCOV_EXCL_STOP
92241a4b83SYohann   }
93241a4b83SYohann 
94ab213215SJeremy L Thompson   // Append typedef and save source string
95241a4b83SYohann   // FIXME: the magic number 16 should be defined somewhere...
96241a4b83SYohann   char *fields_string =
97241a4b83SYohann     "typedef struct { const CeedScalar* inputs[16]; CeedScalar* outputs[16]; } Fields_Cuda_gen;";
98465fc175SJeremy L Thompson   ierr = CeedMalloc(1 + strlen(fields_string) + strlen(buffer),
99465fc175SJeremy L Thompson                     &data->qFunctionSource); CeedChk(ierr);
1009ff7e165SJed Brown   memcpy(data->qFunctionSource, fields_string, strlen(fields_string));
1019ff7e165SJed Brown   memcpy(data->qFunctionSource + strlen(fields_string), buffer,
1029ff7e165SJed Brown          strlen(buffer) + 1);
103241a4b83SYohann 
104465fc175SJeremy L Thompson   // Cleanup
105465fc175SJeremy L Thompson   ierr = CeedFree(&buffer); CeedChk(ierr);
106ab213215SJeremy L Thompson   fclose(fp);
1073636fb3dSJeremy L Thompson   ierr = CeedFree(&cuda_file); CeedChk(ierr);
108241a4b83SYohann   return 0;
109241a4b83SYohann }
110241a4b83SYohann 
111ab213215SJeremy L Thompson //------------------------------------------------------------------------------
112ab213215SJeremy L Thompson // Create QFunction
113ab213215SJeremy L Thompson //------------------------------------------------------------------------------
114241a4b83SYohann int CeedQFunctionCreate_Cuda_gen(CeedQFunction qf) {
115241a4b83SYohann   int ierr;
116241a4b83SYohann   Ceed ceed;
117241a4b83SYohann   CeedQFunctionGetCeed(qf, &ceed);
118241a4b83SYohann   CeedQFunction_Cuda_gen *data;
119241a4b83SYohann   ierr = CeedCalloc(1, &data); CeedChk(ierr);
120*777ff853SJeremy L Thompson   ierr = CeedQFunctionSetData(qf, data); CeedChk(ierr);
121241a4b83SYohann 
122288c0443SJeremy L Thompson   char *source;
123288c0443SJeremy L Thompson   ierr = CeedQFunctionGetSourcePath(qf, &source); CeedChk(ierr);
124503c598bSjeremylt   const char *funname = strrchr(source, ':') + 1;
125241a4b83SYohann   data->qFunctionName = (char *)funname;
126288c0443SJeremy L Thompson   const int filenamelen = funname - source;
127241a4b83SYohann   char filename[filenamelen];
128288c0443SJeremy L Thompson   memcpy(filename, source, filenamelen - 1);
129241a4b83SYohann   filename[filenamelen - 1] = '\0';
130241a4b83SYohann   ierr = loadCudaFunction(qf, filename); CeedChk(ierr);
131241a4b83SYohann 
132241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
133241a4b83SYohann                                 CeedQFunctionApply_Cuda_gen); CeedChk(ierr);
134241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
135241a4b83SYohann                                 CeedQFunctionDestroy_Cuda_gen); CeedChk(ierr);
136241a4b83SYohann   return 0;
137241a4b83SYohann }
138ab213215SJeremy L Thompson //------------------------------------------------------------------------------
139