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*2b730f8bSJeremy L Thompson #include <ceed/backend.h> 10*2b730f8bSJeremy L Thompson #include <ceed/ceed.h> 11*2b730f8bSJeremy 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> 16*2b730f8bSJeremy L Thompson 170d0321e0SJeremy L Thompson #include "../cuda/ceed-cuda-compile.h" 18*2b730f8bSJeremy L Thompson #include "ceed-cuda-ref.h" 190d0321e0SJeremy L Thompson 200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 210d0321e0SJeremy L Thompson // Destroy operator 220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 230d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Cuda(CeedOperator op) { 240d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 25*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 260d0321e0SJeremy L Thompson 270d0321e0SJeremy L Thompson // Apply data 280d0321e0SJeremy L Thompson for (CeedInt i = 0; i < impl->numein + impl->numeout; i++) { 29*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->evecs[i])); 300d0321e0SJeremy L Thompson } 31*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->evecs)); 320d0321e0SJeremy L Thompson 330d0321e0SJeremy L Thompson for (CeedInt i = 0; i < impl->numein; i++) { 34*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->qvecsin[i])); 350d0321e0SJeremy L Thompson } 36*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->qvecsin)); 370d0321e0SJeremy L Thompson 380d0321e0SJeremy L Thompson for (CeedInt i = 0; i < impl->numeout; i++) { 39*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->qvecsout[i])); 400d0321e0SJeremy L Thompson } 41*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->qvecsout)); 420d0321e0SJeremy L Thompson 430d0321e0SJeremy L Thompson // QFunction assembly data 440d0321e0SJeremy L Thompson for (CeedInt i = 0; i < impl->qfnumactivein; i++) { 45*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->qfactivein[i])); 460d0321e0SJeremy L Thompson } 47*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->qfactivein)); 480d0321e0SJeremy L Thompson 490d0321e0SJeremy L Thompson // Diag data 500d0321e0SJeremy L Thompson if (impl->diag) { 510d0321e0SJeremy L Thompson Ceed ceed; 52*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 53*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(impl->diag->module)); 54*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->diag->h_emodein)); 55*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->diag->h_emodeout)); 56*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_emodein)); 57*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_emodeout)); 58*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_identity)); 59*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_interpin)); 60*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_interpout)); 61*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_gradin)); 62*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_gradout)); 63*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->pbdiagrstr)); 64*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->elemdiag)); 65*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->pbelemdiag)); 660d0321e0SJeremy L Thompson } 67*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->diag)); 680d0321e0SJeremy L Thompson 69cc132f9aSnbeams if (impl->asmb) { 70cc132f9aSnbeams Ceed ceed; 71*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 72*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(impl->asmb->module)); 73*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_in)); 74*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_out)); 75cc132f9aSnbeams } 76*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->asmb)); 77cc132f9aSnbeams 78*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 790d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 800d0321e0SJeremy L Thompson } 810d0321e0SJeremy L Thompson 820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 830d0321e0SJeremy L Thompson // Setup infields or outfields 840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 85*2b730f8bSJeremy L Thompson static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool isinput, CeedVector *evecs, CeedVector *qvecs, CeedInt starte, 86*2b730f8bSJeremy L Thompson CeedInt numfields, CeedInt Q, CeedInt numelements) { 87*2b730f8bSJeremy L Thompson CeedInt dim, size; 88d2643443SJeremy L Thompson CeedSize q_size; 890d0321e0SJeremy L Thompson Ceed ceed; 90*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 910d0321e0SJeremy L Thompson CeedBasis basis; 920d0321e0SJeremy L Thompson CeedElemRestriction Erestrict; 930d0321e0SJeremy L Thompson CeedOperatorField *opfields; 940d0321e0SJeremy L Thompson CeedQFunctionField *qffields; 950d0321e0SJeremy L Thompson CeedVector fieldvec; 960d0321e0SJeremy L Thompson bool strided; 970d0321e0SJeremy L Thompson bool skiprestrict; 980d0321e0SJeremy L Thompson 990d0321e0SJeremy L Thompson if (isinput) { 100*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &opfields, NULL, NULL)); 101*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qffields, NULL, NULL)); 1020d0321e0SJeremy L Thompson } else { 103*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &opfields)); 104*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qffields)); 1050d0321e0SJeremy L Thompson } 1060d0321e0SJeremy L Thompson 1070d0321e0SJeremy L Thompson // Loop over fields 1080d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numfields; i++) { 1090d0321e0SJeremy L Thompson CeedEvalMode emode; 110*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode)); 1110d0321e0SJeremy L Thompson 1120d0321e0SJeremy L Thompson strided = false; 1130d0321e0SJeremy L Thompson skiprestrict = false; 1140d0321e0SJeremy L Thompson if (emode != CEED_EVAL_WEIGHT) { 115*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &Erestrict)); 1160d0321e0SJeremy L Thompson 1170d0321e0SJeremy L Thompson // Check whether this field can skip the element restriction: 1180d0321e0SJeremy L Thompson // must be passive input, with emode NONE, and have a strided restriction with 1190d0321e0SJeremy L Thompson // 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: 124*2b730f8bSJeremy 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 129*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(Erestrict, &strided)); 1300d0321e0SJeremy L Thompson if (strided) { 1310d0321e0SJeremy L Thompson // Check if vector is already in preferred backend ordering 132*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(Erestrict, &skiprestrict)); 1330d0321e0SJeremy L Thompson } 1340d0321e0SJeremy L Thompson } 1350d0321e0SJeremy L Thompson } 1360d0321e0SJeremy L Thompson } 1370d0321e0SJeremy L Thompson if (skiprestrict) { 1380d0321e0SJeremy L Thompson // We do not need an E-Vector, but will use the input field vector's data 1390d0321e0SJeremy L Thompson // directly in the operator application. 1400d0321e0SJeremy L Thompson evecs[i + starte] = NULL; 1410d0321e0SJeremy L Thompson } else { 142*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateVector(Erestrict, NULL, &evecs[i + starte])); 1430d0321e0SJeremy L Thompson } 1440d0321e0SJeremy L Thompson } 1450d0321e0SJeremy L Thompson 1460d0321e0SJeremy L Thompson switch (emode) { 1470d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 148*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size)); 149d2643443SJeremy L Thompson q_size = (CeedSize)numelements * Q * size; 150*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i])); 1510d0321e0SJeremy L Thompson break; 1520d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 153*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size)); 154d2643443SJeremy L Thompson q_size = (CeedSize)numelements * Q * size; 155*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i])); 1560d0321e0SJeremy L Thompson break; 1570d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 158*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basis)); 159*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size)); 160*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 161d2643443SJeremy L Thompson q_size = (CeedSize)numelements * Q * size; 162*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i])); 1630d0321e0SJeremy L Thompson break; 1640d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: // Only on input fields 165*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basis)); 166d2643443SJeremy L Thompson q_size = (CeedSize)numelements * Q; 167*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i])); 168*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, NULL, qvecs[i])); 1690d0321e0SJeremy L Thompson break; 1700d0321e0SJeremy L Thompson case CEED_EVAL_DIV: 1710d0321e0SJeremy L Thompson break; // TODO: Not implemented 1720d0321e0SJeremy L Thompson case CEED_EVAL_CURL: 1730d0321e0SJeremy L Thompson break; // TODO: Not implemented 1740d0321e0SJeremy L Thompson } 1750d0321e0SJeremy L Thompson } 1760d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1770d0321e0SJeremy L Thompson } 1780d0321e0SJeremy L Thompson 1790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1800d0321e0SJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) 1810d0321e0SJeremy L Thompson // to the named inputs and outputs of its CeedQFunction. 1820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1830d0321e0SJeremy L Thompson static int CeedOperatorSetup_Cuda(CeedOperator op) { 1840d0321e0SJeremy L Thompson bool setupdone; 185*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &setupdone)); 186*2b730f8bSJeremy L Thompson if (setupdone) return CEED_ERROR_SUCCESS; 1870d0321e0SJeremy L Thompson Ceed ceed; 188*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1890d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 190*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1910d0321e0SJeremy L Thompson CeedQFunction qf; 192*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1930d0321e0SJeremy L Thompson CeedInt Q, numelements, numinputfields, numoutputfields; 194*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 195*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &numelements)); 1960d0321e0SJeremy L Thompson CeedOperatorField *opinputfields, *opoutputfields; 197*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields)); 1980d0321e0SJeremy L Thompson CeedQFunctionField *qfinputfields, *qfoutputfields; 199*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields)); 2000d0321e0SJeremy L Thompson 2010d0321e0SJeremy L Thompson // Allocate 202*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(numinputfields + numoutputfields, &impl->evecs)); 2030d0321e0SJeremy L Thompson 204*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->qvecsin)); 205*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->qvecsout)); 2060d0321e0SJeremy L Thompson 207*2b730f8bSJeremy L Thompson impl->numein = numinputfields; 208*2b730f8bSJeremy L Thompson impl->numeout = numoutputfields; 2090d0321e0SJeremy L Thompson 2100d0321e0SJeremy L Thompson // Set up infield and outfield evecs and qvecs 2110d0321e0SJeremy L Thompson // Infields 212*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, true, impl->evecs, impl->qvecsin, 0, numinputfields, Q, numelements)); 2130d0321e0SJeremy L Thompson 2140d0321e0SJeremy L Thompson // Outfields 215*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, false, impl->evecs, impl->qvecsout, numinputfields, numoutputfields, Q, numelements)); 2160d0321e0SJeremy L Thompson 217*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 2180d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2190d0321e0SJeremy L Thompson } 2200d0321e0SJeremy L Thompson 2210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2220d0321e0SJeremy L Thompson // Setup Operator Inputs 2230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 224*2b730f8bSJeremy L Thompson static inline int CeedOperatorSetupInputs_Cuda(CeedInt numinputfields, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields, 2250d0321e0SJeremy L Thompson CeedVector invec, const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX], 2260d0321e0SJeremy L Thompson CeedOperator_Cuda *impl, CeedRequest *request) { 2270d0321e0SJeremy L Thompson CeedEvalMode emode; 2280d0321e0SJeremy L Thompson CeedVector vec; 2290d0321e0SJeremy L Thompson CeedElemRestriction Erestrict; 2300d0321e0SJeremy L Thompson 2310d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numinputfields; i++) { 2320d0321e0SJeremy L Thompson // Get input vector 233*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec)); 2340d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 235*2b730f8bSJeremy L Thompson if (skipactive) continue; 236*2b730f8bSJeremy L Thompson else vec = invec; 2370d0321e0SJeremy L Thompson } 2380d0321e0SJeremy L Thompson 239*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode)); 2400d0321e0SJeremy L Thompson if (emode == CEED_EVAL_WEIGHT) { // Skip 2410d0321e0SJeremy L Thompson } else { 2420d0321e0SJeremy L Thompson // Get input vector 243*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec)); 2440d0321e0SJeremy L Thompson // Get input element restriction 245*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict)); 246*2b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = invec; 2470d0321e0SJeremy L Thompson // Restrict, if necessary 2480d0321e0SJeremy L Thompson if (!impl->evecs[i]) { 2490d0321e0SJeremy L Thompson // No restriction for this field; read data directly from vec. 250*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&edata[i])); 2510d0321e0SJeremy L Thompson } else { 252*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(Erestrict, CEED_NOTRANSPOSE, vec, impl->evecs[i], request)); 2530d0321e0SJeremy L Thompson // Get evec 254*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(impl->evecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&edata[i])); 2550d0321e0SJeremy L Thompson } 2560d0321e0SJeremy L Thompson } 2570d0321e0SJeremy L Thompson } 2580d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2590d0321e0SJeremy L Thompson } 2600d0321e0SJeremy L Thompson 2610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2620d0321e0SJeremy L Thompson // Input Basis Action 2630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 264*2b730f8bSJeremy L Thompson static inline int CeedOperatorInputBasis_Cuda(CeedInt numelements, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields, 265*2b730f8bSJeremy L Thompson CeedInt numinputfields, const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX], 266*2b730f8bSJeremy L Thompson CeedOperator_Cuda *impl) { 2670d0321e0SJeremy L Thompson CeedInt elemsize, size; 2680d0321e0SJeremy L Thompson CeedElemRestriction Erestrict; 2690d0321e0SJeremy L Thompson CeedEvalMode emode; 2700d0321e0SJeremy L Thompson CeedBasis basis; 2710d0321e0SJeremy L Thompson 2720d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numinputfields; i++) { 2730d0321e0SJeremy L Thompson // Skip active input 2740d0321e0SJeremy L Thompson if (skipactive) { 2750d0321e0SJeremy L Thompson CeedVector vec; 276*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec)); 277*2b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 2780d0321e0SJeremy L Thompson } 2790d0321e0SJeremy L Thompson // Get elemsize, emode, size 280*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict)); 281*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elemsize)); 282*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode)); 283*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qfinputfields[i], &size)); 2840d0321e0SJeremy L Thompson // Basis action 2850d0321e0SJeremy L Thompson switch (emode) { 2860d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 287*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->qvecsin[i], CEED_MEM_DEVICE, CEED_USE_POINTER, edata[i])); 2880d0321e0SJeremy L Thompson break; 2890d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 290*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(opinputfields[i], &basis)); 291*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, impl->evecs[i], impl->qvecsin[i])); 2920d0321e0SJeremy L Thompson break; 2930d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 294*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(opinputfields[i], &basis)); 295*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, impl->evecs[i], impl->qvecsin[i])); 2960d0321e0SJeremy L Thompson break; 2970d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: 2980d0321e0SJeremy L Thompson break; // No action 2990d0321e0SJeremy L Thompson case CEED_EVAL_DIV: 3000d0321e0SJeremy L Thompson break; // TODO: Not implemented 3010d0321e0SJeremy L Thompson case CEED_EVAL_CURL: 3020d0321e0SJeremy L Thompson break; // TODO: Not implemented 3030d0321e0SJeremy L Thompson } 3040d0321e0SJeremy L Thompson } 3050d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3060d0321e0SJeremy L Thompson } 3070d0321e0SJeremy L Thompson 3080d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3090d0321e0SJeremy L Thompson // Restore Input Vectors 3100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 311*2b730f8bSJeremy L Thompson static inline int CeedOperatorRestoreInputs_Cuda(CeedInt numinputfields, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields, 312*2b730f8bSJeremy L Thompson const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) { 3130d0321e0SJeremy L Thompson CeedEvalMode emode; 3140d0321e0SJeremy L Thompson CeedVector vec; 3150d0321e0SJeremy L Thompson 3160d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numinputfields; i++) { 3170d0321e0SJeremy L Thompson // Skip active input 3180d0321e0SJeremy L Thompson if (skipactive) { 319*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec)); 320*2b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 3210d0321e0SJeremy L Thompson } 322*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode)); 3230d0321e0SJeremy L Thompson if (emode == CEED_EVAL_WEIGHT) { // Skip 3240d0321e0SJeremy L Thompson } else { 3250d0321e0SJeremy L Thompson if (!impl->evecs[i]) { // This was a skiprestrict case 326*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec)); 327*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&edata[i])); 3280d0321e0SJeremy L Thompson } else { 329*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(impl->evecs[i], (const CeedScalar **)&edata[i])); 3300d0321e0SJeremy L Thompson } 3310d0321e0SJeremy L Thompson } 3320d0321e0SJeremy L Thompson } 3330d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3340d0321e0SJeremy L Thompson } 3350d0321e0SJeremy L Thompson 3360d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3370d0321e0SJeremy L Thompson // Apply and add to output 3380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 339*2b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector invec, CeedVector outvec, CeedRequest *request) { 3400d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 341*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 3420d0321e0SJeremy L Thompson CeedQFunction qf; 343*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 3440d0321e0SJeremy L Thompson CeedInt Q, numelements, elemsize, numinputfields, numoutputfields, size; 345*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 346*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &numelements)); 3470d0321e0SJeremy L Thompson CeedOperatorField *opinputfields, *opoutputfields; 348*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields)); 3490d0321e0SJeremy L Thompson CeedQFunctionField *qfinputfields, *qfoutputfields; 350*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields)); 3510d0321e0SJeremy L Thompson CeedEvalMode emode; 3520d0321e0SJeremy L Thompson CeedVector vec; 3530d0321e0SJeremy L Thompson CeedBasis basis; 3540d0321e0SJeremy L Thompson CeedElemRestriction Erestrict; 3550d0321e0SJeremy L Thompson CeedScalar *edata[2 * CEED_FIELD_MAX] = {0}; 3560d0321e0SJeremy L Thompson 3570d0321e0SJeremy L Thompson // Setup 358*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetup_Cuda(op)); 3590d0321e0SJeremy L Thompson 3600d0321e0SJeremy L Thompson // Input Evecs and Restriction 361*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(numinputfields, qfinputfields, opinputfields, invec, false, edata, impl, request)); 3620d0321e0SJeremy L Thompson 3630d0321e0SJeremy L Thompson // Input basis apply if needed 364*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Cuda(numelements, qfinputfields, opinputfields, numinputfields, false, edata, impl)); 3650d0321e0SJeremy L Thompson 3660d0321e0SJeremy L Thompson // Output pointers, as necessary 3670d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numoutputfields; i++) { 368*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode)); 3690d0321e0SJeremy L Thompson if (emode == CEED_EVAL_NONE) { 3700d0321e0SJeremy L Thompson // Set the output Q-Vector to use the E-Vector data directly. 371*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->evecs[i + impl->numein], CEED_MEM_DEVICE, &edata[i + numinputfields])); 372*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->qvecsout[i], CEED_MEM_DEVICE, CEED_USE_POINTER, edata[i + numinputfields])); 3730d0321e0SJeremy L Thompson } 3740d0321e0SJeremy L Thompson } 3750d0321e0SJeremy L Thompson 3760d0321e0SJeremy L Thompson // Q function 377*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, numelements * Q, impl->qvecsin, impl->qvecsout)); 3780d0321e0SJeremy L Thompson 3790d0321e0SJeremy L Thompson // Output basis apply if needed 3800d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numoutputfields; i++) { 3810d0321e0SJeremy L Thompson // Get elemsize, emode, size 382*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(opoutputfields[i], &Erestrict)); 383*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elemsize)); 384*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode)); 385*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[i], &size)); 3860d0321e0SJeremy L Thompson // Basis action 3870d0321e0SJeremy L Thompson switch (emode) { 3880d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 3890d0321e0SJeremy L Thompson break; 3900d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 391*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(opoutputfields[i], &basis)); 392*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, numelements, CEED_TRANSPOSE, CEED_EVAL_INTERP, impl->qvecsout[i], impl->evecs[i + impl->numein])); 3930d0321e0SJeremy L Thompson break; 3940d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 395*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(opoutputfields[i], &basis)); 396*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, numelements, CEED_TRANSPOSE, CEED_EVAL_GRAD, impl->qvecsout[i], impl->evecs[i + impl->numein])); 3970d0321e0SJeremy L Thompson break; 3980d0321e0SJeremy L Thompson // LCOV_EXCL_START 3990d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: { 4000d0321e0SJeremy L Thompson Ceed ceed; 401*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 402*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 4030d0321e0SJeremy L Thompson break; // Should not occur 4040d0321e0SJeremy L Thompson } 4050d0321e0SJeremy L Thompson case CEED_EVAL_DIV: 4060d0321e0SJeremy L Thompson break; // TODO: Not implemented 4070d0321e0SJeremy L Thompson case CEED_EVAL_CURL: 4080d0321e0SJeremy L Thompson break; // TODO: Not implemented 4090d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 4100d0321e0SJeremy L Thompson } 4110d0321e0SJeremy L Thompson } 4120d0321e0SJeremy L Thompson 4130d0321e0SJeremy L Thompson // Output restriction 4140d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numoutputfields; i++) { 4150d0321e0SJeremy L Thompson // Restore evec 416*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode)); 4170d0321e0SJeremy L Thompson if (emode == CEED_EVAL_NONE) { 418*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->evecs[i + impl->numein], &edata[i + numinputfields])); 4190d0321e0SJeremy L Thompson } 4200d0321e0SJeremy L Thompson // Get output vector 421*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[i], &vec)); 4220d0321e0SJeremy L Thompson // Restrict 423*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(opoutputfields[i], &Erestrict)); 4240d0321e0SJeremy L Thompson // Active 425*2b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = outvec; 4260d0321e0SJeremy L Thompson 427*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(Erestrict, CEED_TRANSPOSE, impl->evecs[i + impl->numein], vec, request)); 4280d0321e0SJeremy L Thompson } 4290d0321e0SJeremy L Thompson 4300d0321e0SJeremy L Thompson // Restore input arrays 431*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(numinputfields, qfinputfields, opinputfields, false, edata, impl)); 4320d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4330d0321e0SJeremy L Thompson } 4340d0321e0SJeremy L Thompson 4350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4360d0321e0SJeremy L Thompson // Core code for assembling linear QFunction 4370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 438*2b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr, 4390d0321e0SJeremy L Thompson CeedRequest *request) { 4400d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 441*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 4420d0321e0SJeremy L Thompson CeedQFunction qf; 443*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 4440d0321e0SJeremy L Thompson CeedInt Q, numelements, numinputfields, numoutputfields, size; 445d2643443SJeremy L Thompson CeedSize q_size; 446*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 447*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &numelements)); 4480d0321e0SJeremy L Thompson CeedOperatorField *opinputfields, *opoutputfields; 449*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields)); 4500d0321e0SJeremy L Thompson CeedQFunctionField *qfinputfields, *qfoutputfields; 451*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields)); 4520d0321e0SJeremy L Thompson CeedVector vec; 4530d0321e0SJeremy L Thompson CeedInt numactivein = impl->qfnumactivein, numactiveout = impl->qfnumactiveout; 4540d0321e0SJeremy L Thompson CeedVector *activein = impl->qfactivein; 4550d0321e0SJeremy L Thompson CeedScalar *a, *tmp; 4560d0321e0SJeremy L Thompson Ceed ceed, ceedparent; 457*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 458*2b730f8bSJeremy L Thompson CeedCallBackend(CeedGetOperatorFallbackParentCeed(ceed, &ceedparent)); 4590d0321e0SJeremy L Thompson ceedparent = ceedparent ? ceedparent : ceed; 4600d0321e0SJeremy L Thompson CeedScalar *edata[2 * CEED_FIELD_MAX]; 4610d0321e0SJeremy L Thompson 4620d0321e0SJeremy L Thompson // Setup 463*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetup_Cuda(op)); 4640d0321e0SJeremy L Thompson 4650d0321e0SJeremy L Thompson // Check for identity 4660d0321e0SJeremy L Thompson bool identityqf; 467*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionIsIdentity(qf, &identityqf)); 468*2b730f8bSJeremy L Thompson if (identityqf) { 4690d0321e0SJeremy L Thompson // LCOV_EXCL_START 470*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Assembling identity QFunctions not supported"); 4710d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 472*2b730f8bSJeremy L Thompson } 4730d0321e0SJeremy L Thompson 4740d0321e0SJeremy L Thompson // Input Evecs and Restriction 475*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(numinputfields, qfinputfields, opinputfields, NULL, true, edata, impl, request)); 4760d0321e0SJeremy L Thompson 4770d0321e0SJeremy L Thompson // Count number of active input fields 4780d0321e0SJeremy L Thompson if (!numactivein) { 4790d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numinputfields; i++) { 4800d0321e0SJeremy L Thompson // Get input vector 481*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec)); 4820d0321e0SJeremy L Thompson // Check if active input 4830d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 484*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qfinputfields[i], &size)); 485*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(impl->qvecsin[i], 0.0)); 486*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(impl->qvecsin[i], CEED_MEM_DEVICE, &tmp)); 487*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRealloc(numactivein + size, &activein)); 4880d0321e0SJeremy L Thompson for (CeedInt field = 0; field < size; field++) { 489d2643443SJeremy L Thompson q_size = (CeedSize)Q * numelements; 490*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &activein[numactivein + field])); 491*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(activein[numactivein + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &tmp[field * Q * numelements])); 4920d0321e0SJeremy L Thompson } 4930d0321e0SJeremy L Thompson numactivein += size; 494*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->qvecsin[i], &tmp)); 4950d0321e0SJeremy L Thompson } 4960d0321e0SJeremy L Thompson } 4970d0321e0SJeremy L Thompson impl->qfnumactivein = numactivein; 4980d0321e0SJeremy L Thompson impl->qfactivein = activein; 4990d0321e0SJeremy L Thompson } 5000d0321e0SJeremy L Thompson 5010d0321e0SJeremy L Thompson // Count number of active output fields 5020d0321e0SJeremy L Thompson if (!numactiveout) { 5030d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numoutputfields; i++) { 5040d0321e0SJeremy L Thompson // Get output vector 505*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[i], &vec)); 5060d0321e0SJeremy L Thompson // Check if active output 5070d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 508*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[i], &size)); 5090d0321e0SJeremy L Thompson numactiveout += size; 5100d0321e0SJeremy L Thompson } 5110d0321e0SJeremy L Thompson } 5120d0321e0SJeremy L Thompson impl->qfnumactiveout = numactiveout; 5130d0321e0SJeremy L Thompson } 5140d0321e0SJeremy L Thompson 5150d0321e0SJeremy L Thompson // Check sizes 516*2b730f8bSJeremy L Thompson if (!numactivein || !numactiveout) { 5170d0321e0SJeremy L Thompson // LCOV_EXCL_START 518*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); 5190d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 520*2b730f8bSJeremy L Thompson } 5210d0321e0SJeremy L Thompson 5220d0321e0SJeremy L Thompson // Build objects if needed 5230d0321e0SJeremy L Thompson if (build_objects) { 5240d0321e0SJeremy L Thompson // Create output restriction 5250d0321e0SJeremy L Thompson CeedInt strides[3] = {1, numelements * Q, Q}; /* *NOPAD* */ 526*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateStrided(ceedparent, numelements, Q, numactivein * numactiveout, 527*2b730f8bSJeremy L Thompson numactivein * numactiveout * numelements * Q, strides, rstr)); 5280d0321e0SJeremy L Thompson // Create assembled vector 529d2643443SJeremy L Thompson CeedSize l_size = (CeedSize)numelements * Q * numactivein * numactiveout; 530*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceedparent, l_size, assembled)); 5310d0321e0SJeremy L Thompson } 532*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(*assembled, 0.0)); 533*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &a)); 5340d0321e0SJeremy L Thompson 5350d0321e0SJeremy L Thompson // Input basis apply 536*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Cuda(numelements, qfinputfields, opinputfields, numinputfields, true, edata, impl)); 5370d0321e0SJeremy L Thompson 5380d0321e0SJeremy L Thompson // Assemble QFunction 5390d0321e0SJeremy L Thompson for (CeedInt in = 0; in < numactivein; in++) { 5400d0321e0SJeremy L Thompson // Set Inputs 541*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(activein[in], 1.0)); 5420d0321e0SJeremy L Thompson if (numactivein > 1) { 543*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(activein[(in + numactivein - 1) % numactivein], 0.0)); 5440d0321e0SJeremy L Thompson } 5450d0321e0SJeremy L Thompson // Set Outputs 5460d0321e0SJeremy L Thompson for (CeedInt out = 0; out < numoutputfields; out++) { 5470d0321e0SJeremy L Thompson // Get output vector 548*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[out], &vec)); 5490d0321e0SJeremy L Thompson // Check if active output 5500d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 551*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->qvecsout[out], CEED_MEM_DEVICE, CEED_USE_POINTER, a)); 552*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[out], &size)); 5530d0321e0SJeremy L Thompson a += size * Q * numelements; // Advance the pointer by the size of the output 5540d0321e0SJeremy L Thompson } 5550d0321e0SJeremy L Thompson } 5560d0321e0SJeremy L Thompson // Apply QFunction 557*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, Q * numelements, impl->qvecsin, impl->qvecsout)); 5580d0321e0SJeremy L Thompson } 5590d0321e0SJeremy L Thompson 5600d0321e0SJeremy L Thompson // Un-set output Qvecs to prevent accidental overwrite of Assembled 5610d0321e0SJeremy L Thompson for (CeedInt out = 0; out < numoutputfields; out++) { 5620d0321e0SJeremy L Thompson // Get output vector 563*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[out], &vec)); 5640d0321e0SJeremy L Thompson // Check if active output 5650d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 566*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorTakeArray(impl->qvecsout[out], CEED_MEM_DEVICE, NULL)); 5670d0321e0SJeremy L Thompson } 5680d0321e0SJeremy L Thompson } 5690d0321e0SJeremy L Thompson 5700d0321e0SJeremy L Thompson // Restore input arrays 571*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(numinputfields, qfinputfields, opinputfields, true, edata, impl)); 5720d0321e0SJeremy L Thompson 5730d0321e0SJeremy L Thompson // Restore output 574*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(*assembled, &a)); 5750d0321e0SJeremy L Thompson 5760d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5770d0321e0SJeremy L Thompson } 5780d0321e0SJeremy L Thompson 5790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5800d0321e0SJeremy L Thompson // Assemble Linear QFunction 5810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 582*2b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 583*2b730f8bSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, true, assembled, rstr, request); 5840d0321e0SJeremy L Thompson } 5850d0321e0SJeremy L Thompson 5860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5870d0321e0SJeremy L Thompson // Update Assembled Linear QFunction 5880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 589*2b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) { 590*2b730f8bSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, false, &assembled, &rstr, request); 5910d0321e0SJeremy L Thompson } 5920d0321e0SJeremy L Thompson 5930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5940d0321e0SJeremy L Thompson // Create point block restriction 5950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 596*2b730f8bSJeremy L Thompson static int CreatePBRestriction(CeedElemRestriction rstr, CeedElemRestriction *pbRstr) { 5970d0321e0SJeremy L Thompson Ceed ceed; 598*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed)); 5990d0321e0SJeremy L Thompson const CeedInt *offsets; 600*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets)); 6010d0321e0SJeremy L Thompson 6020d0321e0SJeremy L Thompson // Expand offsets 6037b63f5c6SJed Brown CeedInt nelem, ncomp, elemsize, compstride, *pbOffsets; 6047b63f5c6SJed Brown CeedSize l_size; 605*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &nelem)); 606*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &ncomp)); 607*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elemsize)); 608*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &compstride)); 609*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size)); 6100d0321e0SJeremy L Thompson CeedInt shift = ncomp; 611*2b730f8bSJeremy L Thompson if (compstride != 1) shift *= ncomp; 612*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(nelem * elemsize, &pbOffsets)); 6130d0321e0SJeremy L Thompson for (CeedInt i = 0; i < nelem * elemsize; i++) { 6140d0321e0SJeremy L Thompson pbOffsets[i] = offsets[i] * shift; 6150d0321e0SJeremy L Thompson } 6160d0321e0SJeremy L Thompson 6170d0321e0SJeremy L Thompson // Create new restriction 618*2b730f8bSJeremy L Thompson CeedCallBackend( 619*2b730f8bSJeremy L Thompson CeedElemRestrictionCreate(ceed, nelem, elemsize, ncomp * ncomp, 1, l_size * ncomp, CEED_MEM_HOST, CEED_OWN_POINTER, pbOffsets, pbRstr)); 6200d0321e0SJeremy L Thompson 6210d0321e0SJeremy L Thompson // Cleanup 622*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets)); 6230d0321e0SJeremy L Thompson 6240d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6250d0321e0SJeremy L Thompson } 6260d0321e0SJeremy L Thompson 6270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6280d0321e0SJeremy L Thompson // Assemble diagonal setup 6290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 630*2b730f8bSJeremy L Thompson static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op, const bool pointBlock) { 6310d0321e0SJeremy L Thompson Ceed ceed; 632*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 6330d0321e0SJeremy L Thompson CeedQFunction qf; 634*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 6350d0321e0SJeremy L Thompson CeedInt numinputfields, numoutputfields; 636*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields)); 6370d0321e0SJeremy L Thompson 6380d0321e0SJeremy L Thompson // Determine active input basis 6390d0321e0SJeremy L Thompson CeedOperatorField *opfields; 6400d0321e0SJeremy L Thompson CeedQFunctionField *qffields; 641*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &opfields, NULL, NULL)); 642*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qffields, NULL, NULL)); 6430d0321e0SJeremy L Thompson CeedInt numemodein = 0, ncomp = 0, dim = 1; 6440d0321e0SJeremy L Thompson CeedEvalMode *emodein = NULL; 6450d0321e0SJeremy L Thompson CeedBasis basisin = NULL; 6460d0321e0SJeremy L Thompson CeedElemRestriction rstrin = NULL; 6470d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numinputfields; i++) { 6480d0321e0SJeremy L Thompson CeedVector vec; 649*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &vec)); 6500d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 6510d0321e0SJeremy L Thompson CeedElemRestriction rstr; 652*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basisin)); 653*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basisin, &ncomp)); 654*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basisin, &dim)); 655*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &rstr)); 656*2b730f8bSJeremy L Thompson if (rstrin && rstrin != rstr) { 6570d0321e0SJeremy L Thompson // LCOV_EXCL_START 658*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator diagonal assembly"); 6590d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 660*2b730f8bSJeremy L Thompson } 6610d0321e0SJeremy L Thompson rstrin = rstr; 6620d0321e0SJeremy L Thompson CeedEvalMode emode; 663*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode)); 6640d0321e0SJeremy L Thompson switch (emode) { 6650d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 6660d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 667*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRealloc(numemodein + 1, &emodein)); 6680d0321e0SJeremy L Thompson emodein[numemodein] = emode; 6690d0321e0SJeremy L Thompson numemodein += 1; 6700d0321e0SJeremy L Thompson break; 6710d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 672*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRealloc(numemodein + dim, &emodein)); 673*2b730f8bSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) emodein[numemodein + d] = emode; 6740d0321e0SJeremy L Thompson numemodein += dim; 6750d0321e0SJeremy L Thompson break; 6760d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: 6770d0321e0SJeremy L Thompson case CEED_EVAL_DIV: 6780d0321e0SJeremy L Thompson case CEED_EVAL_CURL: 6790d0321e0SJeremy L Thompson break; // Caught by QF Assembly 6800d0321e0SJeremy L Thompson } 6810d0321e0SJeremy L Thompson } 6820d0321e0SJeremy L Thompson } 6830d0321e0SJeremy L Thompson 6840d0321e0SJeremy L Thompson // Determine active output basis 685*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &opfields)); 686*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qffields)); 6870d0321e0SJeremy L Thompson CeedInt numemodeout = 0; 6880d0321e0SJeremy L Thompson CeedEvalMode *emodeout = NULL; 6890d0321e0SJeremy L Thompson CeedBasis basisout = NULL; 6900d0321e0SJeremy L Thompson CeedElemRestriction rstrout = NULL; 6910d0321e0SJeremy L Thompson for (CeedInt i = 0; i < numoutputfields; i++) { 6920d0321e0SJeremy L Thompson CeedVector vec; 693*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &vec)); 6940d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 6950d0321e0SJeremy L Thompson CeedElemRestriction rstr; 696*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basisout)); 697*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &rstr)); 698*2b730f8bSJeremy L Thompson if (rstrout && rstrout != rstr) { 6990d0321e0SJeremy L Thompson // LCOV_EXCL_START 700*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator diagonal assembly"); 7010d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 702*2b730f8bSJeremy L Thompson } 7030d0321e0SJeremy L Thompson rstrout = rstr; 7040d0321e0SJeremy L Thompson CeedEvalMode emode; 705*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode)); 7060d0321e0SJeremy L Thompson switch (emode) { 7070d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 7080d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 709*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRealloc(numemodeout + 1, &emodeout)); 7100d0321e0SJeremy L Thompson emodeout[numemodeout] = emode; 7110d0321e0SJeremy L Thompson numemodeout += 1; 7120d0321e0SJeremy L Thompson break; 7130d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 714*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRealloc(numemodeout + dim, &emodeout)); 715*2b730f8bSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) emodeout[numemodeout + d] = emode; 7160d0321e0SJeremy L Thompson numemodeout += dim; 7170d0321e0SJeremy L Thompson break; 7180d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: 7190d0321e0SJeremy L Thompson case CEED_EVAL_DIV: 7200d0321e0SJeremy L Thompson case CEED_EVAL_CURL: 7210d0321e0SJeremy L Thompson break; // Caught by QF Assembly 7220d0321e0SJeremy L Thompson } 7230d0321e0SJeremy L Thompson } 7240d0321e0SJeremy L Thompson } 7250d0321e0SJeremy L Thompson 7260d0321e0SJeremy L Thompson // Operator data struct 7270d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 728*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 729*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl->diag)); 7300d0321e0SJeremy L Thompson CeedOperatorDiag_Cuda *diag = impl->diag; 7310d0321e0SJeremy L Thompson diag->basisin = basisin; 7320d0321e0SJeremy L Thompson diag->basisout = basisout; 7330d0321e0SJeremy L Thompson diag->h_emodein = emodein; 7340d0321e0SJeremy L Thompson diag->h_emodeout = emodeout; 7350d0321e0SJeremy L Thompson diag->numemodein = numemodein; 7360d0321e0SJeremy L Thompson diag->numemodeout = numemodeout; 7370d0321e0SJeremy L Thompson 7380d0321e0SJeremy L Thompson // Assemble kernel 73907b31e0eSJeremy L Thompson char *diagonal_kernel_path, *diagonal_kernel_source; 740*2b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble-diagonal.h", &diagonal_kernel_path)); 74107b31e0eSJeremy L Thompson CeedDebug256(ceed, 2, "----- Loading Diagonal Assembly Kernel Source -----\n"); 742*2b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source)); 743*2b730f8bSJeremy L Thompson CeedDebug256(ceed, 2, "----- Loading Diagonal Assembly Source Complete! -----\n"); 7440d0321e0SJeremy L Thompson CeedInt nnodes, nqpts; 745*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basisin, &nnodes)); 746*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basisin, &nqpts)); 7470d0321e0SJeremy L Thompson diag->nnodes = nnodes; 748*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, CeedCompileCuda(ceed, diagonal_kernel_source, &diag->module, 5, "NUMEMODEIN", numemodein, "NUMEMODEOUT", numemodeout, "NNODES", 749*2b730f8bSJeremy L Thompson nnodes, "NQPTS", nqpts, "NCOMP", ncomp)); 750*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, CeedGetKernelCuda(ceed, diag->module, "linearDiagonal", &diag->linearDiagonal)); 751*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, CeedGetKernelCuda(ceed, diag->module, "linearPointBlockDiagonal", &diag->linearPointBlock)); 752*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&diagonal_kernel_path)); 753*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&diagonal_kernel_source)); 7540d0321e0SJeremy L Thompson 7550d0321e0SJeremy L Thompson // Basis matrices 7560d0321e0SJeremy L Thompson const CeedInt qBytes = nqpts * sizeof(CeedScalar); 7570d0321e0SJeremy L Thompson const CeedInt iBytes = qBytes * nnodes; 7580d0321e0SJeremy L Thompson const CeedInt gBytes = qBytes * nnodes * dim; 7590d0321e0SJeremy L Thompson const CeedInt eBytes = sizeof(CeedEvalMode); 7600d0321e0SJeremy L Thompson const CeedScalar *interpin, *interpout, *gradin, *gradout; 7610d0321e0SJeremy L Thompson 7620d0321e0SJeremy L Thompson // CEED_EVAL_NONE 7630d0321e0SJeremy L Thompson CeedScalar *identity = NULL; 7640d0321e0SJeremy L Thompson bool evalNone = false; 765*2b730f8bSJeremy L Thompson for (CeedInt i = 0; i < numemodein; i++) evalNone = evalNone || (emodein[i] == CEED_EVAL_NONE); 766*2b730f8bSJeremy L Thompson for (CeedInt i = 0; i < numemodeout; i++) evalNone = evalNone || (emodeout[i] == CEED_EVAL_NONE); 7670d0321e0SJeremy L Thompson if (evalNone) { 768*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(nqpts * nnodes, &identity)); 769*2b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (nnodes < nqpts ? nnodes : nqpts); i++) identity[i * nnodes + i] = 1.0; 770*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_identity, iBytes)); 771*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(diag->d_identity, identity, iBytes, cudaMemcpyHostToDevice)); 7720d0321e0SJeremy L Thompson } 7730d0321e0SJeremy L Thompson 7740d0321e0SJeremy L Thompson // CEED_EVAL_INTERP 775*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetInterp(basisin, &interpin)); 776*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_interpin, iBytes)); 777*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(diag->d_interpin, interpin, iBytes, cudaMemcpyHostToDevice)); 778*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetInterp(basisout, &interpout)); 779*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_interpout, iBytes)); 780*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(diag->d_interpout, interpout, iBytes, cudaMemcpyHostToDevice)); 7810d0321e0SJeremy L Thompson 7820d0321e0SJeremy L Thompson // CEED_EVAL_GRAD 783*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetGrad(basisin, &gradin)); 784*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_gradin, gBytes)); 785*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(diag->d_gradin, gradin, gBytes, cudaMemcpyHostToDevice)); 786*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetGrad(basisout, &gradout)); 787*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_gradout, gBytes)); 788*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(diag->d_gradout, gradout, gBytes, cudaMemcpyHostToDevice)); 7890d0321e0SJeremy L Thompson 7900d0321e0SJeremy L Thompson // Arrays of emodes 791*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_emodein, numemodein * eBytes)); 792*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(diag->d_emodein, emodein, numemodein * eBytes, cudaMemcpyHostToDevice)); 793*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_emodeout, numemodeout * eBytes)); 794*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(diag->d_emodeout, emodeout, numemodeout * eBytes, cudaMemcpyHostToDevice)); 7950d0321e0SJeremy L Thompson 7960d0321e0SJeremy L Thompson // Restriction 7970d0321e0SJeremy L Thompson diag->diagrstr = rstrout; 7980d0321e0SJeremy L Thompson 7990d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8000d0321e0SJeremy L Thompson } 8010d0321e0SJeremy L Thompson 8020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 8030d0321e0SJeremy L Thompson // Assemble diagonal common code 8040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 805*2b730f8bSJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool pointBlock) { 8060d0321e0SJeremy L Thompson Ceed ceed; 807*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 8080d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 809*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 8100d0321e0SJeremy L Thompson 8110d0321e0SJeremy L Thompson // Assemble QFunction 8120d0321e0SJeremy L Thompson CeedVector assembledqf; 8130d0321e0SJeremy L Thompson CeedElemRestriction rstr; 814*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembledqf, &rstr, request)); 815*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr)); 8160d0321e0SJeremy L Thompson 8170d0321e0SJeremy L Thompson // Setup 8180d0321e0SJeremy L Thompson if (!impl->diag) { 819*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Cuda(op, pointBlock)); 8200d0321e0SJeremy L Thompson } 8210d0321e0SJeremy L Thompson CeedOperatorDiag_Cuda *diag = impl->diag; 8220d0321e0SJeremy L Thompson assert(diag != NULL); 8230d0321e0SJeremy L Thompson 8240d0321e0SJeremy L Thompson // Restriction 8250d0321e0SJeremy L Thompson if (pointBlock && !diag->pbdiagrstr) { 8260d0321e0SJeremy L Thompson CeedElemRestriction pbdiagrstr; 827*2b730f8bSJeremy L Thompson CeedCallBackend(CreatePBRestriction(diag->diagrstr, &pbdiagrstr)); 8280d0321e0SJeremy L Thompson diag->pbdiagrstr = pbdiagrstr; 8290d0321e0SJeremy L Thompson } 8300d0321e0SJeremy L Thompson CeedElemRestriction diagrstr = pointBlock ? diag->pbdiagrstr : diag->diagrstr; 8310d0321e0SJeremy L Thompson 8320d0321e0SJeremy L Thompson // Create diagonal vector 8330d0321e0SJeremy L Thompson CeedVector elemdiag = pointBlock ? diag->pbelemdiag : diag->elemdiag; 8340d0321e0SJeremy L Thompson if (!elemdiag) { 835*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateVector(diagrstr, NULL, &elemdiag)); 836*2b730f8bSJeremy L Thompson if (pointBlock) diag->pbelemdiag = elemdiag; 837*2b730f8bSJeremy L Thompson else diag->elemdiag = elemdiag; 8380d0321e0SJeremy L Thompson } 839*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(elemdiag, 0.0)); 8400d0321e0SJeremy L Thompson 8410d0321e0SJeremy L Thompson // Assemble element operator diagonals 8420d0321e0SJeremy L Thompson CeedScalar *elemdiagarray; 8430d0321e0SJeremy L Thompson const CeedScalar *assembledqfarray; 844*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(elemdiag, CEED_MEM_DEVICE, &elemdiagarray)); 845*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(assembledqf, CEED_MEM_DEVICE, &assembledqfarray)); 8460d0321e0SJeremy L Thompson CeedInt nelem; 847*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(diagrstr, &nelem)); 8480d0321e0SJeremy L Thompson 8490d0321e0SJeremy L Thompson // Compute the diagonal of B^T D B 8500d0321e0SJeremy L Thompson int elemsPerBlock = 1; 8510d0321e0SJeremy L Thompson int grid = nelem / elemsPerBlock + ((nelem / elemsPerBlock * elemsPerBlock < nelem) ? 1 : 0); 852*2b730f8bSJeremy L Thompson void *args[] = {(void *)&nelem, &diag->d_identity, &diag->d_interpin, &diag->d_gradin, &diag->d_interpout, 853*2b730f8bSJeremy L Thompson &diag->d_gradout, &diag->d_emodein, &diag->d_emodeout, &assembledqfarray, &elemdiagarray}; 8540d0321e0SJeremy L Thompson if (pointBlock) { 855*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRunKernelDimCuda(ceed, diag->linearPointBlock, grid, diag->nnodes, 1, elemsPerBlock, args)); 8560d0321e0SJeremy L Thompson } else { 857*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRunKernelDimCuda(ceed, diag->linearDiagonal, grid, diag->nnodes, 1, elemsPerBlock, args)); 8580d0321e0SJeremy L Thompson } 8590d0321e0SJeremy L Thompson 8600d0321e0SJeremy L Thompson // Restore arrays 861*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(elemdiag, &elemdiagarray)); 862*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(assembledqf, &assembledqfarray)); 8630d0321e0SJeremy L Thompson 8640d0321e0SJeremy L Thompson // Assemble local operator diagonal 865*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(diagrstr, CEED_TRANSPOSE, elemdiag, assembled, request)); 8660d0321e0SJeremy L Thompson 8670d0321e0SJeremy L Thompson // Cleanup 868*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembledqf)); 8690d0321e0SJeremy L Thompson 8700d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8710d0321e0SJeremy L Thompson } 8720d0321e0SJeremy L Thompson 8730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 8740d0321e0SJeremy L Thompson // Assemble Linear Diagonal 8750d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 876*2b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) { 877*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, false)); 8786aa95790SJeremy L Thompson return CEED_ERROR_SUCCESS; 8790d0321e0SJeremy L Thompson } 8800d0321e0SJeremy L Thompson 8810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 8820d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal 8830d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 884*2b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) { 885*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, true)); 8866aa95790SJeremy L Thompson return CEED_ERROR_SUCCESS; 8870d0321e0SJeremy L Thompson } 8880d0321e0SJeremy L Thompson 8890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 890cc132f9aSnbeams // Single operator assembly setup 891cc132f9aSnbeams //------------------------------------------------------------------------------ 892cc132f9aSnbeams static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op) { 893cc132f9aSnbeams Ceed ceed; 894*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 895cc132f9aSnbeams CeedOperator_Cuda *impl; 896*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 897cc132f9aSnbeams 898cc132f9aSnbeams // Get intput and output fields 899cc132f9aSnbeams CeedInt num_input_fields, num_output_fields; 900cc132f9aSnbeams CeedOperatorField *input_fields; 901cc132f9aSnbeams CeedOperatorField *output_fields; 902*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 903cc132f9aSnbeams 904cc132f9aSnbeams // Determine active input basis eval mode 905cc132f9aSnbeams CeedQFunction qf; 906*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 907cc132f9aSnbeams CeedQFunctionField *qf_fields; 908*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 909cc132f9aSnbeams // Note that the kernel will treat each dimension of a gradient action separately; 910cc132f9aSnbeams // i.e., when an active input has a CEED_EVAL_GRAD mode, num_emode_in will increment 911cc132f9aSnbeams // by dim. However, for the purposes of loading the B matrices, it will be treated 912cc132f9aSnbeams // as one mode, and we will load/copy the entire gradient matrix at once, so 913cc132f9aSnbeams // num_B_in_mats_to_load will be incremented by 1. 914cc132f9aSnbeams CeedInt num_emode_in = 0, dim = 1, num_B_in_mats_to_load = 0, size_B_in = 0; 915cc132f9aSnbeams CeedEvalMode *eval_mode_in = NULL; // will be of size num_B_in_mats_load 916cc132f9aSnbeams CeedBasis basis_in = NULL; 917b216396cSJeremy L Thompson CeedInt nqpts = 0, esize = 0; 918cc132f9aSnbeams CeedElemRestriction rstr_in = NULL; 919cc132f9aSnbeams for (CeedInt i = 0; i < num_input_fields; i++) { 920cc132f9aSnbeams CeedVector vec; 921*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec)); 922cc132f9aSnbeams if (vec == CEED_VECTOR_ACTIVE) { 923*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis_in)); 924*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis_in, &dim)); 925*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &nqpts)); 926*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in)); 927*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &esize)); 928cc132f9aSnbeams CeedEvalMode eval_mode; 929*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 930cc132f9aSnbeams if (eval_mode != CEED_EVAL_NONE) { 931*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRealloc(num_B_in_mats_to_load + 1, &eval_mode_in)); 932cc132f9aSnbeams eval_mode_in[num_B_in_mats_to_load] = eval_mode; 933cc132f9aSnbeams num_B_in_mats_to_load += 1; 934cc132f9aSnbeams if (eval_mode == CEED_EVAL_GRAD) { 935cc132f9aSnbeams num_emode_in += dim; 936cc132f9aSnbeams size_B_in += dim * esize * nqpts; 937cc132f9aSnbeams } else { 938cc132f9aSnbeams num_emode_in += 1; 939cc132f9aSnbeams size_B_in += esize * nqpts; 940cc132f9aSnbeams } 941cc132f9aSnbeams } 942cc132f9aSnbeams } 943cc132f9aSnbeams } 944cc132f9aSnbeams 945cc132f9aSnbeams // Determine active output basis; basis_out and rstr_out only used if same as input, TODO 946*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 947cc132f9aSnbeams CeedInt num_emode_out = 0, num_B_out_mats_to_load = 0, size_B_out = 0; 948cc132f9aSnbeams CeedEvalMode *eval_mode_out = NULL; 949cc132f9aSnbeams CeedBasis basis_out = NULL; 950cc132f9aSnbeams CeedElemRestriction rstr_out = NULL; 951cc132f9aSnbeams for (CeedInt i = 0; i < num_output_fields; i++) { 952cc132f9aSnbeams CeedVector vec; 953*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec)); 954cc132f9aSnbeams if (vec == CEED_VECTOR_ACTIVE) { 955*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis_out)); 956*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out)); 957*2b730f8bSJeremy L Thompson if (rstr_out && rstr_out != rstr_in) { 958cc132f9aSnbeams // LCOV_EXCL_START 959*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator assembly"); 960cc132f9aSnbeams // LCOV_EXCL_STOP 961*2b730f8bSJeremy L Thompson } 962cc132f9aSnbeams CeedEvalMode eval_mode; 963*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 964cc132f9aSnbeams if (eval_mode != CEED_EVAL_NONE) { 965*2b730f8bSJeremy L Thompson CeedCallBackend(CeedRealloc(num_B_out_mats_to_load + 1, &eval_mode_out)); 966cc132f9aSnbeams eval_mode_out[num_B_out_mats_to_load] = eval_mode; 967cc132f9aSnbeams num_B_out_mats_to_load += 1; 968cc132f9aSnbeams if (eval_mode == CEED_EVAL_GRAD) { 969cc132f9aSnbeams num_emode_out += dim; 970cc132f9aSnbeams size_B_out += dim * esize * nqpts; 971cc132f9aSnbeams } else { 972cc132f9aSnbeams num_emode_out += 1; 973cc132f9aSnbeams size_B_out += esize * nqpts; 974cc132f9aSnbeams } 975cc132f9aSnbeams } 976cc132f9aSnbeams } 977cc132f9aSnbeams } 978cc132f9aSnbeams 979*2b730f8bSJeremy L Thompson if (num_emode_in == 0 || num_emode_out == 0) { 980cc132f9aSnbeams // LCOV_EXCL_START 981*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs"); 982cc132f9aSnbeams // LCOV_EXCL_STOP 983*2b730f8bSJeremy L Thompson } 984cc132f9aSnbeams 985cc132f9aSnbeams CeedInt nelem, ncomp; 986*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &nelem)); 987*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &ncomp)); 988cc132f9aSnbeams 989*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl->asmb)); 990cc132f9aSnbeams CeedOperatorAssemble_Cuda *asmb = impl->asmb; 991cc132f9aSnbeams asmb->nelem = nelem; 992cc132f9aSnbeams 993cc132f9aSnbeams // Compile kernels 994cc132f9aSnbeams int elemsPerBlock = 1; 995cc132f9aSnbeams asmb->elemsPerBlock = elemsPerBlock; 99659ad764aSnbeams CeedInt block_size = esize * esize * elemsPerBlock; 997cc132f9aSnbeams Ceed_Cuda *cuda_data; 998*2b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &cuda_data)); 99907b31e0eSJeremy L Thompson char *assembly_kernel_path, *assembly_kernel_source; 1000*2b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble.h", &assembly_kernel_path)); 100107b31e0eSJeremy L Thompson CeedDebug256(ceed, 2, "----- Loading Assembly Kernel Source -----\n"); 1002*2b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source)); 100307b31e0eSJeremy L Thompson CeedDebug256(ceed, 2, "----- Loading Assembly Source Complete! -----\n"); 100407b31e0eSJeremy L Thompson bool fallback = block_size > cuda_data->device_prop.maxThreadsPerBlock; 100507b31e0eSJeremy L Thompson if (fallback) { 100659ad764aSnbeams // Use fallback kernel with 1D threadblock 100759ad764aSnbeams block_size = esize * elemsPerBlock; 100859ad764aSnbeams asmb->block_size_x = esize; 100959ad764aSnbeams asmb->block_size_y = 1; 101059ad764aSnbeams } else { // Use kernel with 2D threadblock 101159ad764aSnbeams asmb->block_size_x = esize; 101259ad764aSnbeams asmb->block_size_y = esize; 101307b31e0eSJeremy L Thompson } 1014*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, CeedCompileCuda(ceed, assembly_kernel_source, &asmb->module, 7, "NELEM", nelem, "NUMEMODEIN", num_emode_in, "NUMEMODEOUT", 1015*2b730f8bSJeremy L Thompson num_emode_out, "NQPTS", nqpts, "NNODES", esize, "BLOCK_SIZE", block_size, "NCOMP", ncomp)); 1016*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, CeedGetKernelCuda(ceed, asmb->module, fallback ? "linearAssembleFallback" : "linearAssemble", &asmb->linearAssemble)); 1017*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&assembly_kernel_path)); 1018*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&assembly_kernel_source)); 1019cc132f9aSnbeams 1020cc132f9aSnbeams // Build 'full' B matrices (not 1D arrays used for tensor-product matrices) 1021cc132f9aSnbeams const CeedScalar *interp_in, *grad_in; 1022*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in)); 1023*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in)); 1024cc132f9aSnbeams 1025cc132f9aSnbeams // Load into B_in, in order that they will be used in eval_mode 1026cc132f9aSnbeams const CeedInt inBytes = size_B_in * sizeof(CeedScalar); 1027cc132f9aSnbeams CeedInt mat_start = 0; 1028*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_in, inBytes)); 1029cc132f9aSnbeams for (int i = 0; i < num_B_in_mats_to_load; i++) { 1030cc132f9aSnbeams CeedEvalMode eval_mode = eval_mode_in[i]; 1031cc132f9aSnbeams if (eval_mode == CEED_EVAL_INTERP) { 1032*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_in[mat_start], interp_in, esize * nqpts * sizeof(CeedScalar), cudaMemcpyHostToDevice)); 1033cc132f9aSnbeams mat_start += esize * nqpts; 1034cc132f9aSnbeams } else if (eval_mode == CEED_EVAL_GRAD) { 1035*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_in[mat_start], grad_in, dim * esize * nqpts * sizeof(CeedScalar), cudaMemcpyHostToDevice)); 1036cc132f9aSnbeams mat_start += dim * esize * nqpts; 1037cc132f9aSnbeams } 1038cc132f9aSnbeams } 1039cc132f9aSnbeams 1040cc132f9aSnbeams const CeedScalar *interp_out, *grad_out; 1041cc132f9aSnbeams // Note that this function currently assumes 1 basis, so this should always be true 1042cc132f9aSnbeams // for now 1043cc132f9aSnbeams if (basis_out == basis_in) { 1044cc132f9aSnbeams interp_out = interp_in; 1045cc132f9aSnbeams grad_out = grad_in; 1046cc132f9aSnbeams } else { 1047*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out)); 1048*2b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out)); 1049cc132f9aSnbeams } 1050cc132f9aSnbeams 1051cc132f9aSnbeams // Load into B_out, in order that they will be used in eval_mode 1052cc132f9aSnbeams const CeedInt outBytes = size_B_out * sizeof(CeedScalar); 1053cc132f9aSnbeams mat_start = 0; 1054*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_out, outBytes)); 1055cc132f9aSnbeams for (int i = 0; i < num_B_out_mats_to_load; i++) { 1056cc132f9aSnbeams CeedEvalMode eval_mode = eval_mode_out[i]; 1057cc132f9aSnbeams if (eval_mode == CEED_EVAL_INTERP) { 1058*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_out[mat_start], interp_out, esize * nqpts * sizeof(CeedScalar), cudaMemcpyHostToDevice)); 1059cc132f9aSnbeams mat_start += esize * nqpts; 1060cc132f9aSnbeams } else if (eval_mode == CEED_EVAL_GRAD) { 1061*2b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_out[mat_start], grad_out, dim * esize * nqpts * sizeof(CeedScalar), cudaMemcpyHostToDevice)); 1062cc132f9aSnbeams mat_start += dim * esize * nqpts; 1063cc132f9aSnbeams } 1064cc132f9aSnbeams } 1065cc132f9aSnbeams return CEED_ERROR_SUCCESS; 1066cc132f9aSnbeams } 1067cc132f9aSnbeams 1068cc132f9aSnbeams //------------------------------------------------------------------------------ 1069cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator. 1070cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic. 1071cefa2673SJeremy L Thompson // 1072cefa2673SJeremy L Thompson // Note that this (and other assembly routines) currently assume only one 1073cefa2673SJeremy L Thompson // active input restriction/basis per operator (could have multiple basis eval 1074cefa2673SJeremy L Thompson // modes). 1075cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects 1076cc132f9aSnbeams //------------------------------------------------------------------------------ 1077*2b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Cuda(CeedOperator op, CeedInt offset, CeedVector values) { 1078cc132f9aSnbeams Ceed ceed; 1079*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1080cc132f9aSnbeams CeedOperator_Cuda *impl; 1081*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1082cc132f9aSnbeams 1083cc132f9aSnbeams // Setup 1084cc132f9aSnbeams if (!impl->asmb) { 1085*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSingleOperatorAssembleSetup_Cuda(op)); 1086b216396cSJeremy L Thompson assert(impl->asmb != NULL); 1087cc132f9aSnbeams } 1088cc132f9aSnbeams 1089cc132f9aSnbeams // Assemble QFunction 1090cc132f9aSnbeams CeedVector assembled_qf; 1091cc132f9aSnbeams CeedElemRestriction rstr_q; 1092*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE)); 1093*2b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_q)); 1094cc132f9aSnbeams CeedScalar *values_array; 1095*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(values, CEED_MEM_DEVICE, &values_array)); 1096cc132f9aSnbeams values_array += offset; 1097cc132f9aSnbeams const CeedScalar *qf_array; 1098*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &qf_array)); 1099cc132f9aSnbeams 1100cc132f9aSnbeams // Compute B^T D B 1101cc132f9aSnbeams const CeedInt nelem = impl->asmb->nelem; 1102cc132f9aSnbeams const CeedInt elemsPerBlock = impl->asmb->elemsPerBlock; 1103*2b730f8bSJeremy L Thompson const CeedInt grid = nelem / elemsPerBlock + ((nelem / elemsPerBlock * elemsPerBlock < nelem) ? 1 : 0); 1104*2b730f8bSJeremy L Thompson void *args[] = {&impl->asmb->d_B_in, &impl->asmb->d_B_out, &qf_array, &values_array}; 1105*2b730f8bSJeremy L Thompson CeedCallBackend( 1106*2b730f8bSJeremy L Thompson CeedRunKernelDimCuda(ceed, impl->asmb->linearAssemble, grid, impl->asmb->block_size_x, impl->asmb->block_size_y, elemsPerBlock, args)); 1107cc132f9aSnbeams 1108cc132f9aSnbeams // Restore arrays 1109*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(values, &values_array)); 1110*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &qf_array)); 1111cc132f9aSnbeams 1112cc132f9aSnbeams // Cleanup 1113*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 1114cc132f9aSnbeams 1115cc132f9aSnbeams return CEED_ERROR_SUCCESS; 1116cc132f9aSnbeams } 1117cc132f9aSnbeams 1118cc132f9aSnbeams //------------------------------------------------------------------------------ 11190d0321e0SJeremy L Thompson // Create operator 11200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 11210d0321e0SJeremy L Thompson int CeedOperatorCreate_Cuda(CeedOperator op) { 11220d0321e0SJeremy L Thompson Ceed ceed; 1123*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 11240d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 11250d0321e0SJeremy L Thompson 1126*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 1127*2b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl)); 11280d0321e0SJeremy L Thompson 1129*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda)); 1130*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Cuda)); 1131*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Cuda)); 1132*2b730f8bSJeremy L Thompson CeedCallBackend( 1133*2b730f8bSJeremy L Thompson CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda)); 1134*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Cuda)); 1135*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda)); 1136*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda)); 11370d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11380d0321e0SJeremy L Thompson } 11390d0321e0SJeremy L Thompson 11400d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1141