xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-operator.c (revision 0a5597cece6e3a62854055430a3012108295aea9)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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;
25b7453713SJeremy L Thompson 
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
270d0321e0SJeremy L Thompson 
280d0321e0SJeremy L Thompson   // Apply data
29b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
30b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
310d0321e0SJeremy L Thompson   }
32b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
33c1222711SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
340d0321e0SJeremy L Thompson 
35b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
36b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
370d0321e0SJeremy L Thompson   }
38b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
390d0321e0SJeremy L Thompson 
40b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
41b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
420d0321e0SJeremy L Thompson   }
43b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
4467d9480aSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem));
450d0321e0SJeremy L Thompson 
46b2165e7aSSebastian Grimberg   // QFunction assembly data
47b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
48b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
490d0321e0SJeremy L Thompson   }
50b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
510d0321e0SJeremy L Thompson 
520d0321e0SJeremy L Thompson   // Diag data
530d0321e0SJeremy L Thompson   if (impl->diag) {
540d0321e0SJeremy L Thompson     Ceed ceed;
55b7453713SJeremy L Thompson 
562b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
57cbfe683aSSebastian Grimberg     if (impl->diag->module) {
582b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
59cbfe683aSSebastian Grimberg     }
60cbfe683aSSebastian Grimberg     if (impl->diag->module_point_block) {
61cbfe683aSSebastian Grimberg       CeedCallHip(ceed, hipModuleUnload(impl->diag->module_point_block));
62cbfe683aSSebastian Grimberg     }
63004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_in));
64004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_out));
652b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
66b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_in));
67b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_out));
68b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_in));
69b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_out));
70004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_in));
71004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_out));
72004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_in));
73004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_out));
74004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
75b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
76b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
77b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
780d0321e0SJeremy L Thompson   }
792b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
800d0321e0SJeremy L Thompson 
81a835093fSnbeams   if (impl->asmb) {
82a835093fSnbeams     Ceed ceed;
83b7453713SJeremy L Thompson 
842b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
852b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
862b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
872b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
88a835093fSnbeams   }
892b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
90a835093fSnbeams 
912b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
920d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
930d0321e0SJeremy L Thompson }
940d0321e0SJeremy L Thompson 
950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
960d0321e0SJeremy L Thompson // Setup infields or outfields
970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9867d9480aSJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, CeedVector *e_vecs, CeedVector *q_vecs,
9967d9480aSJeremy L Thompson                                        CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
1000d0321e0SJeremy L Thompson   Ceed                ceed;
101b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
102b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
1030d0321e0SJeremy L Thompson 
104b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
105b7453713SJeremy L Thompson   if (is_input) {
106b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
107b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1080d0321e0SJeremy L Thompson   } else {
109b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
110b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1110d0321e0SJeremy L Thompson   }
1120d0321e0SJeremy L Thompson 
1130d0321e0SJeremy L Thompson   // Loop over fields
114b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
115004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
116b7453713SJeremy L Thompson     CeedSize     q_size;
117004e4986SSebastian Grimberg     CeedInt      size;
118004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
119b7453713SJeremy L Thompson     CeedBasis    basis;
1200d0321e0SJeremy L Thompson 
121004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
122004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
123004e4986SSebastian Grimberg       CeedElemRestriction elem_rstr;
1240d0321e0SJeremy L Thompson 
1250d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
126004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
127004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1280d0321e0SJeremy L Thompson 
1290d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
130b7453713SJeremy L Thompson       if (is_input) {
131004e4986SSebastian Grimberg         CeedVector vec;
132004e4986SSebastian Grimberg 
133004e4986SSebastian Grimberg         // Check for passive input
134b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
135b7453713SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
136004e4986SSebastian Grimberg           // Check eval_mode
137004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1380d0321e0SJeremy L Thompson             // Check for strided restriction
139b7453713SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
140b7453713SJeremy L Thompson             if (is_strided) {
1410d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
142b7453713SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1430d0321e0SJeremy L Thompson             }
1440d0321e0SJeremy L Thompson           }
1450d0321e0SJeremy L Thompson         }
1460d0321e0SJeremy L Thompson       }
147b7453713SJeremy L Thompson       if (skip_restriction) {
148ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
149b7453713SJeremy L Thompson         e_vecs[i + start_e] = NULL;
1500d0321e0SJeremy L Thompson       } else {
151b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1520d0321e0SJeremy L Thompson       }
1530d0321e0SJeremy L Thompson     }
1540d0321e0SJeremy L Thompson 
155004e4986SSebastian Grimberg     switch (eval_mode) {
1560d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
157b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
158b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
159b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1600d0321e0SJeremy L Thompson         break;
1610d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1620d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
163004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
164004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
165b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
166b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
167b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1680d0321e0SJeremy L Thompson         break;
1690d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
170b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
171b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
172b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
17367d9480aSJeremy L Thompson         if (is_at_points) {
17467d9480aSJeremy L Thompson           CeedInt num_points[num_elem];
17567d9480aSJeremy L Thompson 
17667d9480aSJeremy L Thompson           for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q;
17767d9480aSJeremy L Thompson           CeedCallBackend(
17867d9480aSJeremy L Thompson               CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i]));
17967d9480aSJeremy L Thompson         } else {
180b7453713SJeremy L Thompson           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
18167d9480aSJeremy L Thompson         }
1820d0321e0SJeremy L Thompson         break;
1830d0321e0SJeremy L Thompson     }
1840d0321e0SJeremy L Thompson   }
1850d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1860d0321e0SJeremy L Thompson }
1870d0321e0SJeremy L Thompson 
1880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
189ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
1900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1910d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
1920d0321e0SJeremy L Thompson   Ceed                ceed;
193b7453713SJeremy L Thompson   bool                is_setup_done;
194b7453713SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
195b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1960d0321e0SJeremy L Thompson   CeedQFunction       qf;
197b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
198b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
199b7453713SJeremy L Thompson 
200b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
201b7453713SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
202b7453713SJeremy L Thompson 
203b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
204b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
2052b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
2062b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
207b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
208b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
209b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2100d0321e0SJeremy L Thompson 
2110d0321e0SJeremy L Thompson   // Allocate
212b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
213c1222711SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
214b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
215b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
216b7453713SJeremy L Thompson   impl->num_inputs  = num_input_fields;
217b7453713SJeremy L Thompson   impl->num_outputs = num_output_fields;
2180d0321e0SJeremy L Thompson 
219b7453713SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
2200d0321e0SJeremy L Thompson   // Infields
22167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, false, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2220d0321e0SJeremy L Thompson   // Outfields
22367d9480aSJeremy L Thompson   CeedCallBackend(
22467d9480aSJeremy L Thompson       CeedOperatorSetupFields_Hip(qf, op, false, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem));
2250d0321e0SJeremy L Thompson 
2262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2270d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2280d0321e0SJeremy L Thompson }
2290d0321e0SJeremy L Thompson 
2300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2310d0321e0SJeremy L Thompson // Setup Operator Inputs
2320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
233b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
234b7453713SJeremy L Thompson                                               CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
235b7453713SJeremy L Thompson                                               CeedOperator_Hip *impl, CeedRequest *request) {
236b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
237004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
2380d0321e0SJeremy L Thompson     CeedVector          vec;
239b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2400d0321e0SJeremy L Thompson 
2410d0321e0SJeremy L Thompson     // Get input vector
242b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2430d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
244b7453713SJeremy L Thompson       if (skip_active) continue;
245b7453713SJeremy L Thompson       else vec = in_vec;
2460d0321e0SJeremy L Thompson     }
2470d0321e0SJeremy L Thompson 
248004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
249004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2500d0321e0SJeremy L Thompson     } else {
2510d0321e0SJeremy L Thompson       // Get input vector
252b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2530d0321e0SJeremy L Thompson       // Get input element restriction
254b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
255b7453713SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
2560d0321e0SJeremy L Thompson       // Restrict, if necessary
257b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {
2580d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
259b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2600d0321e0SJeremy L Thompson       } else {
261c1222711SJeremy L Thompson         uint64_t state;
262c1222711SJeremy L Thompson 
263c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
264c1222711SJeremy L Thompson         if (state != impl->input_states[i]) {
265b7453713SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
266c1222711SJeremy L Thompson           impl->input_states[i] = state;
267c1222711SJeremy L Thompson         }
2680d0321e0SJeremy L Thompson         // Get evec
269b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2700d0321e0SJeremy L Thompson       }
2710d0321e0SJeremy L Thompson     }
2720d0321e0SJeremy L Thompson   }
2730d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2740d0321e0SJeremy L Thompson }
2750d0321e0SJeremy L Thompson 
2760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2770d0321e0SJeremy L Thompson // Input Basis Action
2780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
279b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
280b7453713SJeremy L Thompson                                              CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
2812b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
282b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
283b7453713SJeremy L Thompson     CeedInt             elem_size, size;
284004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
285b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2860d0321e0SJeremy L Thompson     CeedBasis           basis;
2870d0321e0SJeremy L Thompson 
2880d0321e0SJeremy L Thompson     // Skip active input
289b7453713SJeremy L Thompson     if (skip_active) {
2900d0321e0SJeremy L Thompson       CeedVector vec;
291b7453713SJeremy L Thompson 
292b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2932b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2940d0321e0SJeremy L Thompson     }
295004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
296b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
297b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
298004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
299b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3000d0321e0SJeremy L Thompson     // Basis action
301004e4986SSebastian Grimberg     switch (eval_mode) {
3020d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
303b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
3040d0321e0SJeremy L Thompson         break;
3050d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3060d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
307004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
308004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
309b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
310004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3110d0321e0SJeremy L Thompson         break;
3120d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
3130d0321e0SJeremy L Thompson         break;  // No action
3140d0321e0SJeremy L Thompson     }
3150d0321e0SJeremy L Thompson   }
3160d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3170d0321e0SJeremy L Thompson }
3180d0321e0SJeremy L Thompson 
3190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3200d0321e0SJeremy L Thompson // Restore Input Vectors
3210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
322b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
323b7453713SJeremy L Thompson                                                 const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
324b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
325004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
3260d0321e0SJeremy L Thompson     CeedVector   vec;
327004e4986SSebastian Grimberg 
3280d0321e0SJeremy L Thompson     // Skip active input
329b7453713SJeremy L Thompson     if (skip_active) {
330b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3312b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3320d0321e0SJeremy L Thompson     }
333004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
334004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3350d0321e0SJeremy L Thompson     } else {
336b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
337b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
338b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
3390d0321e0SJeremy L Thompson       } else {
340b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
3410d0321e0SJeremy L Thompson       }
3420d0321e0SJeremy L Thompson     }
3430d0321e0SJeremy L Thompson   }
3440d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3450d0321e0SJeremy L Thompson }
3460d0321e0SJeremy L Thompson 
3470d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3480d0321e0SJeremy L Thompson // Apply and add to output
3490d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
350b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
351b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
352b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
353b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
3540d0321e0SJeremy L Thompson   CeedQFunction       qf;
355b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
356b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
357b7453713SJeremy L Thompson 
358b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3592b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
361b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
362b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
363b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
3640d0321e0SJeremy L Thompson 
3650d0321e0SJeremy L Thompson   // Setup
3662b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
3670d0321e0SJeremy L Thompson 
3680d0321e0SJeremy L Thompson   // Input Evecs and Restriction
369b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
3700d0321e0SJeremy L Thompson 
3710d0321e0SJeremy L Thompson   // Input basis apply if needed
372b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
3730d0321e0SJeremy L Thompson 
3740d0321e0SJeremy L Thompson   // Output pointers, as necessary
375b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
376004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
377b7453713SJeremy L Thompson 
378004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
379004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
3800d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
381b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
382b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
3830d0321e0SJeremy L Thompson     }
3840d0321e0SJeremy L Thompson   }
3850d0321e0SJeremy L Thompson 
3860d0321e0SJeremy L Thompson   // Q function
387b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
3880d0321e0SJeremy L Thompson 
3890d0321e0SJeremy L Thompson   // Output basis apply if needed
390b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
391004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
392b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
393b7453713SJeremy L Thompson     CeedBasis           basis;
394b7453713SJeremy L Thompson 
395004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
396b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
397b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
398004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
399b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4000d0321e0SJeremy L Thompson     // Basis action
401004e4986SSebastian Grimberg     switch (eval_mode) {
4020d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
403004e4986SSebastian Grimberg         break;  // No action
4040d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4050d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
406004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
407004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
408b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
409004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
4100d0321e0SJeremy L Thompson         break;
4110d0321e0SJeremy L Thompson       // LCOV_EXCL_START
4120d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
4136e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
4140d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
4150d0321e0SJeremy L Thompson       }
4160d0321e0SJeremy L Thompson     }
417004e4986SSebastian Grimberg   }
4180d0321e0SJeremy L Thompson 
4190d0321e0SJeremy L Thompson   // Output restriction
420b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
421004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
422b7453713SJeremy L Thompson     CeedVector          vec;
423b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
424b7453713SJeremy L Thompson 
4250d0321e0SJeremy L Thompson     // Restore evec
426004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
427004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
428b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4290d0321e0SJeremy L Thompson     }
4300d0321e0SJeremy L Thompson     // Get output vector
431b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4320d0321e0SJeremy L Thompson     // Restrict
433b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4340d0321e0SJeremy L Thompson     // Active
435b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4360d0321e0SJeremy L Thompson 
437b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4380d0321e0SJeremy L Thompson   }
4390d0321e0SJeremy L Thompson 
4400d0321e0SJeremy L Thompson   // Restore input arrays
441b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4420d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4430d0321e0SJeremy L Thompson }
4440d0321e0SJeremy L Thompson 
4450d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
44667d9480aSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
44767d9480aSJeremy L Thompson //------------------------------------------------------------------------------
44867d9480aSJeremy L Thompson static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) {
44967d9480aSJeremy L Thompson   Ceed                ceed;
45067d9480aSJeremy L Thompson   bool                is_setup_done;
45167d9480aSJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
45267d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
45367d9480aSJeremy L Thompson   CeedQFunction       qf;
45467d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
45567d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
45667d9480aSJeremy L Thompson 
45767d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
45867d9480aSJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
45967d9480aSJeremy L Thompson 
46067d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
46167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
46267d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
46367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
46467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
46567d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
46667d9480aSJeremy L Thompson   {
46767d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr = NULL;
46867d9480aSJeremy L Thompson 
46967d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL));
47067d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points));
47167d9480aSJeremy L Thompson   }
47267d9480aSJeremy L Thompson   impl->max_num_points = max_num_points;
47367d9480aSJeremy L Thompson 
47467d9480aSJeremy L Thompson   // Allocate
47567d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
47667d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
47767d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
47867d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
47967d9480aSJeremy L Thompson   impl->num_inputs  = num_input_fields;
48067d9480aSJeremy L Thompson   impl->num_outputs = num_output_fields;
48167d9480aSJeremy L Thompson 
48267d9480aSJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
48367d9480aSJeremy L Thompson   // Infields
48467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, true, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, max_num_points, num_elem));
48567d9480aSJeremy L Thompson   // Outfields
48667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, true, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields,
48767d9480aSJeremy L Thompson                                               max_num_points, num_elem));
48867d9480aSJeremy L Thompson 
48967d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
49067d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
49167d9480aSJeremy L Thompson }
49267d9480aSJeremy L Thompson 
49367d9480aSJeremy L Thompson //------------------------------------------------------------------------------
49467d9480aSJeremy L Thompson // Input Basis Action AtPoints
49567d9480aSJeremy L Thompson //------------------------------------------------------------------------------
49667d9480aSJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Hip(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields,
49767d9480aSJeremy L Thompson                                                      CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active,
49867d9480aSJeremy L Thompson                                                      CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
49967d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
50067d9480aSJeremy L Thompson     CeedInt             elem_size, size;
50167d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
50267d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
50367d9480aSJeremy L Thompson     CeedBasis           basis;
50467d9480aSJeremy L Thompson 
50567d9480aSJeremy L Thompson     // Skip active input
50667d9480aSJeremy L Thompson     if (skip_active) {
50767d9480aSJeremy L Thompson       CeedVector vec;
50867d9480aSJeremy L Thompson 
50967d9480aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
51067d9480aSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
51167d9480aSJeremy L Thompson     }
51267d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
51367d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
51467d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
51567d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
51667d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
51767d9480aSJeremy L Thompson     // Basis action
51867d9480aSJeremy L Thompson     switch (eval_mode) {
51967d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
52067d9480aSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
52167d9480aSJeremy L Thompson         break;
52267d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
52367d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
52467d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
52567d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
52667d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
52767d9480aSJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
52867d9480aSJeremy L Thompson                                                impl->q_vecs_in[i]));
52967d9480aSJeremy L Thompson         break;
53067d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT:
53167d9480aSJeremy L Thompson         break;  // No action
53267d9480aSJeremy L Thompson     }
53367d9480aSJeremy L Thompson   }
53467d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
53567d9480aSJeremy L Thompson }
53667d9480aSJeremy L Thompson 
53767d9480aSJeremy L Thompson //------------------------------------------------------------------------------
53867d9480aSJeremy L Thompson // Apply and add to output AtPoints
53967d9480aSJeremy L Thompson //------------------------------------------------------------------------------
54067d9480aSJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
54167d9480aSJeremy L Thompson   CeedInt             max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
54267d9480aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
54367d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
54467d9480aSJeremy L Thompson   CeedQFunction       qf;
54567d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
54667d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
54767d9480aSJeremy L Thompson 
54867d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
54967d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
55067d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
55167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
55267d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
55367d9480aSJeremy L Thompson   CeedInt num_points[num_elem];
55467d9480aSJeremy L Thompson 
55567d9480aSJeremy L Thompson   // Setup
55667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
55767d9480aSJeremy L Thompson   max_num_points = impl->max_num_points;
55867d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
55967d9480aSJeremy L Thompson 
56067d9480aSJeremy L Thompson   // Input Evecs and Restriction
56167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
56267d9480aSJeremy L Thompson 
56367d9480aSJeremy L Thompson   // Get point coordinates
56467d9480aSJeremy L Thompson   if (!impl->point_coords_elem) {
56567d9480aSJeremy L Thompson     CeedVector          point_coords = NULL;
56667d9480aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
56767d9480aSJeremy L Thompson 
56867d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
56967d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
57067d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
57167d9480aSJeremy L Thompson   }
57267d9480aSJeremy L Thompson 
57367d9480aSJeremy L Thompson   // Input basis apply if needed
57467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
57567d9480aSJeremy L Thompson 
57667d9480aSJeremy L Thompson   // Output pointers, as necessary
57767d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
57867d9480aSJeremy L Thompson     CeedEvalMode eval_mode;
57967d9480aSJeremy L Thompson 
58067d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
58167d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
58267d9480aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
58367d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
58467d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
58567d9480aSJeremy L Thompson     }
58667d9480aSJeremy L Thompson   }
58767d9480aSJeremy L Thompson 
58867d9480aSJeremy L Thompson   // Q function
58967d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
59067d9480aSJeremy L Thompson 
59167d9480aSJeremy L Thompson   // Output basis apply if needed
59267d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
59367d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
59467d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
59567d9480aSJeremy L Thompson     CeedBasis           basis;
59667d9480aSJeremy L Thompson 
59767d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
59867d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
59967d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
60067d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
60167d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
60267d9480aSJeremy L Thompson     // Basis action
60367d9480aSJeremy L Thompson     switch (eval_mode) {
60467d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
60567d9480aSJeremy L Thompson         break;  // No action
60667d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
60767d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
60867d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
60967d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
61067d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
61167d9480aSJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
61267d9480aSJeremy L Thompson                                                impl->e_vecs[i + impl->num_inputs]));
61367d9480aSJeremy L Thompson         break;
61467d9480aSJeremy L Thompson       // LCOV_EXCL_START
61567d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT: {
61667d9480aSJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
61767d9480aSJeremy L Thompson         // LCOV_EXCL_STOP
61867d9480aSJeremy L Thompson       }
61967d9480aSJeremy L Thompson     }
62067d9480aSJeremy L Thompson   }
62167d9480aSJeremy L Thompson 
62267d9480aSJeremy L Thompson   // Output restriction
62367d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
62467d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
62567d9480aSJeremy L Thompson     CeedVector          vec;
62667d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
62767d9480aSJeremy L Thompson 
62867d9480aSJeremy L Thompson     // Restore evec
62967d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
63067d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
63167d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
63267d9480aSJeremy L Thompson     }
63367d9480aSJeremy L Thompson     // Get output vector
63467d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
63567d9480aSJeremy L Thompson     // Restrict
63667d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
63767d9480aSJeremy L Thompson     // Active
63867d9480aSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
63967d9480aSJeremy L Thompson 
64067d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
64167d9480aSJeremy L Thompson   }
64267d9480aSJeremy L Thompson 
64367d9480aSJeremy L Thompson   // Restore input arrays
64467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
64567d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
64667d9480aSJeremy L Thompson }
64767d9480aSJeremy L Thompson 
64867d9480aSJeremy L Thompson //------------------------------------------------------------------------------
649004e4986SSebastian Grimberg // Linear QFunction Assembly Core
6500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6512b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
6520d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
653b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
654b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
655b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
656004e4986SSebastian Grimberg   CeedVector         *active_inputs;
657b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
658b7453713SJeremy L Thompson   CeedQFunction       qf;
659b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
660b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
661b7453713SJeremy L Thompson 
6622b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
663b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
664e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
665e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
666b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
667004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
668b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
669b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
670004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
671004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
6720d0321e0SJeremy L Thompson 
6730d0321e0SJeremy L Thompson   // Setup
6742b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
6750d0321e0SJeremy L Thompson 
6760d0321e0SJeremy L Thompson   // Input Evecs and Restriction
677b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
6780d0321e0SJeremy L Thompson 
6790d0321e0SJeremy L Thompson   // Count number of active input fields
680b7453713SJeremy L Thompson   if (!num_active_in) {
681b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
682b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
683b7453713SJeremy L Thompson       CeedVector  vec;
684b7453713SJeremy L Thompson 
6850d0321e0SJeremy L Thompson       // Get input vector
686b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
6870d0321e0SJeremy L Thompson       // Check if active input
6880d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
689b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
690b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
691b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
692004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
6930d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
694004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
695004e4986SSebastian Grimberg 
696004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
697b7453713SJeremy L Thompson           CeedCallBackend(
698004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
6990d0321e0SJeremy L Thompson         }
700b7453713SJeremy L Thompson         num_active_in += size;
701b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
7020d0321e0SJeremy L Thompson       }
7030d0321e0SJeremy L Thompson     }
704b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
705004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
7060d0321e0SJeremy L Thompson   }
7070d0321e0SJeremy L Thompson 
7080d0321e0SJeremy L Thompson   // Count number of active output fields
709b7453713SJeremy L Thompson   if (!num_active_out) {
710b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
711b7453713SJeremy L Thompson       CeedVector vec;
712b7453713SJeremy L Thompson 
7130d0321e0SJeremy L Thompson       // Get output vector
714b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
7150d0321e0SJeremy L Thompson       // Check if active output
7160d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
717b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
718b7453713SJeremy L Thompson         num_active_out += size;
7190d0321e0SJeremy L Thompson       }
7200d0321e0SJeremy L Thompson     }
721b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
7220d0321e0SJeremy L Thompson   }
7230d0321e0SJeremy L Thompson 
7240d0321e0SJeremy L Thompson   // Check sizes
725b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
7260d0321e0SJeremy L Thompson 
7270d0321e0SJeremy L Thompson   // Build objects if needed
7280d0321e0SJeremy L Thompson   if (build_objects) {
729b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
730b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
731b7453713SJeremy L Thompson 
732004e4986SSebastian Grimberg     // Create output restriction
733b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
734*0a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
735*0a5597ceSJeremy L Thompson                                                      rstr));
7360d0321e0SJeremy L Thompson     // Create assembled vector
737b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
7380d0321e0SJeremy L Thompson   }
7392b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
740b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
7410d0321e0SJeremy L Thompson 
7420d0321e0SJeremy L Thompson   // Input basis apply
743b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
7440d0321e0SJeremy L Thompson 
7450d0321e0SJeremy L Thompson   // Assemble QFunction
746b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
7470d0321e0SJeremy L Thompson     // Set Inputs
748004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
749b7453713SJeremy L Thompson     if (num_active_in > 1) {
750004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
7510d0321e0SJeremy L Thompson     }
7520d0321e0SJeremy L Thompson     // Set Outputs
753b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
754b7453713SJeremy L Thompson       CeedVector vec;
755b7453713SJeremy L Thompson 
7560d0321e0SJeremy L Thompson       // Get output vector
757b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
7580d0321e0SJeremy L Thompson       // Check if active output
7590d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
760b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
761b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
762b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
7630d0321e0SJeremy L Thompson       }
7640d0321e0SJeremy L Thompson     }
7650d0321e0SJeremy L Thompson     // Apply QFunction
766b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
7670d0321e0SJeremy L Thompson   }
7680d0321e0SJeremy L Thompson 
769004e4986SSebastian Grimberg   // Un-set output q_vecs to prevent accidental overwrite of Assembled
770b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
771b7453713SJeremy L Thompson     CeedVector vec;
772b7453713SJeremy L Thompson 
7730d0321e0SJeremy L Thompson     // Get output vector
774b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
7750d0321e0SJeremy L Thompson     // Check if active output
7760d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
777b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
7780d0321e0SJeremy L Thompson     }
7790d0321e0SJeremy L Thompson   }
7800d0321e0SJeremy L Thompson 
7810d0321e0SJeremy L Thompson   // Restore input arrays
782b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
7830d0321e0SJeremy L Thompson 
7840d0321e0SJeremy L Thompson   // Restore output
785b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
7860d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7870d0321e0SJeremy L Thompson }
7880d0321e0SJeremy L Thompson 
7890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7900d0321e0SJeremy L Thompson // Assemble Linear QFunction
7910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7922b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
7932b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
7940d0321e0SJeremy L Thompson }
7950d0321e0SJeremy L Thompson 
7960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
797b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
7980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7992b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
8002b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
8010d0321e0SJeremy L Thompson }
8020d0321e0SJeremy L Thompson 
8030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
804004e4986SSebastian Grimberg // Assemble Diagonal Setup
8050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
806cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
8070d0321e0SJeremy L Thompson   Ceed                ceed;
808004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
809cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
810004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
811b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
812b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
8130d0321e0SJeremy L Thompson   CeedQFunction       qf;
814b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
815b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
816b7453713SJeremy L Thompson 
817b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8182b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
819b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
8200d0321e0SJeremy L Thompson 
8210d0321e0SJeremy L Thompson   // Determine active input basis
822b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
823b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
824b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
8250d0321e0SJeremy L Thompson     CeedVector vec;
826b7453713SJeremy L Thompson 
827b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8280d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
829004e4986SSebastian Grimberg       CeedBasis    basis;
830004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
831b7453713SJeremy L Thompson 
832004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
833004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
834004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
835004e4986SSebastian Grimberg       basis_in = basis;
836004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
837004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
838004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
839004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
840004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
841004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
842004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
8430d0321e0SJeremy L Thompson       }
8440d0321e0SJeremy L Thompson     }
8450d0321e0SJeremy L Thompson   }
8460d0321e0SJeremy L Thompson 
8470d0321e0SJeremy L Thompson   // Determine active output basis
848b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
849b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
850b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
8510d0321e0SJeremy L Thompson     CeedVector vec;
852b7453713SJeremy L Thompson 
853b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8540d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
855004e4986SSebastian Grimberg       CeedBasis    basis;
856004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
857b7453713SJeremy L Thompson 
858004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
859004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
860004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
861004e4986SSebastian Grimberg       basis_out = basis;
862004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
863004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
864004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
865004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
866004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
867004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
868004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
8690d0321e0SJeremy L Thompson       }
8700d0321e0SJeremy L Thompson     }
8710d0321e0SJeremy L Thompson   }
8720d0321e0SJeremy L Thompson 
8730d0321e0SJeremy L Thompson   // Operator data struct
8742b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
8752b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
8760d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
877b7453713SJeremy L Thompson 
878cbfe683aSSebastian Grimberg   // Basis matrices
879004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
880004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
881004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
882004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
883004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
884004e4986SSebastian Grimberg   bool          has_eval_none    = false;
8850d0321e0SJeremy L Thompson 
8860d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
887004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
888004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
889004e4986SSebastian Grimberg   if (has_eval_none) {
8900d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
891b7453713SJeremy L Thompson 
892004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
893004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
894b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
895b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
896004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
8970d0321e0SJeremy L Thompson   }
8980d0321e0SJeremy L Thompson 
899004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
900004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
901004e4986SSebastian Grimberg     CeedFESpace fespace;
902004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
9030d0321e0SJeremy L Thompson 
904004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
905004e4986SSebastian Grimberg     switch (fespace) {
906004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
907004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
908004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
909004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
9100d0321e0SJeremy L Thompson 
911004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
912004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
9130d0321e0SJeremy L Thompson 
914004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
915004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
916004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
917004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
918004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
919004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
920004e4986SSebastian Grimberg         if (in) {
921004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
922004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
923004e4986SSebastian Grimberg         } else {
924004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
925004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
926004e4986SSebastian Grimberg         }
927004e4986SSebastian Grimberg       } break;
928004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
929004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
930004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
931004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
932004e4986SSebastian Grimberg 
933004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
934004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
935004e4986SSebastian Grimberg 
936004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
937004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
938004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
939004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
940004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
941004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
942004e4986SSebastian Grimberg         if (in) {
943004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
944004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
945004e4986SSebastian Grimberg         } else {
946004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
947004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
948004e4986SSebastian Grimberg         }
949004e4986SSebastian Grimberg       } break;
950004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
951004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
952004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
953004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
954004e4986SSebastian Grimberg 
955004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
956004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
957004e4986SSebastian Grimberg 
958004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
959004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
960004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
961004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
962004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
963004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
964004e4986SSebastian Grimberg         if (in) {
965004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
966004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
967004e4986SSebastian Grimberg         } else {
968004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
969004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
970004e4986SSebastian Grimberg         }
971004e4986SSebastian Grimberg       } break;
972004e4986SSebastian Grimberg     }
973004e4986SSebastian Grimberg   }
974004e4986SSebastian Grimberg 
975004e4986SSebastian Grimberg   // Arrays of eval_modes
976004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
977004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
978004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
979004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
980004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
981004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
9820d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9830d0321e0SJeremy L Thompson }
9840d0321e0SJeremy L Thompson 
9850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
986cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
987cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
988cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
989cbfe683aSSebastian Grimberg   Ceed                ceed;
99022070f95SJeremy L Thompson   char               *diagonal_kernel_source;
99122070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
992cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
993cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
994cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
995cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
996cbfe683aSSebastian Grimberg   CeedQFunction       qf;
997cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
998cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
999cbfe683aSSebastian Grimberg 
1000cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1001cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1002cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1003cbfe683aSSebastian Grimberg 
1004cbfe683aSSebastian Grimberg   // Determine active input basis
1005cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1006cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1007cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1008cbfe683aSSebastian Grimberg     CeedVector vec;
1009cbfe683aSSebastian Grimberg 
1010cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1011cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1012cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1013cbfe683aSSebastian Grimberg 
1014cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1015cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1016cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1017cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1018cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1019cbfe683aSSebastian Grimberg       }
1020cbfe683aSSebastian Grimberg     }
1021cbfe683aSSebastian Grimberg   }
1022cbfe683aSSebastian Grimberg 
1023cbfe683aSSebastian Grimberg   // Determine active output basis
1024cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1025cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1026cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1027cbfe683aSSebastian Grimberg     CeedVector vec;
1028cbfe683aSSebastian Grimberg 
1029cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1030cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1031cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1032cbfe683aSSebastian Grimberg 
1033cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1034cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1035cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1036cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1037cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1038cbfe683aSSebastian Grimberg       }
1039cbfe683aSSebastian Grimberg     }
1040cbfe683aSSebastian Grimberg   }
1041cbfe683aSSebastian Grimberg 
1042cbfe683aSSebastian Grimberg   // Operator data struct
1043cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1044cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1045cbfe683aSSebastian Grimberg 
1046cbfe683aSSebastian Grimberg   // Assemble kernel
1047cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
1048cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
1049cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1050cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1051cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1052cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1053cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1054cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1055cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1056cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1057cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1058cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1059cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1060cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1061cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1062cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1063cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1064cbfe683aSSebastian Grimberg }
1065cbfe683aSSebastian Grimberg 
1066cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1067004e4986SSebastian Grimberg // Assemble Diagonal Core
10680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1069b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
10700d0321e0SJeremy L Thompson   Ceed                ceed;
1071cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1072b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
1073b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1074004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1075004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
10760d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
1077b7453713SJeremy L Thompson 
1078b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
10792b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
10800d0321e0SJeremy L Thompson 
10810d0321e0SJeremy L Thompson   // Assemble QFunction
1082004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1083004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1084004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
10850d0321e0SJeremy L Thompson 
1086cbfe683aSSebastian Grimberg   // Setup
1087cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
1088cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1089cbfe683aSSebastian Grimberg 
1090cbfe683aSSebastian Grimberg   assert(diag != NULL);
1091cbfe683aSSebastian Grimberg 
1092cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1093cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1094cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1095cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
10969330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1097b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1098b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
10999330daecSnbeams 
1100cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
1101cbfe683aSSebastian Grimberg   }
11020d0321e0SJeremy L Thompson 
1103004e4986SSebastian Grimberg   // Restriction and diagonal vector
1104004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1105004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1106004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1107004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1108004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1109004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1110004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1111004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1112004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
11130d0321e0SJeremy L Thompson   }
1114004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1115004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1116b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
11170d0321e0SJeremy L Thompson 
111891db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1119004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1120004e4986SSebastian Grimberg   if (num_nodes > 0) {
11210d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1122b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
1123b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
11240d0321e0SJeremy L Thompson 
11250d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1126004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1127004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1128004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1129004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1130004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1131b7453713SJeremy L Thompson 
1132b7453713SJeremy L Thompson     if (is_point_block) {
1133004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
11340d0321e0SJeremy L Thompson     } else {
1135004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
11360d0321e0SJeremy L Thompson     }
11370d0321e0SJeremy L Thompson 
11380d0321e0SJeremy L Thompson     // Restore arrays
1139b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1140b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
114191db28b6SZach Atkins   }
11420d0321e0SJeremy L Thompson 
11430d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1144b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
11450d0321e0SJeremy L Thompson 
11460d0321e0SJeremy L Thompson   // Cleanup
1147b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
11480d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11490d0321e0SJeremy L Thompson }
11500d0321e0SJeremy L Thompson 
11510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11520d0321e0SJeremy L Thompson // Assemble Linear Diagonal
11530d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11542b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
11552b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
11566aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11570d0321e0SJeremy L Thompson }
11580d0321e0SJeremy L Thompson 
11590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11600d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
11610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11622b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
11632b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
11646aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11650d0321e0SJeremy L Thompson }
11660d0321e0SJeremy L Thompson 
1167a835093fSnbeams //------------------------------------------------------------------------------
1168004e4986SSebastian Grimberg // Single Operator Assembly Setup
1169a835093fSnbeams //------------------------------------------------------------------------------
11709330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
1171a835093fSnbeams   Ceed                ceed;
117222070f95SJeremy L Thompson   char               *assembly_kernel_source;
117322070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1174004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
11753b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1176004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1177b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1178b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1179b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
1180b7453713SJeremy L Thompson   CeedQFunction       qf;
1181b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1182a835093fSnbeams   CeedOperator_Hip   *impl;
1183b7453713SJeremy L Thompson 
1184b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11852b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1186a835093fSnbeams 
1187a835093fSnbeams   // Get intput and output fields
11882b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1189a835093fSnbeams 
1190a835093fSnbeams   // Determine active input basis eval mode
11912b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
11922b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1193a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1194a835093fSnbeams     CeedVector vec;
1195b7453713SJeremy L Thompson 
11962b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1197a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1198004e4986SSebastian Grimberg       CeedBasis    basis;
1199b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1200b7453713SJeremy L Thompson 
1201004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1202004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1203004e4986SSebastian Grimberg       basis_in = basis;
12042b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1205004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1206004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1207004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
12082b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1209004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1210004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1211004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1212004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1213004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1214004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1215a835093fSnbeams         }
1216004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1217a835093fSnbeams       }
1218a835093fSnbeams     }
1219a835093fSnbeams   }
1220a835093fSnbeams 
1221a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
12222b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1223a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1224a835093fSnbeams     CeedVector vec;
1225b7453713SJeremy L Thompson 
12262b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1227a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1228004e4986SSebastian Grimberg       CeedBasis    basis;
1229b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1230b7453713SJeremy L Thompson 
1231004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1232004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1233004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1234004e4986SSebastian Grimberg       basis_out = basis;
12352b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1236004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1237004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1238004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1239004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1240004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
12412b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1242004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1243004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1244004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1245004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1246004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1247004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1248004e4986SSebastian Grimberg         }
1249004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1250a835093fSnbeams       }
1251a835093fSnbeams     }
1252a835093fSnbeams   }
1253004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1254a835093fSnbeams 
12552b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1256a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1257004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1258004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1259004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1260004e4986SSebastian Grimberg 
1261004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1262004e4986SSebastian Grimberg 
1263004e4986SSebastian Grimberg   if (fallback) {
1264004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1265004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1266004e4986SSebastian Grimberg   }
1267a835093fSnbeams 
1268a835093fSnbeams   // Compile kernels
1269004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1270004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
12712b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
127223d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
12732b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
127423d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1275004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1276004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1277004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1278cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
12799330daecSnbeams                                   use_ceedsize_idx));
1280004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
12812b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
12822b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1283a835093fSnbeams 
1284004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1285004e4986SSebastian Grimberg   {
1286004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1287004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1288004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1289004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1290004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1291a835093fSnbeams 
1292004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1293004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1294004e4986SSebastian Grimberg     }
1295004e4986SSebastian Grimberg     if (has_eval_none) {
1296004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1297004e4986SSebastian Grimberg       for (CeedInt i = 0; i < (elem_size_in < num_qpts_in ? elem_size_in : num_qpts_in); i++) identity[i * elem_size_in + i] = 1.0;
1298004e4986SSebastian Grimberg     }
1299b7453713SJeremy L Thompson 
1300b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1301004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1302004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1303004e4986SSebastian Grimberg 
1304004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1305004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1306004e4986SSebastian Grimberg       if (q_comp > 1) {
1307004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1308004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1309004e4986SSebastian Grimberg       }
1310004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1311004e4986SSebastian Grimberg 
1312004e4986SSebastian Grimberg       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_in[i * elem_size_in * num_qpts_in], h_B_in, elem_size_in * num_qpts_in * sizeof(CeedScalar),
1313004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1314004e4986SSebastian Grimberg     }
1315004e4986SSebastian Grimberg 
1316004e4986SSebastian Grimberg     if (identity) {
1317004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1318a835093fSnbeams     }
1319a835093fSnbeams   }
1320a835093fSnbeams 
1321004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1322004e4986SSebastian Grimberg   {
1323004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1324004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1325004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1326004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1327004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1328b7453713SJeremy L Thompson 
1329004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1330004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1331004e4986SSebastian Grimberg     }
1332004e4986SSebastian Grimberg     if (has_eval_none) {
1333004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1334004e4986SSebastian Grimberg       for (CeedInt i = 0; i < (elem_size_out < num_qpts_out ? elem_size_out : num_qpts_out); i++) identity[i * elem_size_out + i] = 1.0;
1335a835093fSnbeams     }
1336a835093fSnbeams 
1337b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1338004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1339004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1340004e4986SSebastian Grimberg 
1341004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1342004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1343004e4986SSebastian Grimberg       if (q_comp > 1) {
1344004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1345004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1346004e4986SSebastian Grimberg       }
1347004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1348004e4986SSebastian Grimberg 
1349004e4986SSebastian Grimberg       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_out[i * elem_size_out * num_qpts_out], h_B_out, elem_size_out * num_qpts_out * sizeof(CeedScalar),
1350004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1351004e4986SSebastian Grimberg     }
1352004e4986SSebastian Grimberg 
1353004e4986SSebastian Grimberg     if (identity) {
1354004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1355a835093fSnbeams     }
1356a835093fSnbeams   }
1357a835093fSnbeams   return CEED_ERROR_SUCCESS;
1358a835093fSnbeams }
1359a835093fSnbeams 
1360a835093fSnbeams //------------------------------------------------------------------------------
1361cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1362cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1363cefa2673SJeremy L Thompson //
1364ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1365cefa2673SJeremy L Thompson // modes).
1366cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1367a835093fSnbeams //------------------------------------------------------------------------------
13682b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1369a835093fSnbeams   Ceed                ceed;
1370b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1371004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1372b7453713SJeremy L Thompson   CeedScalar         *values_array;
1373004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1374b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1375004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1376004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1377004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1378004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1379a835093fSnbeams   CeedOperator_Hip   *impl;
1380b7453713SJeremy L Thompson 
1381b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
13822b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1383a835093fSnbeams 
1384a835093fSnbeams   // Assemble QFunction
1385004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1386004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1387004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1388a835093fSnbeams 
13899330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
13909330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
13919330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1392004e4986SSebastian Grimberg 
13939330daecSnbeams   // Setup
1394004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1395004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1396004e4986SSebastian Grimberg 
1397004e4986SSebastian Grimberg   assert(asmb != NULL);
1398004e4986SSebastian Grimberg 
1399004e4986SSebastian Grimberg   // Assemble element operator
1400004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1401004e4986SSebastian Grimberg   values_array += offset;
1402004e4986SSebastian Grimberg 
1403004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1404004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1405004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1406004e4986SSebastian Grimberg 
1407004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1408004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1409004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1410004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1411004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1412004e4986SSebastian Grimberg   }
1413004e4986SSebastian Grimberg 
1414004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1415004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1416004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1417004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1418004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1419004e4986SSebastian Grimberg 
1420004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1421004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1422004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1423004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1424004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1425004e4986SSebastian Grimberg     }
1426004e4986SSebastian Grimberg   } else {
1427004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1428004e4986SSebastian Grimberg     orients_out      = orients_in;
1429004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
14309330daecSnbeams   }
14319330daecSnbeams 
1432a835093fSnbeams   // Compute B^T D B
1433004e4986SSebastian Grimberg   CeedInt shared_mem =
1434004e4986SSebastian Grimberg       ((curl_orients_in || curl_orients_out ? elem_size_in * elem_size_out : 0) + (curl_orients_in ? elem_size_in * asmb->block_size_y : 0)) *
1435004e4986SSebastian Grimberg       sizeof(CeedScalar);
1436004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1437004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1438004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1439b7453713SJeremy L Thompson 
14402b730f8bSJeremy L Thompson   CeedCallBackend(
1441004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1442a835093fSnbeams 
1443a835093fSnbeams   // Restore arrays
14442b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1445004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1446a835093fSnbeams 
1447a835093fSnbeams   // Cleanup
14482b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1449004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1450004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1451004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1452004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1453004e4986SSebastian Grimberg   }
1454004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1455004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1456004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1457004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1458004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1459004e4986SSebastian Grimberg     }
1460004e4986SSebastian Grimberg   }
1461a835093fSnbeams   return CEED_ERROR_SUCCESS;
1462a835093fSnbeams }
1463a835093fSnbeams 
1464a835093fSnbeams //------------------------------------------------------------------------------
146567d9480aSJeremy L Thompson // Assemble Linear QFunction AtPoints
146667d9480aSJeremy L Thompson //------------------------------------------------------------------------------
146767d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
146867d9480aSJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
146967d9480aSJeremy L Thompson }
147067d9480aSJeremy L Thompson 
147167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
147267d9480aSJeremy L Thompson // Assemble Linear Diagonal AtPoints
147367d9480aSJeremy L Thompson //------------------------------------------------------------------------------
147467d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1475afe3bc8aSJeremy L Thompson   bool                is_active_at_points = true;
1476afe3bc8aSJeremy L Thompson   CeedSize            e_vec_size          = 0;
1477afe3bc8aSJeremy L Thompson   CeedInt             max_num_points, num_elem, num_input_fields, num_output_fields, elem_size_active = 1, num_comp_active = 1;
1478afe3bc8aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1479afe3bc8aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1480afe3bc8aSJeremy L Thompson   CeedQFunction       qf;
1481afe3bc8aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1482afe3bc8aSJeremy L Thompson   CeedOperator_Hip   *impl;
1483afe3bc8aSJeremy L Thompson 
1484afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1485afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1486afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1487afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1488afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1489afe3bc8aSJeremy L Thompson   CeedInt num_points[num_elem];
1490afe3bc8aSJeremy L Thompson 
1491afe3bc8aSJeremy L Thompson   // Setup
1492afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
1493afe3bc8aSJeremy L Thompson   max_num_points = impl->max_num_points;
1494afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
1495afe3bc8aSJeremy L Thompson 
1496afe3bc8aSJeremy L Thompson   // Input Evecs and Restriction
1497afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1498afe3bc8aSJeremy L Thompson 
1499afe3bc8aSJeremy L Thompson   // Check if active field is at points
1500afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1501afe3bc8aSJeremy L Thompson     CeedRestrictionType rstr_type;
1502afe3bc8aSJeremy L Thompson     CeedVector          vec;
1503afe3bc8aSJeremy L Thompson     CeedElemRestriction elem_rstr;
1504afe3bc8aSJeremy L Thompson 
1505afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1506afe3bc8aSJeremy L Thompson     // Skip non-active input
1507afe3bc8aSJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1508afe3bc8aSJeremy L Thompson 
1509afe3bc8aSJeremy L Thompson     // Get active restriction type
1510afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1511afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1512afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1513afe3bc8aSJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1514afe3bc8aSJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size_active));
1515afe3bc8aSJeremy L Thompson   }
1516afe3bc8aSJeremy L Thompson 
1517afe3bc8aSJeremy L Thompson   // Get point coordinates
1518afe3bc8aSJeremy L Thompson   if (!impl->point_coords_elem) {
1519afe3bc8aSJeremy L Thompson     CeedVector          point_coords = NULL;
1520afe3bc8aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1521afe3bc8aSJeremy L Thompson 
1522afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1523afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1524afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1525afe3bc8aSJeremy L Thompson   }
1526afe3bc8aSJeremy L Thompson 
1527afe3bc8aSJeremy L Thompson   // Input basis apply if needed
1528afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1529afe3bc8aSJeremy L Thompson 
1530afe3bc8aSJeremy L Thompson   // Output pointers, as necessary
1531afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1532afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1533afe3bc8aSJeremy L Thompson 
1534afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1535afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1536afe3bc8aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1537afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1538afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1539afe3bc8aSJeremy L Thompson     }
1540afe3bc8aSJeremy L Thompson   }
1541afe3bc8aSJeremy L Thompson 
1542afe3bc8aSJeremy L Thompson   // Loop over active fields
1543afe3bc8aSJeremy L Thompson   e_vec_size = (is_active_at_points ? max_num_points : elem_size_active) * num_comp_active;
1544afe3bc8aSJeremy L Thompson   for (CeedInt s = 0; s < e_vec_size; s++) {
1545afe3bc8aSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1546afe3bc8aSJeremy L Thompson       bool         is_active_input = false;
1547afe3bc8aSJeremy L Thompson       CeedEvalMode eval_mode;
1548afe3bc8aSJeremy L Thompson       CeedVector   vec;
1549afe3bc8aSJeremy L Thompson       CeedBasis    basis;
1550afe3bc8aSJeremy L Thompson 
1551afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1552afe3bc8aSJeremy L Thompson       // Skip non-active input
1553afe3bc8aSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1554afe3bc8aSJeremy L Thompson       if (!is_active_input) continue;
1555afe3bc8aSJeremy L Thompson 
1556afe3bc8aSJeremy L Thompson       // Update unit vector
1557afe3bc8aSJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1558afe3bc8aSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1559afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1560afe3bc8aSJeremy L Thompson 
1561afe3bc8aSJeremy L Thompson       // Basis action
1562afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1563afe3bc8aSJeremy L Thompson       switch (eval_mode) {
1564afe3bc8aSJeremy L Thompson         case CEED_EVAL_NONE:
1565afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1566afe3bc8aSJeremy L Thompson           break;
1567afe3bc8aSJeremy L Thompson         case CEED_EVAL_INTERP:
1568afe3bc8aSJeremy L Thompson         case CEED_EVAL_GRAD:
1569afe3bc8aSJeremy L Thompson         case CEED_EVAL_DIV:
1570afe3bc8aSJeremy L Thompson         case CEED_EVAL_CURL:
1571afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1572afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1573afe3bc8aSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1574afe3bc8aSJeremy L Thompson           break;
1575afe3bc8aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1576afe3bc8aSJeremy L Thompson           break;  // No action
1577afe3bc8aSJeremy L Thompson       }
1578afe3bc8aSJeremy L Thompson     }
1579afe3bc8aSJeremy L Thompson 
1580afe3bc8aSJeremy L Thompson     // Q function
1581afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1582afe3bc8aSJeremy L Thompson 
1583afe3bc8aSJeremy L Thompson     // Output basis apply if needed
1584afe3bc8aSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1585afe3bc8aSJeremy L Thompson       bool                is_active_output = false;
1586afe3bc8aSJeremy L Thompson       CeedEvalMode        eval_mode;
1587afe3bc8aSJeremy L Thompson       CeedVector          vec;
1588afe3bc8aSJeremy L Thompson       CeedElemRestriction elem_rstr;
1589afe3bc8aSJeremy L Thompson       CeedBasis           basis;
1590afe3bc8aSJeremy L Thompson 
1591afe3bc8aSJeremy L Thompson       // Get output vector
1592afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
1593afe3bc8aSJeremy L Thompson       is_active_output = vec == CEED_VECTOR_ACTIVE;
1594afe3bc8aSJeremy L Thompson       if (!is_active_output) continue;
1595afe3bc8aSJeremy L Thompson 
1596afe3bc8aSJeremy L Thompson       // Basis action
1597afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1598afe3bc8aSJeremy L Thompson       switch (eval_mode) {
1599afe3bc8aSJeremy L Thompson         case CEED_EVAL_NONE:
1600afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1601afe3bc8aSJeremy L Thompson           break;
1602afe3bc8aSJeremy L Thompson         case CEED_EVAL_INTERP:
1603afe3bc8aSJeremy L Thompson         case CEED_EVAL_GRAD:
1604afe3bc8aSJeremy L Thompson         case CEED_EVAL_DIV:
1605afe3bc8aSJeremy L Thompson         case CEED_EVAL_CURL:
1606afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1607afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
1608afe3bc8aSJeremy L Thompson                                                  impl->e_vecs[i + impl->num_inputs]));
1609afe3bc8aSJeremy L Thompson           break;
1610afe3bc8aSJeremy L Thompson         // LCOV_EXCL_START
1611afe3bc8aSJeremy L Thompson         case CEED_EVAL_WEIGHT: {
1612afe3bc8aSJeremy L Thompson           return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1613afe3bc8aSJeremy L Thompson           // LCOV_EXCL_STOP
1614afe3bc8aSJeremy L Thompson         }
1615afe3bc8aSJeremy L Thompson       }
1616afe3bc8aSJeremy L Thompson 
1617afe3bc8aSJeremy L Thompson       // Mask output e-vec
1618afe3bc8aSJeremy L Thompson       {
1619afe3bc8aSJeremy L Thompson         CeedInt  j = num_input_fields;
1620afe3bc8aSJeremy L Thompson         CeedSize out_size;
1621afe3bc8aSJeremy L Thompson 
1622afe3bc8aSJeremy L Thompson         CeedCallBackend(CeedVectorGetLength(impl->e_vecs[i + impl->num_inputs], &out_size));
1623afe3bc8aSJeremy L Thompson         for (j = 0; j < num_input_fields; j++) {
1624afe3bc8aSJeremy L Thompson           bool       is_active_input = false;
1625afe3bc8aSJeremy L Thompson           CeedSize   in_size;
1626afe3bc8aSJeremy L Thompson           CeedVector vec;
1627afe3bc8aSJeremy L Thompson 
1628afe3bc8aSJeremy L Thompson           // Skip non-active input
1629afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec));
1630afe3bc8aSJeremy L Thompson           is_active_input = vec == CEED_VECTOR_ACTIVE;
1631afe3bc8aSJeremy L Thompson           if (!is_active_input) continue;
1632afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorGetLength(impl->e_vecs[j], &in_size));
1633afe3bc8aSJeremy L Thompson           if (in_size == out_size) break;
1634afe3bc8aSJeremy L Thompson         }
1635afe3bc8aSJeremy L Thompson         CeedCheck(j < num_input_fields, CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Matching input field not found");
1636afe3bc8aSJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[i + impl->num_inputs], impl->e_vecs[j], impl->e_vecs[i + impl->num_inputs]));
1637afe3bc8aSJeremy L Thompson       }
1638afe3bc8aSJeremy L Thompson 
1639afe3bc8aSJeremy L Thompson       // Restrict
1640afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1641afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], assembled, request));
1642afe3bc8aSJeremy L Thompson 
1643afe3bc8aSJeremy L Thompson       // Reset q_vec for
1644afe3bc8aSJeremy L Thompson       if (eval_mode == CEED_EVAL_NONE) {
1645afe3bc8aSJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1646afe3bc8aSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1647afe3bc8aSJeremy L Thompson       }
1648afe3bc8aSJeremy L Thompson     }
1649afe3bc8aSJeremy L Thompson   }
1650afe3bc8aSJeremy L Thompson 
1651afe3bc8aSJeremy L Thompson   // Restore CEED_EVAL_NONE
1652afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1653afe3bc8aSJeremy L Thompson     CeedEvalMode        eval_mode;
1654afe3bc8aSJeremy L Thompson     CeedElemRestriction elem_rstr;
1655afe3bc8aSJeremy L Thompson 
1656afe3bc8aSJeremy L Thompson     // Get eval_mode
1657afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1658afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1659afe3bc8aSJeremy L Thompson 
1660afe3bc8aSJeremy L Thompson     // Restore evec
1661afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1662afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1663afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1664afe3bc8aSJeremy L Thompson     }
1665afe3bc8aSJeremy L Thompson   }
1666afe3bc8aSJeremy L Thompson 
1667afe3bc8aSJeremy L Thompson   // Restore input arrays
1668afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1669afe3bc8aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
167067d9480aSJeremy L Thompson }
167167d9480aSJeremy L Thompson 
167267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
16730d0321e0SJeremy L Thompson // Create operator
16740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
16750d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
16760d0321e0SJeremy L Thompson   Ceed              ceed;
16770d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
16780d0321e0SJeremy L Thompson 
1679b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
16802b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
16812b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
16822b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
16832b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
16842b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
16852b730f8bSJeremy L Thompson   CeedCallBackend(
16862b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
16872b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
16882b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
16892b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
16900d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16910d0321e0SJeremy L Thompson }
16920d0321e0SJeremy L Thompson 
16930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
169467d9480aSJeremy L Thompson // Create operator AtPoints
169567d9480aSJeremy L Thompson //------------------------------------------------------------------------------
169667d9480aSJeremy L Thompson int CeedOperatorCreateAtPoints_Hip(CeedOperator op) {
169767d9480aSJeremy L Thompson   Ceed              ceed;
169867d9480aSJeremy L Thompson   CeedOperator_Hip *impl;
169967d9480aSJeremy L Thompson 
170067d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
170167d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
170267d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
170367d9480aSJeremy L Thompson 
170467d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Hip));
170567d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip));
170667d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Hip));
170767d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
170867d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
170967d9480aSJeremy L Thompson }
171067d9480aSJeremy L Thompson 
171167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
1712