xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-gen/ceed-cuda-gen-operator.c (revision ccaff0309dc399f656ea11018b919b30feb8b0fa)
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-backend.h>
18241a4b83SYohann #include "ceed-cuda-gen.h"
19241a4b83SYohann #include "ceed-cuda-gen-operator-build.h"
20241a4b83SYohann #include "../cuda/ceed-cuda.h"
21241a4b83SYohann 
22241a4b83SYohann static int CeedOperatorDestroy_Cuda_gen(CeedOperator op) {
23241a4b83SYohann   int ierr;
24241a4b83SYohann   CeedOperator_Cuda_gen *impl;
25241a4b83SYohann   ierr = CeedOperatorGetData(op, (void *)&impl); CeedChk(ierr);
26241a4b83SYohann   ierr = CeedFree(&impl); CeedChk(ierr);
27241a4b83SYohann   return 0;
28241a4b83SYohann }
29241a4b83SYohann 
303e0c3786SYohann Dudouit static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector invec,
31241a4b83SYohann     CeedVector outvec, CeedRequest *request) {
32241a4b83SYohann   int ierr;
33241a4b83SYohann   Ceed ceed;
34241a4b83SYohann   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
35241a4b83SYohann   CeedOperator_Cuda_gen *data;
36241a4b83SYohann   ierr = CeedOperatorGetData(op, (void *)&data); CeedChk(ierr);
37241a4b83SYohann   CeedQFunction qf;
38241a4b83SYohann   CeedQFunction_Cuda_gen *qf_data;
39241a4b83SYohann   ierr = CeedOperatorGetQFunction(op, &qf); CeedChk(ierr);
40241a4b83SYohann   ierr = CeedQFunctionGetData(qf, (void **)&qf_data); CeedChk(ierr);
41241a4b83SYohann   CeedInt nelem, numinputfields, numoutputfields;
42241a4b83SYohann   ierr = CeedOperatorGetNumElements(op, &nelem); CeedChk(ierr);
43241a4b83SYohann   ierr = CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields);
44241a4b83SYohann   CeedChk(ierr);
45241a4b83SYohann   CeedOperatorField *opinputfields, *opoutputfields;
46241a4b83SYohann   ierr = CeedOperatorGetFields(op, &opinputfields, &opoutputfields);
47241a4b83SYohann   CeedChk(ierr);
48241a4b83SYohann   CeedQFunctionField *qfinputfields, *qfoutputfields;
49241a4b83SYohann   ierr = CeedQFunctionGetFields(qf, &qfinputfields, &qfoutputfields);
50241a4b83SYohann   CeedChk(ierr);
51241a4b83SYohann   CeedEvalMode emode;
523b2939feSjeremylt   CeedVector vec, outvecs[16] = {};
53241a4b83SYohann 
54241a4b83SYohann   //Creation of the operator
55241a4b83SYohann   ierr = CeedCudaGenOperatorBuild(op); CeedChk(ierr);
56241a4b83SYohann 
57241a4b83SYohann   // Input vectors
58241a4b83SYohann   for (CeedInt i = 0; i < numinputfields; i++) {
59241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
60241a4b83SYohann     CeedChk(ierr);
61241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
62241a4b83SYohann       data->fields.in[i] = NULL;
63241a4b83SYohann     } else {
64241a4b83SYohann       // Get input vector
65241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChk(ierr);
66241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
67241a4b83SYohann       ierr = CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &data->fields.in[i]);
68241a4b83SYohann       CeedChk(ierr);
69241a4b83SYohann     }
70241a4b83SYohann   }
71241a4b83SYohann 
72241a4b83SYohann   // Output vectors
73241a4b83SYohann   for (CeedInt i = 0; i < numoutputfields; i++) {
74241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
75241a4b83SYohann     CeedChk(ierr);
76241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
77241a4b83SYohann       data->fields.out[i] = NULL;
78241a4b83SYohann     } else {
79241a4b83SYohann       // Get output vector
80241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr);
81241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
823b2939feSjeremylt       outvecs[i] = vec;
833b2939feSjeremylt       // Check for multiple output modes
843b2939feSjeremylt       CeedInt index = -1;
853b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
863b2939feSjeremylt         if (vec == outvecs[j]) {
873b2939feSjeremylt           index = j;
883b2939feSjeremylt           break;
893b2939feSjeremylt         }
903b2939feSjeremylt       }
913b2939feSjeremylt       if (index == -1) {
92241a4b83SYohann         ierr = CeedVectorGetArray(vec, CEED_MEM_DEVICE, &data->fields.out[i]);
93241a4b83SYohann         CeedChk(ierr);
943b2939feSjeremylt       } else {
953b2939feSjeremylt         data->fields.out[i] = data->fields.out[index];
963b2939feSjeremylt       }
97241a4b83SYohann     }
98241a4b83SYohann   }
99241a4b83SYohann 
100241a4b83SYohann   // Copy the context
101241a4b83SYohann   size_t ctxsize;
102241a4b83SYohann   ierr = CeedQFunctionGetContextSize(qf, &ctxsize); CeedChk(ierr);
103241a4b83SYohann   if (ctxsize > 0) {
104241a4b83SYohann     if (!qf_data->d_c) {
105241a4b83SYohann       ierr = cudaMalloc(&qf_data->d_c, ctxsize); CeedChk_Cu(ceed, ierr);
106241a4b83SYohann     }
107241a4b83SYohann     void *ctx;
108241a4b83SYohann     ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr);
109241a4b83SYohann     ierr = cudaMemcpy(qf_data->d_c, ctx, ctxsize, cudaMemcpyHostToDevice);
110241a4b83SYohann     CeedChk_Cu(ceed, ierr);
111241a4b83SYohann   }
112241a4b83SYohann 
113241a4b83SYohann   // Apply operator
114288c0443SJeremy L Thompson   void *opargs[] = {(void *) &nelem, &qf_data->d_c, &data->indices,
115d80fc06aSjeremylt                     &data->fields, &data->B, &data->G, &data->W
1167f823360Sjeremylt                    };
117241a4b83SYohann   const CeedInt dim = data->dim;
118241a4b83SYohann   const CeedInt Q1d = data->Q1d;
119241a4b83SYohann   if (dim==1) {
120241a4b83SYohann     const CeedInt elemsPerBlock = 32;
121241a4b83SYohann     CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)
122241a4b83SYohann                                            ? 1 : 0 );
123241a4b83SYohann     CeedInt sharedMem = elemsPerBlock*Q1d*sizeof(CeedScalar);
124241a4b83SYohann     ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, 1, elemsPerBlock,
125241a4b83SYohann                                       sharedMem, opargs);
126241a4b83SYohann   } else if (dim==2) {
127241a4b83SYohann     const CeedInt elemsPerBlock = Q1d<4? 16 : 2;
128241a4b83SYohann     CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)
129241a4b83SYohann                                            ? 1 : 0 );
130241a4b83SYohann     CeedInt sharedMem = elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar);
131288c0443SJeremy L Thompson     ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, Q1d,
132288c0443SJeremy L Thompson                                       elemsPerBlock, sharedMem, opargs);
133241a4b83SYohann   } else if (dim==3) {
134ac421f39SYohann     const CeedInt elemsPerBlock = Q1d<6? 4 : (Q1d<8? 2 : 1);
135241a4b83SYohann     CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)
136241a4b83SYohann                                            ? 1 : 0 );
137241a4b83SYohann     CeedInt sharedMem = elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar);
138288c0443SJeremy L Thompson     ierr = CeedRunKernelDimSharedCuda(ceed, data->op, grid, Q1d, Q1d,
139288c0443SJeremy L Thompson                                       elemsPerBlock, sharedMem, opargs);
140241a4b83SYohann   }
141241a4b83SYohann   CeedChk(ierr);
142241a4b83SYohann 
143241a4b83SYohann   // Restore input arrays
144241a4b83SYohann   for (CeedInt i = 0; i < numinputfields; i++) {
145241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode);
146241a4b83SYohann     CeedChk(ierr);
147241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
148241a4b83SYohann     } else {
149241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opinputfields[i], &vec); CeedChk(ierr);
150241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
151241a4b83SYohann       ierr = CeedVectorRestoreArrayRead(vec, &data->fields.in[i]);
152241a4b83SYohann       CeedChk(ierr);
153241a4b83SYohann     }
154241a4b83SYohann   }
155241a4b83SYohann 
156241a4b83SYohann   // Restore output arrays
157241a4b83SYohann   for (CeedInt i = 0; i < numoutputfields; i++) {
158241a4b83SYohann     ierr = CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode);
159241a4b83SYohann     CeedChk(ierr);
160241a4b83SYohann     if (emode == CEED_EVAL_WEIGHT) { // Skip
161241a4b83SYohann     } else {
162241a4b83SYohann       ierr = CeedOperatorFieldGetVector(opoutputfields[i], &vec); CeedChk(ierr);
163241a4b83SYohann       if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
1643b2939feSjeremylt       // Check for multiple output modes
1653b2939feSjeremylt       CeedInt index = -1;
1663b2939feSjeremylt       for (CeedInt j = 0; j < i; j++) {
1673b2939feSjeremylt         if (vec == outvecs[j]) {
1683b2939feSjeremylt           index = j;
1693b2939feSjeremylt           break;
1703b2939feSjeremylt         }
1713b2939feSjeremylt       }
1723b2939feSjeremylt       if (index == -1) {
173241a4b83SYohann         ierr = CeedVectorRestoreArray(vec, &data->fields.out[i]);
174241a4b83SYohann         CeedChk(ierr);
175241a4b83SYohann       }
176241a4b83SYohann     }
1773b2939feSjeremylt   }
178241a4b83SYohann 
179241a4b83SYohann   return 0;
180241a4b83SYohann }
181241a4b83SYohann 
182773cc6e0Sjeremylt static int CeedOperatorAssembleLinearQFunction_Cuda(CeedOperator op) {
183773cc6e0Sjeremylt   int ierr;
184773cc6e0Sjeremylt   Ceed ceed;
185773cc6e0Sjeremylt   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
186773cc6e0Sjeremylt   return CeedError(ceed, 1, "Backend does not implement QFunction assembly");
187773cc6e0Sjeremylt }
188773cc6e0Sjeremylt 
189*ccaff030SJeremy L Thompson static int CeedOperatorAssembleLinearDiagonal_Cuda(CeedOperator op) {
190*ccaff030SJeremy L Thompson   int ierr;
191*ccaff030SJeremy L Thompson   Ceed ceed;
192*ccaff030SJeremy L Thompson   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
193*ccaff030SJeremy L Thompson   return CeedError(ceed, 1,
194*ccaff030SJeremy L Thompson                    "Backend does not implement Operator diagonal assembly");
195*ccaff030SJeremy L Thompson }
196*ccaff030SJeremy L Thompson 
197*ccaff030SJeremy L Thompson static int CeedOperatorCreateFDMElementInverse_Cuda(CeedOperator op) {
198*ccaff030SJeremy L Thompson   int ierr;
199*ccaff030SJeremy L Thompson   Ceed ceed;
200*ccaff030SJeremy L Thompson   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
201*ccaff030SJeremy L Thompson   return CeedError(ceed, 1, "Backend does not implement FDM inverse creation");
202*ccaff030SJeremy L Thompson }
203*ccaff030SJeremy L Thompson 
204241a4b83SYohann int CeedOperatorCreate_Cuda_gen(CeedOperator op) {
205241a4b83SYohann   int ierr;
206241a4b83SYohann   Ceed ceed;
207241a4b83SYohann   ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr);
208241a4b83SYohann   CeedOperator_Cuda_gen *impl;
209241a4b83SYohann 
210241a4b83SYohann   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
211241a4b83SYohann   ierr = CeedOperatorSetData(op, (void *)&impl);
212241a4b83SYohann 
213773cc6e0Sjeremylt   ierr = CeedSetBackendFunction(ceed, "Operator", op, "AssembleLinearQFunction",
214773cc6e0Sjeremylt                                 CeedOperatorAssembleLinearQFunction_Cuda);
215773cc6e0Sjeremylt   CeedChk(ierr);
216*ccaff030SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Operator", op, "AssembleLinearDiagonal",
217*ccaff030SJeremy L Thompson                                 CeedOperatorAssembleLinearDiagonal_Cuda);
218*ccaff030SJeremy L Thompson   CeedChk(ierr);
219*ccaff030SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "Operator", op, "CreateFDMElementInverse",
220*ccaff030SJeremy L Thompson                                 CeedOperatorCreateFDMElementInverse_Cuda);
221*ccaff030SJeremy L Thompson   CeedChk(ierr);
2223e0c3786SYohann Dudouit   ierr = CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd",
2233e0c3786SYohann Dudouit                                 CeedOperatorApplyAdd_Cuda_gen); CeedChk(ierr);
224241a4b83SYohann   ierr = CeedSetBackendFunction(ceed, "Operator", op, "Destroy",
225241a4b83SYohann                                 CeedOperatorDestroy_Cuda_gen); CeedChk(ierr);
226241a4b83SYohann   return 0;
227241a4b83SYohann }
228