xref: /libCEED/backends/hip-ref/ceed-hip-ref-operator.c (revision 3b38d1dfe7a8d0ddfedd40f2e4c35e536721e848) !
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
849aac155SJeremy L Thompson #include <ceed.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
1007b31e0eSJeremy L Thompson #include <ceed/jit-tools.h>
11c85e8640SSebastian Grimberg #include <assert.h>
120d0321e0SJeremy L Thompson #include <stdbool.h>
130d0321e0SJeremy L Thompson #include <string.h>
14c85e8640SSebastian Grimberg #include <hip/hip_runtime.h>
152b730f8bSJeremy L Thompson 
1649aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
170d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
182b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
190d0321e0SJeremy L Thompson 
200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
210d0321e0SJeremy L Thompson // Destroy operator
220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
230d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Hip(CeedOperator op) {
240d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
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: {
3950d0321e0SJeremy L Thompson         Ceed ceed;
396b7453713SJeremy L Thompson 
3972b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
3982b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
3990d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
4000d0321e0SJeremy L Thompson       }
4010d0321e0SJeremy L Thompson     }
402004e4986SSebastian Grimberg   }
4030d0321e0SJeremy L Thompson 
4040d0321e0SJeremy L Thompson   // Output restriction
405b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
406004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
407b7453713SJeremy L Thompson     CeedVector          vec;
408b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
409b7453713SJeremy L Thompson 
4100d0321e0SJeremy L Thompson     // Restore evec
411004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
412004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
413b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4140d0321e0SJeremy L Thompson     }
4150d0321e0SJeremy L Thompson     // Get output vector
416b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4170d0321e0SJeremy L Thompson     // Restrict
418b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4190d0321e0SJeremy L Thompson     // Active
420b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4210d0321e0SJeremy L Thompson 
422b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4230d0321e0SJeremy L Thompson   }
4240d0321e0SJeremy L Thompson 
4250d0321e0SJeremy L Thompson   // Restore input arrays
426b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4270d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4280d0321e0SJeremy L Thompson }
4290d0321e0SJeremy L Thompson 
4300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
431004e4986SSebastian Grimberg // Linear QFunction Assembly Core
4320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4332b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
4340d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
435b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
436b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
437b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
438004e4986SSebastian Grimberg   CeedVector         *active_inputs;
439b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
440b7453713SJeremy L Thompson   CeedQFunction       qf;
441b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
442b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
443b7453713SJeremy L Thompson 
4442b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
445b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
446e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
447e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
448b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
449004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
450b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
451b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
452004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
453004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
4540d0321e0SJeremy L Thompson 
4550d0321e0SJeremy L Thompson   // Setup
4562b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4570d0321e0SJeremy L Thompson 
4580d0321e0SJeremy L Thompson   // Input Evecs and Restriction
459b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
4600d0321e0SJeremy L Thompson 
4610d0321e0SJeremy L Thompson   // Count number of active input fields
462b7453713SJeremy L Thompson   if (!num_active_in) {
463b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
464b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
465b7453713SJeremy L Thompson       CeedVector  vec;
466b7453713SJeremy L Thompson 
4670d0321e0SJeremy L Thompson       // Get input vector
468b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4690d0321e0SJeremy L Thompson       // Check if active input
4700d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
471b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
472b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
473b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
474004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
4750d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
476004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
477004e4986SSebastian Grimberg 
478004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
479b7453713SJeremy L Thompson           CeedCallBackend(
480004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
4810d0321e0SJeremy L Thompson         }
482b7453713SJeremy L Thompson         num_active_in += size;
483b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
4840d0321e0SJeremy L Thompson       }
4850d0321e0SJeremy L Thompson     }
486b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
487004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
4880d0321e0SJeremy L Thompson   }
4890d0321e0SJeremy L Thompson 
4900d0321e0SJeremy L Thompson   // Count number of active output fields
491b7453713SJeremy L Thompson   if (!num_active_out) {
492b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
493b7453713SJeremy L Thompson       CeedVector vec;
494b7453713SJeremy L Thompson 
4950d0321e0SJeremy L Thompson       // Get output vector
496b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4970d0321e0SJeremy L Thompson       // Check if active output
4980d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
499b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
500b7453713SJeremy L Thompson         num_active_out += size;
5010d0321e0SJeremy L Thompson       }
5020d0321e0SJeremy L Thompson     }
503b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
5040d0321e0SJeremy L Thompson   }
5050d0321e0SJeremy L Thompson 
5060d0321e0SJeremy L Thompson   // Check sizes
507b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5080d0321e0SJeremy L Thompson 
5090d0321e0SJeremy L Thompson   // Build objects if needed
5100d0321e0SJeremy L Thompson   if (build_objects) {
511b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
512b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
513b7453713SJeremy L Thompson 
514004e4986SSebastian Grimberg     // Create output restriction
515b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
516b7453713SJeremy L Thompson                                                      num_active_in * num_active_out * num_elem * Q, strides, rstr));
5170d0321e0SJeremy L Thompson     // Create assembled vector
518b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
5190d0321e0SJeremy L Thompson   }
5202b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
521b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
5220d0321e0SJeremy L Thompson 
5230d0321e0SJeremy L Thompson   // Input basis apply
524b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
5250d0321e0SJeremy L Thompson 
5260d0321e0SJeremy L Thompson   // Assemble QFunction
527b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
5280d0321e0SJeremy L Thompson     // Set Inputs
529004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
530b7453713SJeremy L Thompson     if (num_active_in > 1) {
531004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
5320d0321e0SJeremy L Thompson     }
5330d0321e0SJeremy L Thompson     // Set Outputs
534b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
535b7453713SJeremy L Thompson       CeedVector vec;
536b7453713SJeremy L Thompson 
5370d0321e0SJeremy L Thompson       // Get output vector
538b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5390d0321e0SJeremy L Thompson       // Check if active output
5400d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
541b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
542b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
543b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
5440d0321e0SJeremy L Thompson       }
5450d0321e0SJeremy L Thompson     }
5460d0321e0SJeremy L Thompson     // Apply QFunction
547b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
5480d0321e0SJeremy L Thompson   }
5490d0321e0SJeremy L Thompson 
550004e4986SSebastian Grimberg   // Un-set output q_vecs to prevent accidental overwrite of Assembled
551b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
552b7453713SJeremy L Thompson     CeedVector vec;
553b7453713SJeremy L Thompson 
5540d0321e0SJeremy L Thompson     // Get output vector
555b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5560d0321e0SJeremy L Thompson     // Check if active output
5570d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
558b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
5590d0321e0SJeremy L Thompson     }
5600d0321e0SJeremy L Thompson   }
5610d0321e0SJeremy L Thompson 
5620d0321e0SJeremy L Thompson   // Restore input arrays
563b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
5640d0321e0SJeremy L Thompson 
5650d0321e0SJeremy L Thompson   // Restore output
566b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
5670d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5680d0321e0SJeremy L Thompson }
5690d0321e0SJeremy L Thompson 
5700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5710d0321e0SJeremy L Thompson // Assemble Linear QFunction
5720d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5732b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
5742b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
5750d0321e0SJeremy L Thompson }
5760d0321e0SJeremy L Thompson 
5770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
578b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
5790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5802b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
5812b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
5820d0321e0SJeremy L Thompson }
5830d0321e0SJeremy L Thompson 
5840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
585004e4986SSebastian Grimberg // Assemble Diagonal Setup
5860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
587cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
5880d0321e0SJeremy L Thompson   Ceed                ceed;
589004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
590cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
591004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
592b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
593b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
5940d0321e0SJeremy L Thompson   CeedQFunction       qf;
595b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
596b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
597b7453713SJeremy L Thompson 
598b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
5992b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
600b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
6010d0321e0SJeremy L Thompson 
6020d0321e0SJeremy L Thompson   // Determine active input basis
603b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
604b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
605b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
6060d0321e0SJeremy L Thompson     CeedVector vec;
607b7453713SJeremy L Thompson 
608b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
6090d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
610004e4986SSebastian Grimberg       CeedBasis    basis;
611004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
612b7453713SJeremy L Thompson 
613004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
614004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
615004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
616004e4986SSebastian Grimberg       basis_in = basis;
617004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
618004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
619004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
620004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
621004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
622004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
623004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
6240d0321e0SJeremy L Thompson       }
6250d0321e0SJeremy L Thompson     }
6260d0321e0SJeremy L Thompson   }
6270d0321e0SJeremy L Thompson 
6280d0321e0SJeremy L Thompson   // Determine active output basis
629b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
630b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
631b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
6320d0321e0SJeremy L Thompson     CeedVector vec;
633b7453713SJeremy L Thompson 
634b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
6350d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
636004e4986SSebastian Grimberg       CeedBasis    basis;
637004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
638b7453713SJeremy L Thompson 
639004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
640004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
641004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
642004e4986SSebastian Grimberg       basis_out = basis;
643004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
644004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
645004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
646004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
647004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
648004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
649004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
6500d0321e0SJeremy L Thompson       }
6510d0321e0SJeremy L Thompson     }
6520d0321e0SJeremy L Thompson   }
6530d0321e0SJeremy L Thompson 
6540d0321e0SJeremy L Thompson   // Operator data struct
6552b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
6562b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
6570d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
658b7453713SJeremy L Thompson 
659cbfe683aSSebastian Grimberg   // Basis matrices
660004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
661004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
662004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
663004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
664004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
665004e4986SSebastian Grimberg   bool          has_eval_none    = false;
6660d0321e0SJeremy L Thompson 
6670d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
668004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
669004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
670004e4986SSebastian Grimberg   if (has_eval_none) {
6710d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
672b7453713SJeremy L Thompson 
673004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
674004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
675b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
676b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
677004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
6780d0321e0SJeremy L Thompson   }
6790d0321e0SJeremy L Thompson 
680004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
681004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
682004e4986SSebastian Grimberg     CeedFESpace fespace;
683004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
6840d0321e0SJeremy L Thompson 
685004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
686004e4986SSebastian Grimberg     switch (fespace) {
687004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
688004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
689004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
690004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
6910d0321e0SJeremy L Thompson 
692004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
693004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
6940d0321e0SJeremy L Thompson 
695004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
696004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
697004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
698004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
699004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
700004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
701004e4986SSebastian Grimberg         if (in) {
702004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
703004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
704004e4986SSebastian Grimberg         } else {
705004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
706004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
707004e4986SSebastian Grimberg         }
708004e4986SSebastian Grimberg       } break;
709004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
710004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
711004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
712004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
713004e4986SSebastian Grimberg 
714004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
715004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
716004e4986SSebastian Grimberg 
717004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
718004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
719004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
720004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
721004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
722004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
723004e4986SSebastian Grimberg         if (in) {
724004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
725004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
726004e4986SSebastian Grimberg         } else {
727004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
728004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
729004e4986SSebastian Grimberg         }
730004e4986SSebastian Grimberg       } break;
731004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
732004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
733004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
734004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
735004e4986SSebastian Grimberg 
736004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
737004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
738004e4986SSebastian Grimberg 
739004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
740004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
741004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
742004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
743004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
744004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
745004e4986SSebastian Grimberg         if (in) {
746004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
747004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
748004e4986SSebastian Grimberg         } else {
749004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
750004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
751004e4986SSebastian Grimberg         }
752004e4986SSebastian Grimberg       } break;
753004e4986SSebastian Grimberg     }
754004e4986SSebastian Grimberg   }
755004e4986SSebastian Grimberg 
756004e4986SSebastian Grimberg   // Arrays of eval_modes
757004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
758004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
759004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
760004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
761004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
762004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
7630d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7640d0321e0SJeremy L Thompson }
7650d0321e0SJeremy L Thompson 
7660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
767cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
768cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
769cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
770cbfe683aSSebastian Grimberg   Ceed                ceed;
77122070f95SJeremy L Thompson   char               *diagonal_kernel_source;
77222070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
773cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
774cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
775cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
776cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
777cbfe683aSSebastian Grimberg   CeedQFunction       qf;
778cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
779cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
780cbfe683aSSebastian Grimberg 
781cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
782cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
783cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
784cbfe683aSSebastian Grimberg 
785cbfe683aSSebastian Grimberg   // Determine active input basis
786cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
787cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
788cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
789cbfe683aSSebastian Grimberg     CeedVector vec;
790cbfe683aSSebastian Grimberg 
791cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
792cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
793cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
794cbfe683aSSebastian Grimberg 
795cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
796cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
797cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
798cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
799cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
800cbfe683aSSebastian Grimberg       }
801cbfe683aSSebastian Grimberg     }
802cbfe683aSSebastian Grimberg   }
803cbfe683aSSebastian Grimberg 
804cbfe683aSSebastian Grimberg   // Determine active output basis
805cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
806cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
807cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
808cbfe683aSSebastian Grimberg     CeedVector vec;
809cbfe683aSSebastian Grimberg 
810cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
811cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
812cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
813cbfe683aSSebastian Grimberg 
814cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
815cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
816cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
817cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
818cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
819cbfe683aSSebastian Grimberg       }
820cbfe683aSSebastian Grimberg     }
821cbfe683aSSebastian Grimberg   }
822cbfe683aSSebastian Grimberg 
823cbfe683aSSebastian Grimberg   // Operator data struct
824cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
825cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
826cbfe683aSSebastian Grimberg 
827cbfe683aSSebastian Grimberg   // Assemble kernel
828cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
829cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
830cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
831cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
832cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
833cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
834cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
835cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
836cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
837cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
838cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
839cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
840cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
841cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
842cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
843cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
844cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
845cbfe683aSSebastian Grimberg }
846cbfe683aSSebastian Grimberg 
847cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
848004e4986SSebastian Grimberg // Assemble Diagonal Core
8490d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
850b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
8510d0321e0SJeremy L Thompson   Ceed                ceed;
852cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
853b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
854b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
855004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
856004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
8570d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
858b7453713SJeremy L Thompson 
859b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
8610d0321e0SJeremy L Thompson 
8620d0321e0SJeremy L Thompson   // Assemble QFunction
863004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
864004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
865004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
8660d0321e0SJeremy L Thompson 
867cbfe683aSSebastian Grimberg   // Setup
868cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
869cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
870cbfe683aSSebastian Grimberg 
871cbfe683aSSebastian Grimberg   assert(diag != NULL);
872cbfe683aSSebastian Grimberg 
873cbfe683aSSebastian Grimberg   // Assemble kernel if needed
874cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
875cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
876cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
8779330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
878b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
879b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
8809330daecSnbeams 
881cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
882cbfe683aSSebastian Grimberg   }
8830d0321e0SJeremy L Thompson 
884004e4986SSebastian Grimberg   // Restriction and diagonal vector
885004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
886004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
887004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
888004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
889004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
890004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
891004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
892004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
893004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
8940d0321e0SJeremy L Thompson   }
895004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
896004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
897b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
8980d0321e0SJeremy L Thompson 
89991db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
900004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
901004e4986SSebastian Grimberg   if (num_nodes > 0) {
9020d0321e0SJeremy L Thompson     // Assemble element operator diagonals
903b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
904b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
9050d0321e0SJeremy L Thompson 
9060d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
907004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
908004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
909004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
910004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
911004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
912b7453713SJeremy L Thompson 
913b7453713SJeremy L Thompson     if (is_point_block) {
914004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
9150d0321e0SJeremy L Thompson     } else {
916004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
9170d0321e0SJeremy L Thompson     }
9180d0321e0SJeremy L Thompson 
9190d0321e0SJeremy L Thompson     // Restore arrays
920b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
921b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
92291db28b6SZach Atkins   }
9230d0321e0SJeremy L Thompson 
9240d0321e0SJeremy L Thompson   // Assemble local operator diagonal
925b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
9260d0321e0SJeremy L Thompson 
9270d0321e0SJeremy L Thompson   // Cleanup
928b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
9290d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9300d0321e0SJeremy L Thompson }
9310d0321e0SJeremy L Thompson 
9320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9330d0321e0SJeremy L Thompson // Assemble Linear Diagonal
9340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9352b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
9362b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
9376aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9380d0321e0SJeremy L Thompson }
9390d0321e0SJeremy L Thompson 
9400d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9410d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
9420d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9432b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
9442b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
9456aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9460d0321e0SJeremy L Thompson }
9470d0321e0SJeremy L Thompson 
948a835093fSnbeams //------------------------------------------------------------------------------
949004e4986SSebastian Grimberg // Single Operator Assembly Setup
950a835093fSnbeams //------------------------------------------------------------------------------
9519330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
952a835093fSnbeams   Ceed                ceed;
95322070f95SJeremy L Thompson   char               *assembly_kernel_source;
95422070f95SJeremy L Thompson   const char         *assembly_kernel_path;
955004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
956*3b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
957004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
958b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
959b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
960b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
961b7453713SJeremy L Thompson   CeedQFunction       qf;
962b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
963a835093fSnbeams   CeedOperator_Hip   *impl;
964b7453713SJeremy L Thompson 
965b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
9662b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
967a835093fSnbeams 
968a835093fSnbeams   // Get intput and output fields
9692b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
970a835093fSnbeams 
971a835093fSnbeams   // Determine active input basis eval mode
9722b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
9732b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
974a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
975a835093fSnbeams     CeedVector vec;
976b7453713SJeremy L Thompson 
9772b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
978a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
979004e4986SSebastian Grimberg       CeedBasis    basis;
980b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
981b7453713SJeremy L Thompson 
982004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
983004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
984004e4986SSebastian Grimberg       basis_in = basis;
9852b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
986004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
987004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
988004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
9892b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
990004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
991004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
992004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
993004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
994004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
995004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
996a835093fSnbeams         }
997004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
998a835093fSnbeams       }
999a835093fSnbeams     }
1000a835093fSnbeams   }
1001a835093fSnbeams 
1002a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
10032b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1004a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1005a835093fSnbeams     CeedVector vec;
1006b7453713SJeremy L Thompson 
10072b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1008a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1009004e4986SSebastian Grimberg       CeedBasis    basis;
1010b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1011b7453713SJeremy L Thompson 
1012004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1013004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1014004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1015004e4986SSebastian Grimberg       basis_out = basis;
10162b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1017004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1018004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1019004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1020004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1021004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
10222b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1023004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1024004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1025004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1026004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1027004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1028004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1029004e4986SSebastian Grimberg         }
1030004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1031a835093fSnbeams       }
1032a835093fSnbeams     }
1033a835093fSnbeams   }
1034004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1035a835093fSnbeams 
10362b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1037a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1038004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1039004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1040004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1041004e4986SSebastian Grimberg 
1042004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1043004e4986SSebastian Grimberg 
1044004e4986SSebastian Grimberg   if (fallback) {
1045004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1046004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1047004e4986SSebastian Grimberg   }
1048a835093fSnbeams 
1049a835093fSnbeams   // Compile kernels
1050004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1051004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
10522b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
105323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
10542b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
105523d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1056004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1057004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1058004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1059cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
10609330daecSnbeams                                   use_ceedsize_idx));
1061004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
10622b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
10632b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1064a835093fSnbeams 
1065004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1066004e4986SSebastian Grimberg   {
1067004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1068004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1069004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1070004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1071004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1072a835093fSnbeams 
1073004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1074004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1075004e4986SSebastian Grimberg     }
1076004e4986SSebastian Grimberg     if (has_eval_none) {
1077004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1078004e4986SSebastian 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;
1079004e4986SSebastian Grimberg     }
1080b7453713SJeremy L Thompson 
1081b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1082004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1083004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1084004e4986SSebastian Grimberg 
1085004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1086004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1087004e4986SSebastian Grimberg       if (q_comp > 1) {
1088004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1089004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1090004e4986SSebastian Grimberg       }
1091004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1092004e4986SSebastian Grimberg 
1093004e4986SSebastian 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),
1094004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1095004e4986SSebastian Grimberg     }
1096004e4986SSebastian Grimberg 
1097004e4986SSebastian Grimberg     if (identity) {
1098004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1099a835093fSnbeams     }
1100a835093fSnbeams   }
1101a835093fSnbeams 
1102004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1103004e4986SSebastian Grimberg   {
1104004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1105004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1106004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1107004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1108004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1109b7453713SJeremy L Thompson 
1110004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1111004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1112004e4986SSebastian Grimberg     }
1113004e4986SSebastian Grimberg     if (has_eval_none) {
1114004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1115004e4986SSebastian 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;
1116a835093fSnbeams     }
1117a835093fSnbeams 
1118b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1119004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1120004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1121004e4986SSebastian Grimberg 
1122004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1123004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1124004e4986SSebastian Grimberg       if (q_comp > 1) {
1125004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1126004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1127004e4986SSebastian Grimberg       }
1128004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1129004e4986SSebastian Grimberg 
1130004e4986SSebastian 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),
1131004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1132004e4986SSebastian Grimberg     }
1133004e4986SSebastian Grimberg 
1134004e4986SSebastian Grimberg     if (identity) {
1135004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1136a835093fSnbeams     }
1137a835093fSnbeams   }
1138a835093fSnbeams   return CEED_ERROR_SUCCESS;
1139a835093fSnbeams }
1140a835093fSnbeams 
1141a835093fSnbeams //------------------------------------------------------------------------------
1142cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1143cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1144cefa2673SJeremy L Thompson //
1145ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1146cefa2673SJeremy L Thompson // modes).
1147cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1148a835093fSnbeams //------------------------------------------------------------------------------
11492b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1150a835093fSnbeams   Ceed                ceed;
1151b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1152004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1153b7453713SJeremy L Thompson   CeedScalar         *values_array;
1154004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1155b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1156004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1157004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1158004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1159004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1160a835093fSnbeams   CeedOperator_Hip   *impl;
1161b7453713SJeremy L Thompson 
1162b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11632b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1164a835093fSnbeams 
1165a835093fSnbeams   // Assemble QFunction
1166004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1167004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1168004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1169a835093fSnbeams 
11709330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
11719330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
11729330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1173004e4986SSebastian Grimberg 
11749330daecSnbeams   // Setup
1175004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1176004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1177004e4986SSebastian Grimberg 
1178004e4986SSebastian Grimberg   assert(asmb != NULL);
1179004e4986SSebastian Grimberg 
1180004e4986SSebastian Grimberg   // Assemble element operator
1181004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1182004e4986SSebastian Grimberg   values_array += offset;
1183004e4986SSebastian Grimberg 
1184004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1185004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1186004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1187004e4986SSebastian Grimberg 
1188004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1189004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1190004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1191004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1192004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1193004e4986SSebastian Grimberg   }
1194004e4986SSebastian Grimberg 
1195004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1196004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1197004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1198004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1199004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1200004e4986SSebastian Grimberg 
1201004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1202004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1203004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1204004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1205004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1206004e4986SSebastian Grimberg     }
1207004e4986SSebastian Grimberg   } else {
1208004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1209004e4986SSebastian Grimberg     orients_out      = orients_in;
1210004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
12119330daecSnbeams   }
12129330daecSnbeams 
1213a835093fSnbeams   // Compute B^T D B
1214004e4986SSebastian Grimberg   CeedInt shared_mem =
1215004e4986SSebastian 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)) *
1216004e4986SSebastian Grimberg       sizeof(CeedScalar);
1217004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1218004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1219004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1220b7453713SJeremy L Thompson 
12212b730f8bSJeremy L Thompson   CeedCallBackend(
1222004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1223a835093fSnbeams 
1224a835093fSnbeams   // Restore arrays
12252b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1226004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1227a835093fSnbeams 
1228a835093fSnbeams   // Cleanup
12292b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1230004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1231004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1232004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1233004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1234004e4986SSebastian Grimberg   }
1235004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1236004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1237004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1238004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1239004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1240004e4986SSebastian Grimberg     }
1241004e4986SSebastian Grimberg   }
1242a835093fSnbeams   return CEED_ERROR_SUCCESS;
1243a835093fSnbeams }
1244a835093fSnbeams 
1245a835093fSnbeams //------------------------------------------------------------------------------
12460d0321e0SJeremy L Thompson // Create operator
12470d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12480d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
12490d0321e0SJeremy L Thompson   Ceed              ceed;
12500d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
12510d0321e0SJeremy L Thompson 
1252b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12532b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
12542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
12552b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
12562b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
12572b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
12582b730f8bSJeremy L Thompson   CeedCallBackend(
12592b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
12602b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
12612b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
12622b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
12630d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12640d0321e0SJeremy L Thompson }
12650d0321e0SJeremy L Thompson 
12660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1267