xref: /libCEED/backends/hip-ref/ceed-hip-ref-operator.c (revision afe3bc8a75e92753d9e19eda4e2e3134c127758b)
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,
734b7453713SJeremy L Thompson                                                      num_active_in * num_active_out * num_elem * Q, strides, rstr));
7350d0321e0SJeremy L Thompson     // Create assembled vector
736b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
7370d0321e0SJeremy L Thompson   }
7382b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
739b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
7400d0321e0SJeremy L Thompson 
7410d0321e0SJeremy L Thompson   // Input basis apply
742b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
7430d0321e0SJeremy L Thompson 
7440d0321e0SJeremy L Thompson   // Assemble QFunction
745b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
7460d0321e0SJeremy L Thompson     // Set Inputs
747004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
748b7453713SJeremy L Thompson     if (num_active_in > 1) {
749004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
7500d0321e0SJeremy L Thompson     }
7510d0321e0SJeremy L Thompson     // Set Outputs
752b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
753b7453713SJeremy L Thompson       CeedVector vec;
754b7453713SJeremy L Thompson 
7550d0321e0SJeremy L Thompson       // Get output vector
756b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
7570d0321e0SJeremy L Thompson       // Check if active output
7580d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
759b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
760b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
761b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
7620d0321e0SJeremy L Thompson       }
7630d0321e0SJeremy L Thompson     }
7640d0321e0SJeremy L Thompson     // Apply QFunction
765b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
7660d0321e0SJeremy L Thompson   }
7670d0321e0SJeremy L Thompson 
768004e4986SSebastian Grimberg   // Un-set output q_vecs to prevent accidental overwrite of Assembled
769b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
770b7453713SJeremy L Thompson     CeedVector vec;
771b7453713SJeremy L Thompson 
7720d0321e0SJeremy L Thompson     // Get output vector
773b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
7740d0321e0SJeremy L Thompson     // Check if active output
7750d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
776b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
7770d0321e0SJeremy L Thompson     }
7780d0321e0SJeremy L Thompson   }
7790d0321e0SJeremy L Thompson 
7800d0321e0SJeremy L Thompson   // Restore input arrays
781b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
7820d0321e0SJeremy L Thompson 
7830d0321e0SJeremy L Thompson   // Restore output
784b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
7850d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7860d0321e0SJeremy L Thompson }
7870d0321e0SJeremy L Thompson 
7880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7890d0321e0SJeremy L Thompson // Assemble Linear QFunction
7900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7912b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
7922b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
7930d0321e0SJeremy L Thompson }
7940d0321e0SJeremy L Thompson 
7950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
796b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
7970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7982b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
7992b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
8000d0321e0SJeremy L Thompson }
8010d0321e0SJeremy L Thompson 
8020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
803004e4986SSebastian Grimberg // Assemble Diagonal Setup
8040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
805cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
8060d0321e0SJeremy L Thompson   Ceed                ceed;
807004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
808cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
809004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
810b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
811b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
8120d0321e0SJeremy L Thompson   CeedQFunction       qf;
813b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
814b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
815b7453713SJeremy L Thompson 
816b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8172b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
818b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
8190d0321e0SJeremy L Thompson 
8200d0321e0SJeremy L Thompson   // Determine active input basis
821b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
822b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
823b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
8240d0321e0SJeremy L Thompson     CeedVector vec;
825b7453713SJeremy L Thompson 
826b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8270d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
828004e4986SSebastian Grimberg       CeedBasis    basis;
829004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
830b7453713SJeremy L Thompson 
831004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
832004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
833004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
834004e4986SSebastian Grimberg       basis_in = basis;
835004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
836004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
837004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
838004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
839004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
840004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
841004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
8420d0321e0SJeremy L Thompson       }
8430d0321e0SJeremy L Thompson     }
8440d0321e0SJeremy L Thompson   }
8450d0321e0SJeremy L Thompson 
8460d0321e0SJeremy L Thompson   // Determine active output basis
847b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
848b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
849b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
8500d0321e0SJeremy L Thompson     CeedVector vec;
851b7453713SJeremy L Thompson 
852b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8530d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
854004e4986SSebastian Grimberg       CeedBasis    basis;
855004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
856b7453713SJeremy L Thompson 
857004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
858004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
859004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
860004e4986SSebastian Grimberg       basis_out = basis;
861004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
862004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
863004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
864004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
865004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
866004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
867004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
8680d0321e0SJeremy L Thompson       }
8690d0321e0SJeremy L Thompson     }
8700d0321e0SJeremy L Thompson   }
8710d0321e0SJeremy L Thompson 
8720d0321e0SJeremy L Thompson   // Operator data struct
8732b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
8742b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
8750d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
876b7453713SJeremy L Thompson 
877cbfe683aSSebastian Grimberg   // Basis matrices
878004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
879004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
880004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
881004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
882004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
883004e4986SSebastian Grimberg   bool          has_eval_none    = false;
8840d0321e0SJeremy L Thompson 
8850d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
886004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
887004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
888004e4986SSebastian Grimberg   if (has_eval_none) {
8890d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
890b7453713SJeremy L Thompson 
891004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
892004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
893b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
894b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
895004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
8960d0321e0SJeremy L Thompson   }
8970d0321e0SJeremy L Thompson 
898004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
899004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
900004e4986SSebastian Grimberg     CeedFESpace fespace;
901004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
9020d0321e0SJeremy L Thompson 
903004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
904004e4986SSebastian Grimberg     switch (fespace) {
905004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
906004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
907004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
908004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
9090d0321e0SJeremy L Thompson 
910004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
911004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
9120d0321e0SJeremy L Thompson 
913004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
914004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
915004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
916004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
917004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
918004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
919004e4986SSebastian Grimberg         if (in) {
920004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
921004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
922004e4986SSebastian Grimberg         } else {
923004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
924004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
925004e4986SSebastian Grimberg         }
926004e4986SSebastian Grimberg       } break;
927004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
928004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
929004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
930004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
931004e4986SSebastian Grimberg 
932004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
933004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
934004e4986SSebastian Grimberg 
935004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
936004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
937004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
938004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
939004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
940004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
941004e4986SSebastian Grimberg         if (in) {
942004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
943004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
944004e4986SSebastian Grimberg         } else {
945004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
946004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
947004e4986SSebastian Grimberg         }
948004e4986SSebastian Grimberg       } break;
949004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
950004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
951004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
952004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
953004e4986SSebastian Grimberg 
954004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
955004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
956004e4986SSebastian Grimberg 
957004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
958004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
959004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
960004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
961004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
962004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
963004e4986SSebastian Grimberg         if (in) {
964004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
965004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
966004e4986SSebastian Grimberg         } else {
967004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
968004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
969004e4986SSebastian Grimberg         }
970004e4986SSebastian Grimberg       } break;
971004e4986SSebastian Grimberg     }
972004e4986SSebastian Grimberg   }
973004e4986SSebastian Grimberg 
974004e4986SSebastian Grimberg   // Arrays of eval_modes
975004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
976004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
977004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
978004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
979004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
980004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
9810d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9820d0321e0SJeremy L Thompson }
9830d0321e0SJeremy L Thompson 
9840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
985cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
986cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
987cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
988cbfe683aSSebastian Grimberg   Ceed                ceed;
98922070f95SJeremy L Thompson   char               *diagonal_kernel_source;
99022070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
991cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
992cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
993cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
994cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
995cbfe683aSSebastian Grimberg   CeedQFunction       qf;
996cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
997cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
998cbfe683aSSebastian Grimberg 
999cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1000cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1001cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1002cbfe683aSSebastian Grimberg 
1003cbfe683aSSebastian Grimberg   // Determine active input basis
1004cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1005cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1006cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1007cbfe683aSSebastian Grimberg     CeedVector vec;
1008cbfe683aSSebastian Grimberg 
1009cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1010cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1011cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1012cbfe683aSSebastian Grimberg 
1013cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1014cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1015cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1016cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1017cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1018cbfe683aSSebastian Grimberg       }
1019cbfe683aSSebastian Grimberg     }
1020cbfe683aSSebastian Grimberg   }
1021cbfe683aSSebastian Grimberg 
1022cbfe683aSSebastian Grimberg   // Determine active output basis
1023cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1024cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1025cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1026cbfe683aSSebastian Grimberg     CeedVector vec;
1027cbfe683aSSebastian Grimberg 
1028cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1029cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1030cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1031cbfe683aSSebastian Grimberg 
1032cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1033cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1034cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1035cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1036cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1037cbfe683aSSebastian Grimberg       }
1038cbfe683aSSebastian Grimberg     }
1039cbfe683aSSebastian Grimberg   }
1040cbfe683aSSebastian Grimberg 
1041cbfe683aSSebastian Grimberg   // Operator data struct
1042cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1043cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1044cbfe683aSSebastian Grimberg 
1045cbfe683aSSebastian Grimberg   // Assemble kernel
1046cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
1047cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
1048cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1049cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1050cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1051cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1052cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1053cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1054cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1055cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1056cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1057cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1058cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1059cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1060cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1061cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1062cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1063cbfe683aSSebastian Grimberg }
1064cbfe683aSSebastian Grimberg 
1065cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1066004e4986SSebastian Grimberg // Assemble Diagonal Core
10670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1068b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
10690d0321e0SJeremy L Thompson   Ceed                ceed;
1070cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1071b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
1072b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1073004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1074004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
10750d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
1076b7453713SJeremy L Thompson 
1077b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
10782b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
10790d0321e0SJeremy L Thompson 
10800d0321e0SJeremy L Thompson   // Assemble QFunction
1081004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1082004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1083004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
10840d0321e0SJeremy L Thompson 
1085cbfe683aSSebastian Grimberg   // Setup
1086cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
1087cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1088cbfe683aSSebastian Grimberg 
1089cbfe683aSSebastian Grimberg   assert(diag != NULL);
1090cbfe683aSSebastian Grimberg 
1091cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1092cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1093cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1094cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
10959330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1096b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1097b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
10989330daecSnbeams 
1099cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
1100cbfe683aSSebastian Grimberg   }
11010d0321e0SJeremy L Thompson 
1102004e4986SSebastian Grimberg   // Restriction and diagonal vector
1103004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1104004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1105004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1106004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1107004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1108004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1109004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1110004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1111004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
11120d0321e0SJeremy L Thompson   }
1113004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1114004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1115b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
11160d0321e0SJeremy L Thompson 
111791db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1118004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1119004e4986SSebastian Grimberg   if (num_nodes > 0) {
11200d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1121b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
1122b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
11230d0321e0SJeremy L Thompson 
11240d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1125004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1126004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1127004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1128004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1129004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1130b7453713SJeremy L Thompson 
1131b7453713SJeremy L Thompson     if (is_point_block) {
1132004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
11330d0321e0SJeremy L Thompson     } else {
1134004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
11350d0321e0SJeremy L Thompson     }
11360d0321e0SJeremy L Thompson 
11370d0321e0SJeremy L Thompson     // Restore arrays
1138b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1139b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
114091db28b6SZach Atkins   }
11410d0321e0SJeremy L Thompson 
11420d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1143b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
11440d0321e0SJeremy L Thompson 
11450d0321e0SJeremy L Thompson   // Cleanup
1146b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
11470d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11480d0321e0SJeremy L Thompson }
11490d0321e0SJeremy L Thompson 
11500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11510d0321e0SJeremy L Thompson // Assemble Linear Diagonal
11520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11532b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
11542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
11556aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11560d0321e0SJeremy L Thompson }
11570d0321e0SJeremy L Thompson 
11580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11590d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
11600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11612b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
11622b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
11636aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11640d0321e0SJeremy L Thompson }
11650d0321e0SJeremy L Thompson 
1166a835093fSnbeams //------------------------------------------------------------------------------
1167004e4986SSebastian Grimberg // Single Operator Assembly Setup
1168a835093fSnbeams //------------------------------------------------------------------------------
11699330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
1170a835093fSnbeams   Ceed                ceed;
117122070f95SJeremy L Thompson   char               *assembly_kernel_source;
117222070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1173004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
11743b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1175004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1176b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1177b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1178b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
1179b7453713SJeremy L Thompson   CeedQFunction       qf;
1180b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1181a835093fSnbeams   CeedOperator_Hip   *impl;
1182b7453713SJeremy L Thompson 
1183b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11842b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1185a835093fSnbeams 
1186a835093fSnbeams   // Get intput and output fields
11872b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1188a835093fSnbeams 
1189a835093fSnbeams   // Determine active input basis eval mode
11902b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
11912b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1192a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1193a835093fSnbeams     CeedVector vec;
1194b7453713SJeremy L Thompson 
11952b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1196a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1197004e4986SSebastian Grimberg       CeedBasis    basis;
1198b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1199b7453713SJeremy L Thompson 
1200004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1201004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1202004e4986SSebastian Grimberg       basis_in = basis;
12032b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1204004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1205004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1206004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
12072b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1208004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1209004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1210004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1211004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1212004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1213004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1214a835093fSnbeams         }
1215004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1216a835093fSnbeams       }
1217a835093fSnbeams     }
1218a835093fSnbeams   }
1219a835093fSnbeams 
1220a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
12212b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1222a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1223a835093fSnbeams     CeedVector vec;
1224b7453713SJeremy L Thompson 
12252b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1226a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1227004e4986SSebastian Grimberg       CeedBasis    basis;
1228b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1229b7453713SJeremy L Thompson 
1230004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1231004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1232004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1233004e4986SSebastian Grimberg       basis_out = basis;
12342b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1235004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1236004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1237004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1238004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1239004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
12402b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1241004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1242004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1243004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1244004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1245004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1246004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1247004e4986SSebastian Grimberg         }
1248004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1249a835093fSnbeams       }
1250a835093fSnbeams     }
1251a835093fSnbeams   }
1252004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1253a835093fSnbeams 
12542b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1255a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1256004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1257004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1258004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1259004e4986SSebastian Grimberg 
1260004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1261004e4986SSebastian Grimberg 
1262004e4986SSebastian Grimberg   if (fallback) {
1263004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1264004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1265004e4986SSebastian Grimberg   }
1266a835093fSnbeams 
1267a835093fSnbeams   // Compile kernels
1268004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1269004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
12702b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
127123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
12722b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
127323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1274004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1275004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1276004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1277cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
12789330daecSnbeams                                   use_ceedsize_idx));
1279004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
12802b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
12812b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1282a835093fSnbeams 
1283004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1284004e4986SSebastian Grimberg   {
1285004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1286004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1287004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1288004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1289004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1290a835093fSnbeams 
1291004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1292004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1293004e4986SSebastian Grimberg     }
1294004e4986SSebastian Grimberg     if (has_eval_none) {
1295004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1296004e4986SSebastian 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;
1297004e4986SSebastian Grimberg     }
1298b7453713SJeremy L Thompson 
1299b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1300004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1301004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1302004e4986SSebastian Grimberg 
1303004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1304004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1305004e4986SSebastian Grimberg       if (q_comp > 1) {
1306004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1307004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1308004e4986SSebastian Grimberg       }
1309004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1310004e4986SSebastian Grimberg 
1311004e4986SSebastian 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),
1312004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1313004e4986SSebastian Grimberg     }
1314004e4986SSebastian Grimberg 
1315004e4986SSebastian Grimberg     if (identity) {
1316004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1317a835093fSnbeams     }
1318a835093fSnbeams   }
1319a835093fSnbeams 
1320004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1321004e4986SSebastian Grimberg   {
1322004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1323004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1324004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1325004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1326004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1327b7453713SJeremy L Thompson 
1328004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1329004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1330004e4986SSebastian Grimberg     }
1331004e4986SSebastian Grimberg     if (has_eval_none) {
1332004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1333004e4986SSebastian 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;
1334a835093fSnbeams     }
1335a835093fSnbeams 
1336b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1337004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1338004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1339004e4986SSebastian Grimberg 
1340004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1341004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1342004e4986SSebastian Grimberg       if (q_comp > 1) {
1343004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1344004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1345004e4986SSebastian Grimberg       }
1346004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1347004e4986SSebastian Grimberg 
1348004e4986SSebastian 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),
1349004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1350004e4986SSebastian Grimberg     }
1351004e4986SSebastian Grimberg 
1352004e4986SSebastian Grimberg     if (identity) {
1353004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1354a835093fSnbeams     }
1355a835093fSnbeams   }
1356a835093fSnbeams   return CEED_ERROR_SUCCESS;
1357a835093fSnbeams }
1358a835093fSnbeams 
1359a835093fSnbeams //------------------------------------------------------------------------------
1360cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1361cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1362cefa2673SJeremy L Thompson //
1363ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1364cefa2673SJeremy L Thompson // modes).
1365cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1366a835093fSnbeams //------------------------------------------------------------------------------
13672b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1368a835093fSnbeams   Ceed                ceed;
1369b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1370004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1371b7453713SJeremy L Thompson   CeedScalar         *values_array;
1372004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1373b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1374004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1375004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1376004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1377004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1378a835093fSnbeams   CeedOperator_Hip   *impl;
1379b7453713SJeremy L Thompson 
1380b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
13812b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1382a835093fSnbeams 
1383a835093fSnbeams   // Assemble QFunction
1384004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1385004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1386004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1387a835093fSnbeams 
13889330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
13899330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
13909330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1391004e4986SSebastian Grimberg 
13929330daecSnbeams   // Setup
1393004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1394004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1395004e4986SSebastian Grimberg 
1396004e4986SSebastian Grimberg   assert(asmb != NULL);
1397004e4986SSebastian Grimberg 
1398004e4986SSebastian Grimberg   // Assemble element operator
1399004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1400004e4986SSebastian Grimberg   values_array += offset;
1401004e4986SSebastian Grimberg 
1402004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1403004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1404004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1405004e4986SSebastian Grimberg 
1406004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1407004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1408004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1409004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1410004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1411004e4986SSebastian Grimberg   }
1412004e4986SSebastian Grimberg 
1413004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1414004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1415004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1416004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1417004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1418004e4986SSebastian Grimberg 
1419004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1420004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1421004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1422004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1423004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1424004e4986SSebastian Grimberg     }
1425004e4986SSebastian Grimberg   } else {
1426004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1427004e4986SSebastian Grimberg     orients_out      = orients_in;
1428004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
14299330daecSnbeams   }
14309330daecSnbeams 
1431a835093fSnbeams   // Compute B^T D B
1432004e4986SSebastian Grimberg   CeedInt shared_mem =
1433004e4986SSebastian 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)) *
1434004e4986SSebastian Grimberg       sizeof(CeedScalar);
1435004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1436004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1437004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1438b7453713SJeremy L Thompson 
14392b730f8bSJeremy L Thompson   CeedCallBackend(
1440004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1441a835093fSnbeams 
1442a835093fSnbeams   // Restore arrays
14432b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1444004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1445a835093fSnbeams 
1446a835093fSnbeams   // Cleanup
14472b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1448004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1449004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1450004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1451004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1452004e4986SSebastian Grimberg   }
1453004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1454004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1455004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1456004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1457004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1458004e4986SSebastian Grimberg     }
1459004e4986SSebastian Grimberg   }
1460a835093fSnbeams   return CEED_ERROR_SUCCESS;
1461a835093fSnbeams }
1462a835093fSnbeams 
1463a835093fSnbeams //------------------------------------------------------------------------------
146467d9480aSJeremy L Thompson // Assemble Linear QFunction AtPoints
146567d9480aSJeremy L Thompson //------------------------------------------------------------------------------
146667d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
146767d9480aSJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
146867d9480aSJeremy L Thompson }
146967d9480aSJeremy L Thompson 
147067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
147167d9480aSJeremy L Thompson // Assemble Linear Diagonal AtPoints
147267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
147367d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1474*afe3bc8aSJeremy L Thompson   bool                is_active_at_points = true;
1475*afe3bc8aSJeremy L Thompson   CeedSize            e_vec_size          = 0;
1476*afe3bc8aSJeremy L Thompson   CeedInt             max_num_points, num_elem, num_input_fields, num_output_fields, elem_size_active = 1, num_comp_active = 1;
1477*afe3bc8aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1478*afe3bc8aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1479*afe3bc8aSJeremy L Thompson   CeedQFunction       qf;
1480*afe3bc8aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1481*afe3bc8aSJeremy L Thompson   CeedOperator_Hip   *impl;
1482*afe3bc8aSJeremy L Thompson 
1483*afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1484*afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1485*afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1486*afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1487*afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1488*afe3bc8aSJeremy L Thompson   CeedInt num_points[num_elem];
1489*afe3bc8aSJeremy L Thompson 
1490*afe3bc8aSJeremy L Thompson   // Setup
1491*afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
1492*afe3bc8aSJeremy L Thompson   max_num_points = impl->max_num_points;
1493*afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
1494*afe3bc8aSJeremy L Thompson 
1495*afe3bc8aSJeremy L Thompson   // Input Evecs and Restriction
1496*afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1497*afe3bc8aSJeremy L Thompson 
1498*afe3bc8aSJeremy L Thompson   // Check if active field is at points
1499*afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1500*afe3bc8aSJeremy L Thompson     CeedRestrictionType rstr_type;
1501*afe3bc8aSJeremy L Thompson     CeedVector          vec;
1502*afe3bc8aSJeremy L Thompson     CeedElemRestriction elem_rstr;
1503*afe3bc8aSJeremy L Thompson 
1504*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1505*afe3bc8aSJeremy L Thompson     // Skip non-active input
1506*afe3bc8aSJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1507*afe3bc8aSJeremy L Thompson 
1508*afe3bc8aSJeremy L Thompson     // Get active restriction type
1509*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1510*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1511*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1512*afe3bc8aSJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1513*afe3bc8aSJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size_active));
1514*afe3bc8aSJeremy L Thompson   }
1515*afe3bc8aSJeremy L Thompson 
1516*afe3bc8aSJeremy L Thompson   // Get point coordinates
1517*afe3bc8aSJeremy L Thompson   if (!impl->point_coords_elem) {
1518*afe3bc8aSJeremy L Thompson     CeedVector          point_coords = NULL;
1519*afe3bc8aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1520*afe3bc8aSJeremy L Thompson 
1521*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1522*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1523*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1524*afe3bc8aSJeremy L Thompson   }
1525*afe3bc8aSJeremy L Thompson 
1526*afe3bc8aSJeremy L Thompson   // Input basis apply if needed
1527*afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1528*afe3bc8aSJeremy L Thompson 
1529*afe3bc8aSJeremy L Thompson   // Output pointers, as necessary
1530*afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1531*afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1532*afe3bc8aSJeremy L Thompson 
1533*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1534*afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1535*afe3bc8aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1536*afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1537*afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1538*afe3bc8aSJeremy L Thompson     }
1539*afe3bc8aSJeremy L Thompson   }
1540*afe3bc8aSJeremy L Thompson 
1541*afe3bc8aSJeremy L Thompson   // Loop over active fields
1542*afe3bc8aSJeremy L Thompson   e_vec_size = (is_active_at_points ? max_num_points : elem_size_active) * num_comp_active;
1543*afe3bc8aSJeremy L Thompson   for (CeedInt s = 0; s < e_vec_size; s++) {
1544*afe3bc8aSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1545*afe3bc8aSJeremy L Thompson       bool         is_active_input = false;
1546*afe3bc8aSJeremy L Thompson       CeedEvalMode eval_mode;
1547*afe3bc8aSJeremy L Thompson       CeedVector   vec;
1548*afe3bc8aSJeremy L Thompson       CeedBasis    basis;
1549*afe3bc8aSJeremy L Thompson 
1550*afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1551*afe3bc8aSJeremy L Thompson       // Skip non-active input
1552*afe3bc8aSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1553*afe3bc8aSJeremy L Thompson       if (!is_active_input) continue;
1554*afe3bc8aSJeremy L Thompson 
1555*afe3bc8aSJeremy L Thompson       // Update unit vector
1556*afe3bc8aSJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1557*afe3bc8aSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1558*afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1559*afe3bc8aSJeremy L Thompson 
1560*afe3bc8aSJeremy L Thompson       // Basis action
1561*afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1562*afe3bc8aSJeremy L Thompson       switch (eval_mode) {
1563*afe3bc8aSJeremy L Thompson         case CEED_EVAL_NONE:
1564*afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1565*afe3bc8aSJeremy L Thompson           break;
1566*afe3bc8aSJeremy L Thompson         case CEED_EVAL_INTERP:
1567*afe3bc8aSJeremy L Thompson         case CEED_EVAL_GRAD:
1568*afe3bc8aSJeremy L Thompson         case CEED_EVAL_DIV:
1569*afe3bc8aSJeremy L Thompson         case CEED_EVAL_CURL:
1570*afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1571*afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1572*afe3bc8aSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1573*afe3bc8aSJeremy L Thompson           break;
1574*afe3bc8aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1575*afe3bc8aSJeremy L Thompson           break;  // No action
1576*afe3bc8aSJeremy L Thompson       }
1577*afe3bc8aSJeremy L Thompson     }
1578*afe3bc8aSJeremy L Thompson 
1579*afe3bc8aSJeremy L Thompson     // Q function
1580*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1581*afe3bc8aSJeremy L Thompson 
1582*afe3bc8aSJeremy L Thompson     // Output basis apply if needed
1583*afe3bc8aSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1584*afe3bc8aSJeremy L Thompson       bool                is_active_output = false;
1585*afe3bc8aSJeremy L Thompson       CeedEvalMode        eval_mode;
1586*afe3bc8aSJeremy L Thompson       CeedVector          vec;
1587*afe3bc8aSJeremy L Thompson       CeedElemRestriction elem_rstr;
1588*afe3bc8aSJeremy L Thompson       CeedBasis           basis;
1589*afe3bc8aSJeremy L Thompson 
1590*afe3bc8aSJeremy L Thompson       // Get output vector
1591*afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
1592*afe3bc8aSJeremy L Thompson       is_active_output = vec == CEED_VECTOR_ACTIVE;
1593*afe3bc8aSJeremy L Thompson       if (!is_active_output) continue;
1594*afe3bc8aSJeremy L Thompson 
1595*afe3bc8aSJeremy L Thompson       // Basis action
1596*afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1597*afe3bc8aSJeremy L Thompson       switch (eval_mode) {
1598*afe3bc8aSJeremy L Thompson         case CEED_EVAL_NONE:
1599*afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1600*afe3bc8aSJeremy L Thompson           break;
1601*afe3bc8aSJeremy L Thompson         case CEED_EVAL_INTERP:
1602*afe3bc8aSJeremy L Thompson         case CEED_EVAL_GRAD:
1603*afe3bc8aSJeremy L Thompson         case CEED_EVAL_DIV:
1604*afe3bc8aSJeremy L Thompson         case CEED_EVAL_CURL:
1605*afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1606*afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
1607*afe3bc8aSJeremy L Thompson                                                  impl->e_vecs[i + impl->num_inputs]));
1608*afe3bc8aSJeremy L Thompson           break;
1609*afe3bc8aSJeremy L Thompson         // LCOV_EXCL_START
1610*afe3bc8aSJeremy L Thompson         case CEED_EVAL_WEIGHT: {
1611*afe3bc8aSJeremy L Thompson           return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1612*afe3bc8aSJeremy L Thompson           // LCOV_EXCL_STOP
1613*afe3bc8aSJeremy L Thompson         }
1614*afe3bc8aSJeremy L Thompson       }
1615*afe3bc8aSJeremy L Thompson 
1616*afe3bc8aSJeremy L Thompson       // Mask output e-vec
1617*afe3bc8aSJeremy L Thompson       {
1618*afe3bc8aSJeremy L Thompson         CeedInt  j = num_input_fields;
1619*afe3bc8aSJeremy L Thompson         CeedSize out_size;
1620*afe3bc8aSJeremy L Thompson 
1621*afe3bc8aSJeremy L Thompson         CeedCallBackend(CeedVectorGetLength(impl->e_vecs[i + impl->num_inputs], &out_size));
1622*afe3bc8aSJeremy L Thompson         for (j = 0; j < num_input_fields; j++) {
1623*afe3bc8aSJeremy L Thompson           bool       is_active_input = false;
1624*afe3bc8aSJeremy L Thompson           CeedSize   in_size;
1625*afe3bc8aSJeremy L Thompson           CeedVector vec;
1626*afe3bc8aSJeremy L Thompson 
1627*afe3bc8aSJeremy L Thompson           // Skip non-active input
1628*afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec));
1629*afe3bc8aSJeremy L Thompson           is_active_input = vec == CEED_VECTOR_ACTIVE;
1630*afe3bc8aSJeremy L Thompson           if (!is_active_input) continue;
1631*afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorGetLength(impl->e_vecs[j], &in_size));
1632*afe3bc8aSJeremy L Thompson           if (in_size == out_size) break;
1633*afe3bc8aSJeremy L Thompson         }
1634*afe3bc8aSJeremy L Thompson         CeedCheck(j < num_input_fields, CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Matching input field not found");
1635*afe3bc8aSJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[i + impl->num_inputs], impl->e_vecs[j], impl->e_vecs[i + impl->num_inputs]));
1636*afe3bc8aSJeremy L Thompson       }
1637*afe3bc8aSJeremy L Thompson 
1638*afe3bc8aSJeremy L Thompson       // Restrict
1639*afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1640*afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], assembled, request));
1641*afe3bc8aSJeremy L Thompson 
1642*afe3bc8aSJeremy L Thompson       // Reset q_vec for
1643*afe3bc8aSJeremy L Thompson       if (eval_mode == CEED_EVAL_NONE) {
1644*afe3bc8aSJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1645*afe3bc8aSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1646*afe3bc8aSJeremy L Thompson       }
1647*afe3bc8aSJeremy L Thompson     }
1648*afe3bc8aSJeremy L Thompson   }
1649*afe3bc8aSJeremy L Thompson 
1650*afe3bc8aSJeremy L Thompson   // Restore CEED_EVAL_NONE
1651*afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1652*afe3bc8aSJeremy L Thompson     CeedEvalMode        eval_mode;
1653*afe3bc8aSJeremy L Thompson     CeedElemRestriction elem_rstr;
1654*afe3bc8aSJeremy L Thompson 
1655*afe3bc8aSJeremy L Thompson     // Get eval_mode
1656*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1657*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1658*afe3bc8aSJeremy L Thompson 
1659*afe3bc8aSJeremy L Thompson     // Restore evec
1660*afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1661*afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1662*afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1663*afe3bc8aSJeremy L Thompson     }
1664*afe3bc8aSJeremy L Thompson   }
1665*afe3bc8aSJeremy L Thompson 
1666*afe3bc8aSJeremy L Thompson   // Restore input arrays
1667*afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1668*afe3bc8aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
166967d9480aSJeremy L Thompson }
167067d9480aSJeremy L Thompson 
167167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
16720d0321e0SJeremy L Thompson // Create operator
16730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
16740d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
16750d0321e0SJeremy L Thompson   Ceed              ceed;
16760d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
16770d0321e0SJeremy L Thompson 
1678b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
16792b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
16802b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
16812b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
16822b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
16832b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
16842b730f8bSJeremy L Thompson   CeedCallBackend(
16852b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
16862b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
16872b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
16882b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
16890d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16900d0321e0SJeremy L Thompson }
16910d0321e0SJeremy L Thompson 
16920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
169367d9480aSJeremy L Thompson // Create operator AtPoints
169467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
169567d9480aSJeremy L Thompson int CeedOperatorCreateAtPoints_Hip(CeedOperator op) {
169667d9480aSJeremy L Thompson   Ceed              ceed;
169767d9480aSJeremy L Thompson   CeedOperator_Hip *impl;
169867d9480aSJeremy L Thompson 
169967d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
170067d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
170167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
170267d9480aSJeremy L Thompson 
170367d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Hip));
170467d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip));
170567d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Hip));
170667d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
170767d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
170867d9480aSJeremy L Thompson }
170967d9480aSJeremy L Thompson 
171067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
1711