xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-ref/ceed-cuda-ref-operator.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
80d0321e0SJeremy L Thompson #include <assert.h>
9*49aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
112b730f8bSJeremy L Thompson #include <ceed/jit-tools.h>
120d0321e0SJeremy L Thompson #include <cuda.h>
130d0321e0SJeremy L Thompson #include <cuda_runtime.h>
140d0321e0SJeremy L Thompson #include <stdbool.h>
150d0321e0SJeremy L Thompson #include <string.h>
162b730f8bSJeremy L Thompson 
17*49aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h"
180d0321e0SJeremy L Thompson #include "../cuda/ceed-cuda-compile.h"
192b730f8bSJeremy L Thompson #include "ceed-cuda-ref.h"
200d0321e0SJeremy L Thompson 
210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
220d0321e0SJeremy L Thompson // Destroy operator
230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
240d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Cuda(CeedOperator op) {
250d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
270d0321e0SJeremy L Thompson 
280d0321e0SJeremy L Thompson   // Apply data
290d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->numein + impl->numeout; i++) {
302b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->evecs[i]));
310d0321e0SJeremy L Thompson   }
322b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->evecs));
330d0321e0SJeremy L Thompson 
340d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->numein; i++) {
352b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qvecsin[i]));
360d0321e0SJeremy L Thompson   }
372b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qvecsin));
380d0321e0SJeremy L Thompson 
390d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->numeout; i++) {
402b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qvecsout[i]));
410d0321e0SJeremy L Thompson   }
422b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qvecsout));
430d0321e0SJeremy L Thompson 
440d0321e0SJeremy L Thompson   // QFunction assembly data
450d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->qfnumactivein; i++) {
462b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qfactivein[i]));
470d0321e0SJeremy L Thompson   }
482b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qfactivein));
490d0321e0SJeremy L Thompson 
500d0321e0SJeremy L Thompson   // Diag data
510d0321e0SJeremy L Thompson   if (impl->diag) {
520d0321e0SJeremy L Thompson     Ceed ceed;
532b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
542b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cuModuleUnload(impl->diag->module));
552b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&impl->diag->h_emodein));
562b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&impl->diag->h_emodeout));
572b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_emodein));
582b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_emodeout));
592b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_identity));
602b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_interpin));
612b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_interpout));
622b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_gradin));
632b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_gradout));
642b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->pbdiagrstr));
652b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elemdiag));
662b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->pbelemdiag));
670d0321e0SJeremy L Thompson   }
682b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
690d0321e0SJeremy L Thompson 
70cc132f9aSnbeams   if (impl->asmb) {
71cc132f9aSnbeams     Ceed ceed;
722b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
732b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cuModuleUnload(impl->asmb->module));
742b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_in));
752b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_out));
76cc132f9aSnbeams   }
772b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
78cc132f9aSnbeams 
792b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
800d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
810d0321e0SJeremy L Thompson }
820d0321e0SJeremy L Thompson 
830d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
840d0321e0SJeremy L Thompson // Setup infields or outfields
850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
862b730f8bSJeremy L Thompson static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool isinput, CeedVector *evecs, CeedVector *qvecs, CeedInt starte,
872b730f8bSJeremy L Thompson                                         CeedInt numfields, CeedInt Q, CeedInt numelements) {
882b730f8bSJeremy L Thompson   CeedInt  dim, size;
89d2643443SJeremy L Thompson   CeedSize q_size;
900d0321e0SJeremy L Thompson   Ceed     ceed;
912b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
920d0321e0SJeremy L Thompson   CeedBasis           basis;
930d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
940d0321e0SJeremy L Thompson   CeedOperatorField  *opfields;
950d0321e0SJeremy L Thompson   CeedQFunctionField *qffields;
960d0321e0SJeremy L Thompson   CeedVector          fieldvec;
970d0321e0SJeremy L Thompson   bool                strided;
980d0321e0SJeremy L Thompson   bool                skiprestrict;
990d0321e0SJeremy L Thompson 
1000d0321e0SJeremy L Thompson   if (isinput) {
1012b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &opfields, NULL, NULL));
1022b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qffields, NULL, NULL));
1030d0321e0SJeremy L Thompson   } else {
1042b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &opfields));
1052b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qffields));
1060d0321e0SJeremy L Thompson   }
1070d0321e0SJeremy L Thompson 
1080d0321e0SJeremy L Thompson   // Loop over fields
1090d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numfields; i++) {
1100d0321e0SJeremy L Thompson     CeedEvalMode emode;
1112b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode));
1120d0321e0SJeremy L Thompson 
1130d0321e0SJeremy L Thompson     strided      = false;
1140d0321e0SJeremy L Thompson     skiprestrict = false;
1150d0321e0SJeremy L Thompson     if (emode != CEED_EVAL_WEIGHT) {
1162b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &Erestrict));
1170d0321e0SJeremy L Thompson 
1180d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
119ea61e9acSJeremy L Thompson       // must be passive input, with emode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
1200d0321e0SJeremy L Thompson 
1210d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
1220d0321e0SJeremy L Thompson       if (isinput) {
1230d0321e0SJeremy L Thompson         // Check for passive input:
1242b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &fieldvec));
1250d0321e0SJeremy L Thompson         if (fieldvec != CEED_VECTOR_ACTIVE) {
1260d0321e0SJeremy L Thompson           // Check emode
1270d0321e0SJeremy L Thompson           if (emode == CEED_EVAL_NONE) {
1280d0321e0SJeremy L Thompson             // Check for strided restriction
1292b730f8bSJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(Erestrict, &strided));
1300d0321e0SJeremy L Thompson             if (strided) {
1310d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
1322b730f8bSJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(Erestrict, &skiprestrict));
1330d0321e0SJeremy L Thompson             }
1340d0321e0SJeremy L Thompson           }
1350d0321e0SJeremy L Thompson         }
1360d0321e0SJeremy L Thompson       }
1370d0321e0SJeremy L Thompson       if (skiprestrict) {
138ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
1390d0321e0SJeremy L Thompson         evecs[i + starte] = NULL;
1400d0321e0SJeremy L Thompson       } else {
1412b730f8bSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(Erestrict, NULL, &evecs[i + starte]));
1420d0321e0SJeremy L Thompson       }
1430d0321e0SJeremy L Thompson     }
1440d0321e0SJeremy L Thompson 
1450d0321e0SJeremy L Thompson     switch (emode) {
1460d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
1472b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size));
148d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q * size;
1492b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1500d0321e0SJeremy L Thompson         break;
1510d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1522b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size));
153d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q * size;
1542b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1550d0321e0SJeremy L Thompson         break;
1560d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
1572b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basis));
1582b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size));
1592b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetDimension(basis, &dim));
160d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q * size;
1612b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1620d0321e0SJeremy L Thompson         break;
1630d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
1642b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basis));
165d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q;
1662b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1672b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, NULL, qvecs[i]));
1680d0321e0SJeremy L Thompson         break;
1690d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
1700d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
1710d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
1720d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
1730d0321e0SJeremy L Thompson     }
1740d0321e0SJeremy L Thompson   }
1750d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1760d0321e0SJeremy L Thompson }
1770d0321e0SJeremy L Thompson 
1780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
179ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
1800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1810d0321e0SJeremy L Thompson static int CeedOperatorSetup_Cuda(CeedOperator op) {
1820d0321e0SJeremy L Thompson   bool setupdone;
1832b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &setupdone));
1842b730f8bSJeremy L Thompson   if (setupdone) return CEED_ERROR_SUCCESS;
1850d0321e0SJeremy L Thompson   Ceed ceed;
1862b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1870d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
1882b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1890d0321e0SJeremy L Thompson   CeedQFunction qf;
1902b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1910d0321e0SJeremy L Thompson   CeedInt Q, numelements, numinputfields, numoutputfields;
1922b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
1932b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &numelements));
1940d0321e0SJeremy L Thompson   CeedOperatorField *opinputfields, *opoutputfields;
1952b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields));
1960d0321e0SJeremy L Thompson   CeedQFunctionField *qfinputfields, *qfoutputfields;
1972b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields));
1980d0321e0SJeremy L Thompson 
1990d0321e0SJeremy L Thompson   // Allocate
2002b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(numinputfields + numoutputfields, &impl->evecs));
2010d0321e0SJeremy L Thompson 
2022b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->qvecsin));
2032b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->qvecsout));
2040d0321e0SJeremy L Thompson 
2052b730f8bSJeremy L Thompson   impl->numein  = numinputfields;
2062b730f8bSJeremy L Thompson   impl->numeout = numoutputfields;
2070d0321e0SJeremy L Thompson 
2080d0321e0SJeremy L Thompson   // Set up infield and outfield evecs and qvecs
2090d0321e0SJeremy L Thompson   // Infields
2102b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, true, impl->evecs, impl->qvecsin, 0, numinputfields, Q, numelements));
2110d0321e0SJeremy L Thompson 
2120d0321e0SJeremy L Thompson   // Outfields
2132b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, false, impl->evecs, impl->qvecsout, numinputfields, numoutputfields, Q, numelements));
2140d0321e0SJeremy L Thompson 
2152b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2160d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2170d0321e0SJeremy L Thompson }
2180d0321e0SJeremy L Thompson 
2190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2200d0321e0SJeremy L Thompson // Setup Operator Inputs
2210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2222b730f8bSJeremy L Thompson static inline int CeedOperatorSetupInputs_Cuda(CeedInt numinputfields, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields,
2230d0321e0SJeremy L Thompson                                                CeedVector invec, const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX],
2240d0321e0SJeremy L Thompson                                                CeedOperator_Cuda *impl, CeedRequest *request) {
2250d0321e0SJeremy L Thompson   CeedEvalMode        emode;
2260d0321e0SJeremy L Thompson   CeedVector          vec;
2270d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
2280d0321e0SJeremy L Thompson 
2290d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
2300d0321e0SJeremy L Thompson     // Get input vector
2312b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
2320d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2332b730f8bSJeremy L Thompson       if (skipactive) continue;
2342b730f8bSJeremy L Thompson       else vec = invec;
2350d0321e0SJeremy L Thompson     }
2360d0321e0SJeremy L Thompson 
2372b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode));
2380d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_WEIGHT) {  // Skip
2390d0321e0SJeremy L Thompson     } else {
2400d0321e0SJeremy L Thompson       // Get input vector
2412b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
2420d0321e0SJeremy L Thompson       // Get input element restriction
2432b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict));
2442b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
2450d0321e0SJeremy L Thompson       // Restrict, if necessary
2460d0321e0SJeremy L Thompson       if (!impl->evecs[i]) {
2470d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
2482b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&edata[i]));
2490d0321e0SJeremy L Thompson       } else {
2502b730f8bSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(Erestrict, CEED_NOTRANSPOSE, vec, impl->evecs[i], request));
2510d0321e0SJeremy L Thompson         // Get evec
2522b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->evecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&edata[i]));
2530d0321e0SJeremy L Thompson       }
2540d0321e0SJeremy L Thompson     }
2550d0321e0SJeremy L Thompson   }
2560d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2570d0321e0SJeremy L Thompson }
2580d0321e0SJeremy L Thompson 
2590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2600d0321e0SJeremy L Thompson // Input Basis Action
2610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2622b730f8bSJeremy L Thompson static inline int CeedOperatorInputBasis_Cuda(CeedInt numelements, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields,
2632b730f8bSJeremy L Thompson                                               CeedInt numinputfields, const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX],
2642b730f8bSJeremy L Thompson                                               CeedOperator_Cuda *impl) {
2650d0321e0SJeremy L Thompson   CeedInt             elemsize, size;
2660d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
2670d0321e0SJeremy L Thompson   CeedEvalMode        emode;
2680d0321e0SJeremy L Thompson   CeedBasis           basis;
2690d0321e0SJeremy L Thompson 
2700d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
2710d0321e0SJeremy L Thompson     // Skip active input
2720d0321e0SJeremy L Thompson     if (skipactive) {
2730d0321e0SJeremy L Thompson       CeedVector vec;
2742b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
2752b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2760d0321e0SJeremy L Thompson     }
2770d0321e0SJeremy L Thompson     // Get elemsize, emode, size
2782b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict));
2792b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elemsize));
2802b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode));
2812b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qfinputfields[i], &size));
2820d0321e0SJeremy L Thompson     // Basis action
2830d0321e0SJeremy L Thompson     switch (emode) {
2840d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
2852b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->qvecsin[i], CEED_MEM_DEVICE, CEED_USE_POINTER, edata[i]));
2860d0321e0SJeremy L Thompson         break;
2870d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
2882b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opinputfields[i], &basis));
2892b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, impl->evecs[i], impl->qvecsin[i]));
2900d0321e0SJeremy L Thompson         break;
2910d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
2922b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opinputfields[i], &basis));
2932b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, impl->evecs[i], impl->qvecsin[i]));
2940d0321e0SJeremy L Thompson         break;
2950d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
2960d0321e0SJeremy L Thompson         break;  // No action
2970d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
2980d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
2990d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
3000d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
3010d0321e0SJeremy L Thompson     }
3020d0321e0SJeremy L Thompson   }
3030d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3040d0321e0SJeremy L Thompson }
3050d0321e0SJeremy L Thompson 
3060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3070d0321e0SJeremy L Thompson // Restore Input Vectors
3080d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3092b730f8bSJeremy L Thompson static inline int CeedOperatorRestoreInputs_Cuda(CeedInt numinputfields, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields,
3102b730f8bSJeremy L Thompson                                                  const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) {
3110d0321e0SJeremy L Thompson   CeedEvalMode emode;
3120d0321e0SJeremy L Thompson   CeedVector   vec;
3130d0321e0SJeremy L Thompson 
3140d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
3150d0321e0SJeremy L Thompson     // Skip active input
3160d0321e0SJeremy L Thompson     if (skipactive) {
3172b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
3182b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3190d0321e0SJeremy L Thompson     }
3202b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode));
3210d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_WEIGHT) {  // Skip
3220d0321e0SJeremy L Thompson     } else {
3230d0321e0SJeremy L Thompson       if (!impl->evecs[i]) {  // This was a skiprestrict case
3242b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
3252b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&edata[i]));
3260d0321e0SJeremy L Thompson       } else {
3272b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->evecs[i], (const CeedScalar **)&edata[i]));
3280d0321e0SJeremy L Thompson       }
3290d0321e0SJeremy L Thompson     }
3300d0321e0SJeremy L Thompson   }
3310d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3320d0321e0SJeremy L Thompson }
3330d0321e0SJeremy L Thompson 
3340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3350d0321e0SJeremy L Thompson // Apply and add to output
3360d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3372b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector invec, CeedVector outvec, CeedRequest *request) {
3380d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
3392b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3400d0321e0SJeremy L Thompson   CeedQFunction qf;
3412b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3420d0321e0SJeremy L Thompson   CeedInt Q, numelements, elemsize, numinputfields, numoutputfields, size;
3432b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
3442b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &numelements));
3450d0321e0SJeremy L Thompson   CeedOperatorField *opinputfields, *opoutputfields;
3462b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields));
3470d0321e0SJeremy L Thompson   CeedQFunctionField *qfinputfields, *qfoutputfields;
3482b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields));
3490d0321e0SJeremy L Thompson   CeedEvalMode        emode;
3500d0321e0SJeremy L Thompson   CeedVector          vec;
3510d0321e0SJeremy L Thompson   CeedBasis           basis;
3520d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
3530d0321e0SJeremy L Thompson   CeedScalar         *edata[2 * CEED_FIELD_MAX] = {0};
3540d0321e0SJeremy L Thompson 
3550d0321e0SJeremy L Thompson   // Setup
3562b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Cuda(op));
3570d0321e0SJeremy L Thompson 
3580d0321e0SJeremy L Thompson   // Input Evecs and Restriction
3592b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(numinputfields, qfinputfields, opinputfields, invec, false, edata, impl, request));
3600d0321e0SJeremy L Thompson 
3610d0321e0SJeremy L Thompson   // Input basis apply if needed
3622b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Cuda(numelements, qfinputfields, opinputfields, numinputfields, false, edata, impl));
3630d0321e0SJeremy L Thompson 
3640d0321e0SJeremy L Thompson   // Output pointers, as necessary
3650d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
3662b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode));
3670d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_NONE) {
3680d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
3692b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->evecs[i + impl->numein], CEED_MEM_DEVICE, &edata[i + numinputfields]));
3702b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->qvecsout[i], CEED_MEM_DEVICE, CEED_USE_POINTER, edata[i + numinputfields]));
3710d0321e0SJeremy L Thompson     }
3720d0321e0SJeremy L Thompson   }
3730d0321e0SJeremy L Thompson 
3740d0321e0SJeremy L Thompson   // Q function
3752b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, numelements * Q, impl->qvecsin, impl->qvecsout));
3760d0321e0SJeremy L Thompson 
3770d0321e0SJeremy L Thompson   // Output basis apply if needed
3780d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
3790d0321e0SJeremy L Thompson     // Get elemsize, emode, size
3802b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(opoutputfields[i], &Erestrict));
3812b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elemsize));
3822b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode));
3832b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[i], &size));
3840d0321e0SJeremy L Thompson     // Basis action
3850d0321e0SJeremy L Thompson     switch (emode) {
3860d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
3870d0321e0SJeremy L Thompson         break;
3880d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3892b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opoutputfields[i], &basis));
3902b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_TRANSPOSE, CEED_EVAL_INTERP, impl->qvecsout[i], impl->evecs[i + impl->numein]));
3910d0321e0SJeremy L Thompson         break;
3920d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
3932b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opoutputfields[i], &basis));
3942b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_TRANSPOSE, CEED_EVAL_GRAD, impl->qvecsout[i], impl->evecs[i + impl->numein]));
3950d0321e0SJeremy L Thompson         break;
3960d0321e0SJeremy L Thompson       // LCOV_EXCL_START
3970d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
3980d0321e0SJeremy L Thompson         Ceed ceed;
3992b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
4002b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
4010d0321e0SJeremy L Thompson         break;  // Should not occur
4020d0321e0SJeremy L Thompson       }
4030d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
4040d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
4050d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
4060d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
4070d0321e0SJeremy L Thompson                 // LCOV_EXCL_STOP
4080d0321e0SJeremy L Thompson     }
4090d0321e0SJeremy L Thompson   }
4100d0321e0SJeremy L Thompson 
4110d0321e0SJeremy L Thompson   // Output restriction
4120d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
4130d0321e0SJeremy L Thompson     // Restore evec
4142b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode));
4150d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_NONE) {
4162b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->evecs[i + impl->numein], &edata[i + numinputfields]));
4170d0321e0SJeremy L Thompson     }
4180d0321e0SJeremy L Thompson     // Get output vector
4192b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[i], &vec));
4200d0321e0SJeremy L Thompson     // Restrict
4212b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(opoutputfields[i], &Erestrict));
4220d0321e0SJeremy L Thompson     // Active
4232b730f8bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
4240d0321e0SJeremy L Thompson 
4252b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(Erestrict, CEED_TRANSPOSE, impl->evecs[i + impl->numein], vec, request));
4260d0321e0SJeremy L Thompson   }
4270d0321e0SJeremy L Thompson 
4280d0321e0SJeremy L Thompson   // Restore input arrays
4292b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(numinputfields, qfinputfields, opinputfields, false, edata, impl));
4300d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4310d0321e0SJeremy L Thompson }
4320d0321e0SJeremy L Thompson 
4330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4340d0321e0SJeremy L Thompson // Core code for assembling linear QFunction
4350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4362b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
4370d0321e0SJeremy L Thompson                                                                CeedRequest *request) {
4380d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
4392b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4400d0321e0SJeremy L Thompson   CeedQFunction qf;
4412b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4420d0321e0SJeremy L Thompson   CeedInt  Q, numelements, numinputfields, numoutputfields, size;
443d2643443SJeremy L Thompson   CeedSize q_size;
4442b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
4452b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &numelements));
4460d0321e0SJeremy L Thompson   CeedOperatorField *opinputfields, *opoutputfields;
4472b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields));
4480d0321e0SJeremy L Thompson   CeedQFunctionField *qfinputfields, *qfoutputfields;
4492b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields));
4500d0321e0SJeremy L Thompson   CeedVector  vec;
4510d0321e0SJeremy L Thompson   CeedInt     numactivein = impl->qfnumactivein, numactiveout = impl->qfnumactiveout;
4520d0321e0SJeremy L Thompson   CeedVector *activein = impl->qfactivein;
4530d0321e0SJeremy L Thompson   CeedScalar *a, *tmp;
4540d0321e0SJeremy L Thompson   Ceed        ceed, ceedparent;
4552b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
4562b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetOperatorFallbackParentCeed(ceed, &ceedparent));
4570d0321e0SJeremy L Thompson   ceedparent = ceedparent ? ceedparent : ceed;
4580d0321e0SJeremy L Thompson   CeedScalar *edata[2 * CEED_FIELD_MAX];
4590d0321e0SJeremy L Thompson 
4600d0321e0SJeremy L Thompson   // Setup
4612b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Cuda(op));
4620d0321e0SJeremy L Thompson 
4630d0321e0SJeremy L Thompson   // Check for identity
4640d0321e0SJeremy L Thompson   bool identityqf;
4652b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionIsIdentity(qf, &identityqf));
4662b730f8bSJeremy L Thompson   if (identityqf) {
4670d0321e0SJeremy L Thompson     // LCOV_EXCL_START
4682b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Assembling identity QFunctions not supported");
4690d0321e0SJeremy L Thompson     // LCOV_EXCL_STOP
4702b730f8bSJeremy L Thompson   }
4710d0321e0SJeremy L Thompson 
4720d0321e0SJeremy L Thompson   // Input Evecs and Restriction
4732b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(numinputfields, qfinputfields, opinputfields, NULL, true, edata, impl, request));
4740d0321e0SJeremy L Thompson 
4750d0321e0SJeremy L Thompson   // Count number of active input fields
4760d0321e0SJeremy L Thompson   if (!numactivein) {
4770d0321e0SJeremy L Thompson     for (CeedInt i = 0; i < numinputfields; i++) {
4780d0321e0SJeremy L Thompson       // Get input vector
4792b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
4800d0321e0SJeremy L Thompson       // Check if active input
4810d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
4822b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qfinputfields[i], &size));
4832b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->qvecsin[i], 0.0));
4842b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->qvecsin[i], CEED_MEM_DEVICE, &tmp));
4852b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(numactivein + size, &activein));
4860d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
487d2643443SJeremy L Thompson           q_size = (CeedSize)Q * numelements;
4882b730f8bSJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &activein[numactivein + field]));
4892b730f8bSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(activein[numactivein + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &tmp[field * Q * numelements]));
4900d0321e0SJeremy L Thompson         }
4910d0321e0SJeremy L Thompson         numactivein += size;
4922b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->qvecsin[i], &tmp));
4930d0321e0SJeremy L Thompson       }
4940d0321e0SJeremy L Thompson     }
4950d0321e0SJeremy L Thompson     impl->qfnumactivein = numactivein;
4960d0321e0SJeremy L Thompson     impl->qfactivein    = activein;
4970d0321e0SJeremy L Thompson   }
4980d0321e0SJeremy L Thompson 
4990d0321e0SJeremy L Thompson   // Count number of active output fields
5000d0321e0SJeremy L Thompson   if (!numactiveout) {
5010d0321e0SJeremy L Thompson     for (CeedInt i = 0; i < numoutputfields; i++) {
5020d0321e0SJeremy L Thompson       // Get output vector
5032b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[i], &vec));
5040d0321e0SJeremy L Thompson       // Check if active output
5050d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
5062b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[i], &size));
5070d0321e0SJeremy L Thompson         numactiveout += size;
5080d0321e0SJeremy L Thompson       }
5090d0321e0SJeremy L Thompson     }
5100d0321e0SJeremy L Thompson     impl->qfnumactiveout = numactiveout;
5110d0321e0SJeremy L Thompson   }
5120d0321e0SJeremy L Thompson 
5130d0321e0SJeremy L Thompson   // Check sizes
5142b730f8bSJeremy L Thompson   if (!numactivein || !numactiveout) {
5150d0321e0SJeremy L Thompson     // LCOV_EXCL_START
5162b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5170d0321e0SJeremy L Thompson     // LCOV_EXCL_STOP
5182b730f8bSJeremy L Thompson   }
5190d0321e0SJeremy L Thompson 
5200d0321e0SJeremy L Thompson   // Build objects if needed
5210d0321e0SJeremy L Thompson   if (build_objects) {
5220d0321e0SJeremy L Thompson     // Create output restriction
5230d0321e0SJeremy L Thompson     CeedInt strides[3] = {1, numelements * Q, Q}; /* *NOPAD* */
5242b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceedparent, numelements, Q, numactivein * numactiveout,
5252b730f8bSJeremy L Thompson                                                      numactivein * numactiveout * numelements * Q, strides, rstr));
5260d0321e0SJeremy L Thompson     // Create assembled vector
527d2643443SJeremy L Thompson     CeedSize l_size = (CeedSize)numelements * Q * numactivein * numactiveout;
5282b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceedparent, l_size, assembled));
5290d0321e0SJeremy L Thompson   }
5302b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
5312b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &a));
5320d0321e0SJeremy L Thompson 
5330d0321e0SJeremy L Thompson   // Input basis apply
5342b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Cuda(numelements, qfinputfields, opinputfields, numinputfields, true, edata, impl));
5350d0321e0SJeremy L Thompson 
5360d0321e0SJeremy L Thompson   // Assemble QFunction
5370d0321e0SJeremy L Thompson   for (CeedInt in = 0; in < numactivein; in++) {
5380d0321e0SJeremy L Thompson     // Set Inputs
5392b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(activein[in], 1.0));
5400d0321e0SJeremy L Thompson     if (numactivein > 1) {
5412b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(activein[(in + numactivein - 1) % numactivein], 0.0));
5420d0321e0SJeremy L Thompson     }
5430d0321e0SJeremy L Thompson     // Set Outputs
5440d0321e0SJeremy L Thompson     for (CeedInt out = 0; out < numoutputfields; out++) {
5450d0321e0SJeremy L Thompson       // Get output vector
5462b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[out], &vec));
5470d0321e0SJeremy L Thompson       // Check if active output
5480d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
5492b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->qvecsout[out], CEED_MEM_DEVICE, CEED_USE_POINTER, a));
5502b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[out], &size));
5510d0321e0SJeremy L Thompson         a += size * Q * numelements;  // Advance the pointer by the size of the output
5520d0321e0SJeremy L Thompson       }
5530d0321e0SJeremy L Thompson     }
5540d0321e0SJeremy L Thompson     // Apply QFunction
5552b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * numelements, impl->qvecsin, impl->qvecsout));
5560d0321e0SJeremy L Thompson   }
5570d0321e0SJeremy L Thompson 
5580d0321e0SJeremy L Thompson   // Un-set output Qvecs to prevent accidental overwrite of Assembled
5590d0321e0SJeremy L Thompson   for (CeedInt out = 0; out < numoutputfields; out++) {
5600d0321e0SJeremy L Thompson     // Get output vector
5612b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[out], &vec));
5620d0321e0SJeremy L Thompson     // Check if active output
5630d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
5642b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->qvecsout[out], CEED_MEM_DEVICE, NULL));
5650d0321e0SJeremy L Thompson     }
5660d0321e0SJeremy L Thompson   }
5670d0321e0SJeremy L Thompson 
5680d0321e0SJeremy L Thompson   // Restore input arrays
5692b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(numinputfields, qfinputfields, opinputfields, true, edata, impl));
5700d0321e0SJeremy L Thompson 
5710d0321e0SJeremy L Thompson   // Restore output
5722b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &a));
5730d0321e0SJeremy L Thompson 
5740d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5750d0321e0SJeremy L Thompson }
5760d0321e0SJeremy L Thompson 
5770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5780d0321e0SJeremy L Thompson // Assemble Linear QFunction
5790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5802b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
5812b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, true, assembled, rstr, request);
5820d0321e0SJeremy L Thompson }
5830d0321e0SJeremy L Thompson 
5840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5850d0321e0SJeremy L Thompson // Update Assembled Linear QFunction
5860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5872b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
5882b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, false, &assembled, &rstr, request);
5890d0321e0SJeremy L Thompson }
5900d0321e0SJeremy L Thompson 
5910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5920d0321e0SJeremy L Thompson // Create point block restriction
5930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5942b730f8bSJeremy L Thompson static int CreatePBRestriction(CeedElemRestriction rstr, CeedElemRestriction *pbRstr) {
5950d0321e0SJeremy L Thompson   Ceed ceed;
5962b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
5970d0321e0SJeremy L Thompson   const CeedInt *offsets;
5982b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
5990d0321e0SJeremy L Thompson 
6000d0321e0SJeremy L Thompson   // Expand offsets
6017b63f5c6SJed Brown   CeedInt  nelem, ncomp, elemsize, compstride, *pbOffsets;
6027b63f5c6SJed Brown   CeedSize l_size;
6032b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &nelem));
6042b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &ncomp));
6052b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elemsize));
6062b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &compstride));
6072b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
6080d0321e0SJeremy L Thompson   CeedInt shift = ncomp;
6092b730f8bSJeremy L Thompson   if (compstride != 1) shift *= ncomp;
6102b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(nelem * elemsize, &pbOffsets));
6110d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < nelem * elemsize; i++) {
6120d0321e0SJeremy L Thompson     pbOffsets[i] = offsets[i] * shift;
6130d0321e0SJeremy L Thompson   }
6140d0321e0SJeremy L Thompson 
6150d0321e0SJeremy L Thompson   // Create new restriction
6162b730f8bSJeremy L Thompson   CeedCallBackend(
6172b730f8bSJeremy L Thompson       CeedElemRestrictionCreate(ceed, nelem, elemsize, ncomp * ncomp, 1, l_size * ncomp, CEED_MEM_HOST, CEED_OWN_POINTER, pbOffsets, pbRstr));
6180d0321e0SJeremy L Thompson 
6190d0321e0SJeremy L Thompson   // Cleanup
6202b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
6210d0321e0SJeremy L Thompson 
6220d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6230d0321e0SJeremy L Thompson }
6240d0321e0SJeremy L Thompson 
6250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6260d0321e0SJeremy L Thompson // Assemble diagonal setup
6270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6282b730f8bSJeremy L Thompson static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op, const bool pointBlock) {
6290d0321e0SJeremy L Thompson   Ceed ceed;
6302b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
6310d0321e0SJeremy L Thompson   CeedQFunction qf;
6322b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
6330d0321e0SJeremy L Thompson   CeedInt numinputfields, numoutputfields;
6342b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields));
6350d0321e0SJeremy L Thompson 
6360d0321e0SJeremy L Thompson   // Determine active input basis
6370d0321e0SJeremy L Thompson   CeedOperatorField  *opfields;
6380d0321e0SJeremy L Thompson   CeedQFunctionField *qffields;
6392b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &opfields, NULL, NULL));
6402b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qffields, NULL, NULL));
6410d0321e0SJeremy L Thompson   CeedInt             numemodein = 0, ncomp = 0, dim = 1;
6420d0321e0SJeremy L Thompson   CeedEvalMode       *emodein = NULL;
6430d0321e0SJeremy L Thompson   CeedBasis           basisin = NULL;
6440d0321e0SJeremy L Thompson   CeedElemRestriction rstrin  = NULL;
6450d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
6460d0321e0SJeremy L Thompson     CeedVector vec;
6472b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &vec));
6480d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
6490d0321e0SJeremy L Thompson       CeedElemRestriction rstr;
6502b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basisin));
6512b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumComponents(basisin, &ncomp));
6522b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basisin, &dim));
6532b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &rstr));
6542b730f8bSJeremy L Thompson       if (rstrin && rstrin != rstr) {
6550d0321e0SJeremy L Thompson         // LCOV_EXCL_START
6562b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator diagonal assembly");
6570d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
6582b730f8bSJeremy L Thompson       }
6590d0321e0SJeremy L Thompson       rstrin = rstr;
6600d0321e0SJeremy L Thompson       CeedEvalMode emode;
6612b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode));
6620d0321e0SJeremy L Thompson       switch (emode) {
6630d0321e0SJeremy L Thompson         case CEED_EVAL_NONE:
6640d0321e0SJeremy L Thompson         case CEED_EVAL_INTERP:
6652b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodein + 1, &emodein));
6660d0321e0SJeremy L Thompson           emodein[numemodein] = emode;
6670d0321e0SJeremy L Thompson           numemodein += 1;
6680d0321e0SJeremy L Thompson           break;
6690d0321e0SJeremy L Thompson         case CEED_EVAL_GRAD:
6702b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodein + dim, &emodein));
6712b730f8bSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) emodein[numemodein + d] = emode;
6720d0321e0SJeremy L Thompson           numemodein += dim;
6730d0321e0SJeremy L Thompson           break;
6740d0321e0SJeremy L Thompson         case CEED_EVAL_WEIGHT:
6750d0321e0SJeremy L Thompson         case CEED_EVAL_DIV:
6760d0321e0SJeremy L Thompson         case CEED_EVAL_CURL:
6770d0321e0SJeremy L Thompson           break;  // Caught by QF Assembly
6780d0321e0SJeremy L Thompson       }
6790d0321e0SJeremy L Thompson     }
6800d0321e0SJeremy L Thompson   }
6810d0321e0SJeremy L Thompson 
6820d0321e0SJeremy L Thompson   // Determine active output basis
6832b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &opfields));
6842b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qffields));
6850d0321e0SJeremy L Thompson   CeedInt             numemodeout = 0;
6860d0321e0SJeremy L Thompson   CeedEvalMode       *emodeout    = NULL;
6870d0321e0SJeremy L Thompson   CeedBasis           basisout    = NULL;
6880d0321e0SJeremy L Thompson   CeedElemRestriction rstrout     = NULL;
6890d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
6900d0321e0SJeremy L Thompson     CeedVector vec;
6912b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &vec));
6920d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
6930d0321e0SJeremy L Thompson       CeedElemRestriction rstr;
6942b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basisout));
6952b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &rstr));
6962b730f8bSJeremy L Thompson       if (rstrout && rstrout != rstr) {
6970d0321e0SJeremy L Thompson         // LCOV_EXCL_START
6982b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator diagonal assembly");
6990d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
7002b730f8bSJeremy L Thompson       }
7010d0321e0SJeremy L Thompson       rstrout = rstr;
7020d0321e0SJeremy L Thompson       CeedEvalMode emode;
7032b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode));
7040d0321e0SJeremy L Thompson       switch (emode) {
7050d0321e0SJeremy L Thompson         case CEED_EVAL_NONE:
7060d0321e0SJeremy L Thompson         case CEED_EVAL_INTERP:
7072b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodeout + 1, &emodeout));
7080d0321e0SJeremy L Thompson           emodeout[numemodeout] = emode;
7090d0321e0SJeremy L Thompson           numemodeout += 1;
7100d0321e0SJeremy L Thompson           break;
7110d0321e0SJeremy L Thompson         case CEED_EVAL_GRAD:
7122b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodeout + dim, &emodeout));
7132b730f8bSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) emodeout[numemodeout + d] = emode;
7140d0321e0SJeremy L Thompson           numemodeout += dim;
7150d0321e0SJeremy L Thompson           break;
7160d0321e0SJeremy L Thompson         case CEED_EVAL_WEIGHT:
7170d0321e0SJeremy L Thompson         case CEED_EVAL_DIV:
7180d0321e0SJeremy L Thompson         case CEED_EVAL_CURL:
7190d0321e0SJeremy L Thompson           break;  // Caught by QF Assembly
7200d0321e0SJeremy L Thompson       }
7210d0321e0SJeremy L Thompson     }
7220d0321e0SJeremy L Thompson   }
7230d0321e0SJeremy L Thompson 
7240d0321e0SJeremy L Thompson   // Operator data struct
7250d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
7262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
7272b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
7280d0321e0SJeremy L Thompson   CeedOperatorDiag_Cuda *diag = impl->diag;
7290d0321e0SJeremy L Thompson   diag->basisin               = basisin;
7300d0321e0SJeremy L Thompson   diag->basisout              = basisout;
7310d0321e0SJeremy L Thompson   diag->h_emodein             = emodein;
7320d0321e0SJeremy L Thompson   diag->h_emodeout            = emodeout;
7330d0321e0SJeremy L Thompson   diag->numemodein            = numemodein;
7340d0321e0SJeremy L Thompson   diag->numemodeout           = numemodeout;
7350d0321e0SJeremy L Thompson 
7360d0321e0SJeremy L Thompson   // Assemble kernel
73707b31e0eSJeremy L Thompson   char *diagonal_kernel_path, *diagonal_kernel_source;
7382b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
73907b31e0eSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Diagonal Assembly Kernel Source -----\n");
7402b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
7412b730f8bSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Diagonal Assembly Source Complete! -----\n");
7420d0321e0SJeremy L Thompson   CeedInt nnodes, nqpts;
7432b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basisin, &nnodes));
7442b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basisin, &nqpts));
7450d0321e0SJeremy L Thompson   diag->nnodes = nnodes;
7462b730f8bSJeremy L Thompson   CeedCallCuda(ceed, CeedCompileCuda(ceed, diagonal_kernel_source, &diag->module, 5, "NUMEMODEIN", numemodein, "NUMEMODEOUT", numemodeout, "NNODES",
7472b730f8bSJeremy L Thompson                                      nnodes, "NQPTS", nqpts, "NCOMP", ncomp));
7482b730f8bSJeremy L Thompson   CeedCallCuda(ceed, CeedGetKernelCuda(ceed, diag->module, "linearDiagonal", &diag->linearDiagonal));
7492b730f8bSJeremy L Thompson   CeedCallCuda(ceed, CeedGetKernelCuda(ceed, diag->module, "linearPointBlockDiagonal", &diag->linearPointBlock));
7502b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&diagonal_kernel_path));
7512b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&diagonal_kernel_source));
7520d0321e0SJeremy L Thompson 
7530d0321e0SJeremy L Thompson   // Basis matrices
7540d0321e0SJeremy L Thompson   const CeedInt     qBytes = nqpts * sizeof(CeedScalar);
7550d0321e0SJeremy L Thompson   const CeedInt     iBytes = qBytes * nnodes;
7560d0321e0SJeremy L Thompson   const CeedInt     gBytes = qBytes * nnodes * dim;
7570d0321e0SJeremy L Thompson   const CeedInt     eBytes = sizeof(CeedEvalMode);
7580d0321e0SJeremy L Thompson   const CeedScalar *interpin, *interpout, *gradin, *gradout;
7590d0321e0SJeremy L Thompson 
7600d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
7610d0321e0SJeremy L Thompson   CeedScalar *identity = NULL;
7620d0321e0SJeremy L Thompson   bool        evalNone = false;
7632b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < numemodein; i++) evalNone = evalNone || (emodein[i] == CEED_EVAL_NONE);
7642b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < numemodeout; i++) evalNone = evalNone || (emodeout[i] == CEED_EVAL_NONE);
7650d0321e0SJeremy L Thompson   if (evalNone) {
7662b730f8bSJeremy L Thompson     CeedCallBackend(CeedCalloc(nqpts * nnodes, &identity));
7672b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < (nnodes < nqpts ? nnodes : nqpts); i++) identity[i * nnodes + i] = 1.0;
7682b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_identity, iBytes));
7692b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaMemcpy(diag->d_identity, identity, iBytes, cudaMemcpyHostToDevice));
7700d0321e0SJeremy L Thompson   }
7710d0321e0SJeremy L Thompson 
7720d0321e0SJeremy L Thompson   // CEED_EVAL_INTERP
7732b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basisin, &interpin));
7742b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_interpin, iBytes));
7752b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMemcpy(diag->d_interpin, interpin, iBytes, cudaMemcpyHostToDevice));
7762b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basisout, &interpout));
7772b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_interpout, iBytes));
7782b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMemcpy(diag->d_interpout, interpout, iBytes, cudaMemcpyHostToDevice));
7790d0321e0SJeremy L Thompson 
7800d0321e0SJeremy L Thompson   // CEED_EVAL_GRAD
7812b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basisin, &gradin));
7822b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_gradin, gBytes));
7832b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMemcpy(diag->d_gradin, gradin, gBytes, cudaMemcpyHostToDevice));
7842b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basisout, &gradout));
7852b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_gradout, gBytes));
7862b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMemcpy(diag->d_gradout, gradout, gBytes, cudaMemcpyHostToDevice));
7870d0321e0SJeremy L Thompson 
7880d0321e0SJeremy L Thompson   // Arrays of emodes
7892b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_emodein, numemodein * eBytes));
7902b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMemcpy(diag->d_emodein, emodein, numemodein * eBytes, cudaMemcpyHostToDevice));
7912b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_emodeout, numemodeout * eBytes));
7922b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMemcpy(diag->d_emodeout, emodeout, numemodeout * eBytes, cudaMemcpyHostToDevice));
7930d0321e0SJeremy L Thompson 
7940d0321e0SJeremy L Thompson   // Restriction
7950d0321e0SJeremy L Thompson   diag->diagrstr = rstrout;
7960d0321e0SJeremy L Thompson 
7970d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7980d0321e0SJeremy L Thompson }
7990d0321e0SJeremy L Thompson 
8000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8010d0321e0SJeremy L Thompson // Assemble diagonal common code
8020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8032b730f8bSJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool pointBlock) {
8040d0321e0SJeremy L Thompson   Ceed ceed;
8052b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8060d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
8072b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
8080d0321e0SJeremy L Thompson 
8090d0321e0SJeremy L Thompson   // Assemble QFunction
8100d0321e0SJeremy L Thompson   CeedVector          assembledqf;
8110d0321e0SJeremy L Thompson   CeedElemRestriction rstr;
8122b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembledqf, &rstr, request));
8132b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&rstr));
8140d0321e0SJeremy L Thompson 
8150d0321e0SJeremy L Thompson   // Setup
8160d0321e0SJeremy L Thompson   if (!impl->diag) {
8172b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Cuda(op, pointBlock));
8180d0321e0SJeremy L Thompson   }
8190d0321e0SJeremy L Thompson   CeedOperatorDiag_Cuda *diag = impl->diag;
8200d0321e0SJeremy L Thompson   assert(diag != NULL);
8210d0321e0SJeremy L Thompson 
8220d0321e0SJeremy L Thompson   // Restriction
8230d0321e0SJeremy L Thompson   if (pointBlock && !diag->pbdiagrstr) {
8240d0321e0SJeremy L Thompson     CeedElemRestriction pbdiagrstr;
8252b730f8bSJeremy L Thompson     CeedCallBackend(CreatePBRestriction(diag->diagrstr, &pbdiagrstr));
8260d0321e0SJeremy L Thompson     diag->pbdiagrstr = pbdiagrstr;
8270d0321e0SJeremy L Thompson   }
8280d0321e0SJeremy L Thompson   CeedElemRestriction diagrstr = pointBlock ? diag->pbdiagrstr : diag->diagrstr;
8290d0321e0SJeremy L Thompson 
8300d0321e0SJeremy L Thompson   // Create diagonal vector
8310d0321e0SJeremy L Thompson   CeedVector elemdiag = pointBlock ? diag->pbelemdiag : diag->elemdiag;
8320d0321e0SJeremy L Thompson   if (!elemdiag) {
8332b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(diagrstr, NULL, &elemdiag));
8342b730f8bSJeremy L Thompson     if (pointBlock) diag->pbelemdiag = elemdiag;
8352b730f8bSJeremy L Thompson     else diag->elemdiag = elemdiag;
8360d0321e0SJeremy L Thompson   }
8372b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elemdiag, 0.0));
8380d0321e0SJeremy L Thompson 
8390d0321e0SJeremy L Thompson   // Assemble element operator diagonals
8400d0321e0SJeremy L Thompson   CeedScalar       *elemdiagarray;
8410d0321e0SJeremy L Thompson   const CeedScalar *assembledqfarray;
8422b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(elemdiag, CEED_MEM_DEVICE, &elemdiagarray));
8432b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(assembledqf, CEED_MEM_DEVICE, &assembledqfarray));
8440d0321e0SJeremy L Thompson   CeedInt nelem;
8452b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(diagrstr, &nelem));
8460d0321e0SJeremy L Thompson 
8470d0321e0SJeremy L Thompson   // Compute the diagonal of B^T D B
8480d0321e0SJeremy L Thompson   int   elemsPerBlock = 1;
8490d0321e0SJeremy L Thompson   int   grid          = nelem / elemsPerBlock + ((nelem / elemsPerBlock * elemsPerBlock < nelem) ? 1 : 0);
8502b730f8bSJeremy L Thompson   void *args[]        = {(void *)&nelem,   &diag->d_identity, &diag->d_interpin, &diag->d_gradin,   &diag->d_interpout,
8512b730f8bSJeremy L Thompson                          &diag->d_gradout, &diag->d_emodein,  &diag->d_emodeout, &assembledqfarray, &elemdiagarray};
8520d0321e0SJeremy L Thompson   if (pointBlock) {
8532b730f8bSJeremy L Thompson     CeedCallBackend(CeedRunKernelDimCuda(ceed, diag->linearPointBlock, grid, diag->nnodes, 1, elemsPerBlock, args));
8540d0321e0SJeremy L Thompson   } else {
8552b730f8bSJeremy L Thompson     CeedCallBackend(CeedRunKernelDimCuda(ceed, diag->linearDiagonal, grid, diag->nnodes, 1, elemsPerBlock, args));
8560d0321e0SJeremy L Thompson   }
8570d0321e0SJeremy L Thompson 
8580d0321e0SJeremy L Thompson   // Restore arrays
8592b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(elemdiag, &elemdiagarray));
8602b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(assembledqf, &assembledqfarray));
8610d0321e0SJeremy L Thompson 
8620d0321e0SJeremy L Thompson   // Assemble local operator diagonal
8632b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diagrstr, CEED_TRANSPOSE, elemdiag, assembled, request));
8640d0321e0SJeremy L Thompson 
8650d0321e0SJeremy L Thompson   // Cleanup
8662b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembledqf));
8670d0321e0SJeremy L Thompson 
8680d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8690d0321e0SJeremy L Thompson }
8700d0321e0SJeremy L Thompson 
8710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8720d0321e0SJeremy L Thompson // Assemble Linear Diagonal
8730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8742b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
8752b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, false));
8766aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8770d0321e0SJeremy L Thompson }
8780d0321e0SJeremy L Thompson 
8790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8800d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
8810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8822b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
8832b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, true));
8846aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8850d0321e0SJeremy L Thompson }
8860d0321e0SJeremy L Thompson 
8870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
888cc132f9aSnbeams // Single operator assembly setup
889cc132f9aSnbeams //------------------------------------------------------------------------------
890cc132f9aSnbeams static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op) {
891cc132f9aSnbeams   Ceed ceed;
8922b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
893cc132f9aSnbeams   CeedOperator_Cuda *impl;
8942b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
895cc132f9aSnbeams 
896cc132f9aSnbeams   // Get intput and output fields
897cc132f9aSnbeams   CeedInt            num_input_fields, num_output_fields;
898cc132f9aSnbeams   CeedOperatorField *input_fields;
899cc132f9aSnbeams   CeedOperatorField *output_fields;
9002b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
901cc132f9aSnbeams 
902cc132f9aSnbeams   // Determine active input basis eval mode
903cc132f9aSnbeams   CeedQFunction qf;
9042b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
905cc132f9aSnbeams   CeedQFunctionField *qf_fields;
9062b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
907cc132f9aSnbeams   // Note that the kernel will treat each dimension of a gradient action separately;
908ea61e9acSJeremy L Thompson   // i.e., when an active input has a CEED_EVAL_GRAD mode, num_emode_in will increment by dim.
909ea61e9acSJeremy L Thompson   // However, for the purposes of loading the B matrices, it will be treated as one mode, and we will load/copy the entire gradient matrix at once, so
910cc132f9aSnbeams   // num_B_in_mats_to_load will be incremented by 1.
911cc132f9aSnbeams   CeedInt             num_emode_in = 0, dim = 1, num_B_in_mats_to_load = 0, size_B_in = 0;
912cc132f9aSnbeams   CeedEvalMode       *eval_mode_in = NULL;  // will be of size num_B_in_mats_load
913cc132f9aSnbeams   CeedBasis           basis_in     = NULL;
914b216396cSJeremy L Thompson   CeedInt             nqpts = 0, esize = 0;
915cc132f9aSnbeams   CeedElemRestriction rstr_in = NULL;
916cc132f9aSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
917cc132f9aSnbeams     CeedVector vec;
9182b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
919cc132f9aSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
9202b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis_in));
9212b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis_in, &dim));
9222b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &nqpts));
9232b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
9242b730f8bSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &esize));
925cc132f9aSnbeams       CeedEvalMode eval_mode;
9262b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
927cc132f9aSnbeams       if (eval_mode != CEED_EVAL_NONE) {
9282b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_B_in_mats_to_load + 1, &eval_mode_in));
929cc132f9aSnbeams         eval_mode_in[num_B_in_mats_to_load] = eval_mode;
930cc132f9aSnbeams         num_B_in_mats_to_load += 1;
931cc132f9aSnbeams         if (eval_mode == CEED_EVAL_GRAD) {
932cc132f9aSnbeams           num_emode_in += dim;
933cc132f9aSnbeams           size_B_in += dim * esize * nqpts;
934cc132f9aSnbeams         } else {
935cc132f9aSnbeams           num_emode_in += 1;
936cc132f9aSnbeams           size_B_in += esize * nqpts;
937cc132f9aSnbeams         }
938cc132f9aSnbeams       }
939cc132f9aSnbeams     }
940cc132f9aSnbeams   }
941cc132f9aSnbeams 
942cc132f9aSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
9432b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
944cc132f9aSnbeams   CeedInt             num_emode_out = 0, num_B_out_mats_to_load = 0, size_B_out = 0;
945cc132f9aSnbeams   CeedEvalMode       *eval_mode_out = NULL;
946cc132f9aSnbeams   CeedBasis           basis_out     = NULL;
947cc132f9aSnbeams   CeedElemRestriction rstr_out      = NULL;
948cc132f9aSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
949cc132f9aSnbeams     CeedVector vec;
9502b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
951cc132f9aSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
9522b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis_out));
9532b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
9542b730f8bSJeremy L Thompson       if (rstr_out && rstr_out != rstr_in) {
955cc132f9aSnbeams         // LCOV_EXCL_START
9562b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator assembly");
957cc132f9aSnbeams         // LCOV_EXCL_STOP
9582b730f8bSJeremy L Thompson       }
959cc132f9aSnbeams       CeedEvalMode eval_mode;
9602b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
961cc132f9aSnbeams       if (eval_mode != CEED_EVAL_NONE) {
9622b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_B_out_mats_to_load + 1, &eval_mode_out));
963cc132f9aSnbeams         eval_mode_out[num_B_out_mats_to_load] = eval_mode;
964cc132f9aSnbeams         num_B_out_mats_to_load += 1;
965cc132f9aSnbeams         if (eval_mode == CEED_EVAL_GRAD) {
966cc132f9aSnbeams           num_emode_out += dim;
967cc132f9aSnbeams           size_B_out += dim * esize * nqpts;
968cc132f9aSnbeams         } else {
969cc132f9aSnbeams           num_emode_out += 1;
970cc132f9aSnbeams           size_B_out += esize * nqpts;
971cc132f9aSnbeams         }
972cc132f9aSnbeams       }
973cc132f9aSnbeams     }
974cc132f9aSnbeams   }
975cc132f9aSnbeams 
9762b730f8bSJeremy L Thompson   if (num_emode_in == 0 || num_emode_out == 0) {
977cc132f9aSnbeams     // LCOV_EXCL_START
9782b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
979cc132f9aSnbeams     // LCOV_EXCL_STOP
9802b730f8bSJeremy L Thompson   }
981cc132f9aSnbeams 
982cc132f9aSnbeams   CeedInt nelem, ncomp;
9832b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &nelem));
9842b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &ncomp));
985cc132f9aSnbeams 
9862b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
987cc132f9aSnbeams   CeedOperatorAssemble_Cuda *asmb = impl->asmb;
988cc132f9aSnbeams   asmb->nelem                     = nelem;
989cc132f9aSnbeams 
990cc132f9aSnbeams   // Compile kernels
991cc132f9aSnbeams   int elemsPerBlock     = 1;
992cc132f9aSnbeams   asmb->elemsPerBlock   = elemsPerBlock;
99359ad764aSnbeams   CeedInt    block_size = esize * esize * elemsPerBlock;
994cc132f9aSnbeams   Ceed_Cuda *cuda_data;
9952b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &cuda_data));
99607b31e0eSJeremy L Thompson   char *assembly_kernel_path, *assembly_kernel_source;
9972b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble.h", &assembly_kernel_path));
99807b31e0eSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Assembly Kernel Source -----\n");
9992b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
100007b31e0eSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Assembly Source Complete! -----\n");
100107b31e0eSJeremy L Thompson   bool fallback = block_size > cuda_data->device_prop.maxThreadsPerBlock;
100207b31e0eSJeremy L Thompson   if (fallback) {
100359ad764aSnbeams     // Use fallback kernel with 1D threadblock
100459ad764aSnbeams     block_size         = esize * elemsPerBlock;
100559ad764aSnbeams     asmb->block_size_x = esize;
100659ad764aSnbeams     asmb->block_size_y = 1;
100759ad764aSnbeams   } else {  // Use kernel with 2D threadblock
100859ad764aSnbeams     asmb->block_size_x = esize;
100959ad764aSnbeams     asmb->block_size_y = esize;
101007b31e0eSJeremy L Thompson   }
10112b730f8bSJeremy L Thompson   CeedCallCuda(ceed, CeedCompileCuda(ceed, assembly_kernel_source, &asmb->module, 7, "NELEM", nelem, "NUMEMODEIN", num_emode_in, "NUMEMODEOUT",
10122b730f8bSJeremy L Thompson                                      num_emode_out, "NQPTS", nqpts, "NNODES", esize, "BLOCK_SIZE", block_size, "NCOMP", ncomp));
10132b730f8bSJeremy L Thompson   CeedCallCuda(ceed, CeedGetKernelCuda(ceed, asmb->module, fallback ? "linearAssembleFallback" : "linearAssemble", &asmb->linearAssemble));
10142b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
10152b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1016cc132f9aSnbeams 
1017cc132f9aSnbeams   // Build 'full' B matrices (not 1D arrays used for tensor-product matrices)
1018cc132f9aSnbeams   const CeedScalar *interp_in, *grad_in;
10192b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in));
10202b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in));
1021cc132f9aSnbeams 
1022cc132f9aSnbeams   // Load into B_in, in order that they will be used in eval_mode
1023cc132f9aSnbeams   const CeedInt inBytes   = size_B_in * sizeof(CeedScalar);
1024cc132f9aSnbeams   CeedInt       mat_start = 0;
10252b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_in, inBytes));
1026cc132f9aSnbeams   for (int i = 0; i < num_B_in_mats_to_load; i++) {
1027cc132f9aSnbeams     CeedEvalMode eval_mode = eval_mode_in[i];
1028cc132f9aSnbeams     if (eval_mode == CEED_EVAL_INTERP) {
10292b730f8bSJeremy L Thompson       CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_in[mat_start], interp_in, esize * nqpts * sizeof(CeedScalar), cudaMemcpyHostToDevice));
1030cc132f9aSnbeams       mat_start += esize * nqpts;
1031cc132f9aSnbeams     } else if (eval_mode == CEED_EVAL_GRAD) {
10322b730f8bSJeremy L Thompson       CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_in[mat_start], grad_in, dim * esize * nqpts * sizeof(CeedScalar), cudaMemcpyHostToDevice));
1033cc132f9aSnbeams       mat_start += dim * esize * nqpts;
1034cc132f9aSnbeams     }
1035cc132f9aSnbeams   }
1036cc132f9aSnbeams 
1037cc132f9aSnbeams   const CeedScalar *interp_out, *grad_out;
1038cc132f9aSnbeams   // Note that this function currently assumes 1 basis, so this should always be true
1039cc132f9aSnbeams   // for now
1040cc132f9aSnbeams   if (basis_out == basis_in) {
1041cc132f9aSnbeams     interp_out = interp_in;
1042cc132f9aSnbeams     grad_out   = grad_in;
1043cc132f9aSnbeams   } else {
10442b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out));
10452b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out));
1046cc132f9aSnbeams   }
1047cc132f9aSnbeams 
1048cc132f9aSnbeams   // Load into B_out, in order that they will be used in eval_mode
1049cc132f9aSnbeams   const CeedInt outBytes = size_B_out * sizeof(CeedScalar);
1050cc132f9aSnbeams   mat_start              = 0;
10512b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_out, outBytes));
1052cc132f9aSnbeams   for (int i = 0; i < num_B_out_mats_to_load; i++) {
1053cc132f9aSnbeams     CeedEvalMode eval_mode = eval_mode_out[i];
1054cc132f9aSnbeams     if (eval_mode == CEED_EVAL_INTERP) {
10552b730f8bSJeremy L Thompson       CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_out[mat_start], interp_out, esize * nqpts * sizeof(CeedScalar), cudaMemcpyHostToDevice));
1056cc132f9aSnbeams       mat_start += esize * nqpts;
1057cc132f9aSnbeams     } else if (eval_mode == CEED_EVAL_GRAD) {
10582b730f8bSJeremy L Thompson       CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_out[mat_start], grad_out, dim * esize * nqpts * sizeof(CeedScalar), cudaMemcpyHostToDevice));
1059cc132f9aSnbeams       mat_start += dim * esize * nqpts;
1060cc132f9aSnbeams     }
1061cc132f9aSnbeams   }
1062cc132f9aSnbeams   return CEED_ERROR_SUCCESS;
1063cc132f9aSnbeams }
1064cc132f9aSnbeams 
1065cc132f9aSnbeams //------------------------------------------------------------------------------
1066cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1067cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1068cefa2673SJeremy L Thompson //
1069ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1070cefa2673SJeremy L Thompson // modes).
1071cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1072cc132f9aSnbeams //------------------------------------------------------------------------------
10732b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Cuda(CeedOperator op, CeedInt offset, CeedVector values) {
1074cc132f9aSnbeams   Ceed ceed;
10752b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1076cc132f9aSnbeams   CeedOperator_Cuda *impl;
10772b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1078cc132f9aSnbeams 
1079cc132f9aSnbeams   // Setup
1080cc132f9aSnbeams   if (!impl->asmb) {
10812b730f8bSJeremy L Thompson     CeedCallBackend(CeedSingleOperatorAssembleSetup_Cuda(op));
1082b216396cSJeremy L Thompson     assert(impl->asmb != NULL);
1083cc132f9aSnbeams   }
1084cc132f9aSnbeams 
1085cc132f9aSnbeams   // Assemble QFunction
1086cc132f9aSnbeams   CeedVector          assembled_qf;
1087cc132f9aSnbeams   CeedElemRestriction rstr_q;
10882b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
10892b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&rstr_q));
1090cc132f9aSnbeams   CeedScalar *values_array;
109128ec399dSJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1092cc132f9aSnbeams   values_array += offset;
1093cc132f9aSnbeams   const CeedScalar *qf_array;
10942b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &qf_array));
1095cc132f9aSnbeams 
1096cc132f9aSnbeams   // Compute B^T D B
1097cc132f9aSnbeams   const CeedInt nelem         = impl->asmb->nelem;
1098cc132f9aSnbeams   const CeedInt elemsPerBlock = impl->asmb->elemsPerBlock;
10992b730f8bSJeremy L Thompson   const CeedInt grid          = nelem / elemsPerBlock + ((nelem / elemsPerBlock * elemsPerBlock < nelem) ? 1 : 0);
11002b730f8bSJeremy L Thompson   void         *args[]        = {&impl->asmb->d_B_in, &impl->asmb->d_B_out, &qf_array, &values_array};
11012b730f8bSJeremy L Thompson   CeedCallBackend(
11022b730f8bSJeremy L Thompson       CeedRunKernelDimCuda(ceed, impl->asmb->linearAssemble, grid, impl->asmb->block_size_x, impl->asmb->block_size_y, elemsPerBlock, args));
1103cc132f9aSnbeams 
1104cc132f9aSnbeams   // Restore arrays
11052b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
11062b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &qf_array));
1107cc132f9aSnbeams 
1108cc132f9aSnbeams   // Cleanup
11092b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1110cc132f9aSnbeams 
1111cc132f9aSnbeams   return CEED_ERROR_SUCCESS;
1112cc132f9aSnbeams }
1113cc132f9aSnbeams 
1114cc132f9aSnbeams //------------------------------------------------------------------------------
11150d0321e0SJeremy L Thompson // Create operator
11160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11170d0321e0SJeremy L Thompson int CeedOperatorCreate_Cuda(CeedOperator op) {
11180d0321e0SJeremy L Thompson   Ceed ceed;
11192b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11200d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
11210d0321e0SJeremy L Thompson 
11222b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
11232b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
11240d0321e0SJeremy L Thompson 
11252b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda));
11262b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Cuda));
11272b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Cuda));
11282b730f8bSJeremy L Thompson   CeedCallBackend(
11292b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda));
11302b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Cuda));
11312b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda));
11322b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda));
11330d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11340d0321e0SJeremy L Thompson }
11350d0321e0SJeremy L Thompson 
11360d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1137