xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-operator.c (revision b7453713e95c1c6eb59ce174cbcb87227e92884e)
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;
25*b7453713SJeremy L Thompson 
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
270d0321e0SJeremy L Thompson 
280d0321e0SJeremy L Thompson   // Apply data
29*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
30*b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
310d0321e0SJeremy L Thompson   }
32*b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
330d0321e0SJeremy L Thompson 
34*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
35*b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
360d0321e0SJeremy L Thompson   }
37*b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
380d0321e0SJeremy L Thompson 
39*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
40*b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
410d0321e0SJeremy L Thompson   }
42*b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
430d0321e0SJeremy L Thompson 
44b2165e7aSSebastian Grimberg   // QFunction assembly data
45*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
46*b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
470d0321e0SJeremy L Thompson   }
48*b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
490d0321e0SJeremy L Thompson 
500d0321e0SJeremy L Thompson   // Diag data
510d0321e0SJeremy L Thompson   if (impl->diag) {
520d0321e0SJeremy L Thompson     Ceed ceed;
53*b7453713SJeremy L Thompson 
542b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
552b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
56*b7453713SJeremy L Thompson     CeedCallBackend(CeedFree(&impl->diag->h_e_mode_in));
57*b7453713SJeremy L Thompson     CeedCallBackend(CeedFree(&impl->diag->h_e_mode_out));
58*b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_e_mode_in));
59*b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_e_mode_out));
602b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
61*b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_in));
62*b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_out));
63*b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_in));
64*b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_out));
65*b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
66*b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
67*b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
680d0321e0SJeremy L Thompson   }
692b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
700d0321e0SJeremy L Thompson 
71a835093fSnbeams   if (impl->asmb) {
72a835093fSnbeams     Ceed ceed;
73*b7453713SJeremy L Thompson 
742b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
752b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
762b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
772b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
78a835093fSnbeams   }
792b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
80a835093fSnbeams 
812b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
820d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
830d0321e0SJeremy L Thompson }
840d0321e0SJeremy L Thompson 
850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
860d0321e0SJeremy L Thompson // Setup infields or outfields
870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
88*b7453713SJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool is_input, CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e,
89*b7453713SJeremy L Thompson                                        CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
900d0321e0SJeremy L Thompson   Ceed                ceed;
91*b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
92*b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
930d0321e0SJeremy L Thompson 
94*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
95*b7453713SJeremy L Thompson   if (is_input) {
96*b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
97*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
980d0321e0SJeremy L Thompson   } else {
99*b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
100*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1010d0321e0SJeremy L Thompson   }
1020d0321e0SJeremy L Thompson 
1030d0321e0SJeremy L Thompson   // Loop over fields
104*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
105*b7453713SJeremy L Thompson     bool                is_strided, skip_restriction;
106*b7453713SJeremy L Thompson     CeedSize            q_size;
107*b7453713SJeremy L Thompson     CeedInt             dim, size;
108*b7453713SJeremy L Thompson     CeedEvalMode        e_mode;
109*b7453713SJeremy L Thompson     CeedVector          vec;
110*b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
111*b7453713SJeremy L Thompson     CeedBasis           basis;
1120d0321e0SJeremy L Thompson 
113*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode));
114*b7453713SJeremy L Thompson     is_strided       = false;
115*b7453713SJeremy L Thompson     skip_restriction = false;
116*b7453713SJeremy L Thompson     if (e_mode != CEED_EVAL_WEIGHT) {
117*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1180d0321e0SJeremy L Thompson 
1190d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
120*b7453713SJeremy L Thompson       // must be passive input, with e_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
1210d0321e0SJeremy L Thompson 
1220d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
123*b7453713SJeremy L Thompson       if (is_input) {
1240d0321e0SJeremy L Thompson         // Check for passive input:
125*b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
126*b7453713SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
127*b7453713SJeremy L Thompson           // Check e_mode
128*b7453713SJeremy L Thompson           if (e_mode == CEED_EVAL_NONE) {
1290d0321e0SJeremy L Thompson             // Check for strided restriction
130*b7453713SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
131*b7453713SJeremy L Thompson             if (is_strided) {
1320d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
133*b7453713SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1340d0321e0SJeremy L Thompson             }
1350d0321e0SJeremy L Thompson           }
1360d0321e0SJeremy L Thompson         }
1370d0321e0SJeremy L Thompson       }
138*b7453713SJeremy L Thompson       if (skip_restriction) {
139ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
140*b7453713SJeremy L Thompson         e_vecs[i + start_e] = NULL;
1410d0321e0SJeremy L Thompson       } else {
142*b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1430d0321e0SJeremy L Thompson       }
1440d0321e0SJeremy L Thompson     }
1450d0321e0SJeremy L Thompson 
146*b7453713SJeremy L Thompson     switch (e_mode) {
1470d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
148*b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
149*b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
150*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1510d0321e0SJeremy L Thompson         break;
1520d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
153*b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
154*b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
155*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1560d0321e0SJeremy L Thompson         break;
1570d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
158*b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
159*b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
1602b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetDimension(basis, &dim));
161*b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
162*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1630d0321e0SJeremy L Thompson         break;
1640d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
165*b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
166*b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
167*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
168*b7453713SJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
1690d0321e0SJeremy L Thompson         break;
1700d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
1710d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
1720d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
1730d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
1740d0321e0SJeremy L Thompson     }
1750d0321e0SJeremy L Thompson   }
1760d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1770d0321e0SJeremy L Thompson }
1780d0321e0SJeremy L Thompson 
1790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
180ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
1810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1820d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
1830d0321e0SJeremy L Thompson   Ceed                ceed;
184*b7453713SJeremy L Thompson   bool                is_setup_done;
185*b7453713SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
186*b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1870d0321e0SJeremy L Thompson   CeedQFunction       qf;
188*b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
189*b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
190*b7453713SJeremy L Thompson 
191*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
192*b7453713SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
193*b7453713SJeremy L Thompson 
194*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
195*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1962b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1972b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
198*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
199*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
200*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2010d0321e0SJeremy L Thompson 
2020d0321e0SJeremy L Thompson   // Allocate
203*b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
2040d0321e0SJeremy L Thompson 
205*b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
206*b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
2070d0321e0SJeremy L Thompson 
208*b7453713SJeremy L Thompson   impl->num_inputs  = num_input_fields;
209*b7453713SJeremy L Thompson   impl->num_outputs = num_output_fields;
2100d0321e0SJeremy L Thompson 
211*b7453713SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
2120d0321e0SJeremy L Thompson   // Infields
213*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2140d0321e0SJeremy L Thompson 
2150d0321e0SJeremy L Thompson   // Outfields
216*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem));
2170d0321e0SJeremy L Thompson 
2182b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2190d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2200d0321e0SJeremy L Thompson }
2210d0321e0SJeremy L Thompson 
2220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2230d0321e0SJeremy L Thompson // Setup Operator Inputs
2240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
225*b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
226*b7453713SJeremy L Thompson                                               CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
227*b7453713SJeremy L Thompson                                               CeedOperator_Hip *impl, CeedRequest *request) {
228*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
229*b7453713SJeremy L Thompson     CeedEvalMode        e_mode;
2300d0321e0SJeremy L Thompson     CeedVector          vec;
231*b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2320d0321e0SJeremy L Thompson 
2330d0321e0SJeremy L Thompson     // Get input vector
234*b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2350d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
236*b7453713SJeremy L Thompson       if (skip_active) continue;
237*b7453713SJeremy L Thompson       else vec = in_vec;
2380d0321e0SJeremy L Thompson     }
2390d0321e0SJeremy L Thompson 
240*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &e_mode));
241*b7453713SJeremy L Thompson     if (e_mode == CEED_EVAL_WEIGHT) {  // Skip
2420d0321e0SJeremy L Thompson     } else {
2430d0321e0SJeremy L Thompson       // Get input vector
244*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2450d0321e0SJeremy L Thompson       // Get input element restriction
246*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
247*b7453713SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
2480d0321e0SJeremy L Thompson       // Restrict, if necessary
249*b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {
2500d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
251*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2520d0321e0SJeremy L Thompson       } else {
253*b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
2540d0321e0SJeremy L Thompson         // Get evec
255*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2560d0321e0SJeremy L Thompson       }
2570d0321e0SJeremy L Thompson     }
2580d0321e0SJeremy L Thompson   }
2590d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2600d0321e0SJeremy L Thompson }
2610d0321e0SJeremy L Thompson 
2620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2630d0321e0SJeremy L Thompson // Input Basis Action
2640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
265*b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
266*b7453713SJeremy L Thompson                                              CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
2672b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
268*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
269*b7453713SJeremy L Thompson     CeedInt             elem_size, size;
270*b7453713SJeremy L Thompson     CeedEvalMode        e_mode;
271*b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2720d0321e0SJeremy L Thompson     CeedBasis           basis;
2730d0321e0SJeremy L Thompson 
2740d0321e0SJeremy L Thompson     // Skip active input
275*b7453713SJeremy L Thompson     if (skip_active) {
2760d0321e0SJeremy L Thompson       CeedVector vec;
277*b7453713SJeremy L Thompson 
278*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2792b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2800d0321e0SJeremy L Thompson     }
281*b7453713SJeremy L Thompson     // Get elem_size, e_mode, size
282*b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
283*b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
284*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &e_mode));
285*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
2860d0321e0SJeremy L Thompson     // Basis action
287*b7453713SJeremy L Thompson     switch (e_mode) {
2880d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
289*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
2900d0321e0SJeremy L Thompson         break;
2910d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
292*b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
293*b7453713SJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, impl->e_vecs[i], impl->q_vecs_in[i]));
2940d0321e0SJeremy L Thompson         break;
2950d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
296*b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
297*b7453713SJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, impl->e_vecs[i], impl->q_vecs_in[i]));
2980d0321e0SJeremy L Thompson         break;
2990d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
3000d0321e0SJeremy L Thompson         break;  // No action
3010d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
3020d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
3030d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
3040d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
3050d0321e0SJeremy L Thompson     }
3060d0321e0SJeremy L Thompson   }
3070d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3080d0321e0SJeremy L Thompson }
3090d0321e0SJeremy L Thompson 
3100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3110d0321e0SJeremy L Thompson // Restore Input Vectors
3120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
313*b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
314*b7453713SJeremy L Thompson                                                 const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
315*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
316*b7453713SJeremy L Thompson     CeedEvalMode e_mode;
3170d0321e0SJeremy L Thompson     CeedVector   vec;
3180d0321e0SJeremy L Thompson     // Skip active input
319*b7453713SJeremy L Thompson     if (skip_active) {
320*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3212b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3220d0321e0SJeremy L Thompson     }
323*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &e_mode));
324*b7453713SJeremy L Thompson     if (e_mode == CEED_EVAL_WEIGHT) {  // Skip
3250d0321e0SJeremy L Thompson     } else {
326*b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
327*b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
328*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
3290d0321e0SJeremy L Thompson       } else {
330*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
3310d0321e0SJeremy L Thompson       }
3320d0321e0SJeremy L Thompson     }
3330d0321e0SJeremy L Thompson   }
3340d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3350d0321e0SJeremy L Thompson }
3360d0321e0SJeremy L Thompson 
3370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3380d0321e0SJeremy L Thompson // Apply and add to output
3390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
340*b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
341*b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
342*b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
343*b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
3440d0321e0SJeremy L Thompson   CeedQFunction       qf;
345*b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
346*b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
347*b7453713SJeremy L Thompson 
348*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3492b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3502b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
351*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
352*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
353*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
3540d0321e0SJeremy L Thompson 
3550d0321e0SJeremy L Thompson   // Setup
3562b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
3570d0321e0SJeremy L Thompson 
3580d0321e0SJeremy L Thompson   // Input Evecs and Restriction
359*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
3600d0321e0SJeremy L Thompson 
3610d0321e0SJeremy L Thompson   // Input basis apply if needed
362*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
3630d0321e0SJeremy L Thompson 
3640d0321e0SJeremy L Thompson   // Output pointers, as necessary
365*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
366*b7453713SJeremy L Thompson     CeedEvalMode e_mode;
367*b7453713SJeremy L Thompson 
368*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &e_mode));
369*b7453713SJeremy L Thompson     if (e_mode == CEED_EVAL_NONE) {
3700d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
371*b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
372*b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
3730d0321e0SJeremy L Thompson     }
3740d0321e0SJeremy L Thompson   }
3750d0321e0SJeremy L Thompson 
3760d0321e0SJeremy L Thompson   // Q function
377*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
3780d0321e0SJeremy L Thompson 
3790d0321e0SJeremy L Thompson   // Output basis apply if needed
380*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
381*b7453713SJeremy L Thompson     CeedEvalMode        e_mode;
382*b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
383*b7453713SJeremy L Thompson     CeedBasis           basis;
384*b7453713SJeremy L Thompson 
385*b7453713SJeremy L Thompson     // Get elem_size, e_mode, size
386*b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
387*b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
388*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &e_mode));
389*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
3900d0321e0SJeremy L Thompson     // Basis action
391*b7453713SJeremy L Thompson     switch (e_mode) {
3920d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
3930d0321e0SJeremy L Thompson         break;
3940d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
395*b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
396*b7453713SJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, CEED_EVAL_INTERP, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
3970d0321e0SJeremy L Thompson         break;
3980d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
399*b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
400*b7453713SJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, CEED_EVAL_GRAD, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
4010d0321e0SJeremy L Thompson         break;
4020d0321e0SJeremy L Thompson       // LCOV_EXCL_START
4030d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
4040d0321e0SJeremy L Thompson         Ceed ceed;
405*b7453713SJeremy L Thompson 
4062b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
4072b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
4080d0321e0SJeremy L Thompson         break;  // Should not occur
4090d0321e0SJeremy L Thompson       }
4100d0321e0SJeremy L Thompson       case CEED_EVAL_DIV:
4110d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
4120d0321e0SJeremy L Thompson       case CEED_EVAL_CURL:
4130d0321e0SJeremy L Thompson         break;  // TODO: Not implemented
4140d0321e0SJeremy L Thompson                 // LCOV_EXCL_STOP
4150d0321e0SJeremy L Thompson     }
4160d0321e0SJeremy L Thompson   }
4170d0321e0SJeremy L Thompson 
4180d0321e0SJeremy L Thompson   // Output restriction
419*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
420*b7453713SJeremy L Thompson     CeedEvalMode        e_mode;
421*b7453713SJeremy L Thompson     CeedVector          vec;
422*b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
423*b7453713SJeremy L Thompson 
4240d0321e0SJeremy L Thompson     // Restore evec
425*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &e_mode));
426*b7453713SJeremy L Thompson     if (e_mode == CEED_EVAL_NONE) {
427*b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4280d0321e0SJeremy L Thompson     }
4290d0321e0SJeremy L Thompson     // Get output vector
430*b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4310d0321e0SJeremy L Thompson     // Restrict
432*b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4330d0321e0SJeremy L Thompson     // Active
434*b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4350d0321e0SJeremy L Thompson 
436*b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4370d0321e0SJeremy L Thompson   }
4380d0321e0SJeremy L Thompson 
4390d0321e0SJeremy L Thompson   // Restore input arrays
440*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4410d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4420d0321e0SJeremy L Thompson }
4430d0321e0SJeremy L Thompson 
4440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4450d0321e0SJeremy L Thompson // Core code for assembling linear QFunction
4460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4472b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
4480d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
449*b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
450*b7453713SJeremy L Thompson   bool                is_identity_qf;
451e984cf9aSJeremy L Thompson   CeedSize            q_size;
452*b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
453*b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
454*b7453713SJeremy L Thompson   CeedVector         *active_in;
455*b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
456*b7453713SJeremy L Thompson   CeedQFunction       qf;
457*b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
458*b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
459*b7453713SJeremy L Thompson 
4602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
461*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
462e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
463e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
464e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
465*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
466*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
467*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
468*b7453713SJeremy L Thompson   active_in      = impl->qf_active_in;
469*b7453713SJeremy L Thompson   num_active_in  = impl->num_active_in;
470*b7453713SJeremy L Thompson   num_active_out = impl->num_active_out;
4710d0321e0SJeremy L Thompson 
4720d0321e0SJeremy L Thompson   // Setup
4732b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4740d0321e0SJeremy L Thompson 
4750d0321e0SJeremy L Thompson   // Check for identity
476*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf));
477*b7453713SJeremy L Thompson   CeedCheck(!is_identity_qf, ceed, CEED_ERROR_BACKEND, "Assembling identity QFunctions not supported");
4780d0321e0SJeremy L Thompson 
4790d0321e0SJeremy L Thompson   // Input Evecs and Restriction
480*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
4810d0321e0SJeremy L Thompson 
4820d0321e0SJeremy L Thompson   // Count number of active input fields
483*b7453713SJeremy L Thompson   if (!num_active_in) {
484*b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
485*b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
486*b7453713SJeremy L Thompson       CeedVector  vec;
487*b7453713SJeremy L Thompson 
4880d0321e0SJeremy L Thompson       // Get input vector
489*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4900d0321e0SJeremy L Thompson       // Check if active input
4910d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
492*b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
493*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
494*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
495*b7453713SJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_in));
4960d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
497*b7453713SJeremy L Thompson           q_size = (CeedSize)Q * num_elem;
498*b7453713SJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_in[num_active_in + field]));
499*b7453713SJeremy L Thompson           CeedCallBackend(
500*b7453713SJeremy L Thompson               CeedVectorSetArray(active_in[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
5010d0321e0SJeremy L Thompson         }
502*b7453713SJeremy L Thompson         num_active_in += size;
503*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
5040d0321e0SJeremy L Thompson       }
5050d0321e0SJeremy L Thompson     }
506*b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
507*b7453713SJeremy L Thompson     impl->qf_active_in  = active_in;
5080d0321e0SJeremy L Thompson   }
5090d0321e0SJeremy L Thompson 
5100d0321e0SJeremy L Thompson   // Count number of active output fields
511*b7453713SJeremy L Thompson   if (!num_active_out) {
512*b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
513*b7453713SJeremy L Thompson       CeedVector vec;
514*b7453713SJeremy L Thompson 
5150d0321e0SJeremy L Thompson       // Get output vector
516*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5170d0321e0SJeremy L Thompson       // Check if active output
5180d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
519*b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
520*b7453713SJeremy L Thompson         num_active_out += size;
5210d0321e0SJeremy L Thompson       }
5220d0321e0SJeremy L Thompson     }
523*b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
5240d0321e0SJeremy L Thompson   }
5250d0321e0SJeremy L Thompson 
5260d0321e0SJeremy L Thompson   // Check sizes
527*b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5280d0321e0SJeremy L Thompson 
5290d0321e0SJeremy L Thompson   // Build objects if needed
5300d0321e0SJeremy L Thompson   if (build_objects) {
5310d0321e0SJeremy L Thompson     // Create output restriction
532*b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
533*b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
534*b7453713SJeremy L Thompson 
535*b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
536*b7453713SJeremy L Thompson                                                      num_active_in * num_active_out * num_elem * Q, strides, rstr));
5370d0321e0SJeremy L Thompson     // Create assembled vector
538*b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
5390d0321e0SJeremy L Thompson   }
5402b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
541*b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
5420d0321e0SJeremy L Thompson 
5430d0321e0SJeremy L Thompson   // Input basis apply
544*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
5450d0321e0SJeremy L Thompson 
5460d0321e0SJeremy L Thompson   // Assemble QFunction
547*b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
5480d0321e0SJeremy L Thompson     // Set Inputs
549*b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(active_in[in], 1.0));
550*b7453713SJeremy L Thompson     if (num_active_in > 1) {
551*b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_in[(in + num_active_in - 1) % num_active_in], 0.0));
5520d0321e0SJeremy L Thompson     }
5530d0321e0SJeremy L Thompson     // Set Outputs
554*b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
555*b7453713SJeremy L Thompson       CeedVector vec;
556*b7453713SJeremy L Thompson 
5570d0321e0SJeremy L Thompson       // Get output vector
558*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5590d0321e0SJeremy L Thompson       // Check if active output
5600d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
561*b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
562*b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
563*b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
5640d0321e0SJeremy L Thompson       }
5650d0321e0SJeremy L Thompson     }
5660d0321e0SJeremy L Thompson     // Apply QFunction
567*b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
5680d0321e0SJeremy L Thompson   }
5690d0321e0SJeremy L Thompson 
5700d0321e0SJeremy L Thompson   // Un-set output Qvecs to prevent accidental overwrite of Assembled
571*b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
572*b7453713SJeremy L Thompson     CeedVector vec;
573*b7453713SJeremy L Thompson 
5740d0321e0SJeremy L Thompson     // Get output vector
575*b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5760d0321e0SJeremy L Thompson     // Check if active output
5770d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
578*b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
5790d0321e0SJeremy L Thompson     }
5800d0321e0SJeremy L Thompson   }
5810d0321e0SJeremy L Thompson 
5820d0321e0SJeremy L Thompson   // Restore input arrays
583*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
5840d0321e0SJeremy L Thompson 
5850d0321e0SJeremy L Thompson   // Restore output
586*b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
5870d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5880d0321e0SJeremy L Thompson }
5890d0321e0SJeremy L Thompson 
5900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5910d0321e0SJeremy L Thompson // Assemble Linear QFunction
5920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5932b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
5942b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
5950d0321e0SJeremy L Thompson }
5960d0321e0SJeremy L Thompson 
5970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
598b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
5990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6002b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
6012b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
6020d0321e0SJeremy L Thompson }
6030d0321e0SJeremy L Thompson 
6040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6050d0321e0SJeremy L Thompson // Create point block restriction
6060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
607*b7453713SJeremy L Thompson static int CreatePBRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) {
6080d0321e0SJeremy L Thompson   Ceed           ceed;
609*b7453713SJeremy L Thompson   CeedSize       l_size;
610*b7453713SJeremy L Thompson   CeedInt        num_elem, num_comp, elem_size, comp_stride, *point_block_offsets;
6110d0321e0SJeremy L Thompson   const CeedInt *offsets;
612*b7453713SJeremy L Thompson 
613*b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
6142b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
6150d0321e0SJeremy L Thompson 
6160d0321e0SJeremy L Thompson   // Expand offsets
617*b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
618*b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
619*b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
620*b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
6212b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
622*b7453713SJeremy L Thompson   CeedInt shift = num_comp;
623*b7453713SJeremy L Thompson 
624*b7453713SJeremy L Thompson   if (comp_stride != 1) shift *= num_comp;
625*b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_elem * elem_size, &point_block_offsets));
626*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_elem * elem_size; i++) point_block_offsets[i] = offsets[i] * shift;
6270d0321e0SJeremy L Thompson 
6280d0321e0SJeremy L Thompson   // Create new restriction
629*b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
630*b7453713SJeremy L Thompson                                             point_block_offsets, point_block_rstr));
6310d0321e0SJeremy L Thompson 
6320d0321e0SJeremy L Thompson   // Cleanup
6332b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
6340d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6350d0321e0SJeremy L Thompson }
6360d0321e0SJeremy L Thompson 
6370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6380d0321e0SJeremy L Thompson // Assemble diagonal setup
6390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
640*b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op, const bool is_point_block, CeedInt use_ceedsize_idx) {
6410d0321e0SJeremy L Thompson   Ceed                ceed;
642*b7453713SJeremy L Thompson   char               *diagonal_kernel_path, *diagonal_kernel_source;
643*b7453713SJeremy L Thompson   CeedInt             num_input_fields, num_output_fields, num_e_mode_in = 0, num_comp = 0, dim = 1, num_e_mode_out = 0;
644*b7453713SJeremy L Thompson   CeedEvalMode       *e_mode_in = NULL, *e_mode_out = NULL;
645*b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
646*b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
647*b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
6480d0321e0SJeremy L Thompson   CeedQFunction       qf;
649*b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
650*b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
651*b7453713SJeremy L Thompson 
652*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
6532b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
654*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
6550d0321e0SJeremy L Thompson 
6560d0321e0SJeremy L Thompson   // Determine active input basis
657*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
658*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
659*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
6600d0321e0SJeremy L Thompson     CeedVector vec;
661*b7453713SJeremy L Thompson 
662*b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
6630d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
664*b7453713SJeremy L Thompson       CeedEvalMode        e_mode;
6650d0321e0SJeremy L Thompson       CeedElemRestriction rstr;
666*b7453713SJeremy L Thompson 
667*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
668*b7453713SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
669*b7453713SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis_in, &dim));
670*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
671*b7453713SJeremy L Thompson       CeedCheck(!rstr_in || rstr_in == rstr, ceed, CEED_ERROR_BACKEND,
6726574a04fSJeremy L Thompson                 "Backend does not implement multi-field non-composite operator diagonal assembly");
673*b7453713SJeremy L Thompson       rstr_in = rstr;
674*b7453713SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode));
675*b7453713SJeremy L Thompson       switch (e_mode) {
6760d0321e0SJeremy L Thompson         case CEED_EVAL_NONE:
6770d0321e0SJeremy L Thompson         case CEED_EVAL_INTERP:
678*b7453713SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_in + 1, &e_mode_in));
679*b7453713SJeremy L Thompson           e_mode_in[num_e_mode_in] = e_mode;
680*b7453713SJeremy L Thompson           num_e_mode_in += 1;
6810d0321e0SJeremy L Thompson           break;
6820d0321e0SJeremy L Thompson         case CEED_EVAL_GRAD:
683*b7453713SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_in + dim, &e_mode_in));
684*b7453713SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) e_mode_in[num_e_mode_in + d] = e_mode;
685*b7453713SJeremy L Thompson           num_e_mode_in += dim;
6860d0321e0SJeremy L Thompson           break;
6870d0321e0SJeremy L Thompson         case CEED_EVAL_WEIGHT:
6880d0321e0SJeremy L Thompson         case CEED_EVAL_DIV:
6890d0321e0SJeremy L Thompson         case CEED_EVAL_CURL:
6900d0321e0SJeremy L Thompson           break;  // Caught by QF Assembly
6910d0321e0SJeremy L Thompson       }
6920d0321e0SJeremy L Thompson     }
6930d0321e0SJeremy L Thompson   }
6940d0321e0SJeremy L Thompson 
6950d0321e0SJeremy L Thompson   // Determine active output basis
696*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
697*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
698*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
6990d0321e0SJeremy L Thompson     CeedVector vec;
700*b7453713SJeremy L Thompson 
701*b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
7020d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
703*b7453713SJeremy L Thompson       CeedEvalMode        e_mode;
7040d0321e0SJeremy L Thompson       CeedElemRestriction rstr;
705*b7453713SJeremy L Thompson 
706*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
707*b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
708*b7453713SJeremy L Thompson       CeedCheck(!rstr_out || rstr_out == rstr, ceed, CEED_ERROR_BACKEND,
7096574a04fSJeremy L Thompson                 "Backend does not implement multi-field non-composite operator diagonal assembly");
710*b7453713SJeremy L Thompson       rstr_out = rstr;
711*b7453713SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode));
712*b7453713SJeremy L Thompson       switch (e_mode) {
7130d0321e0SJeremy L Thompson         case CEED_EVAL_NONE:
7140d0321e0SJeremy L Thompson         case CEED_EVAL_INTERP:
715*b7453713SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_out + 1, &e_mode_out));
716*b7453713SJeremy L Thompson           e_mode_out[num_e_mode_out] = e_mode;
717*b7453713SJeremy L Thompson           num_e_mode_out += 1;
7180d0321e0SJeremy L Thompson           break;
7190d0321e0SJeremy L Thompson         case CEED_EVAL_GRAD:
720*b7453713SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_out + dim, &e_mode_out));
721*b7453713SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) e_mode_out[num_e_mode_out + d] = e_mode;
722*b7453713SJeremy L Thompson           num_e_mode_out += dim;
7230d0321e0SJeremy L Thompson           break;
7240d0321e0SJeremy L Thompson         case CEED_EVAL_WEIGHT:
7250d0321e0SJeremy L Thompson         case CEED_EVAL_DIV:
7260d0321e0SJeremy L Thompson         case CEED_EVAL_CURL:
7270d0321e0SJeremy L Thompson           break;  // Caught by QF Assembly
7280d0321e0SJeremy L Thompson       }
7290d0321e0SJeremy L Thompson     }
7300d0321e0SJeremy L Thompson   }
7310d0321e0SJeremy L Thompson 
7320d0321e0SJeremy L Thompson   // Operator data struct
7332b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
7342b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
7350d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
736*b7453713SJeremy L Thompson 
737*b7453713SJeremy L Thompson   diag->basis_in       = basis_in;
738*b7453713SJeremy L Thompson   diag->basis_out      = basis_out;
739*b7453713SJeremy L Thompson   diag->h_e_mode_in    = e_mode_in;
740*b7453713SJeremy L Thompson   diag->h_e_mode_out   = e_mode_out;
741*b7453713SJeremy L Thompson   diag->num_e_mode_in  = num_e_mode_in;
742*b7453713SJeremy L Thompson   diag->num_e_mode_out = num_e_mode_out;
7430d0321e0SJeremy L Thompson 
7440d0321e0SJeremy L Thompson   // Assemble kernel
7452b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
74623d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
7472b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
74823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
749*b7453713SJeremy L Thompson   CeedInt num_modes, num_qpts;
750*b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_modes));
751*b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
752*b7453713SJeremy L Thompson   diag->num_modes = num_modes;
753*b7453713SJeremy L Thompson   CeedCallBackend(CeedCompile_Hip(ceed, diagonal_kernel_source, &diag->module, 6, "NUMEMODEIN", num_e_mode_in, "NUMEMODEOUT", num_e_mode_out,
754*b7453713SJeremy L Thompson                                   "NNODES", num_modes, "NQPTS", num_qpts, "NCOMP", num_comp, "CEEDSIZE", use_ceedsize_idx));
755eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, diag->module, "linearDiagonal", &diag->linearDiagonal));
756eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, diag->module, "linearPointBlockDiagonal", &diag->linearPointBlock));
7572b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&diagonal_kernel_path));
7582b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&diagonal_kernel_source));
7590d0321e0SJeremy L Thompson 
7600d0321e0SJeremy L Thompson   // Basis matrices
761*b7453713SJeremy L Thompson   const CeedInt     q_bytes      = num_qpts * sizeof(CeedScalar);
762*b7453713SJeremy L Thompson   const CeedInt     interp_bytes = q_bytes * num_modes;
763*b7453713SJeremy L Thompson   const CeedInt     grad_bytes   = q_bytes * num_modes * dim;
764*b7453713SJeremy L Thompson   const CeedInt     e_mode_bytes = sizeof(CeedEvalMode);
765*b7453713SJeremy L Thompson   const CeedScalar *interp_in, *interp_out, *grad_in, *grad_out;
7660d0321e0SJeremy L Thompson 
7670d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
7680d0321e0SJeremy L Thompson   CeedScalar *identity     = NULL;
769*b7453713SJeremy L Thompson   bool        is_eval_none = false;
770*b7453713SJeremy L Thompson 
771*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_e_mode_in; i++) is_eval_none = is_eval_none || (e_mode_in[i] == CEED_EVAL_NONE);
772*b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_e_mode_out; i++) is_eval_none = is_eval_none || (e_mode_out[i] == CEED_EVAL_NONE);
773*b7453713SJeremy L Thompson   if (is_eval_none) {
774*b7453713SJeremy L Thompson     CeedCallBackend(CeedCalloc(num_qpts * num_modes, &identity));
775*b7453713SJeremy L Thompson     for (CeedInt i = 0; i < (num_modes < num_qpts ? num_modes : num_qpts); i++) identity[i * num_modes + i] = 1.0;
776*b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
777*b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
7780d0321e0SJeremy L Thompson   }
7790d0321e0SJeremy L Thompson 
7800d0321e0SJeremy L Thompson   // CEED_EVAL_INTERP
781*b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in));
782*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_interp_in, interp_bytes));
783*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_interp_in, interp_in, interp_bytes, hipMemcpyHostToDevice));
784*b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out));
785*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_interp_out, interp_bytes));
786*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_interp_out, interp_out, interp_bytes, hipMemcpyHostToDevice));
7870d0321e0SJeremy L Thompson 
7880d0321e0SJeremy L Thompson   // CEED_EVAL_GRAD
789*b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in));
790*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_grad_in, grad_bytes));
791*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_grad_in, grad_in, grad_bytes, hipMemcpyHostToDevice));
792*b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out));
793*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_grad_out, grad_bytes));
794*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_grad_out, grad_out, grad_bytes, hipMemcpyHostToDevice));
7950d0321e0SJeremy L Thompson 
796*b7453713SJeremy L Thompson   // Arrays of e_modes
797*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_e_mode_in, num_e_mode_in * e_mode_bytes));
798*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_e_mode_in, e_mode_in, num_e_mode_in * e_mode_bytes, hipMemcpyHostToDevice));
799*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&diag->d_e_mode_out, num_e_mode_out * e_mode_bytes));
800*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(diag->d_e_mode_out, e_mode_out, num_e_mode_out * e_mode_bytes, hipMemcpyHostToDevice));
8010d0321e0SJeremy L Thompson 
8020d0321e0SJeremy L Thompson   // Restriction
803*b7453713SJeremy L Thompson   diag->diag_rstr = rstr_out;
8040d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8050d0321e0SJeremy L Thompson }
8060d0321e0SJeremy L Thompson 
8070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8080d0321e0SJeremy L Thompson // Assemble diagonal common code
8090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
810*b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
8110d0321e0SJeremy L Thompson   Ceed                ceed;
812*b7453713SJeremy L Thompson   CeedSize            assembled_length = 0, assembled_qf_length = 0;
813*b7453713SJeremy L Thompson   CeedInt             use_ceedsize_idx = 0, num_elem;
814*b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
815*b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
816*b7453713SJeremy L Thompson   CeedVector          assembled_qf = NULL;
817*b7453713SJeremy L Thompson   CeedElemRestriction rstr         = NULL;
8180d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
819*b7453713SJeremy L Thompson 
820*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8212b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
8220d0321e0SJeremy L Thompson 
8230d0321e0SJeremy L Thompson   // Assemble QFunction
824*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr, request));
8252b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&rstr));
8260d0321e0SJeremy L Thompson 
8279330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
828*b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
829*b7453713SJeremy L Thompson   if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
8309330daecSnbeams 
8310d0321e0SJeremy L Thompson   // Setup
832*b7453713SJeremy L Thompson   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op, is_point_block, use_ceedsize_idx));
8330d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
8340d0321e0SJeremy L Thompson   assert(diag != NULL);
8350d0321e0SJeremy L Thompson 
8360d0321e0SJeremy L Thompson   // Restriction
837*b7453713SJeremy L Thompson   if (is_point_block && !diag->point_block_diag_rstr) {
838*b7453713SJeremy L Thompson     CeedElemRestriction point_block_diag_rstr;
839*b7453713SJeremy L Thompson 
840*b7453713SJeremy L Thompson     CeedCallBackend(CreatePBRestriction(diag->diag_rstr, &point_block_diag_rstr));
841*b7453713SJeremy L Thompson     diag->point_block_diag_rstr = point_block_diag_rstr;
8420d0321e0SJeremy L Thompson   }
843*b7453713SJeremy L Thompson   CeedElemRestriction diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
8440d0321e0SJeremy L Thompson 
8450d0321e0SJeremy L Thompson   // Create diagonal vector
846*b7453713SJeremy L Thompson   CeedVector elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
847*b7453713SJeremy L Thompson 
848*b7453713SJeremy L Thompson   if (!elem_diag) {
8490d0321e0SJeremy L Thompson     // Element diagonal vector
850*b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(diag_rstr, NULL, &elem_diag));
851*b7453713SJeremy L Thompson     if (is_point_block) diag->point_block_elem_diag = elem_diag;
852*b7453713SJeremy L Thompson     else diag->elem_diag = elem_diag;
8530d0321e0SJeremy L Thompson   }
854*b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
8550d0321e0SJeremy L Thompson 
8560d0321e0SJeremy L Thompson   // Assemble element operator diagonals
857*b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
858*b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
859*b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
8600d0321e0SJeremy L Thompson 
8610d0321e0SJeremy L Thompson   // Compute the diagonal of B^T D B
862*b7453713SJeremy L Thompson   int   elem_per_block = 1;
863*b7453713SJeremy L Thompson   int   grid           = num_elem / elem_per_block + ((num_elem / elem_per_block * elem_per_block < num_elem) ? 1 : 0);
864*b7453713SJeremy L Thompson   void *args[]         = {(void *)&num_elem, &diag->d_identity,  &diag->d_interp_in,  &diag->d_grad_in,    &diag->d_interp_out,
865*b7453713SJeremy L Thompson                           &diag->d_grad_out, &diag->d_e_mode_in, &diag->d_e_mode_out, &assembled_qf_array, &elem_diag_array};
866*b7453713SJeremy L Thompson 
867*b7453713SJeremy L Thompson   if (is_point_block) {
868*b7453713SJeremy L Thompson     CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->linearPointBlock, grid, diag->num_modes, 1, elem_per_block, args));
8690d0321e0SJeremy L Thompson   } else {
870*b7453713SJeremy L Thompson     CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->linearDiagonal, grid, diag->num_modes, 1, elem_per_block, args));
8710d0321e0SJeremy L Thompson   }
8720d0321e0SJeremy L Thompson 
8730d0321e0SJeremy L Thompson   // Restore arrays
874*b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
875*b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
8760d0321e0SJeremy L Thompson 
8770d0321e0SJeremy L Thompson   // Assemble local operator diagonal
878*b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
8790d0321e0SJeremy L Thompson 
8800d0321e0SJeremy L Thompson   // Cleanup
881*b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
8820d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8830d0321e0SJeremy L Thompson }
8840d0321e0SJeremy L Thompson 
8850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8860d0321e0SJeremy L Thompson // Assemble Linear Diagonal
8870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8882b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
8892b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
8906aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8910d0321e0SJeremy L Thompson }
8920d0321e0SJeremy L Thompson 
8930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8940d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
8950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8962b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
8972b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
8986aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8990d0321e0SJeremy L Thompson }
9000d0321e0SJeremy L Thompson 
901a835093fSnbeams //------------------------------------------------------------------------------
902a835093fSnbeams // Single operator assembly setup
903a835093fSnbeams //------------------------------------------------------------------------------
9049330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
905a835093fSnbeams   Ceed    ceed;
906*b7453713SJeremy L Thompson   CeedInt num_input_fields, num_output_fields, num_e_mode_in = 0, dim = 1, num_B_in_mats_to_load = 0, size_B_in = 0, num_qpts = 0, elem_size = 0,
907*b7453713SJeremy L Thompson                                                num_e_mode_out = 0, num_B_out_mats_to_load = 0, size_B_out = 0, num_elem, num_comp;
908*b7453713SJeremy L Thompson   CeedEvalMode       *eval_mode_in = NULL, *eval_mode_out = NULL;
909*b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
910*b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
911*b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
912*b7453713SJeremy L Thompson   CeedQFunction       qf;
913*b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
914a835093fSnbeams   CeedOperator_Hip   *impl;
915*b7453713SJeremy L Thompson 
916*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
9172b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
918a835093fSnbeams 
919a835093fSnbeams   // Get intput and output fields
9202b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
921a835093fSnbeams 
922a835093fSnbeams   // Determine active input basis eval mode
9232b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
9242b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
925a835093fSnbeams   // Note that the kernel will treat each dimension of a gradient action separately;
926*b7453713SJeremy L Thompson   // i.e., when an active input has a CEED_EVAL_GRAD mode, num_e_mode_in will increment by dim.
927ea61e9acSJeremy 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
928a835093fSnbeams   // num_B_in_mats_to_load will be incremented by 1.
929a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
930a835093fSnbeams     CeedVector vec;
931*b7453713SJeremy L Thompson 
9322b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
933a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
934*b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
935*b7453713SJeremy L Thompson 
9362b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis_in));
9372b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis_in, &dim));
938*b7453713SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
9392b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
940*b7453713SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size));
9412b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
942a835093fSnbeams       if (eval_mode != CEED_EVAL_NONE) {
9432b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_B_in_mats_to_load + 1, &eval_mode_in));
944a835093fSnbeams         eval_mode_in[num_B_in_mats_to_load] = eval_mode;
945a835093fSnbeams         num_B_in_mats_to_load += 1;
946a835093fSnbeams         if (eval_mode == CEED_EVAL_GRAD) {
947*b7453713SJeremy L Thompson           num_e_mode_in += dim;
948*b7453713SJeremy L Thompson           size_B_in += dim * elem_size * num_qpts;
949a835093fSnbeams         } else {
950*b7453713SJeremy L Thompson           num_e_mode_in += 1;
951*b7453713SJeremy L Thompson           size_B_in += elem_size * num_qpts;
952a835093fSnbeams         }
953a835093fSnbeams       }
954a835093fSnbeams     }
955a835093fSnbeams   }
956a835093fSnbeams 
957a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
9582b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
959a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
960a835093fSnbeams     CeedVector vec;
961*b7453713SJeremy L Thompson 
9622b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
963a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
964*b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
965*b7453713SJeremy L Thompson 
9662b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis_out));
9672b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
9686574a04fSJeremy L Thompson       CeedCheck(!rstr_out || rstr_out == rstr_in, ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator assembly");
9692b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
970a835093fSnbeams       if (eval_mode != CEED_EVAL_NONE) {
9712b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_B_out_mats_to_load + 1, &eval_mode_out));
972a835093fSnbeams         eval_mode_out[num_B_out_mats_to_load] = eval_mode;
973a835093fSnbeams         num_B_out_mats_to_load += 1;
974a835093fSnbeams         if (eval_mode == CEED_EVAL_GRAD) {
975*b7453713SJeremy L Thompson           num_e_mode_out += dim;
976*b7453713SJeremy L Thompson           size_B_out += dim * elem_size * num_qpts;
977a835093fSnbeams         } else {
978*b7453713SJeremy L Thompson           num_e_mode_out += 1;
979*b7453713SJeremy L Thompson           size_B_out += elem_size * num_qpts;
980a835093fSnbeams         }
981a835093fSnbeams       }
982a835093fSnbeams     }
983a835093fSnbeams   }
984a835093fSnbeams 
985*b7453713SJeremy L Thompson   CeedCheck(num_e_mode_in > 0 && num_e_mode_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
986a835093fSnbeams 
987*b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem));
988*b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp));
989a835093fSnbeams 
9902b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
991a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
992*b7453713SJeremy L Thompson   asmb->num_elem                 = num_elem;
993a835093fSnbeams 
994a835093fSnbeams   // Compile kernels
995*b7453713SJeremy L Thompson   int elem_per_block   = 1;
996*b7453713SJeremy L Thompson   asmb->elem_per_block = elem_per_block;
997*b7453713SJeremy L Thompson   CeedInt block_size   = elem_size * elem_size * elem_per_block;
99807b31e0eSJeremy L Thompson   char   *assembly_kernel_path, *assembly_kernel_source;
9992b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
100023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
10012b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
100223d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
100307b31e0eSJeremy L Thompson   bool fallback = block_size > 1024;
100407b31e0eSJeremy L Thompson   if (fallback) {  // Use fallback kernel with 1D threadblock
1005*b7453713SJeremy L Thompson     block_size         = elem_size * elem_per_block;
1006*b7453713SJeremy L Thompson     asmb->block_size_x = elem_size;
100759ad764aSnbeams     asmb->block_size_y = 1;
100859ad764aSnbeams   } else {  // Use kernel with 2D threadblock
1009*b7453713SJeremy L Thompson     asmb->block_size_x = elem_size;
1010*b7453713SJeremy L Thompson     asmb->block_size_y = elem_size;
101107b31e0eSJeremy L Thompson   }
1012*b7453713SJeremy L Thompson   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 8, "NELEM", num_elem, "NUMEMODEIN", num_e_mode_in, "NUMEMODEOUT",
1013*b7453713SJeremy L Thompson                                   num_e_mode_out, "NQPTS", num_qpts, "NNODES", elem_size, "BLOCK_SIZE", block_size, "NCOMP", num_comp, "CEEDSIZE",
10149330daecSnbeams                                   use_ceedsize_idx));
1015eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, fallback ? "linearAssembleFallback" : "linearAssemble", &asmb->linearAssemble));
10162b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
10172b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1018a835093fSnbeams 
1019a835093fSnbeams   // Build 'full' B matrices (not 1D arrays used for tensor-product matrices)
1020a835093fSnbeams   const CeedScalar *interp_in, *grad_in;
10212b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in));
10222b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in));
1023a835093fSnbeams 
1024a835093fSnbeams   // Load into B_in, in order that they will be used in eval_mode
1025*b7453713SJeremy L Thompson   const CeedInt in_bytes  = size_B_in * sizeof(CeedScalar);
1026a835093fSnbeams   CeedInt       mat_start = 0;
1027*b7453713SJeremy L Thompson 
1028*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1029a835093fSnbeams   for (int i = 0; i < num_B_in_mats_to_load; i++) {
1030a835093fSnbeams     CeedEvalMode eval_mode = eval_mode_in[i];
1031a835093fSnbeams     if (eval_mode == CEED_EVAL_INTERP) {
1032*b7453713SJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_in[mat_start], interp_in, elem_size * num_qpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1033*b7453713SJeremy L Thompson       mat_start += elem_size * num_qpts;
1034a835093fSnbeams     } else if (eval_mode == CEED_EVAL_GRAD) {
1035*b7453713SJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_in[mat_start], grad_in, dim * elem_size * num_qpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1036*b7453713SJeremy L Thompson       mat_start += dim * elem_size * num_qpts;
1037a835093fSnbeams     }
1038a835093fSnbeams   }
1039a835093fSnbeams 
1040a835093fSnbeams   const CeedScalar *interp_out, *grad_out;
1041*b7453713SJeremy L Thompson 
1042*b7453713SJeremy L Thompson   // Note that this function currently assumes 1 basis, so this should always be true for now
1043a835093fSnbeams   if (basis_out == basis_in) {
1044a835093fSnbeams     interp_out = interp_in;
1045a835093fSnbeams     grad_out   = grad_in;
1046a835093fSnbeams   } else {
10472b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out));
10482b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out));
1049a835093fSnbeams   }
1050a835093fSnbeams 
1051a835093fSnbeams   // Load into B_out, in order that they will be used in eval_mode
1052*b7453713SJeremy L Thompson   const CeedInt out_bytes = size_B_out * sizeof(CeedScalar);
1053*b7453713SJeremy L Thompson 
1054a835093fSnbeams   mat_start = 0;
1055*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1056a835093fSnbeams   for (int i = 0; i < num_B_out_mats_to_load; i++) {
1057a835093fSnbeams     CeedEvalMode eval_mode = eval_mode_out[i];
1058a835093fSnbeams     if (eval_mode == CEED_EVAL_INTERP) {
1059*b7453713SJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_out[mat_start], interp_out, elem_size * num_qpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1060*b7453713SJeremy L Thompson       mat_start += elem_size * num_qpts;
1061a835093fSnbeams     } else if (eval_mode == CEED_EVAL_GRAD) {
1062*b7453713SJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_out[mat_start], grad_out, dim * elem_size * num_qpts * sizeof(CeedScalar), hipMemcpyHostToDevice));
1063*b7453713SJeremy L Thompson       mat_start += dim * elem_size * num_qpts;
1064a835093fSnbeams     }
1065a835093fSnbeams   }
1066a835093fSnbeams   return CEED_ERROR_SUCCESS;
1067a835093fSnbeams }
1068a835093fSnbeams 
1069a835093fSnbeams //------------------------------------------------------------------------------
1070cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1071cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1072cefa2673SJeremy L Thompson //
1073ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1074cefa2673SJeremy L Thompson // modes).
1075cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1076a835093fSnbeams //------------------------------------------------------------------------------
10772b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1078a835093fSnbeams   Ceed                ceed;
1079*b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1080*b7453713SJeremy L Thompson   CeedInt             use_ceedsize_idx = 0;
1081*b7453713SJeremy L Thompson   CeedScalar         *values_array;
1082*b7453713SJeremy L Thompson   const CeedScalar   *qf_array;
1083*b7453713SJeremy L Thompson   CeedVector          assembled_qf = NULL;
1084*b7453713SJeremy L Thompson   CeedElemRestriction rstr_q       = NULL;
1085a835093fSnbeams   CeedOperator_Hip   *impl;
1086*b7453713SJeremy L Thompson 
1087*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
10882b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1089a835093fSnbeams 
1090a835093fSnbeams   // Assemble QFunction
10912b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
10922b730f8bSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&rstr_q));
109328ec399dSJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1094a835093fSnbeams   values_array += offset;
10952b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &qf_array));
1096a835093fSnbeams 
10979330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
10989330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
10999330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
11009330daecSnbeams   // Setup
11019330daecSnbeams   if (!impl->asmb) {
11029330daecSnbeams     CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
11039330daecSnbeams     assert(impl->asmb != NULL);
11049330daecSnbeams   }
11059330daecSnbeams 
1106a835093fSnbeams   // Compute B^T D B
1107*b7453713SJeremy L Thompson   const CeedInt num_elem       = impl->asmb->num_elem;
1108*b7453713SJeremy L Thompson   const CeedInt elem_per_block = impl->asmb->elem_per_block;
1109*b7453713SJeremy L Thompson   const CeedInt grid           = num_elem / elem_per_block + ((num_elem / elem_per_block * elem_per_block < num_elem) ? 1 : 0);
11102b730f8bSJeremy L Thompson   void         *args[]         = {&impl->asmb->d_B_in, &impl->asmb->d_B_out, &qf_array, &values_array};
1111*b7453713SJeremy L Thompson 
11122b730f8bSJeremy L Thompson   CeedCallBackend(
1113*b7453713SJeremy L Thompson       CeedRunKernelDim_Hip(ceed, impl->asmb->linearAssemble, grid, impl->asmb->block_size_x, impl->asmb->block_size_y, elem_per_block, args));
1114a835093fSnbeams 
1115a835093fSnbeams   // Restore arrays
11162b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
11172b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &qf_array));
1118a835093fSnbeams 
1119a835093fSnbeams   // Cleanup
11202b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1121a835093fSnbeams   return CEED_ERROR_SUCCESS;
1122a835093fSnbeams }
1123a835093fSnbeams 
1124a835093fSnbeams //------------------------------------------------------------------------------
11250d0321e0SJeremy L Thompson // Create operator
11260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11270d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
11280d0321e0SJeremy L Thompson   Ceed              ceed;
11290d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
11300d0321e0SJeremy L Thompson 
1131*b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11322b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
11332b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
11342b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
11352b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
11362b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
11372b730f8bSJeremy L Thompson   CeedCallBackend(
11382b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
11392b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
11402b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
11412b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
11420d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11430d0321e0SJeremy L Thompson }
11440d0321e0SJeremy L Thompson 
11450d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1146