xref: /libCEED/backends/cuda-gen/ceed-cuda-gen-qfunction.c (revision 288c044332e33f37503f09b6484fec9d0a55fba1)
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 <stdio.h>
20 #include "../cuda/ceed-cuda.h"
21 #include "ceed-cuda-gen.h"
22 
23 static int CeedQFunctionApply_Cuda_gen(CeedQFunction qf, CeedInt Q,
24                                        CeedVector *U, CeedVector *V) {
25   int ierr;
26   Ceed ceed;
27   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
28   return CeedError(ceed, 1, "Backend does not implement QFunctionApply");
29 }
30 
31 static int CeedQFunctionDestroy_Cuda_gen(CeedQFunction qf) {
32   int ierr;
33   CeedQFunction_Cuda_gen *data;
34   ierr = CeedQFunctionGetData(qf, (void *)&data); CeedChk(ierr);
35   Ceed ceed;
36   ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChk(ierr);
37 
38   ierr = cudaFree(data->d_c); CeedChk_Cu(ceed, ierr);
39 
40   ierr = CeedFree(&data); CeedChk(ierr);
41 
42   return 0;
43 }
44 
45 static int loadCudaFunction(CeedQFunction qf, char *c_src_file) {
46   int ierr;
47   Ceed ceed;
48   CeedQFunctionGetCeed(qf, &ceed);
49   char *cuda_file;
50   ierr = CeedCalloc(CUDA_MAX_PATH, &cuda_file); CeedChk(ierr);
51   memcpy(cuda_file, c_src_file, strlen(c_src_file));
52   const char *last_dot = strrchr(cuda_file, '.');
53   if (!last_dot)
54     return CeedError(ceed, 1, "Cannot find file's extension!");
55   const size_t cuda_path_len = last_dot - cuda_file;
56   strcpy(&cuda_file[cuda_path_len], ".h");
57   //*******************
58   FILE *fp;
59   long lSize;
60   char *buffer;
61 
62   fp = fopen ( cuda_file, "rb" );
63   if( !fp ) CeedError(ceed, 1, "Couldn't open the Cuda file for the QFunction.");
64 
65   fseek( fp, 0L, SEEK_END);
66   lSize = ftell( fp );
67   rewind( fp );
68 
69   /* allocate memory for entire content */
70   ierr = CeedCalloc( lSize+1, &buffer ); CeedChk(ierr);
71 
72   /* copy the file into the buffer */
73   if( 1!=fread( buffer, lSize, 1, fp) ) {
74     fclose(fp);
75     CeedFree(&buffer);
76     CeedError(ceed, 1, "Couldn't read the Cuda file for the QFunction.");
77   }
78 
79   //FIXME: the magic number 16 should be defined somewhere...
80   char *fields_string =
81     "typedef struct { const CeedScalar* inputs[16]; CeedScalar* outputs[16]; } Fields_Cuda_gen;";
82   char *source = (char *) malloc(1 + strlen(fields_string)+ strlen(buffer) );
83   strcpy(source, fields_string);
84   strcat(source, buffer);
85 
86   //********************
87   CeedQFunction_Cuda_gen *data;
88   ierr = CeedQFunctionGetData(qf, (void *)&data); CeedChk(ierr);
89   data->qFunctionSource = buffer;
90 
91   //********************
92   fclose(fp);
93 
94   return 0;
95 }
96 
97 int CeedQFunctionCreate_Cuda_gen(CeedQFunction qf) {
98   int ierr;
99   Ceed ceed;
100   CeedQFunctionGetCeed(qf, &ceed);
101   CeedQFunction_Cuda_gen *data;
102   ierr = CeedCalloc(1,&data); CeedChk(ierr);
103   ierr = CeedQFunctionSetData(qf, (void *)&data); CeedChk(ierr);
104   size_t ctxsize;
105   ierr = CeedQFunctionGetContextSize(qf, &ctxsize); CeedChk(ierr);
106   ierr = cudaMalloc(&data->d_c, ctxsize); CeedChk_Cu(ceed, ierr);
107 
108   char *source;
109   ierr = CeedQFunctionGetSourcePath(qf, &source); CeedChk(ierr);
110   const char *funname = strrchr(source, ':') + 1;
111   data->qFunctionName = (char *)funname;
112   const int filenamelen = funname - source;
113   char filename[filenamelen];
114   memcpy(filename, source, filenamelen - 1);
115   filename[filenamelen - 1] = '\0';
116   ierr = loadCudaFunction(qf, filename); CeedChk(ierr);
117 
118   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply",
119                                 CeedQFunctionApply_Cuda_gen); CeedChk(ierr);
120   ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy",
121                                 CeedQFunctionDestroy_Cuda_gen); CeedChk(ierr);
122   return 0;
123 }
124