xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-operator.c (revision ea61e9ac44808524e4667c1525a05976f536c19c)
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 
82b730f8bSJeremy L Thompson #include <assert.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
102b730f8bSJeremy L Thompson #include <ceed/ceed.h>
1107b31e0eSJeremy L Thompson #include <ceed/jit-tools.h>
120d0321e0SJeremy L Thompson #include <hip/hip_runtime.h>
130d0321e0SJeremy L Thompson #include <stdbool.h>
140d0321e0SJeremy L Thompson #include <string.h>
152b730f8bSJeremy L Thompson 
160d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
172b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
180d0321e0SJeremy L Thompson 
190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
200d0321e0SJeremy L Thompson // Destroy operator
210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
220d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Hip(CeedOperator op) {
230d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
242b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
250d0321e0SJeremy L Thompson 
260d0321e0SJeremy L Thompson   // Apply data
270d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->numein + impl->numeout; i++) {
282b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->evecs[i]));
290d0321e0SJeremy L Thompson   }
302b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->evecs));
310d0321e0SJeremy L Thompson 
320d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->numein; i++) {
332b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qvecsin[i]));
340d0321e0SJeremy L Thompson   }
352b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qvecsin));
360d0321e0SJeremy L Thompson 
370d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->numeout; i++) {
382b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qvecsout[i]));
390d0321e0SJeremy L Thompson   }
402b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qvecsout));
410d0321e0SJeremy L Thompson 
420d0321e0SJeremy L Thompson   // QFunction diagonal assembly data
430d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->qfnumactivein; i++) {
442b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qfactivein[i]));
450d0321e0SJeremy L Thompson   }
462b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qfactivein));
470d0321e0SJeremy L Thompson 
480d0321e0SJeremy L Thompson   // Diag data
490d0321e0SJeremy L Thompson   if (impl->diag) {
500d0321e0SJeremy L Thompson     Ceed ceed;
512b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
522b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
532b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&impl->diag->h_emodein));
542b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&impl->diag->h_emodeout));
552b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_emodein));
562b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_emodeout));
572b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
582b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interpin));
592b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interpout));
602b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_gradin));
612b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_gradout));
622b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->pbdiagrstr));
632b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elemdiag));
642b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->pbelemdiag));
650d0321e0SJeremy L Thompson   }
662b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
670d0321e0SJeremy L Thompson 
68a835093fSnbeams   if (impl->asmb) {
69a835093fSnbeams     Ceed ceed;
702b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
712b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
722b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
732b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
74a835093fSnbeams   }
752b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
76a835093fSnbeams 
772b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
780d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
790d0321e0SJeremy L Thompson }
800d0321e0SJeremy L Thompson 
810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
820d0321e0SJeremy L Thompson // Setup infields or outfields
830d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
842b730f8bSJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool isinput, CeedVector *evecs, CeedVector *qvecs, CeedInt starte,
852b730f8bSJeremy L Thompson                                        CeedInt numfields, CeedInt Q, CeedInt numelements) {
862b730f8bSJeremy L Thompson   CeedInt  dim, size;
87d2643443SJeremy L Thompson   CeedSize q_size;
880d0321e0SJeremy L Thompson   Ceed     ceed;
892b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
900d0321e0SJeremy L Thompson   CeedBasis           basis;
910d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
920d0321e0SJeremy L Thompson   CeedOperatorField  *opfields;
930d0321e0SJeremy L Thompson   CeedQFunctionField *qffields;
940d0321e0SJeremy L Thompson   CeedVector          fieldvec;
950d0321e0SJeremy L Thompson   bool                strided;
960d0321e0SJeremy L Thompson   bool                skiprestrict;
970d0321e0SJeremy L Thompson 
980d0321e0SJeremy L Thompson   if (isinput) {
992b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &opfields, NULL, NULL));
1002b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qffields, NULL, NULL));
1010d0321e0SJeremy L Thompson   } else {
1022b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &opfields));
1032b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qffields));
1040d0321e0SJeremy L Thompson   }
1050d0321e0SJeremy L Thompson 
1060d0321e0SJeremy L Thompson   // Loop over fields
1070d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numfields; i++) {
1080d0321e0SJeremy L Thompson     CeedEvalMode emode;
1092b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode));
1100d0321e0SJeremy L Thompson 
1110d0321e0SJeremy L Thompson     strided      = false;
1120d0321e0SJeremy L Thompson     skiprestrict = false;
1130d0321e0SJeremy L Thompson     if (emode != CEED_EVAL_WEIGHT) {
1142b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &Erestrict));
1150d0321e0SJeremy L Thompson 
1160d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
117*ea61e9acSJeremy L Thompson       // must be passive input, with emode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
1180d0321e0SJeremy L Thompson 
1190d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
1200d0321e0SJeremy L Thompson       if (isinput) {
1210d0321e0SJeremy L Thompson         // Check for passive input:
1222b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &fieldvec));
1230d0321e0SJeremy L Thompson         if (fieldvec != CEED_VECTOR_ACTIVE) {
1240d0321e0SJeremy L Thompson           // Check emode
1250d0321e0SJeremy L Thompson           if (emode == CEED_EVAL_NONE) {
1260d0321e0SJeremy L Thompson             // Check for strided restriction
1272b730f8bSJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(Erestrict, &strided));
1280d0321e0SJeremy L Thompson             if (strided) {
1290d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
1302b730f8bSJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(Erestrict, &skiprestrict));
1310d0321e0SJeremy L Thompson             }
1320d0321e0SJeremy L Thompson           }
1330d0321e0SJeremy L Thompson         }
1340d0321e0SJeremy L Thompson       }
1350d0321e0SJeremy L Thompson       if (skiprestrict) {
136*ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
1370d0321e0SJeremy L Thompson         evecs[i + starte] = NULL;
1380d0321e0SJeremy L Thompson       } else {
1392b730f8bSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(Erestrict, NULL, &evecs[i + starte]));
1400d0321e0SJeremy L Thompson       }
1410d0321e0SJeremy L Thompson     }
1420d0321e0SJeremy L Thompson 
1430d0321e0SJeremy L Thompson     switch (emode) {
1440d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
1452b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size));
146d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q * size;
1472b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1480d0321e0SJeremy L Thompson         break;
1490d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1502b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size));
151d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q * size;
1522b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1530d0321e0SJeremy L Thompson         break;
1540d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
1552b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basis));
1562b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size));
1572b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetDimension(basis, &dim));
158d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q * size;
1592b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1600d0321e0SJeremy L Thompson         break;
1610d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
1622b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basis));
163d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q;
1642b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1652b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, NULL, qvecs[i]));
1660d0321e0SJeremy L Thompson         break;
1670d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
1680d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
1690d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
1700d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
1710d0321e0SJeremy L Thompson     }
1720d0321e0SJeremy L Thompson   }
1730d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1740d0321e0SJeremy L Thompson }
1750d0321e0SJeremy L Thompson 
1760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
177*ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
1780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1790d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
1800d0321e0SJeremy L Thompson   bool setupdone;
1812b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &setupdone));
1822b730f8bSJeremy L Thompson   if (setupdone) return CEED_ERROR_SUCCESS;
1830d0321e0SJeremy L Thompson   Ceed ceed;
1842b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1850d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
1862b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1870d0321e0SJeremy L Thompson   CeedQFunction qf;
1882b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1890d0321e0SJeremy L Thompson   CeedInt Q, numelements, numinputfields, numoutputfields;
1902b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
1912b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &numelements));
1920d0321e0SJeremy L Thompson   CeedOperatorField *opinputfields, *opoutputfields;
1932b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields));
1940d0321e0SJeremy L Thompson   CeedQFunctionField *qfinputfields, *qfoutputfields;
1952b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields));
1960d0321e0SJeremy L Thompson 
1970d0321e0SJeremy L Thompson   // Allocate
1982b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(numinputfields + numoutputfields, &impl->evecs));
1990d0321e0SJeremy L Thompson 
2002b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->qvecsin));
2012b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->qvecsout));
2020d0321e0SJeremy L Thompson 
2032b730f8bSJeremy L Thompson   impl->numein  = numinputfields;
2042b730f8bSJeremy L Thompson   impl->numeout = numoutputfields;
2050d0321e0SJeremy L Thompson 
2060d0321e0SJeremy L Thompson   // Set up infield and outfield evecs and qvecs
2070d0321e0SJeremy L Thompson   // Infields
2082b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, impl->evecs, impl->qvecsin, 0, numinputfields, Q, numelements));
2090d0321e0SJeremy L Thompson 
2100d0321e0SJeremy L Thompson   // Outfields
2112b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, impl->evecs, impl->qvecsout, numinputfields, numoutputfields, Q, numelements));
2120d0321e0SJeremy L Thompson 
2132b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2140d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2150d0321e0SJeremy L Thompson }
2160d0321e0SJeremy L Thompson 
2170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2180d0321e0SJeremy L Thompson // Setup Operator Inputs
2190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2202b730f8bSJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt numinputfields, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields,
2212b730f8bSJeremy L Thompson                                               CeedVector invec, const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl,
2222b730f8bSJeremy L Thompson                                               CeedRequest *request) {
2230d0321e0SJeremy L Thompson   CeedEvalMode        emode;
2240d0321e0SJeremy L Thompson   CeedVector          vec;
2250d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
2260d0321e0SJeremy L Thompson 
2270d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
2280d0321e0SJeremy L Thompson     // Get input vector
2292b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
2300d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2312b730f8bSJeremy L Thompson       if (skipactive) continue;
2322b730f8bSJeremy L Thompson       else vec = invec;
2330d0321e0SJeremy L Thompson     }
2340d0321e0SJeremy L Thompson 
2352b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode));
2360d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_WEIGHT) {  // Skip
2370d0321e0SJeremy L Thompson     } else {
2380d0321e0SJeremy L Thompson       // Get input vector
2392b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
2400d0321e0SJeremy L Thompson       // Get input element restriction
2412b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict));
2422b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
2430d0321e0SJeremy L Thompson       // Restrict, if necessary
2440d0321e0SJeremy L Thompson       if (!impl->evecs[i]) {
2450d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
2462b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&edata[i]));
2470d0321e0SJeremy L Thompson       } else {
2482b730f8bSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(Erestrict, CEED_NOTRANSPOSE, vec, impl->evecs[i], request));
2490d0321e0SJeremy L Thompson         // Get evec
2502b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->evecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&edata[i]));
2510d0321e0SJeremy L Thompson       }
2520d0321e0SJeremy L Thompson     }
2530d0321e0SJeremy L Thompson   }
2540d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2550d0321e0SJeremy L Thompson }
2560d0321e0SJeremy L Thompson 
2570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2580d0321e0SJeremy L Thompson // Input Basis Action
2590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2602b730f8bSJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt numelements, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields,
2612b730f8bSJeremy L Thompson                                              CeedInt numinputfields, const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX],
2622b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
2630d0321e0SJeremy L Thompson   CeedInt             elemsize, size;
2640d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
2650d0321e0SJeremy L Thompson   CeedEvalMode        emode;
2660d0321e0SJeremy L Thompson   CeedBasis           basis;
2670d0321e0SJeremy L Thompson 
2680d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
2690d0321e0SJeremy L Thompson     // Skip active input
2700d0321e0SJeremy L Thompson     if (skipactive) {
2710d0321e0SJeremy L Thompson       CeedVector vec;
2722b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
2732b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2740d0321e0SJeremy L Thompson     }
2750d0321e0SJeremy L Thompson     // Get elemsize, emode, size
2762b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict));
2772b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elemsize));
2782b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode));
2792b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qfinputfields[i], &size));
2800d0321e0SJeremy L Thompson     // Basis action
2810d0321e0SJeremy L Thompson     switch (emode) {
2820d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
2832b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->qvecsin[i], CEED_MEM_DEVICE, CEED_USE_POINTER, edata[i]));
2840d0321e0SJeremy L Thompson         break;
2850d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
2862b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opinputfields[i], &basis));
2872b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, impl->evecs[i], impl->qvecsin[i]));
2880d0321e0SJeremy L Thompson         break;
2890d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
2902b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opinputfields[i], &basis));
2912b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, impl->evecs[i], impl->qvecsin[i]));
2920d0321e0SJeremy L Thompson         break;
2930d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
2940d0321e0SJeremy L Thompson         break;  // No action
2950d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
2960d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
2970d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
2980d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
2990d0321e0SJeremy L Thompson     }
3000d0321e0SJeremy L Thompson   }
3010d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3020d0321e0SJeremy L Thompson }
3030d0321e0SJeremy L Thompson 
3040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3050d0321e0SJeremy L Thompson // Restore Input Vectors
3060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3072b730f8bSJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt numinputfields, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields,
3082b730f8bSJeremy L Thompson                                                 const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
3090d0321e0SJeremy L Thompson   CeedEvalMode emode;
3100d0321e0SJeremy L Thompson   CeedVector   vec;
3110d0321e0SJeremy L Thompson 
3120d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
3130d0321e0SJeremy L Thompson     // Skip active input
3140d0321e0SJeremy L Thompson     if (skipactive) {
3152b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
3162b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3170d0321e0SJeremy L Thompson     }
3182b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode));
3190d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_WEIGHT) {  // Skip
3200d0321e0SJeremy L Thompson     } else {
3210d0321e0SJeremy L Thompson       if (!impl->evecs[i]) {  // This was a skiprestrict case
3222b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
3232b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&edata[i]));
3240d0321e0SJeremy L Thompson       } else {
3252b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->evecs[i], (const CeedScalar **)&edata[i]));
3260d0321e0SJeremy L Thompson       }
3270d0321e0SJeremy L Thompson     }
3280d0321e0SJeremy L Thompson   }
3290d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3300d0321e0SJeremy L Thompson }
3310d0321e0SJeremy L Thompson 
3320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3330d0321e0SJeremy L Thompson // Apply and add to output
3340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3352b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector invec, CeedVector outvec, CeedRequest *request) {
3360d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
3372b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3380d0321e0SJeremy L Thompson   CeedQFunction qf;
3392b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3400d0321e0SJeremy L Thompson   CeedInt Q, numelements, elemsize, numinputfields, numoutputfields, size;
3412b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
3422b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &numelements));
3430d0321e0SJeremy L Thompson   CeedOperatorField *opinputfields, *opoutputfields;
3442b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields));
3450d0321e0SJeremy L Thompson   CeedQFunctionField *qfinputfields, *qfoutputfields;
3462b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields));
3470d0321e0SJeremy L Thompson   CeedEvalMode        emode;
3480d0321e0SJeremy L Thompson   CeedVector          vec;
3490d0321e0SJeremy L Thompson   CeedBasis           basis;
3500d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
3510d0321e0SJeremy L Thompson   CeedScalar         *edata[2 * CEED_FIELD_MAX];
3520d0321e0SJeremy L Thompson 
3530d0321e0SJeremy L Thompson   // Setup
3542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
3550d0321e0SJeremy L Thompson 
3560d0321e0SJeremy L Thompson   // Input Evecs and Restriction
3572b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(numinputfields, qfinputfields, opinputfields, invec, false, edata, impl, request));
3580d0321e0SJeremy L Thompson 
3590d0321e0SJeremy L Thompson   // Input basis apply if needed
3602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(numelements, qfinputfields, opinputfields, numinputfields, false, edata, impl));
3610d0321e0SJeremy L Thompson 
3620d0321e0SJeremy L Thompson   // Output pointers, as necessary
3630d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
3642b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode));
3650d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_NONE) {
3660d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
3672b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->evecs[i + impl->numein], CEED_MEM_DEVICE, &edata[i + numinputfields]));
3682b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->qvecsout[i], CEED_MEM_DEVICE, CEED_USE_POINTER, edata[i + numinputfields]));
3690d0321e0SJeremy L Thompson     }
3700d0321e0SJeremy L Thompson   }
3710d0321e0SJeremy L Thompson 
3720d0321e0SJeremy L Thompson   // Q function
3732b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, numelements * Q, impl->qvecsin, impl->qvecsout));
3740d0321e0SJeremy L Thompson 
3750d0321e0SJeremy L Thompson   // Output basis apply if needed
3760d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
3770d0321e0SJeremy L Thompson     // Get elemsize, emode, size
3782b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(opoutputfields[i], &Erestrict));
3792b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elemsize));
3802b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode));
3812b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[i], &size));
3820d0321e0SJeremy L Thompson     // Basis action
3830d0321e0SJeremy L Thompson     switch (emode) {
3840d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
3850d0321e0SJeremy L Thompson         break;
3860d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3872b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opoutputfields[i], &basis));
3882b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_TRANSPOSE, CEED_EVAL_INTERP, impl->qvecsout[i], impl->evecs[i + impl->numein]));
3890d0321e0SJeremy L Thompson         break;
3900d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
3912b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opoutputfields[i], &basis));
3922b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_TRANSPOSE, CEED_EVAL_GRAD, impl->qvecsout[i], impl->evecs[i + impl->numein]));
3930d0321e0SJeremy L Thompson         break;
3940d0321e0SJeremy L Thompson       // LCOV_EXCL_START
3950d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
3960d0321e0SJeremy L Thompson         Ceed ceed;
3972b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
3982b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
3990d0321e0SJeremy L Thompson         break;  // Should not occur
4000d0321e0SJeremy L Thompson       }
4010d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
4020d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
4030d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
4040d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
4050d0321e0SJeremy L Thompson                 // LCOV_EXCL_STOP
4060d0321e0SJeremy L Thompson     }
4070d0321e0SJeremy L Thompson   }
4080d0321e0SJeremy L Thompson 
4090d0321e0SJeremy L Thompson   // Output restriction
4100d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
4110d0321e0SJeremy L Thompson     // Restore evec
4122b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode));
4130d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_NONE) {
4142b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->evecs[i + impl->numein], &edata[i + numinputfields]));
4150d0321e0SJeremy L Thompson     }
4160d0321e0SJeremy L Thompson     // Get output vector
4172b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[i], &vec));
4180d0321e0SJeremy L Thompson     // Restrict
4192b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(opoutputfields[i], &Erestrict));
4200d0321e0SJeremy L Thompson     // Active
4212b730f8bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
4220d0321e0SJeremy L Thompson 
4232b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(Erestrict, CEED_TRANSPOSE, impl->evecs[i + impl->numein], vec, request));
4240d0321e0SJeremy L Thompson   }
4250d0321e0SJeremy L Thompson 
4260d0321e0SJeremy L Thompson   // Restore input arrays
4272b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(numinputfields, qfinputfields, opinputfields, false, edata, impl));
4280d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4290d0321e0SJeremy L Thompson }
4300d0321e0SJeremy L Thompson 
4310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4320d0321e0SJeremy L Thompson // Core code for assembling linear QFunction
4330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4342b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
4350d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
4360d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
4372b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4380d0321e0SJeremy L Thompson   CeedQFunction qf;
4392b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4400d0321e0SJeremy L Thompson   CeedInt  Q, numelements, numinputfields, numoutputfields, size;
441d2643443SJeremy L Thompson   CeedSize q_size;
4422b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
4432b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &numelements));
4440d0321e0SJeremy L Thompson   CeedOperatorField *opinputfields, *opoutputfields;
4452b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields));
4460d0321e0SJeremy L Thompson   CeedQFunctionField *qfinputfields, *qfoutputfields;
4472b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields));
4480d0321e0SJeremy L Thompson   CeedVector  vec;
4490d0321e0SJeremy L Thompson   CeedInt     numactivein = impl->qfnumactivein, numactiveout = impl->qfnumactiveout;
4500d0321e0SJeremy L Thompson   CeedVector *activein = impl->qfactivein;
4510d0321e0SJeremy L Thompson   CeedScalar *a, *tmp;
4520d0321e0SJeremy L Thompson   Ceed        ceed, ceedparent;
4532b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
4542b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetOperatorFallbackParentCeed(ceed, &ceedparent));
4550d0321e0SJeremy L Thompson   ceedparent = ceedparent ? ceedparent : ceed;
4560d0321e0SJeremy L Thompson   CeedScalar *edata[2 * CEED_FIELD_MAX];
4570d0321e0SJeremy L Thompson 
4580d0321e0SJeremy L Thompson   // Setup
4592b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4600d0321e0SJeremy L Thompson 
4610d0321e0SJeremy L Thompson   // Check for identity
4620d0321e0SJeremy L Thompson   bool identityqf;
4632b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionIsIdentity(qf, &identityqf));
4642b730f8bSJeremy L Thompson   if (identityqf) {
4650d0321e0SJeremy L Thompson     // LCOV_EXCL_START
4662b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Assembling identity QFunctions not supported");
4670d0321e0SJeremy L Thompson     // LCOV_EXCL_STOP
4682b730f8bSJeremy L Thompson   }
4690d0321e0SJeremy L Thompson 
4700d0321e0SJeremy L Thompson   // Input Evecs and Restriction
4712b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(numinputfields, qfinputfields, opinputfields, NULL, true, edata, impl, request));
4720d0321e0SJeremy L Thompson 
4730d0321e0SJeremy L Thompson   // Count number of active input fields
4740d0321e0SJeremy L Thompson   if (!numactivein) {
4750d0321e0SJeremy L Thompson     for (CeedInt i = 0; i < numinputfields; i++) {
4760d0321e0SJeremy L Thompson       // Get input vector
4772b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
4780d0321e0SJeremy L Thompson       // Check if active input
4790d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
4802b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qfinputfields[i], &size));
4812b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->qvecsin[i], 0.0));
4822b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->qvecsin[i], CEED_MEM_DEVICE, &tmp));
4832b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(numactivein + size, &activein));
4840d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
485d2643443SJeremy L Thompson           q_size = (CeedSize)Q * numelements;
4862b730f8bSJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &activein[numactivein + field]));
4872b730f8bSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(activein[numactivein + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &tmp[field * Q * numelements]));
4880d0321e0SJeremy L Thompson         }
4890d0321e0SJeremy L Thompson         numactivein += size;
4902b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->qvecsin[i], &tmp));
4910d0321e0SJeremy L Thompson       }
4920d0321e0SJeremy L Thompson     }
4930d0321e0SJeremy L Thompson     impl->qfnumactivein = numactivein;
4940d0321e0SJeremy L Thompson     impl->qfactivein    = activein;
4950d0321e0SJeremy L Thompson   }
4960d0321e0SJeremy L Thompson 
4970d0321e0SJeremy L Thompson   // Count number of active output fields
4980d0321e0SJeremy L Thompson   if (!numactiveout) {
4990d0321e0SJeremy L Thompson     for (CeedInt i = 0; i < numoutputfields; i++) {
5000d0321e0SJeremy L Thompson       // Get output vector
5012b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[i], &vec));
5020d0321e0SJeremy L Thompson       // Check if active output
5030d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
5042b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[i], &size));
5050d0321e0SJeremy L Thompson         numactiveout += size;
5060d0321e0SJeremy L Thompson       }
5070d0321e0SJeremy L Thompson     }
5080d0321e0SJeremy L Thompson     impl->qfnumactiveout = numactiveout;
5090d0321e0SJeremy L Thompson   }
5100d0321e0SJeremy L Thompson 
5110d0321e0SJeremy L Thompson   // Check sizes
5122b730f8bSJeremy L Thompson   if (!numactivein || !numactiveout) {
5130d0321e0SJeremy L Thompson     // LCOV_EXCL_START
5142b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5150d0321e0SJeremy L Thompson     // LCOV_EXCL_STOP
5162b730f8bSJeremy L Thompson   }
5170d0321e0SJeremy L Thompson 
5180d0321e0SJeremy L Thompson   // Build objects if needed
5190d0321e0SJeremy L Thompson   if (build_objects) {
5200d0321e0SJeremy L Thompson     // Create output restriction
5210d0321e0SJeremy L Thompson     CeedInt strides[3] = {1, numelements * Q, Q}; /* *NOPAD* */
5222b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceedparent, numelements, Q, numactivein * numactiveout,
5232b730f8bSJeremy L Thompson                                                      numactivein * numactiveout * numelements * Q, strides, rstr));
5240d0321e0SJeremy L Thompson     // Create assembled vector
525d2643443SJeremy L Thompson     CeedSize l_size = (CeedSize)numelements * Q * numactivein * numactiveout;
5262b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceedparent, l_size, assembled));
5270d0321e0SJeremy L Thompson   }
5282b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
5292b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &a));
5300d0321e0SJeremy L Thompson 
5310d0321e0SJeremy L Thompson   // Input basis apply
5322b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(numelements, qfinputfields, opinputfields, numinputfields, true, edata, impl));
5330d0321e0SJeremy L Thompson 
5340d0321e0SJeremy L Thompson   // Assemble QFunction
5350d0321e0SJeremy L Thompson   for (CeedInt in = 0; in < numactivein; in++) {
5360d0321e0SJeremy L Thompson     // Set Inputs
5372b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(activein[in], 1.0));
5380d0321e0SJeremy L Thompson     if (numactivein > 1) {
5392b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(activein[(in + numactivein - 1) % numactivein], 0.0));
5400d0321e0SJeremy L Thompson     }
5410d0321e0SJeremy L Thompson     // Set Outputs
5420d0321e0SJeremy L Thompson     for (CeedInt out = 0; out < numoutputfields; out++) {
5430d0321e0SJeremy L Thompson       // Get output vector
5442b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[out], &vec));
5450d0321e0SJeremy L Thompson       // Check if active output
5460d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
5472b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->qvecsout[out], CEED_MEM_DEVICE, CEED_USE_POINTER, a));
5482b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[out], &size));
5490d0321e0SJeremy L Thompson         a += size * Q * numelements;  // Advance the pointer by the size of the output
5500d0321e0SJeremy L Thompson       }
5510d0321e0SJeremy L Thompson     }
5520d0321e0SJeremy L Thompson     // Apply QFunction
5532b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * numelements, impl->qvecsin, impl->qvecsout));
5540d0321e0SJeremy L Thompson   }
5550d0321e0SJeremy L Thompson 
5560d0321e0SJeremy L Thompson   // Un-set output Qvecs to prevent accidental overwrite of Assembled
5570d0321e0SJeremy L Thompson   for (CeedInt out = 0; out < numoutputfields; out++) {
5580d0321e0SJeremy L Thompson     // Get output vector
5592b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[out], &vec));
5600d0321e0SJeremy L Thompson     // Check if active output
5610d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
5622b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->qvecsout[out], CEED_MEM_DEVICE, NULL));
5630d0321e0SJeremy L Thompson     }
5640d0321e0SJeremy L Thompson   }
5650d0321e0SJeremy L Thompson 
5660d0321e0SJeremy L Thompson   // Restore input arrays
5672b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(numinputfields, qfinputfields, opinputfields, true, edata, impl));
5680d0321e0SJeremy L Thompson 
5690d0321e0SJeremy L Thompson   // Restore output
5702b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &a));
5710d0321e0SJeremy L Thompson 
5720d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5730d0321e0SJeremy L Thompson }
5740d0321e0SJeremy L Thompson 
5750d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5760d0321e0SJeremy L Thompson // Assemble Linear QFunction
5770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5782b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
5792b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
5800d0321e0SJeremy L Thompson }
5810d0321e0SJeremy L Thompson 
5820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5830d0321e0SJeremy L Thompson // Assemble Linear QFunction
5840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5852b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
5862b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
5870d0321e0SJeremy L Thompson }
5880d0321e0SJeremy L Thompson 
5890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5900d0321e0SJeremy L Thompson // Create point block restriction
5910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5922b730f8bSJeremy L Thompson static int CreatePBRestriction(CeedElemRestriction rstr, CeedElemRestriction *pbRstr) {
5930d0321e0SJeremy L Thompson   Ceed ceed;
5942b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
5950d0321e0SJeremy L Thompson   const CeedInt *offsets;
5962b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
5970d0321e0SJeremy L Thompson 
5980d0321e0SJeremy L Thompson   // Expand offsets
5997b63f5c6SJed Brown   CeedInt  nelem, ncomp, elemsize, compstride, *pbOffsets;
6007b63f5c6SJed Brown   CeedSize l_size;
6012b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &nelem));
6022b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &ncomp));
6032b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elemsize));
6042b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &compstride));
6052b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
6060d0321e0SJeremy L Thompson   CeedInt shift = ncomp;
6072b730f8bSJeremy L Thompson   if (compstride != 1) shift *= ncomp;
6082b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(nelem * elemsize, &pbOffsets));
6090d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < nelem * elemsize; i++) {
6100d0321e0SJeremy L Thompson     pbOffsets[i] = offsets[i] * shift;
6110d0321e0SJeremy L Thompson   }
6120d0321e0SJeremy L Thompson 
6130d0321e0SJeremy L Thompson   // Create new restriction
6142b730f8bSJeremy L Thompson   CeedCallBackend(
6152b730f8bSJeremy L Thompson       CeedElemRestrictionCreate(ceed, nelem, elemsize, ncomp * ncomp, 1, l_size * ncomp, CEED_MEM_HOST, CEED_OWN_POINTER, pbOffsets, pbRstr));
6160d0321e0SJeremy L Thompson 
6170d0321e0SJeremy L Thompson   // Cleanup
6182b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
6190d0321e0SJeremy L Thompson 
6200d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6210d0321e0SJeremy L Thompson }
6220d0321e0SJeremy L Thompson 
6230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6240d0321e0SJeremy L Thompson // Assemble diagonal setup
6250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6262b730f8bSJeremy L Thompson static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op, const bool pointBlock) {
6270d0321e0SJeremy L Thompson   Ceed ceed;
6282b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
6290d0321e0SJeremy L Thompson   CeedQFunction qf;
6302b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
6310d0321e0SJeremy L Thompson   CeedInt numinputfields, numoutputfields;
6322b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields));
6330d0321e0SJeremy L Thompson 
6340d0321e0SJeremy L Thompson   // Determine active input basis
6350d0321e0SJeremy L Thompson   CeedOperatorField  *opfields;
6360d0321e0SJeremy L Thompson   CeedQFunctionField *qffields;
6372b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &opfields, NULL, NULL));
6382b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qffields, NULL, NULL));
6390d0321e0SJeremy L Thompson   CeedInt             numemodein = 0, ncomp = 0, dim = 1;
6400d0321e0SJeremy L Thompson   CeedEvalMode       *emodein = NULL;
6410d0321e0SJeremy L Thompson   CeedBasis           basisin = NULL;
6420d0321e0SJeremy L Thompson   CeedElemRestriction rstrin  = NULL;
6430d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
6440d0321e0SJeremy L Thompson     CeedVector vec;
6452b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &vec));
6460d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
6470d0321e0SJeremy L Thompson       CeedElemRestriction rstr;
6482b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basisin));
6492b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumComponents(basisin, &ncomp));
6502b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basisin, &dim));
6512b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &rstr));
6522b730f8bSJeremy L Thompson       if (rstrin && rstrin != rstr) {
6530d0321e0SJeremy L Thompson         // LCOV_EXCL_START
6542b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator diagonal assembly");
6550d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
6562b730f8bSJeremy L Thompson       }
6570d0321e0SJeremy L Thompson       rstrin = rstr;
6580d0321e0SJeremy L Thompson       CeedEvalMode emode;
6592b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode));
6600d0321e0SJeremy L Thompson       switch (emode) {
6610d0321e0SJeremy L Thompson         case CEED_EVAL_NONE:
6620d0321e0SJeremy L Thompson         case CEED_EVAL_INTERP:
6632b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodein + 1, &emodein));
6640d0321e0SJeremy L Thompson           emodein[numemodein] = emode;
6650d0321e0SJeremy L Thompson           numemodein += 1;
6660d0321e0SJeremy L Thompson           break;
6670d0321e0SJeremy L Thompson         case CEED_EVAL_GRAD:
6682b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodein + dim, &emodein));
6692b730f8bSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) emodein[numemodein + d] = emode;
6700d0321e0SJeremy L Thompson           numemodein += dim;
6710d0321e0SJeremy L Thompson           break;
6720d0321e0SJeremy L Thompson         case CEED_EVAL_WEIGHT:
6730d0321e0SJeremy L Thompson         case CEED_EVAL_DIV:
6740d0321e0SJeremy L Thompson         case CEED_EVAL_CURL:
6750d0321e0SJeremy L Thompson           break;  // Caught by QF Assembly
6760d0321e0SJeremy L Thompson       }
6770d0321e0SJeremy L Thompson     }
6780d0321e0SJeremy L Thompson   }
6790d0321e0SJeremy L Thompson 
6800d0321e0SJeremy L Thompson   // Determine active output basis
6812b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &opfields));
6822b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qffields));
6830d0321e0SJeremy L Thompson   CeedInt             numemodeout = 0;
6840d0321e0SJeremy L Thompson   CeedEvalMode       *emodeout    = NULL;
6850d0321e0SJeremy L Thompson   CeedBasis           basisout    = NULL;
6860d0321e0SJeremy L Thompson   CeedElemRestriction rstrout     = NULL;
6870d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
6880d0321e0SJeremy L Thompson     CeedVector vec;
6892b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &vec));
6900d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
6910d0321e0SJeremy L Thompson       CeedElemRestriction rstr;
6922b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basisout));
6932b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &rstr));
6942b730f8bSJeremy L Thompson       if (rstrout && rstrout != rstr) {
6950d0321e0SJeremy L Thompson         // LCOV_EXCL_START
6962b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator diagonal assembly");
6970d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
6982b730f8bSJeremy L Thompson       }
6990d0321e0SJeremy L Thompson       rstrout = rstr;
7000d0321e0SJeremy L Thompson       CeedEvalMode emode;
7012b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode));
7020d0321e0SJeremy L Thompson       switch (emode) {
7030d0321e0SJeremy L Thompson         case CEED_EVAL_NONE:
7040d0321e0SJeremy L Thompson         case CEED_EVAL_INTERP:
7052b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodeout + 1, &emodeout));
7060d0321e0SJeremy L Thompson           emodeout[numemodeout] = emode;
7070d0321e0SJeremy L Thompson           numemodeout += 1;
7080d0321e0SJeremy L Thompson           break;
7090d0321e0SJeremy L Thompson         case CEED_EVAL_GRAD:
7102b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodeout + dim, &emodeout));
7112b730f8bSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) emodeout[numemodeout + d] = emode;
7120d0321e0SJeremy L Thompson           numemodeout += dim;
7130d0321e0SJeremy L Thompson           break;
7140d0321e0SJeremy L Thompson         case CEED_EVAL_WEIGHT:
7150d0321e0SJeremy L Thompson         case CEED_EVAL_DIV:
7160d0321e0SJeremy L Thompson         case CEED_EVAL_CURL:
7170d0321e0SJeremy L Thompson           break;  // Caught by QF Assembly
7180d0321e0SJeremy L Thompson       }
7190d0321e0SJeremy L Thompson     }
7200d0321e0SJeremy L Thompson   }
7210d0321e0SJeremy L Thompson 
7220d0321e0SJeremy L Thompson   // Operator data struct
7230d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
7242b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
7252b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
7260d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
7270d0321e0SJeremy L Thompson   diag->basisin              = basisin;
7280d0321e0SJeremy L Thompson   diag->basisout             = basisout;
7290d0321e0SJeremy L Thompson   diag->h_emodein            = emodein;
7300d0321e0SJeremy L Thompson   diag->h_emodeout           = emodeout;
7310d0321e0SJeremy L Thompson   diag->numemodein           = numemodein;
7320d0321e0SJeremy L Thompson   diag->numemodeout          = numemodeout;
7330d0321e0SJeremy L Thompson 
7340d0321e0SJeremy L Thompson   // Assemble kernel
73507b31e0eSJeremy L Thompson 
73607b31e0eSJeremy L Thompson   char *diagonal_kernel_path, *diagonal_kernel_source;
7372b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
73807b31e0eSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Diagonal Assembly Kernel Source -----\n");
7392b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
7402b730f8bSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Diagonal Assembly Source Complete! -----\n");
7410d0321e0SJeremy L Thompson   CeedInt nnodes, nqpts;
7422b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basisin, &nnodes));
7432b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basisin, &nqpts));
7440d0321e0SJeremy L Thompson   diag->nnodes = nnodes;
7452b730f8bSJeremy L Thompson   CeedCallBackend(CeedCompileHip(ceed, diagonal_kernel_source, &diag->module, 5, "NUMEMODEIN", numemodein, "NUMEMODEOUT", numemodeout, "NNODES",
7462b730f8bSJeremy L Thompson                                  nnodes, "NQPTS", nqpts, "NCOMP", ncomp));
7472b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetKernelHip(ceed, diag->module, "linearDiagonal", &diag->linearDiagonal));
7482b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetKernelHip(ceed, diag->module, "linearPointBlockDiagonal", &diag->linearPointBlock));
7492b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&diagonal_kernel_path));
7502b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&diagonal_kernel_source));
7510d0321e0SJeremy L Thompson 
7520d0321e0SJeremy L Thompson   // Basis matrices
7530d0321e0SJeremy L Thompson   const CeedInt     qBytes = nqpts * sizeof(CeedScalar);
7540d0321e0SJeremy L Thompson   const CeedInt     iBytes = qBytes * nnodes;
7550d0321e0SJeremy L Thompson   const CeedInt     gBytes = qBytes * nnodes * dim;
7560d0321e0SJeremy L Thompson   const CeedInt     eBytes = sizeof(CeedEvalMode);
7570d0321e0SJeremy L Thompson   const CeedScalar *interpin, *interpout, *gradin, *gradout;
7580d0321e0SJeremy L Thompson 
7590d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
7600d0321e0SJeremy L Thompson   CeedScalar *identity = NULL;
7610d0321e0SJeremy L Thompson   bool        evalNone = false;
7622b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < numemodein; i++) evalNone = evalNone || (emodein[i] == CEED_EVAL_NONE);
7632b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < numemodeout; i++) evalNone = evalNone || (emodeout[i] == CEED_EVAL_NONE);
7640d0321e0SJeremy L Thompson   if (evalNone) {
7652b730f8bSJeremy L Thompson     CeedCallBackend(CeedCalloc(nqpts * nnodes, &identity));
7662b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < (nnodes < nqpts ? nnodes : nqpts); i++) identity[i * nnodes + i] = 1.0;
7672b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, iBytes));
7682b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, iBytes, hipMemcpyHostToDevice));
7690d0321e0SJeremy L Thompson   }
7700d0321e0SJeremy L Thompson 
7710d0321e0SJeremy L Thompson   // CEED_EVAL_INTERP
7722b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basisin, &interpin));
7732b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_interpin, iBytes));
7742b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_interpin, interpin, iBytes, hipMemcpyHostToDevice));
7752b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basisout, &interpout));
7762b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_interpout, iBytes));
7772b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_interpout, interpout, iBytes, hipMemcpyHostToDevice));
7780d0321e0SJeremy L Thompson 
7790d0321e0SJeremy L Thompson   // CEED_EVAL_GRAD
7802b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basisin, &gradin));
7812b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_gradin, gBytes));
7822b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_gradin, gradin, gBytes, hipMemcpyHostToDevice));
7832b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basisout, &gradout));
7842b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_gradout, gBytes));
7852b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_gradout, gradout, gBytes, hipMemcpyHostToDevice));
7860d0321e0SJeremy L Thompson 
7870d0321e0SJeremy L Thompson   // Arrays of emodes
7882b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_emodein, numemodein * eBytes));
7892b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_emodein, emodein, numemodein * eBytes, hipMemcpyHostToDevice));
7902b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_emodeout, numemodeout * eBytes));
7912b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_emodeout, emodeout, numemodeout * eBytes, hipMemcpyHostToDevice));
7920d0321e0SJeremy L Thompson 
7930d0321e0SJeremy L Thompson   // Restriction
7940d0321e0SJeremy L Thompson   diag->diagrstr = rstrout;
7950d0321e0SJeremy L Thompson 
7960d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7970d0321e0SJeremy L Thompson }
7980d0321e0SJeremy L Thompson 
7990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8000d0321e0SJeremy L Thompson // Assemble diagonal common code
8010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8022b730f8bSJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool pointBlock) {
8030d0321e0SJeremy L Thompson   Ceed ceed;
8042b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8050d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
8062b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
8070d0321e0SJeremy L Thompson 
8080d0321e0SJeremy L Thompson   // Assemble QFunction
8090d0321e0SJeremy L Thompson   CeedVector          assembledqf;
8100d0321e0SJeremy L Thompson   CeedElemRestriction rstr;
8112b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembledqf, &rstr, request));
8122b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&rstr));
8130d0321e0SJeremy L Thompson 
8140d0321e0SJeremy L Thompson   // Setup
8152b730f8bSJeremy L Thompson   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op, pointBlock));
8160d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
8170d0321e0SJeremy L Thompson   assert(diag != NULL);
8180d0321e0SJeremy L Thompson 
8190d0321e0SJeremy L Thompson   // Restriction
8200d0321e0SJeremy L Thompson   if (pointBlock && !diag->pbdiagrstr) {
8210d0321e0SJeremy L Thompson     CeedElemRestriction pbdiagrstr;
8222b730f8bSJeremy L Thompson     CeedCallBackend(CreatePBRestriction(diag->diagrstr, &pbdiagrstr));
8230d0321e0SJeremy L Thompson     diag->pbdiagrstr = pbdiagrstr;
8240d0321e0SJeremy L Thompson   }
8250d0321e0SJeremy L Thompson   CeedElemRestriction diagrstr = pointBlock ? diag->pbdiagrstr : diag->diagrstr;
8260d0321e0SJeremy L Thompson 
8270d0321e0SJeremy L Thompson   // Create diagonal vector
8280d0321e0SJeremy L Thompson   CeedVector elemdiag = pointBlock ? diag->pbelemdiag : diag->elemdiag;
8290d0321e0SJeremy L Thompson   if (!elemdiag) {
8300d0321e0SJeremy L Thompson     // Element diagonal vector
8312b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(diagrstr, NULL, &elemdiag));
8322b730f8bSJeremy L Thompson     if (pointBlock) diag->pbelemdiag = elemdiag;
8332b730f8bSJeremy L Thompson     else diag->elemdiag = elemdiag;
8340d0321e0SJeremy L Thompson   }
8352b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elemdiag, 0.0));
8360d0321e0SJeremy L Thompson 
8370d0321e0SJeremy L Thompson   // Assemble element operator diagonals
8380d0321e0SJeremy L Thompson   CeedScalar       *elemdiagarray;
8390d0321e0SJeremy L Thompson   const CeedScalar *assembledqfarray;
8402b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(elemdiag, CEED_MEM_DEVICE, &elemdiagarray));
8412b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(assembledqf, CEED_MEM_DEVICE, &assembledqfarray));
8420d0321e0SJeremy L Thompson   CeedInt nelem;
8432b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(diagrstr, &nelem));
8440d0321e0SJeremy L Thompson 
8450d0321e0SJeremy L Thompson   // Compute the diagonal of B^T D B
8460d0321e0SJeremy L Thompson   int   elemsPerBlock = 1;
8470d0321e0SJeremy L Thompson   int   grid          = nelem / elemsPerBlock + ((nelem / elemsPerBlock * elemsPerBlock < nelem) ? 1 : 0);
8482b730f8bSJeremy L Thompson   void *args[]        = {(void *)&nelem,   &diag->d_identity, &diag->d_interpin, &diag->d_gradin,   &diag->d_interpout,
8492b730f8bSJeremy L Thompson                          &diag->d_gradout, &diag->d_emodein,  &diag->d_emodeout, &assembledqfarray, &elemdiagarray};
8500d0321e0SJeremy L Thompson   if (pointBlock) {
8512b730f8bSJeremy L Thompson     CeedCallBackend(CeedRunKernelDimHip(ceed, diag->linearPointBlock, grid, diag->nnodes, 1, elemsPerBlock, args));
8520d0321e0SJeremy L Thompson   } else {
8532b730f8bSJeremy L Thompson     CeedCallBackend(CeedRunKernelDimHip(ceed, diag->linearDiagonal, grid, diag->nnodes, 1, elemsPerBlock, args));
8540d0321e0SJeremy L Thompson   }
8550d0321e0SJeremy L Thompson 
8560d0321e0SJeremy L Thompson   // Restore arrays
8572b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(elemdiag, &elemdiagarray));
8582b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(assembledqf, &assembledqfarray));
8590d0321e0SJeremy L Thompson 
8600d0321e0SJeremy L Thompson   // Assemble local operator diagonal
8612b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diagrstr, CEED_TRANSPOSE, elemdiag, assembled, request));
8620d0321e0SJeremy L Thompson 
8630d0321e0SJeremy L Thompson   // Cleanup
8642b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembledqf));
8650d0321e0SJeremy L Thompson 
8660d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8670d0321e0SJeremy L Thompson }
8680d0321e0SJeremy L Thompson 
8690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8700d0321e0SJeremy L Thompson // Assemble Linear Diagonal
8710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8722b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
8732b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
8746aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8750d0321e0SJeremy L Thompson }
8760d0321e0SJeremy L Thompson 
8770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8780d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
8790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8802b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
8812b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
8826aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8830d0321e0SJeremy L Thompson }
8840d0321e0SJeremy L Thompson 
885a835093fSnbeams //------------------------------------------------------------------------------
886a835093fSnbeams // Single operator assembly setup
887a835093fSnbeams //------------------------------------------------------------------------------
888a835093fSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op) {
889a835093fSnbeams   Ceed ceed;
8902b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
891a835093fSnbeams   CeedOperator_Hip *impl;
8922b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
893a835093fSnbeams 
894a835093fSnbeams   // Get intput and output fields
895a835093fSnbeams   CeedInt            num_input_fields, num_output_fields;
896a835093fSnbeams   CeedOperatorField *input_fields;
897a835093fSnbeams   CeedOperatorField *output_fields;
8982b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
899a835093fSnbeams 
900a835093fSnbeams   // Determine active input basis eval mode
901a835093fSnbeams   CeedQFunction qf;
9022b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
903a835093fSnbeams   CeedQFunctionField *qf_fields;
9042b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
905a835093fSnbeams   // Note that the kernel will treat each dimension of a gradient action separately;
906*ea61e9acSJeremy L Thompson   // i.e., when an active input has a CEED_EVAL_GRAD mode, num_emode_in will increment by dim.
907*ea61e9acSJeremy 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
908a835093fSnbeams   // num_B_in_mats_to_load will be incremented by 1.
909a835093fSnbeams   CeedInt             num_emode_in = 0, dim = 1, num_B_in_mats_to_load = 0, size_B_in = 0;
910a835093fSnbeams   CeedEvalMode       *eval_mode_in = NULL;  // will be of size num_B_in_mats_load
911a835093fSnbeams   CeedBasis           basis_in     = NULL;
912b216396cSJeremy L Thompson   CeedInt             nqpts = 0, esize = 0;
913a835093fSnbeams   CeedElemRestriction rstr_in = NULL;
914a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
915a835093fSnbeams     CeedVector vec;
9162b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
917a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
9182b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis_in));
9192b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis_in, &dim));
9202b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &nqpts));
9212b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
9222b730f8bSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &esize));
923a835093fSnbeams       CeedEvalMode eval_mode;
9242b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
925a835093fSnbeams       if (eval_mode != CEED_EVAL_NONE) {
9262b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_B_in_mats_to_load + 1, &eval_mode_in));
927a835093fSnbeams         eval_mode_in[num_B_in_mats_to_load] = eval_mode;
928a835093fSnbeams         num_B_in_mats_to_load += 1;
929a835093fSnbeams         if (eval_mode == CEED_EVAL_GRAD) {
930a835093fSnbeams           num_emode_in += dim;
931a835093fSnbeams           size_B_in += dim * esize * nqpts;
932a835093fSnbeams         } else {
933a835093fSnbeams           num_emode_in += 1;
934a835093fSnbeams           size_B_in += esize * nqpts;
935a835093fSnbeams         }
936a835093fSnbeams       }
937a835093fSnbeams     }
938a835093fSnbeams   }
939a835093fSnbeams 
940a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
9412b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
942a835093fSnbeams   CeedInt             num_emode_out = 0, num_B_out_mats_to_load = 0, size_B_out = 0;
943a835093fSnbeams   CeedEvalMode       *eval_mode_out = NULL;
944a835093fSnbeams   CeedBasis           basis_out     = NULL;
945a835093fSnbeams   CeedElemRestriction rstr_out      = NULL;
946a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
947a835093fSnbeams     CeedVector vec;
9482b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
949a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
9502b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis_out));
9512b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
9522b730f8bSJeremy L Thompson       if (rstr_out && rstr_out != rstr_in) {
953a835093fSnbeams         // LCOV_EXCL_START
9542b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator assembly");
955a835093fSnbeams         // LCOV_EXCL_STOP
9562b730f8bSJeremy L Thompson       }
957a835093fSnbeams       CeedEvalMode eval_mode;
9582b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
959a835093fSnbeams       if (eval_mode != CEED_EVAL_NONE) {
9602b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_B_out_mats_to_load + 1, &eval_mode_out));
961a835093fSnbeams         eval_mode_out[num_B_out_mats_to_load] = eval_mode;
962a835093fSnbeams         num_B_out_mats_to_load += 1;
963a835093fSnbeams         if (eval_mode == CEED_EVAL_GRAD) {
964a835093fSnbeams           num_emode_out += dim;
965a835093fSnbeams           size_B_out += dim * esize * nqpts;
966a835093fSnbeams         } else {
967a835093fSnbeams           num_emode_out += 1;
968a835093fSnbeams           size_B_out += esize * nqpts;
969a835093fSnbeams         }
970a835093fSnbeams       }
971a835093fSnbeams     }
972a835093fSnbeams   }
973a835093fSnbeams 
9742b730f8bSJeremy L Thompson   if (num_emode_in == 0 || num_emode_out == 0) {
975a835093fSnbeams     // LCOV_EXCL_START
9762b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
977a835093fSnbeams     // LCOV_EXCL_STOP
9782b730f8bSJeremy L Thompson   }
979a835093fSnbeams 
980a835093fSnbeams   CeedInt nelem, ncomp;
9812b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &nelem));
9822b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &ncomp));
983a835093fSnbeams 
9842b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
985a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
986a835093fSnbeams   asmb->nelem                    = nelem;
987a835093fSnbeams 
988a835093fSnbeams   // Compile kernels
989a835093fSnbeams   int elemsPerBlock   = 1;
990a835093fSnbeams   asmb->elemsPerBlock = elemsPerBlock;
99159ad764aSnbeams   CeedInt block_size  = esize * esize * elemsPerBlock;
99207b31e0eSJeremy L Thompson   char   *assembly_kernel_path, *assembly_kernel_source;
9932b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
99407b31e0eSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Assembly Kernel Source -----\n");
9952b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
99607b31e0eSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Assembly Source Complete! -----\n");
99707b31e0eSJeremy L Thompson   bool fallback = block_size > 1024;
99807b31e0eSJeremy L Thompson   if (fallback) {  // Use fallback kernel with 1D threadblock
99959ad764aSnbeams     block_size         = esize * elemsPerBlock;
100059ad764aSnbeams     asmb->block_size_x = esize;
100159ad764aSnbeams     asmb->block_size_y = 1;
100259ad764aSnbeams   } else {  // Use kernel with 2D threadblock
100359ad764aSnbeams     asmb->block_size_x = esize;
100459ad764aSnbeams     asmb->block_size_y = esize;
100507b31e0eSJeremy L Thompson   }
10062b730f8bSJeremy L Thompson   CeedCallBackend(CeedCompileHip(ceed, assembly_kernel_source, &asmb->module, 7, "NELEM", nelem, "NUMEMODEIN", num_emode_in, "NUMEMODEOUT",
10072b730f8bSJeremy L Thompson                                  num_emode_out, "NQPTS", nqpts, "NNODES", esize, "BLOCK_SIZE", block_size, "NCOMP", ncomp));
10082b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetKernelHip(ceed, asmb->module, fallback ? "linearAssembleFallback" : "linearAssemble", &asmb->linearAssemble));
10092b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
10102b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1011a835093fSnbeams 
1012a835093fSnbeams   // Build 'full' B matrices (not 1D arrays used for tensor-product matrices)
1013a835093fSnbeams   const CeedScalar *interp_in, *grad_in;
10142b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in));
10152b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in));
1016a835093fSnbeams 
1017a835093fSnbeams   // Load into B_in, in order that they will be used in eval_mode
1018a835093fSnbeams   const CeedInt inBytes   = size_B_in * sizeof(CeedScalar);
1019a835093fSnbeams   CeedInt       mat_start = 0;
10202b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, inBytes));
1021a835093fSnbeams   for (int i = 0; i < num_B_in_mats_to_load; i++) {
1022a835093fSnbeams     CeedEvalMode eval_mode = eval_mode_in[i];
1023a835093fSnbeams     if (eval_mode == CEED_EVAL_INTERP) {
10242b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_in[mat_start], interp_in, esize * nqpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1025a835093fSnbeams       mat_start += esize * nqpts;
1026a835093fSnbeams     } else if (eval_mode == CEED_EVAL_GRAD) {
10272b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_in[mat_start], grad_in, dim * esize * nqpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1028a835093fSnbeams       mat_start += dim * esize * nqpts;
1029a835093fSnbeams     }
1030a835093fSnbeams   }
1031a835093fSnbeams 
1032a835093fSnbeams   const CeedScalar *interp_out, *grad_out;
1033a835093fSnbeams   // Note that this function currently assumes 1 basis, so this should always be true
1034a835093fSnbeams   // for now
1035a835093fSnbeams   if (basis_out == basis_in) {
1036a835093fSnbeams     interp_out = interp_in;
1037a835093fSnbeams     grad_out   = grad_in;
1038a835093fSnbeams   } else {
10392b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out));
10402b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out));
1041a835093fSnbeams   }
1042a835093fSnbeams 
1043a835093fSnbeams   // Load into B_out, in order that they will be used in eval_mode
1044a835093fSnbeams   const CeedInt outBytes = size_B_out * sizeof(CeedScalar);
1045a835093fSnbeams   mat_start              = 0;
10462b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, outBytes));
1047a835093fSnbeams   for (int i = 0; i < num_B_out_mats_to_load; i++) {
1048a835093fSnbeams     CeedEvalMode eval_mode = eval_mode_out[i];
1049a835093fSnbeams     if (eval_mode == CEED_EVAL_INTERP) {
10502b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_out[mat_start], interp_out, esize * nqpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1051a835093fSnbeams       mat_start += esize * nqpts;
1052a835093fSnbeams     } else if (eval_mode == CEED_EVAL_GRAD) {
10532b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_out[mat_start], grad_out, dim * esize * nqpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1054a835093fSnbeams       mat_start += dim * esize * nqpts;
1055a835093fSnbeams     }
1056a835093fSnbeams   }
1057a835093fSnbeams   return CEED_ERROR_SUCCESS;
1058a835093fSnbeams }
1059a835093fSnbeams 
1060a835093fSnbeams //------------------------------------------------------------------------------
1061cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1062cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1063cefa2673SJeremy L Thompson //
1064*ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1065cefa2673SJeremy L Thompson // modes).
1066cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1067a835093fSnbeams //------------------------------------------------------------------------------
10682b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1069a835093fSnbeams   Ceed ceed;
10702b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1071a835093fSnbeams   CeedOperator_Hip *impl;
10722b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1073a835093fSnbeams 
1074a835093fSnbeams   // Setup
1075a835093fSnbeams   if (!impl->asmb) {
10762b730f8bSJeremy L Thompson     CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op));
1077b216396cSJeremy L Thompson     assert(impl->asmb != NULL);
1078a835093fSnbeams   }
1079a835093fSnbeams 
1080a835093fSnbeams   // Assemble QFunction
1081a835093fSnbeams   CeedVector          assembled_qf;
1082a835093fSnbeams   CeedElemRestriction rstr_q;
10832b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
10842b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&rstr_q));
1085a835093fSnbeams   CeedScalar *values_array;
10862b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(values, CEED_MEM_DEVICE, &values_array));
1087a835093fSnbeams   values_array += offset;
1088a835093fSnbeams   const CeedScalar *qf_array;
10892b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &qf_array));
1090a835093fSnbeams 
1091a835093fSnbeams   // Compute B^T D B
1092b216396cSJeremy L Thompson   const CeedInt nelem         = impl->asmb->nelem;  // to satisfy clang-tidy
1093a835093fSnbeams   const CeedInt elemsPerBlock = impl->asmb->elemsPerBlock;
10942b730f8bSJeremy L Thompson   const CeedInt grid          = nelem / elemsPerBlock + ((nelem / elemsPerBlock * elemsPerBlock < nelem) ? 1 : 0);
10952b730f8bSJeremy L Thompson   void         *args[]        = {&impl->asmb->d_B_in, &impl->asmb->d_B_out, &qf_array, &values_array};
10962b730f8bSJeremy L Thompson   CeedCallBackend(
10972b730f8bSJeremy L Thompson       CeedRunKernelDimHip(ceed, impl->asmb->linearAssemble, grid, impl->asmb->block_size_x, impl->asmb->block_size_y, elemsPerBlock, args));
1098a835093fSnbeams 
1099a835093fSnbeams   // Restore arrays
11002b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
11012b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &qf_array));
1102a835093fSnbeams 
1103a835093fSnbeams   // Cleanup
11042b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1105a835093fSnbeams 
1106a835093fSnbeams   return CEED_ERROR_SUCCESS;
1107a835093fSnbeams }
1108a835093fSnbeams 
1109a835093fSnbeams //------------------------------------------------------------------------------
11100d0321e0SJeremy L Thompson // Create operator
11110d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11120d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
11130d0321e0SJeremy L Thompson   Ceed ceed;
11142b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11150d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
11160d0321e0SJeremy L Thompson 
11172b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
11182b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
11190d0321e0SJeremy L Thompson 
11202b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
11212b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
11222b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
11232b730f8bSJeremy L Thompson   CeedCallBackend(
11242b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
11252b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
11262b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
11272b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
11280d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11290d0321e0SJeremy L Thompson }
11300d0321e0SJeremy L Thompson 
11310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1132