xref: /libCEED/backends/hip-ref/ceed-hip-ref-operator.c (revision eb7e6cafeb5d6cc94d59355f95e7bc9ae3fc1c25)
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 
849aac155SJeremy L Thompson #include <ceed.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
1007b31e0eSJeremy L Thompson #include <ceed/jit-tools.h>
11c85e8640SSebastian Grimberg #include <assert.h>
120d0321e0SJeremy L Thompson #include <stdbool.h>
130d0321e0SJeremy L Thompson #include <string.h>
14c85e8640SSebastian Grimberg #include <hip/hip_runtime.h>
152b730f8bSJeremy L Thompson 
1649aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
170d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
182b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
190d0321e0SJeremy L Thompson 
200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
210d0321e0SJeremy L Thompson // Destroy operator
220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
230d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Hip(CeedOperator op) {
240d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
252b730f8bSJeremy 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++) {
292b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->evecs[i]));
300d0321e0SJeremy L Thompson   }
312b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->evecs));
320d0321e0SJeremy L Thompson 
330d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->numein; i++) {
342b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qvecsin[i]));
350d0321e0SJeremy L Thompson   }
362b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qvecsin));
370d0321e0SJeremy L Thompson 
380d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->numeout; i++) {
392b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qvecsout[i]));
400d0321e0SJeremy L Thompson   }
412b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qvecsout));
420d0321e0SJeremy L Thompson 
430d0321e0SJeremy L Thompson   // QFunction diagonal assembly data
440d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < impl->qfnumactivein; i++) {
452b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qfactivein[i]));
460d0321e0SJeremy L Thompson   }
472b730f8bSJeremy 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;
522b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
532b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
542b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&impl->diag->h_emodein));
552b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&impl->diag->h_emodeout));
562b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_emodein));
572b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_emodeout));
582b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
592b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interpin));
602b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interpout));
612b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_gradin));
622b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_gradout));
632b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->pbdiagrstr));
642b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elemdiag));
652b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->pbelemdiag));
660d0321e0SJeremy L Thompson   }
672b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
680d0321e0SJeremy L Thompson 
69a835093fSnbeams   if (impl->asmb) {
70a835093fSnbeams     Ceed ceed;
712b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
722b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
732b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
742b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
75a835093fSnbeams   }
762b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
77a835093fSnbeams 
782b730f8bSJeremy 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 //------------------------------------------------------------------------------
852b730f8bSJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool isinput, CeedVector *evecs, CeedVector *qvecs, CeedInt starte,
862b730f8bSJeremy L Thompson                                        CeedInt numfields, CeedInt Q, CeedInt numelements) {
872b730f8bSJeremy L Thompson   CeedInt  dim, size;
88d2643443SJeremy L Thompson   CeedSize q_size;
890d0321e0SJeremy L Thompson   Ceed     ceed;
902b730f8bSJeremy 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) {
1002b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &opfields, NULL, NULL));
1012b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qffields, NULL, NULL));
1020d0321e0SJeremy L Thompson   } else {
1032b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &opfields));
1042b730f8bSJeremy 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;
1102b730f8bSJeremy 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) {
1152b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &Erestrict));
1160d0321e0SJeremy L Thompson 
1170d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
118ea61e9acSJeremy L Thompson       // must be passive input, with emode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
1190d0321e0SJeremy L Thompson 
1200d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
1210d0321e0SJeremy L Thompson       if (isinput) {
1220d0321e0SJeremy L Thompson         // Check for passive input:
1232b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &fieldvec));
1240d0321e0SJeremy L Thompson         if (fieldvec != CEED_VECTOR_ACTIVE) {
1250d0321e0SJeremy L Thompson           // Check emode
1260d0321e0SJeremy L Thompson           if (emode == CEED_EVAL_NONE) {
1270d0321e0SJeremy L Thompson             // Check for strided restriction
1282b730f8bSJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(Erestrict, &strided));
1290d0321e0SJeremy L Thompson             if (strided) {
1300d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
1312b730f8bSJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(Erestrict, &skiprestrict));
1320d0321e0SJeremy L Thompson             }
1330d0321e0SJeremy L Thompson           }
1340d0321e0SJeremy L Thompson         }
1350d0321e0SJeremy L Thompson       }
1360d0321e0SJeremy L Thompson       if (skiprestrict) {
137ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
1380d0321e0SJeremy L Thompson         evecs[i + starte] = NULL;
1390d0321e0SJeremy L Thompson       } else {
1402b730f8bSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(Erestrict, NULL, &evecs[i + starte]));
1410d0321e0SJeremy L Thompson       }
1420d0321e0SJeremy L Thompson     }
1430d0321e0SJeremy L Thompson 
1440d0321e0SJeremy L Thompson     switch (emode) {
1450d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
1462b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size));
147d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q * size;
1482b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1490d0321e0SJeremy L Thompson         break;
1500d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1512b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size));
152d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q * size;
1532b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1540d0321e0SJeremy L Thompson         break;
1550d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
1562b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basis));
1572b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qffields[i], &size));
1582b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetDimension(basis, &dim));
159d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q * size;
1602b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1610d0321e0SJeremy L Thompson         break;
1620d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
1632b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basis));
164d2643443SJeremy L Thompson         q_size = (CeedSize)numelements * Q;
1652b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &qvecs[i]));
1666574a04fSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, qvecs[i]));
1670d0321e0SJeremy L Thompson         break;
1680d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
1690d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
1700d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
1710d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
1720d0321e0SJeremy L Thompson     }
1730d0321e0SJeremy L Thompson   }
1740d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1750d0321e0SJeremy L Thompson }
1760d0321e0SJeremy L Thompson 
1770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
178ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
1790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1800d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
1810d0321e0SJeremy L Thompson   bool setupdone;
1822b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &setupdone));
1832b730f8bSJeremy L Thompson   if (setupdone) return CEED_ERROR_SUCCESS;
1840d0321e0SJeremy L Thompson   Ceed ceed;
1852b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1860d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
1872b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1880d0321e0SJeremy L Thompson   CeedQFunction qf;
1892b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1900d0321e0SJeremy L Thompson   CeedInt Q, numelements, numinputfields, numoutputfields;
1912b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
1922b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &numelements));
1930d0321e0SJeremy L Thompson   CeedOperatorField *opinputfields, *opoutputfields;
1942b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields));
1950d0321e0SJeremy L Thompson   CeedQFunctionField *qfinputfields, *qfoutputfields;
1962b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields));
1970d0321e0SJeremy L Thompson 
1980d0321e0SJeremy L Thompson   // Allocate
1992b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(numinputfields + numoutputfields, &impl->evecs));
2000d0321e0SJeremy L Thompson 
2012b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->qvecsin));
2022b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->qvecsout));
2030d0321e0SJeremy L Thompson 
2042b730f8bSJeremy L Thompson   impl->numein  = numinputfields;
2052b730f8bSJeremy L Thompson   impl->numeout = numoutputfields;
2060d0321e0SJeremy L Thompson 
2070d0321e0SJeremy L Thompson   // Set up infield and outfield evecs and qvecs
2080d0321e0SJeremy L Thompson   // Infields
2092b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, impl->evecs, impl->qvecsin, 0, numinputfields, Q, numelements));
2100d0321e0SJeremy L Thompson 
2110d0321e0SJeremy L Thompson   // Outfields
2122b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, impl->evecs, impl->qvecsout, numinputfields, numoutputfields, Q, numelements));
2130d0321e0SJeremy L Thompson 
2142b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2150d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2160d0321e0SJeremy L Thompson }
2170d0321e0SJeremy L Thompson 
2180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2190d0321e0SJeremy L Thompson // Setup Operator Inputs
2200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2212b730f8bSJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt numinputfields, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields,
2222b730f8bSJeremy L Thompson                                               CeedVector invec, const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl,
2232b730f8bSJeremy L Thompson                                               CeedRequest *request) {
2240d0321e0SJeremy L Thompson   CeedEvalMode        emode;
2250d0321e0SJeremy L Thompson   CeedVector          vec;
2260d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
2270d0321e0SJeremy L Thompson 
2280d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
2290d0321e0SJeremy L Thompson     // Get input vector
2302b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
2310d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2322b730f8bSJeremy L Thompson       if (skipactive) continue;
2332b730f8bSJeremy L Thompson       else vec = invec;
2340d0321e0SJeremy L Thompson     }
2350d0321e0SJeremy L Thompson 
2362b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode));
2370d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_WEIGHT) {  // Skip
2380d0321e0SJeremy L Thompson     } else {
2390d0321e0SJeremy L Thompson       // Get input vector
2402b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
2410d0321e0SJeremy L Thompson       // Get input element restriction
2422b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict));
2432b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = invec;
2440d0321e0SJeremy L Thompson       // Restrict, if necessary
2450d0321e0SJeremy L Thompson       if (!impl->evecs[i]) {
2460d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
2472b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&edata[i]));
2480d0321e0SJeremy L Thompson       } else {
2492b730f8bSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(Erestrict, CEED_NOTRANSPOSE, vec, impl->evecs[i], request));
2500d0321e0SJeremy L Thompson         // Get evec
2512b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->evecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&edata[i]));
2520d0321e0SJeremy L Thompson       }
2530d0321e0SJeremy L Thompson     }
2540d0321e0SJeremy L Thompson   }
2550d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2560d0321e0SJeremy L Thompson }
2570d0321e0SJeremy L Thompson 
2580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2590d0321e0SJeremy L Thompson // Input Basis Action
2600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2612b730f8bSJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt numelements, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields,
2622b730f8bSJeremy L Thompson                                              CeedInt numinputfields, const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX],
2632b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
2640d0321e0SJeremy L Thompson   CeedInt             elemsize, size;
2650d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
2660d0321e0SJeremy L Thompson   CeedEvalMode        emode;
2670d0321e0SJeremy L Thompson   CeedBasis           basis;
2680d0321e0SJeremy L Thompson 
2690d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
2700d0321e0SJeremy L Thompson     // Skip active input
2710d0321e0SJeremy L Thompson     if (skipactive) {
2720d0321e0SJeremy L Thompson       CeedVector vec;
2732b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
2742b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2750d0321e0SJeremy L Thompson     }
2760d0321e0SJeremy L Thompson     // Get elemsize, emode, size
2772b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(opinputfields[i], &Erestrict));
2782b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elemsize));
2792b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode));
2802b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qfinputfields[i], &size));
2810d0321e0SJeremy L Thompson     // Basis action
2820d0321e0SJeremy L Thompson     switch (emode) {
2830d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
2842b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->qvecsin[i], CEED_MEM_DEVICE, CEED_USE_POINTER, edata[i]));
2850d0321e0SJeremy L Thompson         break;
2860d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
2872b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opinputfields[i], &basis));
2882b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, impl->evecs[i], impl->qvecsin[i]));
2890d0321e0SJeremy L Thompson         break;
2900d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
2912b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opinputfields[i], &basis));
2922b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, impl->evecs[i], impl->qvecsin[i]));
2930d0321e0SJeremy L Thompson         break;
2940d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
2950d0321e0SJeremy L Thompson         break;  // No action
2960d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
2970d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
2980d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
2990d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
3000d0321e0SJeremy L Thompson     }
3010d0321e0SJeremy L Thompson   }
3020d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3030d0321e0SJeremy L Thompson }
3040d0321e0SJeremy L Thompson 
3050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3060d0321e0SJeremy L Thompson // Restore Input Vectors
3070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3082b730f8bSJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt numinputfields, CeedQFunctionField *qfinputfields, CeedOperatorField *opinputfields,
3092b730f8bSJeremy L Thompson                                                 const bool skipactive, CeedScalar *edata[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
3100d0321e0SJeremy L Thompson   CeedEvalMode emode;
3110d0321e0SJeremy L Thompson   CeedVector   vec;
3120d0321e0SJeremy L Thompson 
3130d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
3140d0321e0SJeremy L Thompson     // Skip active input
3150d0321e0SJeremy L Thompson     if (skipactive) {
3162b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
3172b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3180d0321e0SJeremy L Thompson     }
3192b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfinputfields[i], &emode));
3200d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_WEIGHT) {  // Skip
3210d0321e0SJeremy L Thompson     } else {
3220d0321e0SJeremy L Thompson       if (!impl->evecs[i]) {  // This was a skiprestrict case
3232b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
3242b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&edata[i]));
3250d0321e0SJeremy L Thompson       } else {
3262b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->evecs[i], (const CeedScalar **)&edata[i]));
3270d0321e0SJeremy L Thompson       }
3280d0321e0SJeremy L Thompson     }
3290d0321e0SJeremy L Thompson   }
3300d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3310d0321e0SJeremy L Thompson }
3320d0321e0SJeremy L Thompson 
3330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3340d0321e0SJeremy L Thompson // Apply and add to output
3350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3362b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector invec, CeedVector outvec, CeedRequest *request) {
3370d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
3382b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3390d0321e0SJeremy L Thompson   CeedQFunction qf;
3402b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3410d0321e0SJeremy L Thompson   CeedInt Q, numelements, elemsize, numinputfields, numoutputfields, size;
3422b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
3432b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &numelements));
3440d0321e0SJeremy L Thompson   CeedOperatorField *opinputfields, *opoutputfields;
3452b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields));
3460d0321e0SJeremy L Thompson   CeedQFunctionField *qfinputfields, *qfoutputfields;
3472b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields));
3480d0321e0SJeremy L Thompson   CeedEvalMode        emode;
3490d0321e0SJeremy L Thompson   CeedVector          vec;
3500d0321e0SJeremy L Thompson   CeedBasis           basis;
3510d0321e0SJeremy L Thompson   CeedElemRestriction Erestrict;
3520d0321e0SJeremy L Thompson   CeedScalar         *edata[2 * CEED_FIELD_MAX];
3530d0321e0SJeremy L Thompson 
3540d0321e0SJeremy L Thompson   // Setup
3552b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
3560d0321e0SJeremy L Thompson 
3570d0321e0SJeremy L Thompson   // Input Evecs and Restriction
3582b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(numinputfields, qfinputfields, opinputfields, invec, false, edata, impl, request));
3590d0321e0SJeremy L Thompson 
3600d0321e0SJeremy L Thompson   // Input basis apply if needed
3612b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(numelements, qfinputfields, opinputfields, numinputfields, false, edata, impl));
3620d0321e0SJeremy L Thompson 
3630d0321e0SJeremy L Thompson   // Output pointers, as necessary
3640d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
3652b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode));
3660d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_NONE) {
3670d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
3682b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->evecs[i + impl->numein], CEED_MEM_DEVICE, &edata[i + numinputfields]));
3692b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->qvecsout[i], CEED_MEM_DEVICE, CEED_USE_POINTER, edata[i + numinputfields]));
3700d0321e0SJeremy L Thompson     }
3710d0321e0SJeremy L Thompson   }
3720d0321e0SJeremy L Thompson 
3730d0321e0SJeremy L Thompson   // Q function
3742b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, numelements * Q, impl->qvecsin, impl->qvecsout));
3750d0321e0SJeremy L Thompson 
3760d0321e0SJeremy L Thompson   // Output basis apply if needed
3770d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
3780d0321e0SJeremy L Thompson     // Get elemsize, emode, size
3792b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(opoutputfields[i], &Erestrict));
3802b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elemsize));
3812b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode));
3822b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[i], &size));
3830d0321e0SJeremy L Thompson     // Basis action
3840d0321e0SJeremy L Thompson     switch (emode) {
3850d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
3860d0321e0SJeremy L Thompson         break;
3870d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3882b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opoutputfields[i], &basis));
3892b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_TRANSPOSE, CEED_EVAL_INTERP, impl->qvecsout[i], impl->evecs[i + impl->numein]));
3900d0321e0SJeremy L Thompson         break;
3910d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
3922b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(opoutputfields[i], &basis));
3932b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, numelements, CEED_TRANSPOSE, CEED_EVAL_GRAD, impl->qvecsout[i], impl->evecs[i + impl->numein]));
3940d0321e0SJeremy L Thompson         break;
3950d0321e0SJeremy L Thompson       // LCOV_EXCL_START
3960d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
3970d0321e0SJeremy L Thompson         Ceed ceed;
3982b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
3992b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
4000d0321e0SJeremy L Thompson         break;  // Should not occur
4010d0321e0SJeremy L Thompson       }
4020d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
4030d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
4040d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
4050d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
4060d0321e0SJeremy L Thompson                 // LCOV_EXCL_STOP
4070d0321e0SJeremy L Thompson     }
4080d0321e0SJeremy L Thompson   }
4090d0321e0SJeremy L Thompson 
4100d0321e0SJeremy L Thompson   // Output restriction
4110d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
4120d0321e0SJeremy L Thompson     // Restore evec
4132b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qfoutputfields[i], &emode));
4140d0321e0SJeremy L Thompson     if (emode == CEED_EVAL_NONE) {
4152b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->evecs[i + impl->numein], &edata[i + numinputfields]));
4160d0321e0SJeremy L Thompson     }
4170d0321e0SJeremy L Thompson     // Get output vector
4182b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[i], &vec));
4190d0321e0SJeremy L Thompson     // Restrict
4202b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(opoutputfields[i], &Erestrict));
4210d0321e0SJeremy L Thompson     // Active
4222b730f8bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = outvec;
4230d0321e0SJeremy L Thompson 
4242b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(Erestrict, CEED_TRANSPOSE, impl->evecs[i + impl->numein], vec, request));
4250d0321e0SJeremy L Thompson   }
4260d0321e0SJeremy L Thompson 
4270d0321e0SJeremy L Thompson   // Restore input arrays
4282b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(numinputfields, qfinputfields, opinputfields, false, edata, impl));
4290d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4300d0321e0SJeremy L Thompson }
4310d0321e0SJeremy L Thompson 
4320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4330d0321e0SJeremy L Thompson // Core code for assembling linear QFunction
4340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4352b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
4360d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
4370d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
4382b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4390d0321e0SJeremy L Thompson   CeedQFunction qf;
4402b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4410d0321e0SJeremy L Thompson   CeedInt  Q, numelements, numinputfields, numoutputfields, size;
442d2643443SJeremy L Thompson   CeedSize q_size;
4432b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
4442b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &numelements));
4450d0321e0SJeremy L Thompson   CeedOperatorField *opinputfields, *opoutputfields;
4462b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &numinputfields, &opinputfields, &numoutputfields, &opoutputfields));
4470d0321e0SJeremy L Thompson   CeedQFunctionField *qfinputfields, *qfoutputfields;
4482b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qfinputfields, NULL, &qfoutputfields));
4490d0321e0SJeremy L Thompson   CeedVector  vec;
4500d0321e0SJeremy L Thompson   CeedInt     numactivein = impl->qfnumactivein, numactiveout = impl->qfnumactiveout;
4510d0321e0SJeremy L Thompson   CeedVector *activein = impl->qfactivein;
4520d0321e0SJeremy L Thompson   CeedScalar *a, *tmp;
4530d0321e0SJeremy L Thompson   Ceed        ceed, ceedparent;
4542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
4552b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetOperatorFallbackParentCeed(ceed, &ceedparent));
4560d0321e0SJeremy L Thompson   ceedparent = ceedparent ? ceedparent : ceed;
4570d0321e0SJeremy L Thompson   CeedScalar *edata[2 * CEED_FIELD_MAX];
4580d0321e0SJeremy L Thompson 
4590d0321e0SJeremy L Thompson   // Setup
4602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4610d0321e0SJeremy L Thompson 
4620d0321e0SJeremy L Thompson   // Check for identity
4630d0321e0SJeremy L Thompson   bool identityqf;
4642b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionIsIdentity(qf, &identityqf));
4656574a04fSJeremy L Thompson   CeedCheck(!identityqf, ceed, CEED_ERROR_BACKEND, "Assembling identity QFunctions not supported");
4660d0321e0SJeremy L Thompson 
4670d0321e0SJeremy L Thompson   // Input Evecs and Restriction
4682b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(numinputfields, qfinputfields, opinputfields, NULL, true, edata, impl, request));
4690d0321e0SJeremy L Thompson 
4700d0321e0SJeremy L Thompson   // Count number of active input fields
4710d0321e0SJeremy L Thompson   if (!numactivein) {
4720d0321e0SJeremy L Thompson     for (CeedInt i = 0; i < numinputfields; i++) {
4730d0321e0SJeremy L Thompson       // Get input vector
4742b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opinputfields[i], &vec));
4750d0321e0SJeremy L Thompson       // Check if active input
4760d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
4772b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qfinputfields[i], &size));
4782b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->qvecsin[i], 0.0));
4792b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->qvecsin[i], CEED_MEM_DEVICE, &tmp));
4802b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(numactivein + size, &activein));
4810d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
482d2643443SJeremy L Thompson           q_size = (CeedSize)Q * numelements;
4832b730f8bSJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &activein[numactivein + field]));
4842b730f8bSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(activein[numactivein + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &tmp[field * Q * numelements]));
4850d0321e0SJeremy L Thompson         }
4860d0321e0SJeremy L Thompson         numactivein += size;
4872b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->qvecsin[i], &tmp));
4880d0321e0SJeremy L Thompson       }
4890d0321e0SJeremy L Thompson     }
4900d0321e0SJeremy L Thompson     impl->qfnumactivein = numactivein;
4910d0321e0SJeremy L Thompson     impl->qfactivein    = activein;
4920d0321e0SJeremy L Thompson   }
4930d0321e0SJeremy L Thompson 
4940d0321e0SJeremy L Thompson   // Count number of active output fields
4950d0321e0SJeremy L Thompson   if (!numactiveout) {
4960d0321e0SJeremy L Thompson     for (CeedInt i = 0; i < numoutputfields; i++) {
4970d0321e0SJeremy L Thompson       // Get output vector
4982b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[i], &vec));
4990d0321e0SJeremy L Thompson       // Check if active output
5000d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
5012b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[i], &size));
5020d0321e0SJeremy L Thompson         numactiveout += size;
5030d0321e0SJeremy L Thompson       }
5040d0321e0SJeremy L Thompson     }
5050d0321e0SJeremy L Thompson     impl->qfnumactiveout = numactiveout;
5060d0321e0SJeremy L Thompson   }
5070d0321e0SJeremy L Thompson 
5080d0321e0SJeremy L Thompson   // Check sizes
5096574a04fSJeremy L Thompson   CeedCheck(numactivein > 0 && numactiveout > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5100d0321e0SJeremy L Thompson 
5110d0321e0SJeremy L Thompson   // Build objects if needed
5120d0321e0SJeremy L Thompson   if (build_objects) {
5130d0321e0SJeremy L Thompson     // Create output restriction
5140d0321e0SJeremy L Thompson     CeedInt strides[3] = {1, numelements * Q, Q}; /* *NOPAD* */
5152b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceedparent, numelements, Q, numactivein * numactiveout,
5162b730f8bSJeremy L Thompson                                                      numactivein * numactiveout * numelements * Q, strides, rstr));
5170d0321e0SJeremy L Thompson     // Create assembled vector
518d2643443SJeremy L Thompson     CeedSize l_size = (CeedSize)numelements * Q * numactivein * numactiveout;
5192b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceedparent, l_size, assembled));
5200d0321e0SJeremy L Thompson   }
5212b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
5222b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &a));
5230d0321e0SJeremy L Thompson 
5240d0321e0SJeremy L Thompson   // Input basis apply
5252b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(numelements, qfinputfields, opinputfields, numinputfields, true, edata, impl));
5260d0321e0SJeremy L Thompson 
5270d0321e0SJeremy L Thompson   // Assemble QFunction
5280d0321e0SJeremy L Thompson   for (CeedInt in = 0; in < numactivein; in++) {
5290d0321e0SJeremy L Thompson     // Set Inputs
5302b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(activein[in], 1.0));
5310d0321e0SJeremy L Thompson     if (numactivein > 1) {
5322b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(activein[(in + numactivein - 1) % numactivein], 0.0));
5330d0321e0SJeremy L Thompson     }
5340d0321e0SJeremy L Thompson     // Set Outputs
5350d0321e0SJeremy L Thompson     for (CeedInt out = 0; out < numoutputfields; out++) {
5360d0321e0SJeremy L Thompson       // Get output vector
5372b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[out], &vec));
5380d0321e0SJeremy L Thompson       // Check if active output
5390d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
5402b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->qvecsout[out], CEED_MEM_DEVICE, CEED_USE_POINTER, a));
5412b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qfoutputfields[out], &size));
5420d0321e0SJeremy L Thompson         a += size * Q * numelements;  // Advance the pointer by the size of the output
5430d0321e0SJeremy L Thompson       }
5440d0321e0SJeremy L Thompson     }
5450d0321e0SJeremy L Thompson     // Apply QFunction
5462b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * numelements, impl->qvecsin, impl->qvecsout));
5470d0321e0SJeremy L Thompson   }
5480d0321e0SJeremy L Thompson 
5490d0321e0SJeremy L Thompson   // Un-set output Qvecs to prevent accidental overwrite of Assembled
5500d0321e0SJeremy L Thompson   for (CeedInt out = 0; out < numoutputfields; out++) {
5510d0321e0SJeremy L Thompson     // Get output vector
5522b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opoutputfields[out], &vec));
5530d0321e0SJeremy L Thompson     // Check if active output
5540d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
5552b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->qvecsout[out], CEED_MEM_DEVICE, NULL));
5560d0321e0SJeremy L Thompson     }
5570d0321e0SJeremy L Thompson   }
5580d0321e0SJeremy L Thompson 
5590d0321e0SJeremy L Thompson   // Restore input arrays
5602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(numinputfields, qfinputfields, opinputfields, true, edata, impl));
5610d0321e0SJeremy L Thompson 
5620d0321e0SJeremy L Thompson   // Restore output
5632b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &a));
5640d0321e0SJeremy L Thompson 
5650d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5660d0321e0SJeremy L Thompson }
5670d0321e0SJeremy L Thompson 
5680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5690d0321e0SJeremy L Thompson // Assemble Linear QFunction
5700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5712b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
5722b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
5730d0321e0SJeremy L Thompson }
5740d0321e0SJeremy L Thompson 
5750d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5760d0321e0SJeremy L Thompson // Assemble Linear QFunction
5770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5782b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
5792b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
5800d0321e0SJeremy L Thompson }
5810d0321e0SJeremy L Thompson 
5820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5830d0321e0SJeremy L Thompson // Create point block restriction
5840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5852b730f8bSJeremy L Thompson static int CreatePBRestriction(CeedElemRestriction rstr, CeedElemRestriction *pbRstr) {
5860d0321e0SJeremy L Thompson   Ceed ceed;
5872b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
5880d0321e0SJeremy L Thompson   const CeedInt *offsets;
5892b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
5900d0321e0SJeremy L Thompson 
5910d0321e0SJeremy L Thompson   // Expand offsets
5927b63f5c6SJed Brown   CeedInt  nelem, ncomp, elemsize, compstride, *pbOffsets;
5937b63f5c6SJed Brown   CeedSize l_size;
5942b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &nelem));
5952b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &ncomp));
5962b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elemsize));
5972b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &compstride));
5982b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
5990d0321e0SJeremy L Thompson   CeedInt shift = ncomp;
6002b730f8bSJeremy L Thompson   if (compstride != 1) shift *= ncomp;
6012b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(nelem * elemsize, &pbOffsets));
6020d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < nelem * elemsize; i++) {
6030d0321e0SJeremy L Thompson     pbOffsets[i] = offsets[i] * shift;
6040d0321e0SJeremy L Thompson   }
6050d0321e0SJeremy L Thompson 
6060d0321e0SJeremy L Thompson   // Create new restriction
6072b730f8bSJeremy L Thompson   CeedCallBackend(
6082b730f8bSJeremy L Thompson       CeedElemRestrictionCreate(ceed, nelem, elemsize, ncomp * ncomp, 1, l_size * ncomp, CEED_MEM_HOST, CEED_OWN_POINTER, pbOffsets, pbRstr));
6090d0321e0SJeremy L Thompson 
6100d0321e0SJeremy L Thompson   // Cleanup
6112b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
6120d0321e0SJeremy L Thompson 
6130d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6140d0321e0SJeremy L Thompson }
6150d0321e0SJeremy L Thompson 
6160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6170d0321e0SJeremy L Thompson // Assemble diagonal setup
6180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6192b730f8bSJeremy L Thompson static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op, const bool pointBlock) {
6200d0321e0SJeremy L Thompson   Ceed ceed;
6212b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
6220d0321e0SJeremy L Thompson   CeedQFunction qf;
6232b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
6240d0321e0SJeremy L Thompson   CeedInt numinputfields, numoutputfields;
6252b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &numinputfields, &numoutputfields));
6260d0321e0SJeremy L Thompson 
6270d0321e0SJeremy L Thompson   // Determine active input basis
6280d0321e0SJeremy L Thompson   CeedOperatorField  *opfields;
6290d0321e0SJeremy L Thompson   CeedQFunctionField *qffields;
6302b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &opfields, NULL, NULL));
6312b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qffields, NULL, NULL));
6320d0321e0SJeremy L Thompson   CeedInt             numemodein = 0, ncomp = 0, dim = 1;
6330d0321e0SJeremy L Thompson   CeedEvalMode       *emodein = NULL;
6340d0321e0SJeremy L Thompson   CeedBasis           basisin = NULL;
6350d0321e0SJeremy L Thompson   CeedElemRestriction rstrin  = NULL;
6360d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numinputfields; i++) {
6370d0321e0SJeremy L Thompson     CeedVector vec;
6382b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &vec));
6390d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
6400d0321e0SJeremy L Thompson       CeedElemRestriction rstr;
6412b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basisin));
6422b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumComponents(basisin, &ncomp));
6432b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basisin, &dim));
6442b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &rstr));
6456574a04fSJeremy L Thompson       CeedCheck(!rstrin || rstrin == rstr, ceed, CEED_ERROR_BACKEND,
6466574a04fSJeremy L Thompson                 "Backend does not implement multi-field non-composite operator diagonal assembly");
6470d0321e0SJeremy L Thompson       rstrin = rstr;
6480d0321e0SJeremy L Thompson       CeedEvalMode emode;
6492b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode));
6500d0321e0SJeremy L Thompson       switch (emode) {
6510d0321e0SJeremy L Thompson         case CEED_EVAL_NONE:
6520d0321e0SJeremy L Thompson         case CEED_EVAL_INTERP:
6532b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodein + 1, &emodein));
6540d0321e0SJeremy L Thompson           emodein[numemodein] = emode;
6550d0321e0SJeremy L Thompson           numemodein += 1;
6560d0321e0SJeremy L Thompson           break;
6570d0321e0SJeremy L Thompson         case CEED_EVAL_GRAD:
6582b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodein + dim, &emodein));
6592b730f8bSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) emodein[numemodein + d] = emode;
6600d0321e0SJeremy L Thompson           numemodein += dim;
6610d0321e0SJeremy L Thompson           break;
6620d0321e0SJeremy L Thompson         case CEED_EVAL_WEIGHT:
6630d0321e0SJeremy L Thompson         case CEED_EVAL_DIV:
6640d0321e0SJeremy L Thompson         case CEED_EVAL_CURL:
6650d0321e0SJeremy L Thompson           break;  // Caught by QF Assembly
6660d0321e0SJeremy L Thompson       }
6670d0321e0SJeremy L Thompson     }
6680d0321e0SJeremy L Thompson   }
6690d0321e0SJeremy L Thompson 
6700d0321e0SJeremy L Thompson   // Determine active output basis
6712b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &opfields));
6722b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qffields));
6730d0321e0SJeremy L Thompson   CeedInt             numemodeout = 0;
6740d0321e0SJeremy L Thompson   CeedEvalMode       *emodeout    = NULL;
6750d0321e0SJeremy L Thompson   CeedBasis           basisout    = NULL;
6760d0321e0SJeremy L Thompson   CeedElemRestriction rstrout     = NULL;
6770d0321e0SJeremy L Thompson   for (CeedInt i = 0; i < numoutputfields; i++) {
6780d0321e0SJeremy L Thompson     CeedVector vec;
6792b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(opfields[i], &vec));
6800d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
6810d0321e0SJeremy L Thompson       CeedElemRestriction rstr;
6822b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(opfields[i], &basisout));
6832b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(opfields[i], &rstr));
6846574a04fSJeremy L Thompson       CeedCheck(!rstrout || rstrout == rstr, ceed, CEED_ERROR_BACKEND,
6856574a04fSJeremy L Thompson                 "Backend does not implement multi-field non-composite operator diagonal assembly");
6860d0321e0SJeremy L Thompson       rstrout = rstr;
6870d0321e0SJeremy L Thompson       CeedEvalMode emode;
6882b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qffields[i], &emode));
6890d0321e0SJeremy L Thompson       switch (emode) {
6900d0321e0SJeremy L Thompson         case CEED_EVAL_NONE:
6910d0321e0SJeremy L Thompson         case CEED_EVAL_INTERP:
6922b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodeout + 1, &emodeout));
6930d0321e0SJeremy L Thompson           emodeout[numemodeout] = emode;
6940d0321e0SJeremy L Thompson           numemodeout += 1;
6950d0321e0SJeremy L Thompson           break;
6960d0321e0SJeremy L Thompson         case CEED_EVAL_GRAD:
6972b730f8bSJeremy L Thompson           CeedCallBackend(CeedRealloc(numemodeout + dim, &emodeout));
6982b730f8bSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) emodeout[numemodeout + d] = emode;
6990d0321e0SJeremy L Thompson           numemodeout += dim;
7000d0321e0SJeremy L Thompson           break;
7010d0321e0SJeremy L Thompson         case CEED_EVAL_WEIGHT:
7020d0321e0SJeremy L Thompson         case CEED_EVAL_DIV:
7030d0321e0SJeremy L Thompson         case CEED_EVAL_CURL:
7040d0321e0SJeremy L Thompson           break;  // Caught by QF Assembly
7050d0321e0SJeremy L Thompson       }
7060d0321e0SJeremy L Thompson     }
7070d0321e0SJeremy L Thompson   }
7080d0321e0SJeremy L Thompson 
7090d0321e0SJeremy L Thompson   // Operator data struct
7100d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
7112b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
7122b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
7130d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
7140d0321e0SJeremy L Thompson   diag->basisin              = basisin;
7150d0321e0SJeremy L Thompson   diag->basisout             = basisout;
7160d0321e0SJeremy L Thompson   diag->h_emodein            = emodein;
7170d0321e0SJeremy L Thompson   diag->h_emodeout           = emodeout;
7180d0321e0SJeremy L Thompson   diag->numemodein           = numemodein;
7190d0321e0SJeremy L Thompson   diag->numemodeout          = numemodeout;
7200d0321e0SJeremy L Thompson 
7210d0321e0SJeremy L Thompson   // Assemble kernel
72207b31e0eSJeremy L Thompson 
72307b31e0eSJeremy L Thompson   char *diagonal_kernel_path, *diagonal_kernel_source;
7242b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
72507b31e0eSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Diagonal Assembly Kernel Source -----\n");
7262b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
7272b730f8bSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Diagonal Assembly Source Complete! -----\n");
7280d0321e0SJeremy L Thompson   CeedInt nnodes, nqpts;
7292b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basisin, &nnodes));
7302b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basisin, &nqpts));
7310d0321e0SJeremy L Thompson   diag->nnodes = nnodes;
732*eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedCompile_Hip(ceed, diagonal_kernel_source, &diag->module, 5, "NUMEMODEIN", numemodein, "NUMEMODEOUT", numemodeout, "NNODES",
7332b730f8bSJeremy L Thompson                                   nnodes, "NQPTS", nqpts, "NCOMP", ncomp));
734*eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, diag->module, "linearDiagonal", &diag->linearDiagonal));
735*eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, diag->module, "linearPointBlockDiagonal", &diag->linearPointBlock));
7362b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&diagonal_kernel_path));
7372b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&diagonal_kernel_source));
7380d0321e0SJeremy L Thompson 
7390d0321e0SJeremy L Thompson   // Basis matrices
7400d0321e0SJeremy L Thompson   const CeedInt     qBytes = nqpts * sizeof(CeedScalar);
7410d0321e0SJeremy L Thompson   const CeedInt     iBytes = qBytes * nnodes;
7420d0321e0SJeremy L Thompson   const CeedInt     gBytes = qBytes * nnodes * dim;
7430d0321e0SJeremy L Thompson   const CeedInt     eBytes = sizeof(CeedEvalMode);
7440d0321e0SJeremy L Thompson   const CeedScalar *interpin, *interpout, *gradin, *gradout;
7450d0321e0SJeremy L Thompson 
7460d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
7470d0321e0SJeremy L Thompson   CeedScalar *identity = NULL;
7480d0321e0SJeremy L Thompson   bool        evalNone = false;
7492b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < numemodein; i++) evalNone = evalNone || (emodein[i] == CEED_EVAL_NONE);
7502b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < numemodeout; i++) evalNone = evalNone || (emodeout[i] == CEED_EVAL_NONE);
7510d0321e0SJeremy L Thompson   if (evalNone) {
7522b730f8bSJeremy L Thompson     CeedCallBackend(CeedCalloc(nqpts * nnodes, &identity));
7532b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < (nnodes < nqpts ? nnodes : nqpts); i++) identity[i * nnodes + i] = 1.0;
7542b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, iBytes));
7552b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, iBytes, hipMemcpyHostToDevice));
7560d0321e0SJeremy L Thompson   }
7570d0321e0SJeremy L Thompson 
7580d0321e0SJeremy L Thompson   // CEED_EVAL_INTERP
7592b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basisin, &interpin));
7602b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_interpin, iBytes));
7612b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_interpin, interpin, iBytes, hipMemcpyHostToDevice));
7622b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basisout, &interpout));
7632b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_interpout, iBytes));
7642b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_interpout, interpout, iBytes, hipMemcpyHostToDevice));
7650d0321e0SJeremy L Thompson 
7660d0321e0SJeremy L Thompson   // CEED_EVAL_GRAD
7672b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basisin, &gradin));
7682b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_gradin, gBytes));
7692b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_gradin, gradin, gBytes, hipMemcpyHostToDevice));
7702b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basisout, &gradout));
7712b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_gradout, gBytes));
7722b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_gradout, gradout, gBytes, hipMemcpyHostToDevice));
7730d0321e0SJeremy L Thompson 
7740d0321e0SJeremy L Thompson   // Arrays of emodes
7752b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_emodein, numemodein * eBytes));
7762b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_emodein, emodein, numemodein * eBytes, hipMemcpyHostToDevice));
7772b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_emodeout, numemodeout * eBytes));
7782b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_emodeout, emodeout, numemodeout * eBytes, hipMemcpyHostToDevice));
7790d0321e0SJeremy L Thompson 
7800d0321e0SJeremy L Thompson   // Restriction
7810d0321e0SJeremy L Thompson   diag->diagrstr = rstrout;
7820d0321e0SJeremy L Thompson 
7830d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7840d0321e0SJeremy L Thompson }
7850d0321e0SJeremy L Thompson 
7860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7870d0321e0SJeremy L Thompson // Assemble diagonal common code
7880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7892b730f8bSJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool pointBlock) {
7900d0321e0SJeremy L Thompson   Ceed ceed;
7912b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
7920d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
7932b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
7940d0321e0SJeremy L Thompson 
7950d0321e0SJeremy L Thompson   // Assemble QFunction
796c5f45aeaSJeremy L Thompson   CeedVector          assembledqf = NULL;
797c5f45aeaSJeremy L Thompson   CeedElemRestriction rstr        = NULL;
7982b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembledqf, &rstr, request));
7992b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&rstr));
8000d0321e0SJeremy L Thompson 
8010d0321e0SJeremy L Thompson   // Setup
8022b730f8bSJeremy L Thompson   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op, pointBlock));
8030d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
8040d0321e0SJeremy L Thompson   assert(diag != NULL);
8050d0321e0SJeremy L Thompson 
8060d0321e0SJeremy L Thompson   // Restriction
8070d0321e0SJeremy L Thompson   if (pointBlock && !diag->pbdiagrstr) {
8080d0321e0SJeremy L Thompson     CeedElemRestriction pbdiagrstr;
8092b730f8bSJeremy L Thompson     CeedCallBackend(CreatePBRestriction(diag->diagrstr, &pbdiagrstr));
8100d0321e0SJeremy L Thompson     diag->pbdiagrstr = pbdiagrstr;
8110d0321e0SJeremy L Thompson   }
8120d0321e0SJeremy L Thompson   CeedElemRestriction diagrstr = pointBlock ? diag->pbdiagrstr : diag->diagrstr;
8130d0321e0SJeremy L Thompson 
8140d0321e0SJeremy L Thompson   // Create diagonal vector
8150d0321e0SJeremy L Thompson   CeedVector elemdiag = pointBlock ? diag->pbelemdiag : diag->elemdiag;
8160d0321e0SJeremy L Thompson   if (!elemdiag) {
8170d0321e0SJeremy L Thompson     // Element diagonal vector
8182b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(diagrstr, NULL, &elemdiag));
8192b730f8bSJeremy L Thompson     if (pointBlock) diag->pbelemdiag = elemdiag;
8202b730f8bSJeremy L Thompson     else diag->elemdiag = elemdiag;
8210d0321e0SJeremy L Thompson   }
8222b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elemdiag, 0.0));
8230d0321e0SJeremy L Thompson 
8240d0321e0SJeremy L Thompson   // Assemble element operator diagonals
8250d0321e0SJeremy L Thompson   CeedScalar       *elemdiagarray;
8260d0321e0SJeremy L Thompson   const CeedScalar *assembledqfarray;
8272b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(elemdiag, CEED_MEM_DEVICE, &elemdiagarray));
8282b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(assembledqf, CEED_MEM_DEVICE, &assembledqfarray));
8290d0321e0SJeremy L Thompson   CeedInt nelem;
8302b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(diagrstr, &nelem));
8310d0321e0SJeremy L Thompson 
8320d0321e0SJeremy L Thompson   // Compute the diagonal of B^T D B
8330d0321e0SJeremy L Thompson   int   elemsPerBlock = 1;
8340d0321e0SJeremy L Thompson   int   grid          = nelem / elemsPerBlock + ((nelem / elemsPerBlock * elemsPerBlock < nelem) ? 1 : 0);
8352b730f8bSJeremy L Thompson   void *args[]        = {(void *)&nelem,   &diag->d_identity, &diag->d_interpin, &diag->d_gradin,   &diag->d_interpout,
8362b730f8bSJeremy L Thompson                          &diag->d_gradout, &diag->d_emodein,  &diag->d_emodeout, &assembledqfarray, &elemdiagarray};
8370d0321e0SJeremy L Thompson   if (pointBlock) {
838*eb7e6cafSJeremy L Thompson     CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->linearPointBlock, grid, diag->nnodes, 1, elemsPerBlock, args));
8390d0321e0SJeremy L Thompson   } else {
840*eb7e6cafSJeremy L Thompson     CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->linearDiagonal, grid, diag->nnodes, 1, elemsPerBlock, args));
8410d0321e0SJeremy L Thompson   }
8420d0321e0SJeremy L Thompson 
8430d0321e0SJeremy L Thompson   // Restore arrays
8442b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(elemdiag, &elemdiagarray));
8452b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(assembledqf, &assembledqfarray));
8460d0321e0SJeremy L Thompson 
8470d0321e0SJeremy L Thompson   // Assemble local operator diagonal
8482b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diagrstr, CEED_TRANSPOSE, elemdiag, assembled, request));
8490d0321e0SJeremy L Thompson 
8500d0321e0SJeremy L Thompson   // Cleanup
8512b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembledqf));
8520d0321e0SJeremy L Thompson 
8530d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8540d0321e0SJeremy L Thompson }
8550d0321e0SJeremy L Thompson 
8560d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8570d0321e0SJeremy L Thompson // Assemble Linear Diagonal
8580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8592b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
8602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
8616aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8620d0321e0SJeremy L Thompson }
8630d0321e0SJeremy L Thompson 
8640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8650d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
8660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8672b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
8682b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
8696aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8700d0321e0SJeremy L Thompson }
8710d0321e0SJeremy L Thompson 
872a835093fSnbeams //------------------------------------------------------------------------------
873a835093fSnbeams // Single operator assembly setup
874a835093fSnbeams //------------------------------------------------------------------------------
875a835093fSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op) {
876a835093fSnbeams   Ceed ceed;
8772b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
878a835093fSnbeams   CeedOperator_Hip *impl;
8792b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
880a835093fSnbeams 
881a835093fSnbeams   // Get intput and output fields
882a835093fSnbeams   CeedInt            num_input_fields, num_output_fields;
883a835093fSnbeams   CeedOperatorField *input_fields;
884a835093fSnbeams   CeedOperatorField *output_fields;
8852b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
886a835093fSnbeams 
887a835093fSnbeams   // Determine active input basis eval mode
888a835093fSnbeams   CeedQFunction qf;
8892b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
890a835093fSnbeams   CeedQFunctionField *qf_fields;
8912b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
892a835093fSnbeams   // Note that the kernel will treat each dimension of a gradient action separately;
893ea61e9acSJeremy L Thompson   // i.e., when an active input has a CEED_EVAL_GRAD mode, num_emode_in will increment by dim.
894ea61e9acSJeremy 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
895a835093fSnbeams   // num_B_in_mats_to_load will be incremented by 1.
896a835093fSnbeams   CeedInt             num_emode_in = 0, dim = 1, num_B_in_mats_to_load = 0, size_B_in = 0;
897a835093fSnbeams   CeedEvalMode       *eval_mode_in = NULL;  // will be of size num_B_in_mats_load
898a835093fSnbeams   CeedBasis           basis_in     = NULL;
899b216396cSJeremy L Thompson   CeedInt             nqpts = 0, esize = 0;
900a835093fSnbeams   CeedElemRestriction rstr_in = NULL;
901a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
902a835093fSnbeams     CeedVector vec;
9032b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
904a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
9052b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis_in));
9062b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis_in, &dim));
9072b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &nqpts));
9082b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
9092b730f8bSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &esize));
910a835093fSnbeams       CeedEvalMode eval_mode;
9112b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
912a835093fSnbeams       if (eval_mode != CEED_EVAL_NONE) {
9132b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_B_in_mats_to_load + 1, &eval_mode_in));
914a835093fSnbeams         eval_mode_in[num_B_in_mats_to_load] = eval_mode;
915a835093fSnbeams         num_B_in_mats_to_load += 1;
916a835093fSnbeams         if (eval_mode == CEED_EVAL_GRAD) {
917a835093fSnbeams           num_emode_in += dim;
918a835093fSnbeams           size_B_in += dim * esize * nqpts;
919a835093fSnbeams         } else {
920a835093fSnbeams           num_emode_in += 1;
921a835093fSnbeams           size_B_in += esize * nqpts;
922a835093fSnbeams         }
923a835093fSnbeams       }
924a835093fSnbeams     }
925a835093fSnbeams   }
926a835093fSnbeams 
927a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
9282b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
929a835093fSnbeams   CeedInt             num_emode_out = 0, num_B_out_mats_to_load = 0, size_B_out = 0;
930a835093fSnbeams   CeedEvalMode       *eval_mode_out = NULL;
931a835093fSnbeams   CeedBasis           basis_out     = NULL;
932a835093fSnbeams   CeedElemRestriction rstr_out      = NULL;
933a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
934a835093fSnbeams     CeedVector vec;
9352b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
936a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
9372b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis_out));
9382b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
9396574a04fSJeremy L Thompson       CeedCheck(!rstr_out || rstr_out == rstr_in, ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator assembly");
940a835093fSnbeams       CeedEvalMode eval_mode;
9412b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
942a835093fSnbeams       if (eval_mode != CEED_EVAL_NONE) {
9432b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_B_out_mats_to_load + 1, &eval_mode_out));
944a835093fSnbeams         eval_mode_out[num_B_out_mats_to_load] = eval_mode;
945a835093fSnbeams         num_B_out_mats_to_load += 1;
946a835093fSnbeams         if (eval_mode == CEED_EVAL_GRAD) {
947a835093fSnbeams           num_emode_out += dim;
948a835093fSnbeams           size_B_out += dim * esize * nqpts;
949a835093fSnbeams         } else {
950a835093fSnbeams           num_emode_out += 1;
951a835093fSnbeams           size_B_out += esize * nqpts;
952a835093fSnbeams         }
953a835093fSnbeams       }
954a835093fSnbeams     }
955a835093fSnbeams   }
956a835093fSnbeams 
9576574a04fSJeremy L Thompson   CeedCheck(num_emode_in > 0 && num_emode_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
958a835093fSnbeams 
959a835093fSnbeams   CeedInt nelem, ncomp;
9602b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &nelem));
9612b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &ncomp));
962a835093fSnbeams 
9632b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
964a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
965a835093fSnbeams   asmb->nelem                    = nelem;
966a835093fSnbeams 
967a835093fSnbeams   // Compile kernels
968a835093fSnbeams   int elemsPerBlock   = 1;
969a835093fSnbeams   asmb->elemsPerBlock = elemsPerBlock;
97059ad764aSnbeams   CeedInt block_size  = esize * esize * elemsPerBlock;
97107b31e0eSJeremy L Thompson   char   *assembly_kernel_path, *assembly_kernel_source;
9722b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
97307b31e0eSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Assembly Kernel Source -----\n");
9742b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
97507b31e0eSJeremy L Thompson   CeedDebug256(ceed, 2, "----- Loading Assembly Source Complete! -----\n");
97607b31e0eSJeremy L Thompson   bool fallback = block_size > 1024;
97707b31e0eSJeremy L Thompson   if (fallback) {  // Use fallback kernel with 1D threadblock
97859ad764aSnbeams     block_size         = esize * elemsPerBlock;
97959ad764aSnbeams     asmb->block_size_x = esize;
98059ad764aSnbeams     asmb->block_size_y = 1;
98159ad764aSnbeams   } else {  // Use kernel with 2D threadblock
98259ad764aSnbeams     asmb->block_size_x = esize;
98359ad764aSnbeams     asmb->block_size_y = esize;
98407b31e0eSJeremy L Thompson   }
985*eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 7, "NELEM", nelem, "NUMEMODEIN", num_emode_in, "NUMEMODEOUT",
9862b730f8bSJeremy L Thompson                                   num_emode_out, "NQPTS", nqpts, "NNODES", esize, "BLOCK_SIZE", block_size, "NCOMP", ncomp));
987*eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, fallback ? "linearAssembleFallback" : "linearAssemble", &asmb->linearAssemble));
9882b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
9892b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
990a835093fSnbeams 
991a835093fSnbeams   // Build 'full' B matrices (not 1D arrays used for tensor-product matrices)
992a835093fSnbeams   const CeedScalar *interp_in, *grad_in;
9932b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in));
9942b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in));
995a835093fSnbeams 
996a835093fSnbeams   // Load into B_in, in order that they will be used in eval_mode
997a835093fSnbeams   const CeedInt inBytes   = size_B_in * sizeof(CeedScalar);
998a835093fSnbeams   CeedInt       mat_start = 0;
9992b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, inBytes));
1000a835093fSnbeams   for (int i = 0; i < num_B_in_mats_to_load; i++) {
1001a835093fSnbeams     CeedEvalMode eval_mode = eval_mode_in[i];
1002a835093fSnbeams     if (eval_mode == CEED_EVAL_INTERP) {
10032b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_in[mat_start], interp_in, esize * nqpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1004a835093fSnbeams       mat_start += esize * nqpts;
1005a835093fSnbeams     } else if (eval_mode == CEED_EVAL_GRAD) {
10062b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_in[mat_start], grad_in, dim * esize * nqpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1007a835093fSnbeams       mat_start += dim * esize * nqpts;
1008a835093fSnbeams     }
1009a835093fSnbeams   }
1010a835093fSnbeams 
1011a835093fSnbeams   const CeedScalar *interp_out, *grad_out;
1012a835093fSnbeams   // Note that this function currently assumes 1 basis, so this should always be true
1013a835093fSnbeams   // for now
1014a835093fSnbeams   if (basis_out == basis_in) {
1015a835093fSnbeams     interp_out = interp_in;
1016a835093fSnbeams     grad_out   = grad_in;
1017a835093fSnbeams   } else {
10182b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out));
10192b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out));
1020a835093fSnbeams   }
1021a835093fSnbeams 
1022a835093fSnbeams   // Load into B_out, in order that they will be used in eval_mode
1023a835093fSnbeams   const CeedInt outBytes = size_B_out * sizeof(CeedScalar);
1024a835093fSnbeams   mat_start              = 0;
10252b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, outBytes));
1026a835093fSnbeams   for (int i = 0; i < num_B_out_mats_to_load; i++) {
1027a835093fSnbeams     CeedEvalMode eval_mode = eval_mode_out[i];
1028a835093fSnbeams     if (eval_mode == CEED_EVAL_INTERP) {
10292b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_out[mat_start], interp_out, esize * nqpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1030a835093fSnbeams       mat_start += esize * nqpts;
1031a835093fSnbeams     } else if (eval_mode == CEED_EVAL_GRAD) {
10322b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_out[mat_start], grad_out, dim * esize * nqpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1033a835093fSnbeams       mat_start += dim * esize * nqpts;
1034a835093fSnbeams     }
1035a835093fSnbeams   }
1036a835093fSnbeams   return CEED_ERROR_SUCCESS;
1037a835093fSnbeams }
1038a835093fSnbeams 
1039a835093fSnbeams //------------------------------------------------------------------------------
1040cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1041cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1042cefa2673SJeremy L Thompson //
1043ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1044cefa2673SJeremy L Thompson // modes).
1045cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1046a835093fSnbeams //------------------------------------------------------------------------------
10472b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1048a835093fSnbeams   Ceed ceed;
10492b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1050a835093fSnbeams   CeedOperator_Hip *impl;
10512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1052a835093fSnbeams 
1053a835093fSnbeams   // Setup
1054a835093fSnbeams   if (!impl->asmb) {
10552b730f8bSJeremy L Thompson     CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op));
1056b216396cSJeremy L Thompson     assert(impl->asmb != NULL);
1057a835093fSnbeams   }
1058a835093fSnbeams 
1059a835093fSnbeams   // Assemble QFunction
1060c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf = NULL;
1061c5f45aeaSJeremy L Thompson   CeedElemRestriction rstr_q       = NULL;
10622b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
10632b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&rstr_q));
1064a835093fSnbeams   CeedScalar *values_array;
106528ec399dSJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1066a835093fSnbeams   values_array += offset;
1067a835093fSnbeams   const CeedScalar *qf_array;
10682b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &qf_array));
1069a835093fSnbeams 
1070a835093fSnbeams   // Compute B^T D B
1071b216396cSJeremy L Thompson   const CeedInt nelem         = impl->asmb->nelem;  // to satisfy clang-tidy
1072a835093fSnbeams   const CeedInt elemsPerBlock = impl->asmb->elemsPerBlock;
10732b730f8bSJeremy L Thompson   const CeedInt grid          = nelem / elemsPerBlock + ((nelem / elemsPerBlock * elemsPerBlock < nelem) ? 1 : 0);
10742b730f8bSJeremy L Thompson   void         *args[]        = {&impl->asmb->d_B_in, &impl->asmb->d_B_out, &qf_array, &values_array};
10752b730f8bSJeremy L Thompson   CeedCallBackend(
1076*eb7e6cafSJeremy L Thompson       CeedRunKernelDim_Hip(ceed, impl->asmb->linearAssemble, grid, impl->asmb->block_size_x, impl->asmb->block_size_y, elemsPerBlock, args));
1077a835093fSnbeams 
1078a835093fSnbeams   // Restore arrays
10792b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
10802b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &qf_array));
1081a835093fSnbeams 
1082a835093fSnbeams   // Cleanup
10832b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1084a835093fSnbeams 
1085a835093fSnbeams   return CEED_ERROR_SUCCESS;
1086a835093fSnbeams }
1087a835093fSnbeams 
1088a835093fSnbeams //------------------------------------------------------------------------------
10890d0321e0SJeremy L Thompson // Create operator
10900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
10910d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
10920d0321e0SJeremy L Thompson   Ceed ceed;
10932b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
10940d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
10950d0321e0SJeremy L Thompson 
10962b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
10972b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
10980d0321e0SJeremy L Thompson 
10992b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
11002b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
11012b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
11022b730f8bSJeremy L Thompson   CeedCallBackend(
11032b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
11042b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
11052b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
11062b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
11070d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11080d0321e0SJeremy L Thompson }
11090d0321e0SJeremy L Thompson 
11100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1111