xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-gen/ceed-cuda-gen-operator.c (revision 3d576824e8d990e1f48c6609089904bee9170514)
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 
17*3d576824SJeremy L Thompson #include <ceed.h>
18*3d576824SJeremy L Thompson #include <ceed-backend.h>
19*3d576824SJeremy L Thompson #include <stddef.h>
20241a4b83SYohann #include "ceed-cuda-gen.h"
21241a4b83SYohann #include "ceed-cuda-gen-operator-build.h"
22*3d576824SJeremy L Thompson #include "../cuda/ceed-cuda.h"
23241a4b83SYohann 
24ab213215SJeremy L Thompson //------------------------------------------------------------------------------
25ab213215SJeremy L Thompson // Destroy operator
26ab213215SJeremy L Thompson //------------------------------------------------------------------------------
27241a4b83SYohann static int CeedOperatorDestroy_Cuda_gen(CeedOperator op) {
28241a4b83SYohann   int ierr;
29241a4b83SYohann   CeedOperator_Cuda_gen *impl;
30777ff853SJeremy L Thompson   ierr = CeedOperatorGetData(op, &impl); CeedChk(ierr);
31241a4b83SYohann   ierr = CeedFree(&impl); CeedChk(ierr);
32241a4b83SYohann   return 0;
33241a4b83SYohann }
34241a4b83SYohann 
35ab213215SJeremy L Thompson //------------------------------------------------------------------------------
36ab213215SJeremy L Thompson // Apply and add to output
37ab213215SJeremy L Thompson //------------------------------------------------------------------------------
383e0c3786SYohann Dudouit static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector invec,
39241a4b83SYohann     CeedVector outvec, CeedRequest *request) {
40241a4b83SYohann   int ierr;
41241a4b83SYohann   Ceed ceed;
42241a4b83SYohann   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
43241a4b83SYohann   CeedOperator_Cuda_gen *data;
44777ff853SJeremy L Thompson   ierr = CeedOperatorGetData(op, &data); CeedChk(ierr);
45241a4b83SYohann   CeedQFunction qf;
46241a4b83SYohann   CeedQFunction_Cuda_gen *qf_data;
47241a4b83SYohann   ierr = CeedOperatorGetQFunction(op, &qf); CeedChk(ierr);
48777ff853SJeremy L Thompson   ierr = CeedQFunctionGetData(qf, &qf_data); CeedChk(ierr);
49241a4b83SYohann   CeedInt nelem, numinputfields, numoutputfields;
50241a4b83SYohann   ierr = CeedOperatorGetNumElements(op, &nelem); CeedChk(ierr);
51241a4b83SYohann   ierr = CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields);
52241a4b83SYohann   CeedChk(ierr);
53241a4b83SYohann   CeedOperatorField *opinputfields, *opoutputfields;
54241a4b83SYohann   ierr = CeedOperatorGetFields(op, &opinputfields, &opoutputfields);
55241a4b83SYohann   CeedChk(ierr);
56241a4b83SYohann   CeedQFunctionField *qfinputfields, *qfoutputfields;
57241a4b83SYohann   ierr = CeedQFunctionGetFields(qf, &qfinputfields, &qfoutputfields);
58241a4b83SYohann   CeedChk(ierr);
59241a4b83SYohann   CeedEvalMode emode;
603b2939feSjeremylt   CeedVector vec, outvecs[16] = {};
61241a4b83SYohann 
62241a4b83SYohann   //Creation of the operator
63241a4b83SYohann   ierr = CeedCudaGenOperatorBuild(op); CeedChk(ierr);
64241a4b83SYohann 
65241a4b83SYohann   // Input vectors
66241a4b83SYohann   for (CeedInt i = 0; i < numinputfields; i++) {
67241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
68241a4b83SYohann     CeedChk(ierr);
69241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
70241a4b83SYohann       data->fields.in[i] = NULL;
71241a4b83SYohann     } else {
72241a4b83SYohann       // Get input vector
73241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChk(ierr);
74241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
75241a4b83SYohann       ierr = CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.in[i]);
76241a4b83SYohann       CeedChk(ierr);
77241a4b83SYohann     }
78241a4b83SYohann   }
79241a4b83SYohann 
80241a4b83SYohann   // Output vectors
81241a4b83SYohann   for (CeedInt i = 0; i < numoutputfields; i++) {
82241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
83241a4b83SYohann     CeedChk(ierr);
84241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
85241a4b83SYohann       data->fields.out[i] = NULL;
86241a4b83SYohann     } else {
87241a4b83SYohann       // Get output vector
88241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr);
89241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
903b2939feSjeremylt       outvecs[i] = vec;
913b2939feSjeremylt       // Check for multiple output modes
923b2939feSjeremylt       CeedInt index = -1;
933b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
943b2939feSjeremylt         if (vec == outvecs[j]) {
953b2939feSjeremylt           index = j;
963b2939feSjeremylt           break;
973b2939feSjeremylt         }
983b2939feSjeremylt       }
993b2939feSjeremylt       if (index == -1) {
100241a4b83SYohann         ierr = CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.out[i]);
101241a4b83SYohann         CeedChk(ierr);
1023b2939feSjeremylt       } else {
1033b2939feSjeremylt         data->fields.out[i] = data->fields.out[index];
1043b2939feSjeremylt       }
105241a4b83SYohann     }
106241a4b83SYohann   }
107241a4b83SYohann 
108777ff853SJeremy L Thompson   // Get context data
109777ff853SJeremy L Thompson   CeedQFunctionContext ctx;
110241a4b83SYohann   ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
111777ff853SJeremy L Thompson   if (ctx) {
112777ff853SJeremy L Thompson     ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_DEVICE, &qf_data->d_c);
113777ff853SJeremy L Thompson     CeedChk(ierr);
114241a4b83SYohann   }
115241a4b83SYohann 
116241a4b83SYohann   // Apply operator
117288c0443SJeremy L Thompson   void *opargs[] = {(void *) &nelem, &qf_data->d_c, &data->indices,
118d80fc06aSjeremylt                     &data->fields, &data->B, &data->G, &data->W
1197f823360Sjeremylt                    };
120241a4b83SYohann   const CeedInt dim = data->dim;
121241a4b83SYohann   const CeedInt Q1d = data->Q1d;
12218d499f1SYohann   const CeedInt P1d = data->maxP1d;
12318d499f1SYohann   const CeedInt thread1d = CeedIntMax(Q1d, P1d);
124241a4b83SYohann   if (dim==1) {
125241a4b83SYohann     const CeedInt elemsPerBlock = 32;
126241a4b83SYohann     CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)
127241a4b83SYohann                                            ? 1 : 0 );
12818d499f1SYohann     CeedInt sharedMem = elemsPerBlock*thread1d*sizeof(CeedScalar);
12918d499f1SYohann     ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, thread1d, 1,
13018d499f1SYohann                                       elemsPerBlock, sharedMem, opargs);
131241a4b83SYohann   } else if (dim==2) {
13218d499f1SYohann     const CeedInt elemsPerBlock = thread1d<4? 16 : 2;
133241a4b83SYohann     CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)
134241a4b83SYohann                                            ? 1 : 0 );
13518d499f1SYohann     CeedInt sharedMem = elemsPerBlock*thread1d*thread1d*sizeof(CeedScalar);
13618d499f1SYohann     ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, thread1d, thread1d,
137288c0443SJeremy L Thompson                                       elemsPerBlock, sharedMem, opargs);
138241a4b83SYohann   } else if (dim==3) {
13918d499f1SYohann     const CeedInt elemsPerBlock = thread1d<6? 4 : (thread1d<8? 2 : 1);
140241a4b83SYohann     CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)
141241a4b83SYohann                                            ? 1 : 0 );
14218d499f1SYohann     CeedInt sharedMem = elemsPerBlock*thread1d*thread1d*sizeof(CeedScalar);
14318d499f1SYohann     ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, thread1d, thread1d,
144288c0443SJeremy L Thompson                                       elemsPerBlock, sharedMem, opargs);
145241a4b83SYohann   }
146241a4b83SYohann   CeedChk(ierr);
147241a4b83SYohann 
148241a4b83SYohann   // Restore input arrays
149241a4b83SYohann   for (CeedInt i = 0; i < numinputfields; i++) {
150241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
151241a4b83SYohann     CeedChk(ierr);
152241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
153241a4b83SYohann     } else {
154241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChk(ierr);
155241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
156241a4b83SYohann       ierr = CeedVectorRestoreArrayRead(vec, &data->fields.in[i]);
157241a4b83SYohann       CeedChk(ierr);
158241a4b83SYohann     }
159241a4b83SYohann   }
160241a4b83SYohann 
161241a4b83SYohann   // Restore output arrays
162241a4b83SYohann   for (CeedInt i = 0; i < numoutputfields; i++) {
163241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
164241a4b83SYohann     CeedChk(ierr);
165241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
166241a4b83SYohann     } else {
167241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr);
168241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
1693b2939feSjeremylt       // Check for multiple output modes
1703b2939feSjeremylt       CeedInt index = -1;
1713b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
1723b2939feSjeremylt         if (vec == outvecs[j]) {
1733b2939feSjeremylt           index = j;
1743b2939feSjeremylt           break;
1753b2939feSjeremylt         }
1763b2939feSjeremylt       }
1773b2939feSjeremylt       if (index == -1) {
178241a4b83SYohann         ierr = CeedVectorRestoreArray(vec, &data->fields.out[i]);
179241a4b83SYohann         CeedChk(ierr);
180241a4b83SYohann       }
181241a4b83SYohann     }
1823b2939feSjeremylt   }
183777ff853SJeremy L Thompson 
184777ff853SJeremy L Thompson   // Restore context data
185777ff853SJeremy L Thompson   if (ctx) {
186777ff853SJeremy L Thompson     ierr = CeedQFunctionContextRestoreData(ctx, &qf_data->d_c);
187777ff853SJeremy L Thompson     CeedChk(ierr);
188777ff853SJeremy L Thompson   }
189241a4b83SYohann   return 0;
190241a4b83SYohann }
191241a4b83SYohann 
192ab213215SJeremy L Thompson //------------------------------------------------------------------------------
193ab213215SJeremy L Thompson // Create FDM element inverse not supported
194ab213215SJeremy L Thompson //------------------------------------------------------------------------------
195ccaff030SJeremy L Thompson static int CeedOperatorCreateFDMElementInverse_Cuda(CeedOperator op) {
196e9f4dca0SJeremy L Thompson   // LCOV_EXCL_START
197ccaff030SJeremy L Thompson   int ierr;
198ccaff030SJeremy L Thompson   Ceed ceed;
199ccaff030SJeremy L Thompson   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
200ccaff030SJeremy L Thompson   return CeedError(ceed, 1, "Backend does not implement FDM inverse creation");
201e9f4dca0SJeremy L Thompson   // LCOV_EXCL_STOP
202ccaff030SJeremy L Thompson }
203ccaff030SJeremy L Thompson 
204ab213215SJeremy L Thompson //------------------------------------------------------------------------------
205ab213215SJeremy L Thompson // Create operator
206ab213215SJeremy L Thompson //------------------------------------------------------------------------------
207241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) {
208241a4b83SYohann   int ierr;
209241a4b83SYohann   Ceed ceed;
210241a4b83SYohann   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
211241a4b83SYohann   CeedOperator_Cuda_gen *impl;
212241a4b83SYohann 
213241a4b83SYohann   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
214777ff853SJeremy L Thompson   ierr = CeedOperatorSetData(op, impl); CeedChk(ierr);
215241a4b83SYohann 
216ccaff030SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Operator", op, "CreateFDMElementInverse",
217ccaff030SJeremy L Thompson                                 CeedOperatorCreateFDMElementInverse_Cuda);
218ccaff030SJeremy L Thompson   CeedChk(ierr);
2193e0c3786SYohann Dudouit   ierr = CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd",
2203e0c3786SYohann Dudouit                                 CeedOperatorApplyAdd_Cuda_gen); CeedChk(ierr);
221241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Operator", op, "Destroy",
222241a4b83SYohann                                 CeedOperatorDestroy_Cuda_gen); CeedChk(ierr);
223241a4b83SYohann   return 0;
224241a4b83SYohann }
225ab213215SJeremy L Thompson //------------------------------------------------------------------------------
226