xref: /libCEED/backends/hip-ref/ceed-hip-ref-operator.c (revision f8a0df597ca176fee6b07766b6124704acaa0050)
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
293aab95c0SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_in));
30*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_out));
31*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->apply_add_basis_out));
32b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
33b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
340d0321e0SJeremy L Thompson   }
35b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
36c1222711SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
370d0321e0SJeremy L Thompson 
38b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
39b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
400d0321e0SJeremy L Thompson   }
41b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
420d0321e0SJeremy L Thompson 
43b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
44b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
450d0321e0SJeremy L Thompson   }
46b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
4767d9480aSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem));
480d0321e0SJeremy L Thompson 
49b2165e7aSSebastian Grimberg   // QFunction assembly data
50b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
51b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
520d0321e0SJeremy L Thompson   }
53b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
540d0321e0SJeremy L Thompson 
550d0321e0SJeremy L Thompson   // Diag data
560d0321e0SJeremy L Thompson   if (impl->diag) {
570d0321e0SJeremy L Thompson     Ceed ceed;
58b7453713SJeremy L Thompson 
592b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
60cbfe683aSSebastian Grimberg     if (impl->diag->module) {
612b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
62cbfe683aSSebastian Grimberg     }
63cbfe683aSSebastian Grimberg     if (impl->diag->module_point_block) {
64cbfe683aSSebastian Grimberg       CeedCallHip(ceed, hipModuleUnload(impl->diag->module_point_block));
65cbfe683aSSebastian Grimberg     }
66004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_in));
67004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_out));
682b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
69b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_in));
70b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_out));
71b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_in));
72b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_out));
73004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_in));
74004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_out));
75004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_in));
76004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_out));
77004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
78b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
79b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
80b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
810d0321e0SJeremy L Thompson   }
822b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
830d0321e0SJeremy L Thompson 
84a835093fSnbeams   if (impl->asmb) {
85a835093fSnbeams     Ceed ceed;
86b7453713SJeremy L Thompson 
872b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
882b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
892b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
902b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
91a835093fSnbeams   }
922b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
93a835093fSnbeams 
942b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
950d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
960d0321e0SJeremy L Thompson }
970d0321e0SJeremy L Thompson 
980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
990d0321e0SJeremy L Thompson // Setup infields or outfields
1000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
101*f8a0df59SJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, bool *skip_rstr, bool *apply_add_basis,
102*f8a0df59SJeremy L Thompson                                        CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
1030d0321e0SJeremy L Thompson   Ceed                ceed;
104b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
105b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
1060d0321e0SJeremy L Thompson 
107b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
108b7453713SJeremy L Thompson   if (is_input) {
109b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
110b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1110d0321e0SJeremy L Thompson   } else {
112b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
113b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1140d0321e0SJeremy L Thompson   }
1150d0321e0SJeremy L Thompson 
1160d0321e0SJeremy L Thompson   // Loop over fields
117b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
118004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
119b7453713SJeremy L Thompson     CeedSize     q_size;
120004e4986SSebastian Grimberg     CeedInt      size;
121004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
122b7453713SJeremy L Thompson     CeedBasis    basis;
1230d0321e0SJeremy L Thompson 
124004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
125004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
126004e4986SSebastian Grimberg       CeedElemRestriction elem_rstr;
1270d0321e0SJeremy L Thompson 
1280d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
129004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
130004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1310d0321e0SJeremy L Thompson 
1320d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
133b7453713SJeremy L Thompson       if (is_input) {
134004e4986SSebastian Grimberg         CeedVector vec;
135004e4986SSebastian Grimberg 
136004e4986SSebastian Grimberg         // Check for passive input
137b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
138b7453713SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
139004e4986SSebastian Grimberg           // Check eval_mode
140004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1410d0321e0SJeremy L Thompson             // Check for strided restriction
142b7453713SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
143b7453713SJeremy L Thompson             if (is_strided) {
1440d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
145b7453713SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1460d0321e0SJeremy L Thompson             }
1470d0321e0SJeremy L Thompson           }
1480d0321e0SJeremy L Thompson         }
1490d0321e0SJeremy L Thompson       }
150b7453713SJeremy L Thompson       if (skip_restriction) {
151ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
152b7453713SJeremy L Thompson         e_vecs[i + start_e] = NULL;
1530d0321e0SJeremy L Thompson       } else {
154b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1550d0321e0SJeremy L Thompson       }
1560d0321e0SJeremy L Thompson     }
1570d0321e0SJeremy L Thompson 
158004e4986SSebastian Grimberg     switch (eval_mode) {
1590d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
160b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
161b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
162b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1630d0321e0SJeremy L Thompson         break;
1640d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1650d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
166004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
167004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
168b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
169b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
170b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1710d0321e0SJeremy L Thompson         break;
1720d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
173b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
174b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
175b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
17667d9480aSJeremy L Thompson         if (is_at_points) {
17767d9480aSJeremy L Thompson           CeedInt num_points[num_elem];
17867d9480aSJeremy L Thompson 
17967d9480aSJeremy L Thompson           for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q;
18067d9480aSJeremy L Thompson           CeedCallBackend(
18167d9480aSJeremy L Thompson               CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i]));
18267d9480aSJeremy L Thompson         } else {
183b7453713SJeremy L Thompson           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
18467d9480aSJeremy L Thompson         }
1850d0321e0SJeremy L Thompson         break;
1860d0321e0SJeremy L Thompson     }
1870d0321e0SJeremy L Thompson   }
188*f8a0df59SJeremy L Thompson   // Drop duplicate restrictions
1893aab95c0SJeremy L Thompson   if (is_input) {
1903aab95c0SJeremy L Thompson     for (CeedInt i = 0; i < num_fields; i++) {
1913aab95c0SJeremy L Thompson       CeedVector          vec_i;
1923aab95c0SJeremy L Thompson       CeedElemRestriction rstr_i;
1933aab95c0SJeremy L Thompson 
1943aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
1953aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
1963aab95c0SJeremy L Thompson       for (CeedInt j = i + 1; j < num_fields; j++) {
1973aab95c0SJeremy L Thompson         CeedVector          vec_j;
1983aab95c0SJeremy L Thompson         CeedElemRestriction rstr_j;
1993aab95c0SJeremy L Thompson 
2003aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
2013aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
2023aab95c0SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
203*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e]));
2043aab95c0SJeremy L Thompson           skip_rstr[j] = true;
2053aab95c0SJeremy L Thompson         }
2063aab95c0SJeremy L Thompson       }
2073aab95c0SJeremy L Thompson     }
208*f8a0df59SJeremy L Thompson   } else {
209*f8a0df59SJeremy L Thompson     for (CeedInt i = num_fields - 1; i >= 0; i--) {
210*f8a0df59SJeremy L Thompson       CeedVector          vec_i;
211*f8a0df59SJeremy L Thompson       CeedElemRestriction rstr_i;
212*f8a0df59SJeremy L Thompson 
213*f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
214*f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
215*f8a0df59SJeremy L Thompson       for (CeedInt j = i - 1; j >= 0; j--) {
216*f8a0df59SJeremy L Thompson         CeedVector          vec_j;
217*f8a0df59SJeremy L Thompson         CeedElemRestriction rstr_j;
218*f8a0df59SJeremy L Thompson 
219*f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
220*f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
221*f8a0df59SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
222*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e]));
223*f8a0df59SJeremy L Thompson           skip_rstr[j]       = true;
224*f8a0df59SJeremy L Thompson           apply_add_basis[i] = true;
225*f8a0df59SJeremy L Thompson         }
226*f8a0df59SJeremy L Thompson       }
227*f8a0df59SJeremy L Thompson     }
2283aab95c0SJeremy L Thompson   }
2290d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2300d0321e0SJeremy L Thompson }
2310d0321e0SJeremy L Thompson 
2320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
233ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
2340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2350d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
2360d0321e0SJeremy L Thompson   Ceed                ceed;
237b7453713SJeremy L Thompson   bool                is_setup_done;
238b7453713SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
239b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
2400d0321e0SJeremy L Thompson   CeedQFunction       qf;
241b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
242b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
243b7453713SJeremy L Thompson 
244b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
245b7453713SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
246b7453713SJeremy L Thompson 
247b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
248b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
2492b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
2502b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
251b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
252b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
253b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2540d0321e0SJeremy L Thompson 
2550d0321e0SJeremy L Thompson   // Allocate
256b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
2573aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
258*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
259*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
260c1222711SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
261b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
262b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
263b7453713SJeremy L Thompson   impl->num_inputs  = num_input_fields;
264b7453713SJeremy L Thompson   impl->num_outputs = num_output_fields;
2650d0321e0SJeremy L Thompson 
266b7453713SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
2670d0321e0SJeremy L Thompson   // Infields
2683aab95c0SJeremy L Thompson   CeedCallBackend(
269*f8a0df59SJeremy L Thompson       CeedOperatorSetupFields_Hip(qf, op, true, false, impl->skip_rstr_in, NULL, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2700d0321e0SJeremy L Thompson   // Outfields
271*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, false, impl->skip_rstr_out, impl->apply_add_basis_out, impl->e_vecs, impl->q_vecs_out,
272*f8a0df59SJeremy L Thompson                                               num_input_fields, num_output_fields, Q, num_elem));
2730d0321e0SJeremy L Thompson 
2742b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2750d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2760d0321e0SJeremy L Thompson }
2770d0321e0SJeremy L Thompson 
2780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2790d0321e0SJeremy L Thompson // Setup Operator Inputs
2800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
281b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
282b7453713SJeremy L Thompson                                               CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
283b7453713SJeremy L Thompson                                               CeedOperator_Hip *impl, CeedRequest *request) {
284b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
285004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
2860d0321e0SJeremy L Thompson     CeedVector          vec;
287b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2880d0321e0SJeremy L Thompson 
2890d0321e0SJeremy L Thompson     // Get input vector
290b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2910d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
292b7453713SJeremy L Thompson       if (skip_active) continue;
293b7453713SJeremy L Thompson       else vec = in_vec;
2940d0321e0SJeremy L Thompson     }
2950d0321e0SJeremy L Thompson 
296004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
297004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2980d0321e0SJeremy L Thompson     } else {
2990d0321e0SJeremy L Thompson       // Get input vector
300b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3010d0321e0SJeremy L Thompson       // Get input element restriction
302b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
303b7453713SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
3040d0321e0SJeremy L Thompson       // Restrict, if necessary
305b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {
3060d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
307b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3080d0321e0SJeremy L Thompson       } else {
309c1222711SJeremy L Thompson         uint64_t state;
310c1222711SJeremy L Thompson 
311c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
3123aab95c0SJeremy L Thompson         if (state != impl->input_states[i] && !impl->skip_rstr_in[i]) {
313b7453713SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
314c1222711SJeremy L Thompson         }
3153aab95c0SJeremy L Thompson         impl->input_states[i] = state;
3160d0321e0SJeremy L Thompson         // Get evec
317b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3180d0321e0SJeremy L Thompson       }
3190d0321e0SJeremy L Thompson     }
3200d0321e0SJeremy L Thompson   }
3210d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3220d0321e0SJeremy L Thompson }
3230d0321e0SJeremy L Thompson 
3240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3250d0321e0SJeremy L Thompson // Input Basis Action
3260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
327b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
328b7453713SJeremy L Thompson                                              CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
3292b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
330b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
331b7453713SJeremy L Thompson     CeedInt             elem_size, size;
332004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
333b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
3340d0321e0SJeremy L Thompson     CeedBasis           basis;
3350d0321e0SJeremy L Thompson 
3360d0321e0SJeremy L Thompson     // Skip active input
337b7453713SJeremy L Thompson     if (skip_active) {
3380d0321e0SJeremy L Thompson       CeedVector vec;
339b7453713SJeremy L Thompson 
340b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3412b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3420d0321e0SJeremy L Thompson     }
343004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
344b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
345b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
346004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
347b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3480d0321e0SJeremy L Thompson     // Basis action
349004e4986SSebastian Grimberg     switch (eval_mode) {
3500d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
351b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
3520d0321e0SJeremy L Thompson         break;
3530d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3540d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
355004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
356004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
357b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
358004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3590d0321e0SJeremy L Thompson         break;
3600d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
3610d0321e0SJeremy L Thompson         break;  // No action
3620d0321e0SJeremy L Thompson     }
3630d0321e0SJeremy L Thompson   }
3640d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3650d0321e0SJeremy L Thompson }
3660d0321e0SJeremy L Thompson 
3670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3680d0321e0SJeremy L Thompson // Restore Input Vectors
3690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
370b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
371b7453713SJeremy L Thompson                                                 const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
372b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
373004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
3740d0321e0SJeremy L Thompson     CeedVector   vec;
375004e4986SSebastian Grimberg 
3760d0321e0SJeremy L Thompson     // Skip active input
377b7453713SJeremy L Thompson     if (skip_active) {
378b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3792b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3800d0321e0SJeremy L Thompson     }
381004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
382004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3830d0321e0SJeremy L Thompson     } else {
384b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
385b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
386b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
3870d0321e0SJeremy L Thompson       } else {
388b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
3890d0321e0SJeremy L Thompson       }
3900d0321e0SJeremy L Thompson     }
3910d0321e0SJeremy L Thompson   }
3920d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3930d0321e0SJeremy L Thompson }
3940d0321e0SJeremy L Thompson 
3950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3960d0321e0SJeremy L Thompson // Apply and add to output
3970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
398b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
399b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
400b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
401b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
4020d0321e0SJeremy L Thompson   CeedQFunction       qf;
403b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
404b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
405b7453713SJeremy L Thompson 
406b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4072b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4082b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
409b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
410b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
411b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4120d0321e0SJeremy L Thompson 
4130d0321e0SJeremy L Thompson   // Setup
4142b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4150d0321e0SJeremy L Thompson 
4160d0321e0SJeremy L Thompson   // Input Evecs and Restriction
417b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
4180d0321e0SJeremy L Thompson 
4190d0321e0SJeremy L Thompson   // Input basis apply if needed
420b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
4210d0321e0SJeremy L Thompson 
4220d0321e0SJeremy L Thompson   // Output pointers, as necessary
423b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
424004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
425b7453713SJeremy L Thompson 
426004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
427004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
4280d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
429b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
430b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
4310d0321e0SJeremy L Thompson     }
4320d0321e0SJeremy L Thompson   }
4330d0321e0SJeremy L Thompson 
4340d0321e0SJeremy L Thompson   // Q function
435b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
4360d0321e0SJeremy L Thompson 
4370d0321e0SJeremy L Thompson   // Output basis apply if needed
438b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
439004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
440b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
441b7453713SJeremy L Thompson     CeedBasis           basis;
442b7453713SJeremy L Thompson 
443004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
444b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
445b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
446004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
447b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4480d0321e0SJeremy L Thompson     // Basis action
449004e4986SSebastian Grimberg     switch (eval_mode) {
4500d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
451004e4986SSebastian Grimberg         break;  // No action
4520d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4530d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
454004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
455004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
456b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
457*f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
458*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAdd(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
459*f8a0df59SJeremy L Thompson         } else {
460004e4986SSebastian Grimberg           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
461*f8a0df59SJeremy L Thompson         }
4620d0321e0SJeremy L Thompson         break;
4630d0321e0SJeremy L Thompson       // LCOV_EXCL_START
4640d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
4656e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
4660d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
4670d0321e0SJeremy L Thompson       }
4680d0321e0SJeremy L Thompson     }
469004e4986SSebastian Grimberg   }
4700d0321e0SJeremy L Thompson 
4710d0321e0SJeremy L Thompson   // Output restriction
472b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
473004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
474b7453713SJeremy L Thompson     CeedVector          vec;
475b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
476b7453713SJeremy L Thompson 
4770d0321e0SJeremy L Thompson     // Restore evec
478004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
479004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
480b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4810d0321e0SJeremy L Thompson     }
482*f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
4830d0321e0SJeremy L Thompson     // Get output vector
484b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4850d0321e0SJeremy L Thompson     // Restrict
486b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4870d0321e0SJeremy L Thompson     // Active
488b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4890d0321e0SJeremy L Thompson 
490b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4910d0321e0SJeremy L Thompson   }
4920d0321e0SJeremy L Thompson 
4930d0321e0SJeremy L Thompson   // Restore input arrays
494b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4950d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4960d0321e0SJeremy L Thompson }
4970d0321e0SJeremy L Thompson 
4980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
49967d9480aSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
50067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
50167d9480aSJeremy L Thompson static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) {
50267d9480aSJeremy L Thompson   Ceed                ceed;
50367d9480aSJeremy L Thompson   bool                is_setup_done;
50467d9480aSJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
50567d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
50667d9480aSJeremy L Thompson   CeedQFunction       qf;
50767d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
50867d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
50967d9480aSJeremy L Thompson 
51067d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
51167d9480aSJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
51267d9480aSJeremy L Thompson 
51367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
51467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
51567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
51667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
51767d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
51867d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
51967d9480aSJeremy L Thompson   {
52067d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr = NULL;
52167d9480aSJeremy L Thompson 
52267d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL));
52367d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points));
52467d9480aSJeremy L Thompson   }
52567d9480aSJeremy L Thompson   impl->max_num_points = max_num_points;
52667d9480aSJeremy L Thompson 
52767d9480aSJeremy L Thompson   // Allocate
52867d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
5293aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
530*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
531*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
53267d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
53367d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
53467d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
53567d9480aSJeremy L Thompson   impl->num_inputs  = num_input_fields;
53667d9480aSJeremy L Thompson   impl->num_outputs = num_output_fields;
53767d9480aSJeremy L Thompson 
53867d9480aSJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
53967d9480aSJeremy L Thompson   // Infields
540*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, true, impl->skip_rstr_in, NULL, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields,
5413aab95c0SJeremy L Thompson                                               max_num_points, num_elem));
54267d9480aSJeremy L Thompson   // Outfields
543*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, true, impl->skip_rstr_out, impl->apply_add_basis_out, impl->e_vecs, impl->q_vecs_out,
544*f8a0df59SJeremy L Thompson                                               num_input_fields, num_output_fields, max_num_points, num_elem));
54567d9480aSJeremy L Thompson 
54667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
54767d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
54867d9480aSJeremy L Thompson }
54967d9480aSJeremy L Thompson 
55067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
55167d9480aSJeremy L Thompson // Input Basis Action AtPoints
55267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
55367d9480aSJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Hip(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields,
55467d9480aSJeremy L Thompson                                                      CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active,
55567d9480aSJeremy L Thompson                                                      CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
55667d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
55767d9480aSJeremy L Thompson     CeedInt             elem_size, size;
55867d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
55967d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
56067d9480aSJeremy L Thompson     CeedBasis           basis;
56167d9480aSJeremy L Thompson 
56267d9480aSJeremy L Thompson     // Skip active input
56367d9480aSJeremy L Thompson     if (skip_active) {
56467d9480aSJeremy L Thompson       CeedVector vec;
56567d9480aSJeremy L Thompson 
56667d9480aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
56767d9480aSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
56867d9480aSJeremy L Thompson     }
56967d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
57067d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
57167d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
57267d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
57367d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
57467d9480aSJeremy L Thompson     // Basis action
57567d9480aSJeremy L Thompson     switch (eval_mode) {
57667d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
57767d9480aSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
57867d9480aSJeremy L Thompson         break;
57967d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
58067d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
58167d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
58267d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
58367d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
58467d9480aSJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
58567d9480aSJeremy L Thompson                                                impl->q_vecs_in[i]));
58667d9480aSJeremy L Thompson         break;
58767d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT:
58867d9480aSJeremy L Thompson         break;  // No action
58967d9480aSJeremy L Thompson     }
59067d9480aSJeremy L Thompson   }
59167d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
59267d9480aSJeremy L Thompson }
59367d9480aSJeremy L Thompson 
59467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
59567d9480aSJeremy L Thompson // Apply and add to output AtPoints
59667d9480aSJeremy L Thompson //------------------------------------------------------------------------------
59767d9480aSJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
59867d9480aSJeremy L Thompson   CeedInt             max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
59967d9480aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
60067d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
60167d9480aSJeremy L Thompson   CeedQFunction       qf;
60267d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
60367d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
60467d9480aSJeremy L Thompson 
60567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
60667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
60767d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
60867d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
60967d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
61067d9480aSJeremy L Thompson   CeedInt num_points[num_elem];
61167d9480aSJeremy L Thompson 
61267d9480aSJeremy L Thompson   // Setup
61367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
61467d9480aSJeremy L Thompson   max_num_points = impl->max_num_points;
61567d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
61667d9480aSJeremy L Thompson 
61767d9480aSJeremy L Thompson   // Input Evecs and Restriction
61867d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
61967d9480aSJeremy L Thompson 
62067d9480aSJeremy L Thompson   // Get point coordinates
62167d9480aSJeremy L Thompson   if (!impl->point_coords_elem) {
62267d9480aSJeremy L Thompson     CeedVector          point_coords = NULL;
62367d9480aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
62467d9480aSJeremy L Thompson 
62567d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
62667d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
62767d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
62867d9480aSJeremy L Thompson   }
62967d9480aSJeremy L Thompson 
63067d9480aSJeremy L Thompson   // Input basis apply if needed
63167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
63267d9480aSJeremy L Thompson 
63367d9480aSJeremy L Thompson   // Output pointers, as necessary
63467d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
63567d9480aSJeremy L Thompson     CeedEvalMode eval_mode;
63667d9480aSJeremy L Thompson 
63767d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
63867d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
63967d9480aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
64067d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
64167d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
64267d9480aSJeremy L Thompson     }
64367d9480aSJeremy L Thompson   }
64467d9480aSJeremy L Thompson 
64567d9480aSJeremy L Thompson   // Q function
64667d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
64767d9480aSJeremy L Thompson 
64867d9480aSJeremy L Thompson   // Output basis apply if needed
64967d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
65067d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
65167d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
65267d9480aSJeremy L Thompson     CeedBasis           basis;
65367d9480aSJeremy L Thompson 
65467d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
65567d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
65667d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
65767d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
65867d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
65967d9480aSJeremy L Thompson     // Basis action
66067d9480aSJeremy L Thompson     switch (eval_mode) {
66167d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
66267d9480aSJeremy L Thompson         break;  // No action
66367d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
66467d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
66567d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
66667d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
66767d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
668*f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
669*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAddAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
670*f8a0df59SJeremy L Thompson                                                     impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
671*f8a0df59SJeremy L Thompson         } else {
67267d9480aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
67367d9480aSJeremy L Thompson                                                  impl->e_vecs[i + impl->num_inputs]));
674*f8a0df59SJeremy L Thompson         }
67567d9480aSJeremy L Thompson         break;
67667d9480aSJeremy L Thompson       // LCOV_EXCL_START
67767d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT: {
67867d9480aSJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
67967d9480aSJeremy L Thompson         // LCOV_EXCL_STOP
68067d9480aSJeremy L Thompson       }
68167d9480aSJeremy L Thompson     }
68267d9480aSJeremy L Thompson   }
68367d9480aSJeremy L Thompson 
68467d9480aSJeremy L Thompson   // Output restriction
68567d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
68667d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
68767d9480aSJeremy L Thompson     CeedVector          vec;
68867d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
68967d9480aSJeremy L Thompson 
69067d9480aSJeremy L Thompson     // Restore evec
69167d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
69267d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
69367d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
69467d9480aSJeremy L Thompson     }
695*f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
69667d9480aSJeremy L Thompson     // Get output vector
69767d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
69867d9480aSJeremy L Thompson     // Restrict
69967d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
70067d9480aSJeremy L Thompson     // Active
70167d9480aSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
70267d9480aSJeremy L Thompson 
70367d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
70467d9480aSJeremy L Thompson   }
70567d9480aSJeremy L Thompson 
70667d9480aSJeremy L Thompson   // Restore input arrays
70767d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
70867d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
70967d9480aSJeremy L Thompson }
71067d9480aSJeremy L Thompson 
71167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
712004e4986SSebastian Grimberg // Linear QFunction Assembly Core
7130d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7142b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
7150d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
716b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
717b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
718b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
719004e4986SSebastian Grimberg   CeedVector         *active_inputs;
720b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
721b7453713SJeremy L Thompson   CeedQFunction       qf;
722b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
723b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
724b7453713SJeremy L Thompson 
7252b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
726b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
727e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
728e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
729b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
730004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
731b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
732b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
733004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
734004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
7350d0321e0SJeremy L Thompson 
7360d0321e0SJeremy L Thompson   // Setup
7372b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
7380d0321e0SJeremy L Thompson 
7390d0321e0SJeremy L Thompson   // Input Evecs and Restriction
740b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
7410d0321e0SJeremy L Thompson 
7420d0321e0SJeremy L Thompson   // Count number of active input fields
743b7453713SJeremy L Thompson   if (!num_active_in) {
744b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
745b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
746b7453713SJeremy L Thompson       CeedVector  vec;
747b7453713SJeremy L Thompson 
7480d0321e0SJeremy L Thompson       // Get input vector
749b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
7500d0321e0SJeremy L Thompson       // Check if active input
7510d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
752b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
753b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
754b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
755004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
7560d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
757004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
758004e4986SSebastian Grimberg 
759004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
760b7453713SJeremy L Thompson           CeedCallBackend(
761004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
7620d0321e0SJeremy L Thompson         }
763b7453713SJeremy L Thompson         num_active_in += size;
764b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
7650d0321e0SJeremy L Thompson       }
7660d0321e0SJeremy L Thompson     }
767b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
768004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
7690d0321e0SJeremy L Thompson   }
7700d0321e0SJeremy L Thompson 
7710d0321e0SJeremy L Thompson   // Count number of active output fields
772b7453713SJeremy L Thompson   if (!num_active_out) {
773b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
774b7453713SJeremy L Thompson       CeedVector vec;
775b7453713SJeremy L Thompson 
7760d0321e0SJeremy L Thompson       // Get output vector
777b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
7780d0321e0SJeremy L Thompson       // Check if active output
7790d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
780b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
781b7453713SJeremy L Thompson         num_active_out += size;
7820d0321e0SJeremy L Thompson       }
7830d0321e0SJeremy L Thompson     }
784b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
7850d0321e0SJeremy L Thompson   }
7860d0321e0SJeremy L Thompson 
7870d0321e0SJeremy L Thompson   // Check sizes
788b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
7890d0321e0SJeremy L Thompson 
7900d0321e0SJeremy L Thompson   // Build objects if needed
7910d0321e0SJeremy L Thompson   if (build_objects) {
792b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
793b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
794b7453713SJeremy L Thompson 
795004e4986SSebastian Grimberg     // Create output restriction
796b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
7970a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
7980a5597ceSJeremy L Thompson                                                      rstr));
7990d0321e0SJeremy L Thompson     // Create assembled vector
800b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
8010d0321e0SJeremy L Thompson   }
8022b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
803b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
8040d0321e0SJeremy L Thompson 
8050d0321e0SJeremy L Thompson   // Input basis apply
806b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
8070d0321e0SJeremy L Thompson 
8080d0321e0SJeremy L Thompson   // Assemble QFunction
809b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
8100d0321e0SJeremy L Thompson     // Set Inputs
811004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
812b7453713SJeremy L Thompson     if (num_active_in > 1) {
813004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
8140d0321e0SJeremy L Thompson     }
8150d0321e0SJeremy L Thompson     // Set Outputs
816b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
817b7453713SJeremy L Thompson       CeedVector vec;
818b7453713SJeremy L Thompson 
8190d0321e0SJeremy L Thompson       // Get output vector
820b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
8210d0321e0SJeremy L Thompson       // Check if active output
8220d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
823b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
824b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
825b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
8260d0321e0SJeremy L Thompson       }
8270d0321e0SJeremy L Thompson     }
8280d0321e0SJeremy L Thompson     // Apply QFunction
829b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
8300d0321e0SJeremy L Thompson   }
8310d0321e0SJeremy L Thompson 
832004e4986SSebastian Grimberg   // Un-set output q_vecs to prevent accidental overwrite of Assembled
833b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
834b7453713SJeremy L Thompson     CeedVector vec;
835b7453713SJeremy L Thompson 
8360d0321e0SJeremy L Thompson     // Get output vector
837b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
8380d0321e0SJeremy L Thompson     // Check if active output
8390d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
840b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
8410d0321e0SJeremy L Thompson     }
8420d0321e0SJeremy L Thompson   }
8430d0321e0SJeremy L Thompson 
8440d0321e0SJeremy L Thompson   // Restore input arrays
845b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
8460d0321e0SJeremy L Thompson 
8470d0321e0SJeremy L Thompson   // Restore output
848b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
8490d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8500d0321e0SJeremy L Thompson }
8510d0321e0SJeremy L Thompson 
8520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8530d0321e0SJeremy L Thompson // Assemble Linear QFunction
8540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8552b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
8562b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
8570d0321e0SJeremy L Thompson }
8580d0321e0SJeremy L Thompson 
8590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
860b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
8610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8622b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
8632b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
8640d0321e0SJeremy L Thompson }
8650d0321e0SJeremy L Thompson 
8660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
867004e4986SSebastian Grimberg // Assemble Diagonal Setup
8680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
869cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
8700d0321e0SJeremy L Thompson   Ceed                ceed;
871004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
872cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
873004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
874b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
875b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
8760d0321e0SJeremy L Thompson   CeedQFunction       qf;
877b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
878b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
879b7453713SJeremy L Thompson 
880b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8812b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
882b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
8830d0321e0SJeremy L Thompson 
8840d0321e0SJeremy L Thompson   // Determine active input basis
885b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
886b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
887b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
8880d0321e0SJeremy L Thompson     CeedVector vec;
889b7453713SJeremy L Thompson 
890b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8910d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
892004e4986SSebastian Grimberg       CeedBasis    basis;
893004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
894b7453713SJeremy L Thompson 
895004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
896004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
897004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
898004e4986SSebastian Grimberg       basis_in = basis;
899004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
900004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
901004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
902004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
903004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
904004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
905004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
9060d0321e0SJeremy L Thompson       }
9070d0321e0SJeremy L Thompson     }
9080d0321e0SJeremy L Thompson   }
9090d0321e0SJeremy L Thompson 
9100d0321e0SJeremy L Thompson   // Determine active output basis
911b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
912b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
913b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9140d0321e0SJeremy L Thompson     CeedVector vec;
915b7453713SJeremy L Thompson 
916b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
9170d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
918004e4986SSebastian Grimberg       CeedBasis    basis;
919004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
920b7453713SJeremy L Thompson 
921004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
922004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
923004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
924004e4986SSebastian Grimberg       basis_out = basis;
925004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
926004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
927004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
928004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
929004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
930004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
931004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
9320d0321e0SJeremy L Thompson       }
9330d0321e0SJeremy L Thompson     }
9340d0321e0SJeremy L Thompson   }
9350d0321e0SJeremy L Thompson 
9360d0321e0SJeremy L Thompson   // Operator data struct
9372b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
9382b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
9390d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
940b7453713SJeremy L Thompson 
941cbfe683aSSebastian Grimberg   // Basis matrices
942004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
943004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
944004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
945004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
946004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
947004e4986SSebastian Grimberg   bool          has_eval_none    = false;
9480d0321e0SJeremy L Thompson 
9490d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
950004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
951004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
952004e4986SSebastian Grimberg   if (has_eval_none) {
9530d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
954b7453713SJeremy L Thompson 
955004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
956004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
957b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
958b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
959004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
9600d0321e0SJeremy L Thompson   }
9610d0321e0SJeremy L Thompson 
962004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
963004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
964004e4986SSebastian Grimberg     CeedFESpace fespace;
965004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
9660d0321e0SJeremy L Thompson 
967004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
968004e4986SSebastian Grimberg     switch (fespace) {
969004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
970004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
971004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
972004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
9730d0321e0SJeremy L Thompson 
974004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
975004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
9760d0321e0SJeremy L Thompson 
977004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
978004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
979004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
980004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
981004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
982004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
983004e4986SSebastian Grimberg         if (in) {
984004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
985004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
986004e4986SSebastian Grimberg         } else {
987004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
988004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
989004e4986SSebastian Grimberg         }
990004e4986SSebastian Grimberg       } break;
991004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
992004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
993004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
994004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
995004e4986SSebastian Grimberg 
996004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
997004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
998004e4986SSebastian Grimberg 
999004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1000004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1001004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1002004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
1003004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
1004004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
1005004e4986SSebastian Grimberg         if (in) {
1006004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1007004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
1008004e4986SSebastian Grimberg         } else {
1009004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1010004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
1011004e4986SSebastian Grimberg         }
1012004e4986SSebastian Grimberg       } break;
1013004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
1014004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
1015004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
1016004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
1017004e4986SSebastian Grimberg 
1018004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1019004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
1020004e4986SSebastian Grimberg 
1021004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1022004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1023004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1024004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
1025004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
1026004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
1027004e4986SSebastian Grimberg         if (in) {
1028004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1029004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
1030004e4986SSebastian Grimberg         } else {
1031004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1032004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
1033004e4986SSebastian Grimberg         }
1034004e4986SSebastian Grimberg       } break;
1035004e4986SSebastian Grimberg     }
1036004e4986SSebastian Grimberg   }
1037004e4986SSebastian Grimberg 
1038004e4986SSebastian Grimberg   // Arrays of eval_modes
1039004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
1040004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
1041004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
1042004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
1043004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
1044004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
10450d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10460d0321e0SJeremy L Thompson }
10470d0321e0SJeremy L Thompson 
10480d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1049cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
1050cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1051cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
1052cbfe683aSSebastian Grimberg   Ceed                ceed;
105322070f95SJeremy L Thompson   char               *diagonal_kernel_source;
105422070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
1055cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
1056cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
1057cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
1058cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
1059cbfe683aSSebastian Grimberg   CeedQFunction       qf;
1060cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
1061cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
1062cbfe683aSSebastian Grimberg 
1063cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1064cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1065cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1066cbfe683aSSebastian Grimberg 
1067cbfe683aSSebastian Grimberg   // Determine active input basis
1068cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1069cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1070cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1071cbfe683aSSebastian Grimberg     CeedVector vec;
1072cbfe683aSSebastian Grimberg 
1073cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1074cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1075cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1076cbfe683aSSebastian Grimberg 
1077cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1078cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1079cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1080cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1081cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1082cbfe683aSSebastian Grimberg       }
1083cbfe683aSSebastian Grimberg     }
1084cbfe683aSSebastian Grimberg   }
1085cbfe683aSSebastian Grimberg 
1086cbfe683aSSebastian Grimberg   // Determine active output basis
1087cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1088cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1089cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1090cbfe683aSSebastian Grimberg     CeedVector vec;
1091cbfe683aSSebastian Grimberg 
1092cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1093cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1094cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1095cbfe683aSSebastian Grimberg 
1096cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1097cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1098cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1099cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1100cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1101cbfe683aSSebastian Grimberg       }
1102cbfe683aSSebastian Grimberg     }
1103cbfe683aSSebastian Grimberg   }
1104cbfe683aSSebastian Grimberg 
1105cbfe683aSSebastian Grimberg   // Operator data struct
1106cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1107cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1108cbfe683aSSebastian Grimberg 
1109cbfe683aSSebastian Grimberg   // Assemble kernel
1110cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
1111cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
1112cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1113cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1114cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1115cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1116cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1117cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1118cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1119cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1120cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1121cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1122cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1123cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1124cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1125cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1126cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1127cbfe683aSSebastian Grimberg }
1128cbfe683aSSebastian Grimberg 
1129cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1130004e4986SSebastian Grimberg // Assemble Diagonal Core
11310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1132b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
11330d0321e0SJeremy L Thompson   Ceed                ceed;
1134cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1135b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
1136b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1137004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1138004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
11390d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
1140b7453713SJeremy L Thompson 
1141b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11422b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
11430d0321e0SJeremy L Thompson 
11440d0321e0SJeremy L Thompson   // Assemble QFunction
1145004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1146004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1147004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
11480d0321e0SJeremy L Thompson 
1149cbfe683aSSebastian Grimberg   // Setup
1150cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
1151cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1152cbfe683aSSebastian Grimberg 
1153cbfe683aSSebastian Grimberg   assert(diag != NULL);
1154cbfe683aSSebastian Grimberg 
1155cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1156cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1157cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1158cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
11599330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1160b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1161b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
11629330daecSnbeams 
1163cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
1164cbfe683aSSebastian Grimberg   }
11650d0321e0SJeremy L Thompson 
1166004e4986SSebastian Grimberg   // Restriction and diagonal vector
1167004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1168004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1169004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1170004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1171004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1172004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1173004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1174004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1175004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
11760d0321e0SJeremy L Thompson   }
1177004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1178004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1179b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
11800d0321e0SJeremy L Thompson 
118191db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1182004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1183004e4986SSebastian Grimberg   if (num_nodes > 0) {
11840d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1185b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
1186b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
11870d0321e0SJeremy L Thompson 
11880d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1189004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1190004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1191004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1192004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1193004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1194b7453713SJeremy L Thompson 
1195b7453713SJeremy L Thompson     if (is_point_block) {
1196004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
11970d0321e0SJeremy L Thompson     } else {
1198004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
11990d0321e0SJeremy L Thompson     }
12000d0321e0SJeremy L Thompson 
12010d0321e0SJeremy L Thompson     // Restore arrays
1202b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1203b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
120491db28b6SZach Atkins   }
12050d0321e0SJeremy L Thompson 
12060d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1207b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
12080d0321e0SJeremy L Thompson 
12090d0321e0SJeremy L Thompson   // Cleanup
1210b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
12110d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12120d0321e0SJeremy L Thompson }
12130d0321e0SJeremy L Thompson 
12140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12150d0321e0SJeremy L Thompson // Assemble Linear Diagonal
12160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12172b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
12182b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
12196aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12200d0321e0SJeremy L Thompson }
12210d0321e0SJeremy L Thompson 
12220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12230d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
12240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12252b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
12262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
12276aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12280d0321e0SJeremy L Thompson }
12290d0321e0SJeremy L Thompson 
1230a835093fSnbeams //------------------------------------------------------------------------------
1231004e4986SSebastian Grimberg // Single Operator Assembly Setup
1232a835093fSnbeams //------------------------------------------------------------------------------
12339330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
1234a835093fSnbeams   Ceed                ceed;
123522070f95SJeremy L Thompson   char               *assembly_kernel_source;
123622070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1237004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
12383b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1239004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1240b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1241b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1242b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
1243b7453713SJeremy L Thompson   CeedQFunction       qf;
1244b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1245a835093fSnbeams   CeedOperator_Hip   *impl;
1246b7453713SJeremy L Thompson 
1247b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12482b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1249a835093fSnbeams 
1250a835093fSnbeams   // Get intput and output fields
12512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1252a835093fSnbeams 
1253a835093fSnbeams   // Determine active input basis eval mode
12542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
12552b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1256a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1257a835093fSnbeams     CeedVector vec;
1258b7453713SJeremy L Thompson 
12592b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1260a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1261004e4986SSebastian Grimberg       CeedBasis    basis;
1262b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1263b7453713SJeremy L Thompson 
1264004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1265004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1266004e4986SSebastian Grimberg       basis_in = basis;
12672b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1268004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1269004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1270004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
12712b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1272004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1273004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1274004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1275004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1276004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1277004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1278a835093fSnbeams         }
1279004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1280a835093fSnbeams       }
1281a835093fSnbeams     }
1282a835093fSnbeams   }
1283a835093fSnbeams 
1284a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
12852b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1286a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1287a835093fSnbeams     CeedVector vec;
1288b7453713SJeremy L Thompson 
12892b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1290a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1291004e4986SSebastian Grimberg       CeedBasis    basis;
1292b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1293b7453713SJeremy L Thompson 
1294004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1295004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1296004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1297004e4986SSebastian Grimberg       basis_out = basis;
12982b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1299004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1300004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1301004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1302004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1303004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
13042b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1305004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1306004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1307004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1308004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1309004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1310004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1311004e4986SSebastian Grimberg         }
1312004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1313a835093fSnbeams       }
1314a835093fSnbeams     }
1315a835093fSnbeams   }
1316004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1317a835093fSnbeams 
13182b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1319a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1320004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1321004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1322004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1323004e4986SSebastian Grimberg 
1324004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1325004e4986SSebastian Grimberg 
1326004e4986SSebastian Grimberg   if (fallback) {
1327004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1328004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1329004e4986SSebastian Grimberg   }
1330a835093fSnbeams 
1331a835093fSnbeams   // Compile kernels
1332004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1333004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
13342b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
133523d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
13362b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
133723d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1338004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1339004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1340004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1341cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
13429330daecSnbeams                                   use_ceedsize_idx));
1343004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
13442b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
13452b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1346a835093fSnbeams 
1347004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1348004e4986SSebastian Grimberg   {
1349004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1350004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1351004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1352004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1353004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1354a835093fSnbeams 
1355004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1356004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1357004e4986SSebastian Grimberg     }
1358004e4986SSebastian Grimberg     if (has_eval_none) {
1359004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1360004e4986SSebastian 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;
1361004e4986SSebastian Grimberg     }
1362b7453713SJeremy L Thompson 
1363b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1364004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1365004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1366004e4986SSebastian Grimberg 
1367004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1368004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1369004e4986SSebastian Grimberg       if (q_comp > 1) {
1370004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1371004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1372004e4986SSebastian Grimberg       }
1373004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1374004e4986SSebastian Grimberg 
1375004e4986SSebastian 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),
1376004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1377004e4986SSebastian Grimberg     }
1378004e4986SSebastian Grimberg 
1379004e4986SSebastian Grimberg     if (identity) {
1380004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1381a835093fSnbeams     }
1382a835093fSnbeams   }
1383a835093fSnbeams 
1384004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1385004e4986SSebastian Grimberg   {
1386004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1387004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1388004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1389004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1390004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1391b7453713SJeremy L Thompson 
1392004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1393004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1394004e4986SSebastian Grimberg     }
1395004e4986SSebastian Grimberg     if (has_eval_none) {
1396004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1397004e4986SSebastian 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;
1398a835093fSnbeams     }
1399a835093fSnbeams 
1400b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1401004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1402004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1403004e4986SSebastian Grimberg 
1404004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1405004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1406004e4986SSebastian Grimberg       if (q_comp > 1) {
1407004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1408004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1409004e4986SSebastian Grimberg       }
1410004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1411004e4986SSebastian Grimberg 
1412004e4986SSebastian 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),
1413004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1414004e4986SSebastian Grimberg     }
1415004e4986SSebastian Grimberg 
1416004e4986SSebastian Grimberg     if (identity) {
1417004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1418a835093fSnbeams     }
1419a835093fSnbeams   }
1420a835093fSnbeams   return CEED_ERROR_SUCCESS;
1421a835093fSnbeams }
1422a835093fSnbeams 
1423a835093fSnbeams //------------------------------------------------------------------------------
1424cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1425cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1426cefa2673SJeremy L Thompson //
1427ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1428cefa2673SJeremy L Thompson // modes).
1429cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1430a835093fSnbeams //------------------------------------------------------------------------------
14312b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1432a835093fSnbeams   Ceed                ceed;
1433b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1434004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1435b7453713SJeremy L Thompson   CeedScalar         *values_array;
1436004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1437b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1438004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1439004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1440004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1441004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1442a835093fSnbeams   CeedOperator_Hip   *impl;
1443b7453713SJeremy L Thompson 
1444b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
14452b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1446a835093fSnbeams 
1447a835093fSnbeams   // Assemble QFunction
1448004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1449004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1450004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1451a835093fSnbeams 
14529330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
14539330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
14549330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1455004e4986SSebastian Grimberg 
14569330daecSnbeams   // Setup
1457004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1458004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1459004e4986SSebastian Grimberg 
1460004e4986SSebastian Grimberg   assert(asmb != NULL);
1461004e4986SSebastian Grimberg 
1462004e4986SSebastian Grimberg   // Assemble element operator
1463004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1464004e4986SSebastian Grimberg   values_array += offset;
1465004e4986SSebastian Grimberg 
1466004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1467004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1468004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1469004e4986SSebastian Grimberg 
1470004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1471004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1472004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1473004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1474004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1475004e4986SSebastian Grimberg   }
1476004e4986SSebastian Grimberg 
1477004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1478004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1479004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1480004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1481004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1482004e4986SSebastian Grimberg 
1483004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1484004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1485004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1486004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1487004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1488004e4986SSebastian Grimberg     }
1489004e4986SSebastian Grimberg   } else {
1490004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1491004e4986SSebastian Grimberg     orients_out      = orients_in;
1492004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
14939330daecSnbeams   }
14949330daecSnbeams 
1495a835093fSnbeams   // Compute B^T D B
1496004e4986SSebastian Grimberg   CeedInt shared_mem =
1497004e4986SSebastian 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)) *
1498004e4986SSebastian Grimberg       sizeof(CeedScalar);
1499004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1500004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1501004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1502b7453713SJeremy L Thompson 
15032b730f8bSJeremy L Thompson   CeedCallBackend(
1504004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1505a835093fSnbeams 
1506a835093fSnbeams   // Restore arrays
15072b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1508004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1509a835093fSnbeams 
1510a835093fSnbeams   // Cleanup
15112b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1512004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1513004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1514004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1515004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1516004e4986SSebastian Grimberg   }
1517004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1518004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1519004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1520004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1521004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1522004e4986SSebastian Grimberg     }
1523004e4986SSebastian Grimberg   }
1524a835093fSnbeams   return CEED_ERROR_SUCCESS;
1525a835093fSnbeams }
1526a835093fSnbeams 
1527a835093fSnbeams //------------------------------------------------------------------------------
152867d9480aSJeremy L Thompson // Assemble Linear QFunction AtPoints
152967d9480aSJeremy L Thompson //------------------------------------------------------------------------------
153067d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
153167d9480aSJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
153267d9480aSJeremy L Thompson }
153367d9480aSJeremy L Thompson 
153467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
153567d9480aSJeremy L Thompson // Assemble Linear Diagonal AtPoints
153667d9480aSJeremy L Thompson //------------------------------------------------------------------------------
153767d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1538382e9c83SJeremy L Thompson   CeedInt             max_num_points, num_elem, num_input_fields, num_output_fields;
1539afe3bc8aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1540afe3bc8aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1541afe3bc8aSJeremy L Thompson   CeedQFunction       qf;
1542afe3bc8aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1543afe3bc8aSJeremy L Thompson   CeedOperator_Hip   *impl;
1544afe3bc8aSJeremy L Thompson 
1545afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1546afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1547afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1548afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1549afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1550afe3bc8aSJeremy L Thompson   CeedInt num_points[num_elem];
1551afe3bc8aSJeremy L Thompson 
1552afe3bc8aSJeremy L Thompson   // Setup
1553afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
1554afe3bc8aSJeremy L Thompson   max_num_points = impl->max_num_points;
1555afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
1556afe3bc8aSJeremy L Thompson 
1557afe3bc8aSJeremy L Thompson   // Input Evecs and Restriction
1558afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1559afe3bc8aSJeremy L Thompson 
1560afe3bc8aSJeremy L Thompson   // Get point coordinates
1561afe3bc8aSJeremy L Thompson   if (!impl->point_coords_elem) {
1562afe3bc8aSJeremy L Thompson     CeedVector          point_coords = NULL;
1563afe3bc8aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1564afe3bc8aSJeremy L Thompson 
1565afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1566afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1567afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1568afe3bc8aSJeremy L Thompson   }
1569afe3bc8aSJeremy L Thompson 
1570382e9c83SJeremy L Thompson   // Clear active input Qvecs
1571382e9c83SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1572382e9c83SJeremy L Thompson     CeedVector vec;
1573382e9c83SJeremy L Thompson 
1574382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1575382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1576382e9c83SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
1577382e9c83SJeremy L Thompson   }
1578382e9c83SJeremy L Thompson 
1579afe3bc8aSJeremy L Thompson   // Input basis apply if needed
1580afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1581afe3bc8aSJeremy L Thompson 
1582afe3bc8aSJeremy L Thompson   // Output pointers, as necessary
1583afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1584afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1585afe3bc8aSJeremy L Thompson 
1586afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1587afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1588afe3bc8aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1589afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1590afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1591afe3bc8aSJeremy L Thompson     }
1592afe3bc8aSJeremy L Thompson   }
1593afe3bc8aSJeremy L Thompson 
1594afe3bc8aSJeremy L Thompson   // Loop over active fields
1595afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1596382e9c83SJeremy L Thompson     bool                is_active_at_points = true;
1597382e9c83SJeremy L Thompson     CeedInt             elem_size = 1, num_comp_active = 1, e_vec_size = 0;
1598382e9c83SJeremy L Thompson     CeedRestrictionType rstr_type;
1599382e9c83SJeremy L Thompson     CeedVector          vec;
1600382e9c83SJeremy L Thompson     CeedElemRestriction elem_rstr;
1601382e9c83SJeremy L Thompson 
1602382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1603382e9c83SJeremy L Thompson     // -- Skip non-active input
1604382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1605382e9c83SJeremy L Thompson 
1606382e9c83SJeremy L Thompson     // -- Get active restriction type
1607382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1608382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1609382e9c83SJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1610382e9c83SJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1611382e9c83SJeremy L Thompson     else elem_size = max_num_points;
1612382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1613382e9c83SJeremy L Thompson 
1614382e9c83SJeremy L Thompson     e_vec_size = elem_size * num_comp_active;
1615382e9c83SJeremy L Thompson     for (CeedInt s = 0; s < e_vec_size; s++) {
1616afe3bc8aSJeremy L Thompson       bool         is_active_input = false;
1617afe3bc8aSJeremy L Thompson       CeedEvalMode eval_mode;
1618afe3bc8aSJeremy L Thompson       CeedVector   vec;
1619afe3bc8aSJeremy L Thompson       CeedBasis    basis;
1620afe3bc8aSJeremy L Thompson 
1621afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1622afe3bc8aSJeremy L Thompson       // Skip non-active input
1623afe3bc8aSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1624afe3bc8aSJeremy L Thompson       if (!is_active_input) continue;
1625afe3bc8aSJeremy L Thompson 
1626afe3bc8aSJeremy L Thompson       // Update unit vector
162713062808SJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1628afe3bc8aSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1629afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1630afe3bc8aSJeremy L Thompson 
1631afe3bc8aSJeremy L Thompson       // Basis action
1632afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1633afe3bc8aSJeremy L Thompson       switch (eval_mode) {
1634afe3bc8aSJeremy L Thompson         case CEED_EVAL_NONE:
1635afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1636afe3bc8aSJeremy L Thompson           break;
1637afe3bc8aSJeremy L Thompson         case CEED_EVAL_INTERP:
1638afe3bc8aSJeremy L Thompson         case CEED_EVAL_GRAD:
1639afe3bc8aSJeremy L Thompson         case CEED_EVAL_DIV:
1640afe3bc8aSJeremy L Thompson         case CEED_EVAL_CURL:
1641afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1642afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1643afe3bc8aSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1644afe3bc8aSJeremy L Thompson           break;
1645afe3bc8aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1646afe3bc8aSJeremy L Thompson           break;  // No action
1647afe3bc8aSJeremy L Thompson       }
1648afe3bc8aSJeremy L Thompson 
1649afe3bc8aSJeremy L Thompson       // Q function
1650afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1651afe3bc8aSJeremy L Thompson 
1652afe3bc8aSJeremy L Thompson       // Output basis apply if needed
1653382e9c83SJeremy L Thompson       for (CeedInt j = 0; j < num_output_fields; j++) {
1654afe3bc8aSJeremy L Thompson         bool                is_active_output = false;
1655382e9c83SJeremy L Thompson         CeedInt             elem_size        = 0;
1656382e9c83SJeremy L Thompson         CeedRestrictionType rstr_type;
1657afe3bc8aSJeremy L Thompson         CeedEvalMode        eval_mode;
1658afe3bc8aSJeremy L Thompson         CeedVector          vec;
1659afe3bc8aSJeremy L Thompson         CeedElemRestriction elem_rstr;
1660afe3bc8aSJeremy L Thompson         CeedBasis           basis;
1661afe3bc8aSJeremy L Thompson 
1662382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec));
1663382e9c83SJeremy L Thompson         // ---- Skip non-active output
1664afe3bc8aSJeremy L Thompson         is_active_output = vec == CEED_VECTOR_ACTIVE;
1665afe3bc8aSJeremy L Thompson         if (!is_active_output) continue;
1666afe3bc8aSJeremy L Thompson 
1667382e9c83SJeremy L Thompson         // ---- Check if elem size matches
1668382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1669382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1670382e9c83SJeremy L Thompson         if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue;
1671382e9c83SJeremy L Thompson         if (rstr_type == CEED_RESTRICTION_POINTS) {
1672382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size));
1673382e9c83SJeremy L Thompson         } else {
1674382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1675382e9c83SJeremy L Thompson         }
1676382e9c83SJeremy L Thompson         {
1677382e9c83SJeremy L Thompson           CeedInt num_comp = 0;
1678382e9c83SJeremy L Thompson 
1679382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1680382e9c83SJeremy L Thompson           if (e_vec_size != num_comp * elem_size) continue;
1681382e9c83SJeremy L Thompson         }
1682382e9c83SJeremy L Thompson 
1683afe3bc8aSJeremy L Thompson         // Basis action
1684382e9c83SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
1685afe3bc8aSJeremy L Thompson         switch (eval_mode) {
1686afe3bc8aSJeremy L Thompson           case CEED_EVAL_NONE:
1687382e9c83SJeremy L Thompson             CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields]));
1688afe3bc8aSJeremy L Thompson             break;
1689afe3bc8aSJeremy L Thompson           case CEED_EVAL_INTERP:
1690afe3bc8aSJeremy L Thompson           case CEED_EVAL_GRAD:
1691afe3bc8aSJeremy L Thompson           case CEED_EVAL_DIV:
1692afe3bc8aSJeremy L Thompson           case CEED_EVAL_CURL:
1693382e9c83SJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis));
1694382e9c83SJeremy L Thompson             CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
1695382e9c83SJeremy L Thompson                                                    impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs]));
1696afe3bc8aSJeremy L Thompson             break;
1697afe3bc8aSJeremy L Thompson           // LCOV_EXCL_START
1698afe3bc8aSJeremy L Thompson           case CEED_EVAL_WEIGHT: {
1699afe3bc8aSJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1700afe3bc8aSJeremy L Thompson             // LCOV_EXCL_STOP
1701afe3bc8aSJeremy L Thompson           }
1702afe3bc8aSJeremy L Thompson         }
1703afe3bc8aSJeremy L Thompson 
1704afe3bc8aSJeremy L Thompson         // Mask output e-vec
1705382e9c83SJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs]));
1706afe3bc8aSJeremy L Thompson 
1707afe3bc8aSJeremy L Thompson         // Restrict
1708382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1709382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request));
1710afe3bc8aSJeremy L Thompson 
1711afe3bc8aSJeremy L Thompson         // Reset q_vec for
1712afe3bc8aSJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) {
1713382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields]));
1714382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields]));
1715afe3bc8aSJeremy L Thompson         }
1716afe3bc8aSJeremy L Thompson       }
1717382e9c83SJeremy L Thompson 
1718382e9c83SJeremy L Thompson       // Reset vec
171913062808SJeremy L Thompson       if (s == e_vec_size - 1 && i != num_input_fields - 1) CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
172086e10729SJeremy L Thompson     }
1721afe3bc8aSJeremy L Thompson   }
1722afe3bc8aSJeremy L Thompson 
1723afe3bc8aSJeremy L Thompson   // Restore CEED_EVAL_NONE
1724afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1725afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1726afe3bc8aSJeremy L Thompson 
1727afe3bc8aSJeremy L Thompson     // Get eval_mode
1728afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1729afe3bc8aSJeremy L Thompson 
1730afe3bc8aSJeremy L Thompson     // Restore evec
1731afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1732afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1733afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1734afe3bc8aSJeremy L Thompson     }
1735afe3bc8aSJeremy L Thompson   }
1736afe3bc8aSJeremy L Thompson 
1737afe3bc8aSJeremy L Thompson   // Restore input arrays
1738afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1739afe3bc8aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
174067d9480aSJeremy L Thompson }
174167d9480aSJeremy L Thompson 
174267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
17430d0321e0SJeremy L Thompson // Create operator
17440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
17450d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
17460d0321e0SJeremy L Thompson   Ceed              ceed;
17470d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
17480d0321e0SJeremy L Thompson 
1749b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
17502b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
17512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
17522b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
17532b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
17542b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
17552b730f8bSJeremy L Thompson   CeedCallBackend(
17562b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
17572b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
17582b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
17592b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
17600d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17610d0321e0SJeremy L Thompson }
17620d0321e0SJeremy L Thompson 
17630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
176467d9480aSJeremy L Thompson // Create operator AtPoints
176567d9480aSJeremy L Thompson //------------------------------------------------------------------------------
176667d9480aSJeremy L Thompson int CeedOperatorCreateAtPoints_Hip(CeedOperator op) {
176767d9480aSJeremy L Thompson   Ceed              ceed;
176867d9480aSJeremy L Thompson   CeedOperator_Hip *impl;
176967d9480aSJeremy L Thompson 
177067d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
177167d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
177267d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
177367d9480aSJeremy L Thompson 
177467d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Hip));
177567d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip));
177667d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Hip));
177767d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
177867d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
177967d9480aSJeremy L Thompson }
178067d9480aSJeremy L Thompson 
178167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
1782