xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-gen/ceed-cuda-gen-operator.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 "ceed-cuda-gen.h"
18241a4b83SYohann #include "ceed-cuda-gen-operator-build.h"
19241a4b83SYohann 
20ab213215SJeremy L Thompson //------------------------------------------------------------------------------
21ab213215SJeremy L Thompson // Destroy operator
22ab213215SJeremy L Thompson //------------------------------------------------------------------------------
23241a4b83SYohann static int CeedOperatorDestroy_Cuda_gen(CeedOperator op) {
24241a4b83SYohann   int ierr;
25241a4b83SYohann   CeedOperator_Cuda_gen *impl;
26*777ff853SJeremy L Thompson   ierr = CeedOperatorGetData(op, &impl); CeedChk(ierr);
27241a4b83SYohann   ierr = CeedFree(&impl); CeedChk(ierr);
28241a4b83SYohann   return 0;
29241a4b83SYohann }
30241a4b83SYohann 
31ab213215SJeremy L Thompson //------------------------------------------------------------------------------
32ab213215SJeremy L Thompson // Apply and add to output
33ab213215SJeremy L Thompson //------------------------------------------------------------------------------
343e0c3786SYohann Dudouit static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector invec,
35241a4b83SYohann     CeedVector outvec, CeedRequest *request) {
36241a4b83SYohann   int ierr;
37241a4b83SYohann   Ceed ceed;
38241a4b83SYohann   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
39241a4b83SYohann   CeedOperator_Cuda_gen *data;
40*777ff853SJeremy L Thompson   ierr = CeedOperatorGetData(op, &data); CeedChk(ierr);
41241a4b83SYohann   CeedQFunction qf;
42241a4b83SYohann   CeedQFunction_Cuda_gen *qf_data;
43241a4b83SYohann   ierr = CeedOperatorGetQFunction(op, &qf); CeedChk(ierr);
44*777ff853SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &qf_data); CeedChk(ierr);
45241a4b83SYohann   CeedInt nelem, numinputfields, numoutputfields;
46241a4b83SYohann   ierr = CeedOperatorGetNumElements(op, &nelem); CeedChk(ierr);
47241a4b83SYohann   ierr = CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields);
48241a4b83SYohann   CeedChk(ierr);
49241a4b83SYohann   CeedOperatorField *opinputfields, *opoutputfields;
50241a4b83SYohann   ierr = CeedOperatorGetFields(op, &opinputfields, &opoutputfields);
51241a4b83SYohann   CeedChk(ierr);
52241a4b83SYohann   CeedQFunctionField *qfinputfields, *qfoutputfields;
53241a4b83SYohann   ierr = CeedQFunctionGetFields(qf, &qfinputfields, &qfoutputfields);
54241a4b83SYohann   CeedChk(ierr);
55241a4b83SYohann   CeedEvalMode emode;
563b2939feSjeremylt   CeedVector vec, outvecs[16] = {};
57241a4b83SYohann 
58241a4b83SYohann   //Creation of the operator
59241a4b83SYohann   ierr = CeedCudaGenOperatorBuild(op); CeedChk(ierr);
60241a4b83SYohann 
61241a4b83SYohann   // Input vectors
62241a4b83SYohann   for (CeedInt i = 0; i < numinputfields; i++) {
63241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
64241a4b83SYohann     CeedChk(ierr);
65241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
66241a4b83SYohann       data->fields.in[i] = NULL;
67241a4b83SYohann     } else {
68241a4b83SYohann       // Get input vector
69241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChk(ierr);
70241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
71241a4b83SYohann       ierr = CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.in[i]);
72241a4b83SYohann       CeedChk(ierr);
73241a4b83SYohann     }
74241a4b83SYohann   }
75241a4b83SYohann 
76241a4b83SYohann   // Output vectors
77241a4b83SYohann   for (CeedInt i = 0; i < numoutputfields; i++) {
78241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
79241a4b83SYohann     CeedChk(ierr);
80241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
81241a4b83SYohann       data->fields.out[i] = NULL;
82241a4b83SYohann     } else {
83241a4b83SYohann       // Get output vector
84241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr);
85241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
863b2939feSjeremylt       outvecs[i] = vec;
873b2939feSjeremylt       // Check for multiple output modes
883b2939feSjeremylt       CeedInt index = -1;
893b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
903b2939feSjeremylt         if (vec == outvecs[j]) {
913b2939feSjeremylt           index = j;
923b2939feSjeremylt           break;
933b2939feSjeremylt         }
943b2939feSjeremylt       }
953b2939feSjeremylt       if (index == -1) {
96241a4b83SYohann         ierr = CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.out[i]);
97241a4b83SYohann         CeedChk(ierr);
983b2939feSjeremylt       } else {
993b2939feSjeremylt         data->fields.out[i] = data->fields.out[index];
1003b2939feSjeremylt       }
101241a4b83SYohann     }
102241a4b83SYohann   }
103241a4b83SYohann 
104*777ff853SJeremy L Thompson   // Get context data
105*777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
106241a4b83SYohann   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
107*777ff853SJeremy L Thompson   if (ctx) {
108*777ff853SJeremy L Thompson     ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_DEVICE, &qf_data->d_c);
109*777ff853SJeremy L Thompson     CeedChk(ierr);
110241a4b83SYohann   }
111241a4b83SYohann 
112241a4b83SYohann   // Apply operator
113288c0443SJeremy L Thompson   void *opargs[] = {(void *) &nelem, &qf_data->d_c, &data->indices,
114d80fc06aSjeremylt                     &data->fields, &data->B, &data->G, &data->W
1157f823360Sjeremylt                    };
116241a4b83SYohann   const CeedInt dim = data->dim;
117241a4b83SYohann   const CeedInt Q1d = data->Q1d;
118241a4b83SYohann   if (dim==1) {
119241a4b83SYohann     const CeedInt elemsPerBlock = 32;
120241a4b83SYohann     CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)
121241a4b83SYohann                                            ? 1 : 0 );
122241a4b83SYohann     CeedInt sharedMem = elemsPerBlock*Q1d*sizeof(CeedScalar);
123241a4b83SYohann     ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, 1, elemsPerBlock,
124241a4b83SYohann                                       sharedMem, opargs);
125241a4b83SYohann   } else if (dim==2) {
126241a4b83SYohann     const CeedInt elemsPerBlock = Q1d<4? 16 : 2;
127241a4b83SYohann     CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)
128241a4b83SYohann                                            ? 1 : 0 );
129241a4b83SYohann     CeedInt sharedMem = elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar);
130288c0443SJeremy L Thompson     ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, Q1d,
131288c0443SJeremy L Thompson                                       elemsPerBlock, sharedMem, opargs);
132241a4b83SYohann   } else if (dim==3) {
133ac421f39SYohann     const CeedInt elemsPerBlock = Q1d<6? 4 : (Q1d<8? 2 : 1);
134241a4b83SYohann     CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)
135241a4b83SYohann                                            ? 1 : 0 );
136241a4b83SYohann     CeedInt sharedMem = elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar);
137288c0443SJeremy L Thompson     ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, Q1d,
138288c0443SJeremy L Thompson                                       elemsPerBlock, sharedMem, opargs);
139241a4b83SYohann   }
140241a4b83SYohann   CeedChk(ierr);
141241a4b83SYohann 
142241a4b83SYohann   // Restore input arrays
143241a4b83SYohann   for (CeedInt i = 0; i < numinputfields; i++) {
144241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
145241a4b83SYohann     CeedChk(ierr);
146241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
147241a4b83SYohann     } else {
148241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChk(ierr);
149241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
150241a4b83SYohann       ierr = CeedVectorRestoreArrayRead(vec, &data->fields.in[i]);
151241a4b83SYohann       CeedChk(ierr);
152241a4b83SYohann     }
153241a4b83SYohann   }
154241a4b83SYohann 
155241a4b83SYohann   // Restore output arrays
156241a4b83SYohann   for (CeedInt i = 0; i < numoutputfields; i++) {
157241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
158241a4b83SYohann     CeedChk(ierr);
159241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
160241a4b83SYohann     } else {
161241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr);
162241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
1633b2939feSjeremylt       // Check for multiple output modes
1643b2939feSjeremylt       CeedInt index = -1;
1653b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
1663b2939feSjeremylt         if (vec == outvecs[j]) {
1673b2939feSjeremylt           index = j;
1683b2939feSjeremylt           break;
1693b2939feSjeremylt         }
1703b2939feSjeremylt       }
1713b2939feSjeremylt       if (index == -1) {
172241a4b83SYohann         ierr = CeedVectorRestoreArray(vec, &data->fields.out[i]);
173241a4b83SYohann         CeedChk(ierr);
174241a4b83SYohann       }
175241a4b83SYohann     }
1763b2939feSjeremylt   }
177*777ff853SJeremy L Thompson 
178*777ff853SJeremy L Thompson   // Restore context data
179*777ff853SJeremy L Thompson   if (ctx) {
180*777ff853SJeremy L Thompson     ierr = CeedQFunctionContextRestoreData(ctx, &qf_data->d_c);
181*777ff853SJeremy L Thompson     CeedChk(ierr);
182*777ff853SJeremy L Thompson   }
183241a4b83SYohann   return 0;
184241a4b83SYohann }
185241a4b83SYohann 
186ab213215SJeremy L Thompson //------------------------------------------------------------------------------
187ab213215SJeremy L Thompson // Create FDM element inverse not supported
188ab213215SJeremy L Thompson //------------------------------------------------------------------------------
189ccaff030SJeremy L Thompson static int CeedOperatorCreateFDMElementInverse_Cuda(CeedOperator op) {
190e9f4dca0SJeremy L Thompson   // LCOV_EXCL_START
191ccaff030SJeremy L Thompson   int ierr;
192ccaff030SJeremy L Thompson   Ceed ceed;
193ccaff030SJeremy L Thompson   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
194ccaff030SJeremy L Thompson   return CeedError(ceed, 1, "Backend does not implement FDM inverse creation");
195e9f4dca0SJeremy L Thompson   // LCOV_EXCL_STOP
196ccaff030SJeremy L Thompson }
197ccaff030SJeremy L Thompson 
198ab213215SJeremy L Thompson //------------------------------------------------------------------------------
199ab213215SJeremy L Thompson // Create operator
200ab213215SJeremy L Thompson //------------------------------------------------------------------------------
201241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) {
202241a4b83SYohann   int ierr;
203241a4b83SYohann   Ceed ceed;
204241a4b83SYohann   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
205241a4b83SYohann   CeedOperator_Cuda_gen *impl;
206241a4b83SYohann 
207241a4b83SYohann   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
208*777ff853SJeremy L Thompson   ierr = CeedOperatorSetData(op, impl); CeedChk(ierr);
209241a4b83SYohann 
210ccaff030SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Operator", op, "CreateFDMElementInverse",
211ccaff030SJeremy L Thompson                                 CeedOperatorCreateFDMElementInverse_Cuda);
212ccaff030SJeremy L Thompson   CeedChk(ierr);
2133e0c3786SYohann Dudouit   ierr = CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd",
2143e0c3786SYohann Dudouit                                 CeedOperatorApplyAdd_Cuda_gen); CeedChk(ierr);
215241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Operator", op, "Destroy",
216241a4b83SYohann                                 CeedOperatorDestroy_Cuda_gen); CeedChk(ierr);
217241a4b83SYohann   return 0;
218241a4b83SYohann }
219ab213215SJeremy L Thompson //------------------------------------------------------------------------------
220