xref: /libCEED/backends/hip-ref/ceed-hip-ref-operator.c (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1*5aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
849aac155SJeremy L Thompson #include <ceed.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
1007b31e0eSJeremy L Thompson #include <ceed/jit-tools.h>
11c85e8640SSebastian Grimberg #include <assert.h>
120d0321e0SJeremy L Thompson #include <stdbool.h>
130d0321e0SJeremy L Thompson #include <string.h>
14c85e8640SSebastian Grimberg #include <hip/hip_runtime.h>
152b730f8bSJeremy L Thompson 
1649aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
170d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
182b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
190d0321e0SJeremy L Thompson 
200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
210d0321e0SJeremy L Thompson // Destroy operator
220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
230d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Hip(CeedOperator op) {
240d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
25b7453713SJeremy L Thompson 
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
270d0321e0SJeremy L Thompson 
280d0321e0SJeremy L Thompson   // Apply data
29b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
30b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
310d0321e0SJeremy L Thompson   }
32b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
330d0321e0SJeremy L Thompson 
34b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
35b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
360d0321e0SJeremy L Thompson   }
37b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
380d0321e0SJeremy L Thompson 
39b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
40b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
410d0321e0SJeremy L Thompson   }
42b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
430d0321e0SJeremy L Thompson 
44b2165e7aSSebastian Grimberg   // QFunction assembly data
45b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
46b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
470d0321e0SJeremy L Thompson   }
48b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
490d0321e0SJeremy L Thompson 
500d0321e0SJeremy L Thompson   // Diag data
510d0321e0SJeremy L Thompson   if (impl->diag) {
520d0321e0SJeremy L Thompson     Ceed ceed;
53b7453713SJeremy L Thompson 
542b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
55cbfe683aSSebastian Grimberg     if (impl->diag->module) {
562b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
57cbfe683aSSebastian Grimberg     }
58cbfe683aSSebastian Grimberg     if (impl->diag->module_point_block) {
59cbfe683aSSebastian Grimberg       CeedCallHip(ceed, hipModuleUnload(impl->diag->module_point_block));
60cbfe683aSSebastian Grimberg     }
61004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_in));
62004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_out));
632b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
64b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_in));
65b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_out));
66b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_in));
67b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_out));
68004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_in));
69004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_out));
70004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_in));
71004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_out));
72004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
73b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
74b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
75b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
760d0321e0SJeremy L Thompson   }
772b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
780d0321e0SJeremy L Thompson 
79a835093fSnbeams   if (impl->asmb) {
80a835093fSnbeams     Ceed ceed;
81b7453713SJeremy L Thompson 
822b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
832b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
842b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
852b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
86a835093fSnbeams   }
872b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
88a835093fSnbeams 
892b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
900d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
910d0321e0SJeremy L Thompson }
920d0321e0SJeremy L Thompson 
930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
940d0321e0SJeremy L Thompson // Setup infields or outfields
950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
96b7453713SJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool is_input, CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e,
97b7453713SJeremy L Thompson                                        CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
980d0321e0SJeremy L Thompson   Ceed                ceed;
99b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
100b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
1010d0321e0SJeremy L Thompson 
102b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
103b7453713SJeremy L Thompson   if (is_input) {
104b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
105b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1060d0321e0SJeremy L Thompson   } else {
107b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
108b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1090d0321e0SJeremy L Thompson   }
1100d0321e0SJeremy L Thompson 
1110d0321e0SJeremy L Thompson   // Loop over fields
112b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
113004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
114b7453713SJeremy L Thompson     CeedSize     q_size;
115004e4986SSebastian Grimberg     CeedInt      size;
116004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
117b7453713SJeremy L Thompson     CeedBasis    basis;
1180d0321e0SJeremy L Thompson 
119004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
120004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
121004e4986SSebastian Grimberg       CeedElemRestriction elem_rstr;
1220d0321e0SJeremy L Thompson 
1230d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
124004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
125004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1260d0321e0SJeremy L Thompson 
1270d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
128b7453713SJeremy L Thompson       if (is_input) {
129004e4986SSebastian Grimberg         CeedVector vec;
130004e4986SSebastian Grimberg 
131004e4986SSebastian Grimberg         // Check for passive input
132b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
133b7453713SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
134004e4986SSebastian Grimberg           // Check eval_mode
135004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1360d0321e0SJeremy L Thompson             // Check for strided restriction
137b7453713SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
138b7453713SJeremy L Thompson             if (is_strided) {
1390d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
140b7453713SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1410d0321e0SJeremy L Thompson             }
1420d0321e0SJeremy L Thompson           }
1430d0321e0SJeremy L Thompson         }
1440d0321e0SJeremy L Thompson       }
145b7453713SJeremy L Thompson       if (skip_restriction) {
146ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
147b7453713SJeremy L Thompson         e_vecs[i + start_e] = NULL;
1480d0321e0SJeremy L Thompson       } else {
149b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1500d0321e0SJeremy L Thompson       }
1510d0321e0SJeremy L Thompson     }
1520d0321e0SJeremy L Thompson 
153004e4986SSebastian Grimberg     switch (eval_mode) {
1540d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
155b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
156b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
157b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1580d0321e0SJeremy L Thompson         break;
1590d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1600d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
161004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
162004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
163b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
164b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
165b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1660d0321e0SJeremy L Thompson         break;
1670d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
168b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
169b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
170b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
171b7453713SJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
1720d0321e0SJeremy L Thompson         break;
1730d0321e0SJeremy L Thompson     }
1740d0321e0SJeremy L Thompson   }
1750d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1760d0321e0SJeremy L Thompson }
1770d0321e0SJeremy L Thompson 
1780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
179ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
1800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1810d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
1820d0321e0SJeremy L Thompson   Ceed                ceed;
183b7453713SJeremy L Thompson   bool                is_setup_done;
184b7453713SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
185b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1860d0321e0SJeremy L Thompson   CeedQFunction       qf;
187b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
188b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
189b7453713SJeremy L Thompson 
190b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
191b7453713SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
192b7453713SJeremy L Thompson 
193b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
194b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1952b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1962b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
197b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
198b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
199b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2000d0321e0SJeremy L Thompson 
2010d0321e0SJeremy L Thompson   // Allocate
202b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
203b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
204b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
205b7453713SJeremy L Thompson   impl->num_inputs  = num_input_fields;
206b7453713SJeremy L Thompson   impl->num_outputs = num_output_fields;
2070d0321e0SJeremy L Thompson 
208b7453713SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
2090d0321e0SJeremy L Thompson   // Infields
210b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2110d0321e0SJeremy L Thompson   // Outfields
212b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem));
2130d0321e0SJeremy L Thompson 
2142b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2150d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2160d0321e0SJeremy L Thompson }
2170d0321e0SJeremy L Thompson 
2180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2190d0321e0SJeremy L Thompson // Setup Operator Inputs
2200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
221b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
222b7453713SJeremy L Thompson                                               CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
223b7453713SJeremy L Thompson                                               CeedOperator_Hip *impl, CeedRequest *request) {
224b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
225004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
2260d0321e0SJeremy L Thompson     CeedVector          vec;
227b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2280d0321e0SJeremy L Thompson 
2290d0321e0SJeremy L Thompson     // Get input vector
230b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2310d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
232b7453713SJeremy L Thompson       if (skip_active) continue;
233b7453713SJeremy L Thompson       else vec = in_vec;
2340d0321e0SJeremy L Thompson     }
2350d0321e0SJeremy L Thompson 
236004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
237004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2380d0321e0SJeremy L Thompson     } else {
2390d0321e0SJeremy L Thompson       // Get input vector
240b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2410d0321e0SJeremy L Thompson       // Get input element restriction
242b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
243b7453713SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
2440d0321e0SJeremy L Thompson       // Restrict, if necessary
245b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {
2460d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
247b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2480d0321e0SJeremy L Thompson       } else {
249b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
2500d0321e0SJeremy L Thompson         // Get evec
251b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2520d0321e0SJeremy L Thompson       }
2530d0321e0SJeremy L Thompson     }
2540d0321e0SJeremy L Thompson   }
2550d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2560d0321e0SJeremy L Thompson }
2570d0321e0SJeremy L Thompson 
2580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2590d0321e0SJeremy L Thompson // Input Basis Action
2600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
261b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
262b7453713SJeremy L Thompson                                              CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
2632b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
264b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
265b7453713SJeremy L Thompson     CeedInt             elem_size, size;
266004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
267b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2680d0321e0SJeremy L Thompson     CeedBasis           basis;
2690d0321e0SJeremy L Thompson 
2700d0321e0SJeremy L Thompson     // Skip active input
271b7453713SJeremy L Thompson     if (skip_active) {
2720d0321e0SJeremy L Thompson       CeedVector vec;
273b7453713SJeremy L Thompson 
274b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2752b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2760d0321e0SJeremy L Thompson     }
277004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
278b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
279b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
280004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
281b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
2820d0321e0SJeremy L Thompson     // Basis action
283004e4986SSebastian Grimberg     switch (eval_mode) {
2840d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
285b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
2860d0321e0SJeremy L Thompson         break;
2870d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
2880d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
289004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
290004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
291b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
292004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
2930d0321e0SJeremy L Thompson         break;
2940d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
2950d0321e0SJeremy L Thompson         break;  // No action
2960d0321e0SJeremy L Thompson     }
2970d0321e0SJeremy L Thompson   }
2980d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2990d0321e0SJeremy L Thompson }
3000d0321e0SJeremy L Thompson 
3010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3020d0321e0SJeremy L Thompson // Restore Input Vectors
3030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
304b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
305b7453713SJeremy L Thompson                                                 const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
306b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
307004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
3080d0321e0SJeremy L Thompson     CeedVector   vec;
309004e4986SSebastian Grimberg 
3100d0321e0SJeremy L Thompson     // Skip active input
311b7453713SJeremy L Thompson     if (skip_active) {
312b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3132b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3140d0321e0SJeremy L Thompson     }
315004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
316004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3170d0321e0SJeremy L Thompson     } else {
318b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
319b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
320b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
3210d0321e0SJeremy L Thompson       } else {
322b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
3230d0321e0SJeremy L Thompson       }
3240d0321e0SJeremy L Thompson     }
3250d0321e0SJeremy L Thompson   }
3260d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3270d0321e0SJeremy L Thompson }
3280d0321e0SJeremy L Thompson 
3290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3300d0321e0SJeremy L Thompson // Apply and add to output
3310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
332b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
333b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
334b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
335b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
3360d0321e0SJeremy L Thompson   CeedQFunction       qf;
337b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
338b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
339b7453713SJeremy L Thompson 
340b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3412b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3422b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
343b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
344b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
345b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
3460d0321e0SJeremy L Thompson 
3470d0321e0SJeremy L Thompson   // Setup
3482b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
3490d0321e0SJeremy L Thompson 
3500d0321e0SJeremy L Thompson   // Input Evecs and Restriction
351b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
3520d0321e0SJeremy L Thompson 
3530d0321e0SJeremy L Thompson   // Input basis apply if needed
354b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
3550d0321e0SJeremy L Thompson 
3560d0321e0SJeremy L Thompson   // Output pointers, as necessary
357b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
358004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
359b7453713SJeremy L Thompson 
360004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
361004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
3620d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
363b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
364b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
3650d0321e0SJeremy L Thompson     }
3660d0321e0SJeremy L Thompson   }
3670d0321e0SJeremy L Thompson 
3680d0321e0SJeremy L Thompson   // Q function
369b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
3700d0321e0SJeremy L Thompson 
3710d0321e0SJeremy L Thompson   // Output basis apply if needed
372b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
373004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
374b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
375b7453713SJeremy L Thompson     CeedBasis           basis;
376b7453713SJeremy L Thompson 
377004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
378b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
379b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
380004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
381b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
3820d0321e0SJeremy L Thompson     // Basis action
383004e4986SSebastian Grimberg     switch (eval_mode) {
3840d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
385004e4986SSebastian Grimberg         break;  // No action
3860d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3870d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
388004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
389004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
390b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
391004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
3920d0321e0SJeremy L Thompson         break;
3930d0321e0SJeremy L Thompson       // LCOV_EXCL_START
3940d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
3956e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
3960d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
3970d0321e0SJeremy L Thompson       }
3980d0321e0SJeremy L Thompson     }
399004e4986SSebastian Grimberg   }
4000d0321e0SJeremy L Thompson 
4010d0321e0SJeremy L Thompson   // Output restriction
402b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
403004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
404b7453713SJeremy L Thompson     CeedVector          vec;
405b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
406b7453713SJeremy L Thompson 
4070d0321e0SJeremy L Thompson     // Restore evec
408004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
409004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
410b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4110d0321e0SJeremy L Thompson     }
4120d0321e0SJeremy L Thompson     // Get output vector
413b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4140d0321e0SJeremy L Thompson     // Restrict
415b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4160d0321e0SJeremy L Thompson     // Active
417b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4180d0321e0SJeremy L Thompson 
419b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4200d0321e0SJeremy L Thompson   }
4210d0321e0SJeremy L Thompson 
4220d0321e0SJeremy L Thompson   // Restore input arrays
423b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4240d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4250d0321e0SJeremy L Thompson }
4260d0321e0SJeremy L Thompson 
4270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
428004e4986SSebastian Grimberg // Linear QFunction Assembly Core
4290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4302b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
4310d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
432b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
433b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
434b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
435004e4986SSebastian Grimberg   CeedVector         *active_inputs;
436b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
437b7453713SJeremy L Thompson   CeedQFunction       qf;
438b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
439b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
440b7453713SJeremy L Thompson 
4412b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
442b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
443e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
444e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
445b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
446004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
447b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
448b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
449004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
450004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
4510d0321e0SJeremy L Thompson 
4520d0321e0SJeremy L Thompson   // Setup
4532b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4540d0321e0SJeremy L Thompson 
4550d0321e0SJeremy L Thompson   // Input Evecs and Restriction
456b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
4570d0321e0SJeremy L Thompson 
4580d0321e0SJeremy L Thompson   // Count number of active input fields
459b7453713SJeremy L Thompson   if (!num_active_in) {
460b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
461b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
462b7453713SJeremy L Thompson       CeedVector  vec;
463b7453713SJeremy L Thompson 
4640d0321e0SJeremy L Thompson       // Get input vector
465b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4660d0321e0SJeremy L Thompson       // Check if active input
4670d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
468b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
469b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
470b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
471004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
4720d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
473004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
474004e4986SSebastian Grimberg 
475004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
476b7453713SJeremy L Thompson           CeedCallBackend(
477004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
4780d0321e0SJeremy L Thompson         }
479b7453713SJeremy L Thompson         num_active_in += size;
480b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
4810d0321e0SJeremy L Thompson       }
4820d0321e0SJeremy L Thompson     }
483b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
484004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
4850d0321e0SJeremy L Thompson   }
4860d0321e0SJeremy L Thompson 
4870d0321e0SJeremy L Thompson   // Count number of active output fields
488b7453713SJeremy L Thompson   if (!num_active_out) {
489b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
490b7453713SJeremy L Thompson       CeedVector vec;
491b7453713SJeremy L Thompson 
4920d0321e0SJeremy L Thompson       // Get output vector
493b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4940d0321e0SJeremy L Thompson       // Check if active output
4950d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
496b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
497b7453713SJeremy L Thompson         num_active_out += size;
4980d0321e0SJeremy L Thompson       }
4990d0321e0SJeremy L Thompson     }
500b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
5010d0321e0SJeremy L Thompson   }
5020d0321e0SJeremy L Thompson 
5030d0321e0SJeremy L Thompson   // Check sizes
504b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5050d0321e0SJeremy L Thompson 
5060d0321e0SJeremy L Thompson   // Build objects if needed
5070d0321e0SJeremy L Thompson   if (build_objects) {
508b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
509b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
510b7453713SJeremy L Thompson 
511004e4986SSebastian Grimberg     // Create output restriction
512b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
513b7453713SJeremy L Thompson                                                      num_active_in * num_active_out * num_elem * Q, strides, rstr));
5140d0321e0SJeremy L Thompson     // Create assembled vector
515b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
5160d0321e0SJeremy L Thompson   }
5172b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
518b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
5190d0321e0SJeremy L Thompson 
5200d0321e0SJeremy L Thompson   // Input basis apply
521b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
5220d0321e0SJeremy L Thompson 
5230d0321e0SJeremy L Thompson   // Assemble QFunction
524b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
5250d0321e0SJeremy L Thompson     // Set Inputs
526004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
527b7453713SJeremy L Thompson     if (num_active_in > 1) {
528004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
5290d0321e0SJeremy L Thompson     }
5300d0321e0SJeremy L Thompson     // Set Outputs
531b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
532b7453713SJeremy L Thompson       CeedVector vec;
533b7453713SJeremy L Thompson 
5340d0321e0SJeremy L Thompson       // Get output vector
535b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5360d0321e0SJeremy L Thompson       // Check if active output
5370d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
538b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
539b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
540b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
5410d0321e0SJeremy L Thompson       }
5420d0321e0SJeremy L Thompson     }
5430d0321e0SJeremy L Thompson     // Apply QFunction
544b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
5450d0321e0SJeremy L Thompson   }
5460d0321e0SJeremy L Thompson 
547004e4986SSebastian Grimberg   // Un-set output q_vecs to prevent accidental overwrite of Assembled
548b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
549b7453713SJeremy L Thompson     CeedVector vec;
550b7453713SJeremy L Thompson 
5510d0321e0SJeremy L Thompson     // Get output vector
552b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5530d0321e0SJeremy L Thompson     // Check if active output
5540d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
555b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
5560d0321e0SJeremy L Thompson     }
5570d0321e0SJeremy L Thompson   }
5580d0321e0SJeremy L Thompson 
5590d0321e0SJeremy L Thompson   // Restore input arrays
560b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
5610d0321e0SJeremy L Thompson 
5620d0321e0SJeremy L Thompson   // Restore output
563b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
5640d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5650d0321e0SJeremy L Thompson }
5660d0321e0SJeremy L Thompson 
5670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5680d0321e0SJeremy L Thompson // Assemble Linear QFunction
5690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5702b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
5712b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
5720d0321e0SJeremy L Thompson }
5730d0321e0SJeremy L Thompson 
5740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
575b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
5760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5772b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
5782b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
5790d0321e0SJeremy L Thompson }
5800d0321e0SJeremy L Thompson 
5810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
582004e4986SSebastian Grimberg // Assemble Diagonal Setup
5830d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
584cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
5850d0321e0SJeremy L Thompson   Ceed                ceed;
586004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
587cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
588004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
589b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
590b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
5910d0321e0SJeremy L Thompson   CeedQFunction       qf;
592b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
593b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
594b7453713SJeremy L Thompson 
595b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
5962b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
597b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
5980d0321e0SJeremy L Thompson 
5990d0321e0SJeremy L Thompson   // Determine active input basis
600b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
601b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
602b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
6030d0321e0SJeremy L Thompson     CeedVector vec;
604b7453713SJeremy L Thompson 
605b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
6060d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
607004e4986SSebastian Grimberg       CeedBasis    basis;
608004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
609b7453713SJeremy L Thompson 
610004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
611004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
612004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
613004e4986SSebastian Grimberg       basis_in = basis;
614004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
615004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
616004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
617004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
618004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
619004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
620004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
6210d0321e0SJeremy L Thompson       }
6220d0321e0SJeremy L Thompson     }
6230d0321e0SJeremy L Thompson   }
6240d0321e0SJeremy L Thompson 
6250d0321e0SJeremy L Thompson   // Determine active output basis
626b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
627b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
628b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
6290d0321e0SJeremy L Thompson     CeedVector vec;
630b7453713SJeremy L Thompson 
631b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
6320d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
633004e4986SSebastian Grimberg       CeedBasis    basis;
634004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
635b7453713SJeremy L Thompson 
636004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
637004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
638004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
639004e4986SSebastian Grimberg       basis_out = basis;
640004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
641004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
642004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
643004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
644004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
645004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
646004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
6470d0321e0SJeremy L Thompson       }
6480d0321e0SJeremy L Thompson     }
6490d0321e0SJeremy L Thompson   }
6500d0321e0SJeremy L Thompson 
6510d0321e0SJeremy L Thompson   // Operator data struct
6522b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
6532b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
6540d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
655b7453713SJeremy L Thompson 
656cbfe683aSSebastian Grimberg   // Basis matrices
657004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
658004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
659004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
660004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
661004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
662004e4986SSebastian Grimberg   bool          has_eval_none    = false;
6630d0321e0SJeremy L Thompson 
6640d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
665004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
666004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
667004e4986SSebastian Grimberg   if (has_eval_none) {
6680d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
669b7453713SJeremy L Thompson 
670004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
671004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
672b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
673b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
674004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
6750d0321e0SJeremy L Thompson   }
6760d0321e0SJeremy L Thompson 
677004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
678004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
679004e4986SSebastian Grimberg     CeedFESpace fespace;
680004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
6810d0321e0SJeremy L Thompson 
682004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
683004e4986SSebastian Grimberg     switch (fespace) {
684004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
685004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
686004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
687004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
6880d0321e0SJeremy L Thompson 
689004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
690004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
6910d0321e0SJeremy L Thompson 
692004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
693004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
694004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
695004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
696004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
697004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
698004e4986SSebastian Grimberg         if (in) {
699004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
700004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
701004e4986SSebastian Grimberg         } else {
702004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
703004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
704004e4986SSebastian Grimberg         }
705004e4986SSebastian Grimberg       } break;
706004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
707004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
708004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
709004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
710004e4986SSebastian Grimberg 
711004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
712004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
713004e4986SSebastian Grimberg 
714004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
715004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
716004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
717004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
718004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
719004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
720004e4986SSebastian Grimberg         if (in) {
721004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
722004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
723004e4986SSebastian Grimberg         } else {
724004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
725004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
726004e4986SSebastian Grimberg         }
727004e4986SSebastian Grimberg       } break;
728004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
729004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
730004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
731004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
732004e4986SSebastian Grimberg 
733004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
734004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
735004e4986SSebastian Grimberg 
736004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
737004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
738004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
739004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
740004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
741004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
742004e4986SSebastian Grimberg         if (in) {
743004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
744004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
745004e4986SSebastian Grimberg         } else {
746004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
747004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
748004e4986SSebastian Grimberg         }
749004e4986SSebastian Grimberg       } break;
750004e4986SSebastian Grimberg     }
751004e4986SSebastian Grimberg   }
752004e4986SSebastian Grimberg 
753004e4986SSebastian Grimberg   // Arrays of eval_modes
754004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
755004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
756004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
757004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
758004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
759004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
7600d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7610d0321e0SJeremy L Thompson }
7620d0321e0SJeremy L Thompson 
7630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
764cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
765cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
766cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
767cbfe683aSSebastian Grimberg   Ceed                ceed;
76822070f95SJeremy L Thompson   char               *diagonal_kernel_source;
76922070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
770cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
771cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
772cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
773cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
774cbfe683aSSebastian Grimberg   CeedQFunction       qf;
775cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
776cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
777cbfe683aSSebastian Grimberg 
778cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
779cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
780cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
781cbfe683aSSebastian Grimberg 
782cbfe683aSSebastian Grimberg   // Determine active input basis
783cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
784cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
785cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
786cbfe683aSSebastian Grimberg     CeedVector vec;
787cbfe683aSSebastian Grimberg 
788cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
789cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
790cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
791cbfe683aSSebastian Grimberg 
792cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
793cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
794cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
795cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
796cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
797cbfe683aSSebastian Grimberg       }
798cbfe683aSSebastian Grimberg     }
799cbfe683aSSebastian Grimberg   }
800cbfe683aSSebastian Grimberg 
801cbfe683aSSebastian Grimberg   // Determine active output basis
802cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
803cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
804cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
805cbfe683aSSebastian Grimberg     CeedVector vec;
806cbfe683aSSebastian Grimberg 
807cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
808cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
809cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
810cbfe683aSSebastian Grimberg 
811cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
812cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
813cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
814cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
815cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
816cbfe683aSSebastian Grimberg       }
817cbfe683aSSebastian Grimberg     }
818cbfe683aSSebastian Grimberg   }
819cbfe683aSSebastian Grimberg 
820cbfe683aSSebastian Grimberg   // Operator data struct
821cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
822cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
823cbfe683aSSebastian Grimberg 
824cbfe683aSSebastian Grimberg   // Assemble kernel
825cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
826cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
827cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
828cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
829cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
830cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
831cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
832cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
833cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
834cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
835cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
836cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
837cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
838cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
839cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
840cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
841cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
842cbfe683aSSebastian Grimberg }
843cbfe683aSSebastian Grimberg 
844cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
845004e4986SSebastian Grimberg // Assemble Diagonal Core
8460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
847b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
8480d0321e0SJeremy L Thompson   Ceed                ceed;
849cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
850b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
851b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
852004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
853004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
8540d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
855b7453713SJeremy L Thompson 
856b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8572b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
8580d0321e0SJeremy L Thompson 
8590d0321e0SJeremy L Thompson   // Assemble QFunction
860004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
861004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
862004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
8630d0321e0SJeremy L Thompson 
864cbfe683aSSebastian Grimberg   // Setup
865cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
866cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
867cbfe683aSSebastian Grimberg 
868cbfe683aSSebastian Grimberg   assert(diag != NULL);
869cbfe683aSSebastian Grimberg 
870cbfe683aSSebastian Grimberg   // Assemble kernel if needed
871cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
872cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
873cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
8749330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
875b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
876b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
8779330daecSnbeams 
878cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
879cbfe683aSSebastian Grimberg   }
8800d0321e0SJeremy L Thompson 
881004e4986SSebastian Grimberg   // Restriction and diagonal vector
882004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
883004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
884004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
885004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
886004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
887004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
888004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
889004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
890004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
8910d0321e0SJeremy L Thompson   }
892004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
893004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
894b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
8950d0321e0SJeremy L Thompson 
89691db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
897004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
898004e4986SSebastian Grimberg   if (num_nodes > 0) {
8990d0321e0SJeremy L Thompson     // Assemble element operator diagonals
900b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
901b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
9020d0321e0SJeremy L Thompson 
9030d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
904004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
905004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
906004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
907004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
908004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
909b7453713SJeremy L Thompson 
910b7453713SJeremy L Thompson     if (is_point_block) {
911004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
9120d0321e0SJeremy L Thompson     } else {
913004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
9140d0321e0SJeremy L Thompson     }
9150d0321e0SJeremy L Thompson 
9160d0321e0SJeremy L Thompson     // Restore arrays
917b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
918b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
91991db28b6SZach Atkins   }
9200d0321e0SJeremy L Thompson 
9210d0321e0SJeremy L Thompson   // Assemble local operator diagonal
922b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
9230d0321e0SJeremy L Thompson 
9240d0321e0SJeremy L Thompson   // Cleanup
925b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
9260d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9270d0321e0SJeremy L Thompson }
9280d0321e0SJeremy L Thompson 
9290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9300d0321e0SJeremy L Thompson // Assemble Linear Diagonal
9310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9322b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
9332b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
9346aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9350d0321e0SJeremy L Thompson }
9360d0321e0SJeremy L Thompson 
9370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9380d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
9390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9402b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
9412b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
9426aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9430d0321e0SJeremy L Thompson }
9440d0321e0SJeremy L Thompson 
945a835093fSnbeams //------------------------------------------------------------------------------
946004e4986SSebastian Grimberg // Single Operator Assembly Setup
947a835093fSnbeams //------------------------------------------------------------------------------
9489330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
949a835093fSnbeams   Ceed                ceed;
95022070f95SJeremy L Thompson   char               *assembly_kernel_source;
95122070f95SJeremy L Thompson   const char         *assembly_kernel_path;
952004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
9533b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
954004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
955b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
956b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
957b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
958b7453713SJeremy L Thompson   CeedQFunction       qf;
959b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
960a835093fSnbeams   CeedOperator_Hip   *impl;
961b7453713SJeremy L Thompson 
962b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
9632b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
964a835093fSnbeams 
965a835093fSnbeams   // Get intput and output fields
9662b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
967a835093fSnbeams 
968a835093fSnbeams   // Determine active input basis eval mode
9692b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
9702b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
971a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
972a835093fSnbeams     CeedVector vec;
973b7453713SJeremy L Thompson 
9742b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
975a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
976004e4986SSebastian Grimberg       CeedBasis    basis;
977b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
978b7453713SJeremy L Thompson 
979004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
980004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
981004e4986SSebastian Grimberg       basis_in = basis;
9822b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
983004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
984004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
985004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
9862b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
987004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
988004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
989004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
990004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
991004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
992004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
993a835093fSnbeams         }
994004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
995a835093fSnbeams       }
996a835093fSnbeams     }
997a835093fSnbeams   }
998a835093fSnbeams 
999a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
10002b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1001a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1002a835093fSnbeams     CeedVector vec;
1003b7453713SJeremy L Thompson 
10042b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1005a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1006004e4986SSebastian Grimberg       CeedBasis    basis;
1007b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1008b7453713SJeremy L Thompson 
1009004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1010004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1011004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1012004e4986SSebastian Grimberg       basis_out = basis;
10132b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1014004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1015004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1016004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1017004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1018004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
10192b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1020004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1021004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1022004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1023004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1024004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1025004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1026004e4986SSebastian Grimberg         }
1027004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1028a835093fSnbeams       }
1029a835093fSnbeams     }
1030a835093fSnbeams   }
1031004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1032a835093fSnbeams 
10332b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1034a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1035004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1036004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1037004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1038004e4986SSebastian Grimberg 
1039004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1040004e4986SSebastian Grimberg 
1041004e4986SSebastian Grimberg   if (fallback) {
1042004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1043004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1044004e4986SSebastian Grimberg   }
1045a835093fSnbeams 
1046a835093fSnbeams   // Compile kernels
1047004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1048004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
10492b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
105023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
10512b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
105223d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1053004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1054004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1055004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1056cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
10579330daecSnbeams                                   use_ceedsize_idx));
1058004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
10592b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
10602b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1061a835093fSnbeams 
1062004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1063004e4986SSebastian Grimberg   {
1064004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1065004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1066004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1067004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1068004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1069a835093fSnbeams 
1070004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1071004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1072004e4986SSebastian Grimberg     }
1073004e4986SSebastian Grimberg     if (has_eval_none) {
1074004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1075004e4986SSebastian 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;
1076004e4986SSebastian Grimberg     }
1077b7453713SJeremy L Thompson 
1078b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1079004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1080004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1081004e4986SSebastian Grimberg 
1082004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1083004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1084004e4986SSebastian Grimberg       if (q_comp > 1) {
1085004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1086004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1087004e4986SSebastian Grimberg       }
1088004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1089004e4986SSebastian Grimberg 
1090004e4986SSebastian 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),
1091004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1092004e4986SSebastian Grimberg     }
1093004e4986SSebastian Grimberg 
1094004e4986SSebastian Grimberg     if (identity) {
1095004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1096a835093fSnbeams     }
1097a835093fSnbeams   }
1098a835093fSnbeams 
1099004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1100004e4986SSebastian Grimberg   {
1101004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1102004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1103004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1104004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1105004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1106b7453713SJeremy L Thompson 
1107004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1108004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1109004e4986SSebastian Grimberg     }
1110004e4986SSebastian Grimberg     if (has_eval_none) {
1111004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1112004e4986SSebastian 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;
1113a835093fSnbeams     }
1114a835093fSnbeams 
1115b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1116004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1117004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1118004e4986SSebastian Grimberg 
1119004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1120004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1121004e4986SSebastian Grimberg       if (q_comp > 1) {
1122004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1123004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1124004e4986SSebastian Grimberg       }
1125004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1126004e4986SSebastian Grimberg 
1127004e4986SSebastian 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),
1128004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1129004e4986SSebastian Grimberg     }
1130004e4986SSebastian Grimberg 
1131004e4986SSebastian Grimberg     if (identity) {
1132004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1133a835093fSnbeams     }
1134a835093fSnbeams   }
1135a835093fSnbeams   return CEED_ERROR_SUCCESS;
1136a835093fSnbeams }
1137a835093fSnbeams 
1138a835093fSnbeams //------------------------------------------------------------------------------
1139cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1140cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1141cefa2673SJeremy L Thompson //
1142ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1143cefa2673SJeremy L Thompson // modes).
1144cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1145a835093fSnbeams //------------------------------------------------------------------------------
11462b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1147a835093fSnbeams   Ceed                ceed;
1148b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1149004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1150b7453713SJeremy L Thompson   CeedScalar         *values_array;
1151004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1152b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1153004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1154004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1155004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1156004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1157a835093fSnbeams   CeedOperator_Hip   *impl;
1158b7453713SJeremy L Thompson 
1159b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1161a835093fSnbeams 
1162a835093fSnbeams   // Assemble QFunction
1163004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1164004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1165004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1166a835093fSnbeams 
11679330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
11689330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
11699330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1170004e4986SSebastian Grimberg 
11719330daecSnbeams   // Setup
1172004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1173004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1174004e4986SSebastian Grimberg 
1175004e4986SSebastian Grimberg   assert(asmb != NULL);
1176004e4986SSebastian Grimberg 
1177004e4986SSebastian Grimberg   // Assemble element operator
1178004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1179004e4986SSebastian Grimberg   values_array += offset;
1180004e4986SSebastian Grimberg 
1181004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1182004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1183004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1184004e4986SSebastian Grimberg 
1185004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1186004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1187004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1188004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1189004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1190004e4986SSebastian Grimberg   }
1191004e4986SSebastian Grimberg 
1192004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1193004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1194004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1195004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1196004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1197004e4986SSebastian Grimberg 
1198004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1199004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1200004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1201004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1202004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1203004e4986SSebastian Grimberg     }
1204004e4986SSebastian Grimberg   } else {
1205004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1206004e4986SSebastian Grimberg     orients_out      = orients_in;
1207004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
12089330daecSnbeams   }
12099330daecSnbeams 
1210a835093fSnbeams   // Compute B^T D B
1211004e4986SSebastian Grimberg   CeedInt shared_mem =
1212004e4986SSebastian 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)) *
1213004e4986SSebastian Grimberg       sizeof(CeedScalar);
1214004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1215004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1216004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1217b7453713SJeremy L Thompson 
12182b730f8bSJeremy L Thompson   CeedCallBackend(
1219004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1220a835093fSnbeams 
1221a835093fSnbeams   // Restore arrays
12222b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1223004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1224a835093fSnbeams 
1225a835093fSnbeams   // Cleanup
12262b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1227004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1228004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1229004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1230004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1231004e4986SSebastian Grimberg   }
1232004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1233004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1234004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1235004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1236004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1237004e4986SSebastian Grimberg     }
1238004e4986SSebastian Grimberg   }
1239a835093fSnbeams   return CEED_ERROR_SUCCESS;
1240a835093fSnbeams }
1241a835093fSnbeams 
1242a835093fSnbeams //------------------------------------------------------------------------------
12430d0321e0SJeremy L Thompson // Create operator
12440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12450d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
12460d0321e0SJeremy L Thompson   Ceed              ceed;
12470d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
12480d0321e0SJeremy L Thompson 
1249b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12502b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
12512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
12522b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
12532b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
12542b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
12552b730f8bSJeremy L Thompson   CeedCallBackend(
12562b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
12572b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
12582b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
12592b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
12600d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12610d0321e0SJeremy L Thompson }
12620d0321e0SJeremy L Thompson 
12630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1264