xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-operator.c (revision c1222711bbaa154065e44a2caf05c765092ea957)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
849aac155SJeremy L Thompson #include <ceed.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
1007b31e0eSJeremy L Thompson #include <ceed/jit-tools.h>
11c85e8640SSebastian Grimberg #include <assert.h>
120d0321e0SJeremy L Thompson #include <stdbool.h>
130d0321e0SJeremy L Thompson #include <string.h>
14c85e8640SSebastian Grimberg #include <hip/hip_runtime.h>
152b730f8bSJeremy L Thompson 
1649aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
170d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
182b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
190d0321e0SJeremy L Thompson 
200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
210d0321e0SJeremy L Thompson // Destroy operator
220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
230d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Hip(CeedOperator op) {
240d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
25b7453713SJeremy L Thompson 
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
270d0321e0SJeremy L Thompson 
280d0321e0SJeremy L Thompson   // Apply data
29b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
30b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
310d0321e0SJeremy L Thompson   }
32b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
33*c1222711SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
340d0321e0SJeremy L Thompson 
35b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
36b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
370d0321e0SJeremy L Thompson   }
38b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
390d0321e0SJeremy L Thompson 
40b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
41b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
420d0321e0SJeremy L Thompson   }
43b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
440d0321e0SJeremy L Thompson 
45b2165e7aSSebastian Grimberg   // QFunction assembly data
46b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
47b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
480d0321e0SJeremy L Thompson   }
49b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
500d0321e0SJeremy L Thompson 
510d0321e0SJeremy L Thompson   // Diag data
520d0321e0SJeremy L Thompson   if (impl->diag) {
530d0321e0SJeremy L Thompson     Ceed ceed;
54b7453713SJeremy L Thompson 
552b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
56cbfe683aSSebastian Grimberg     if (impl->diag->module) {
572b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
58cbfe683aSSebastian Grimberg     }
59cbfe683aSSebastian Grimberg     if (impl->diag->module_point_block) {
60cbfe683aSSebastian Grimberg       CeedCallHip(ceed, hipModuleUnload(impl->diag->module_point_block));
61cbfe683aSSebastian Grimberg     }
62004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_in));
63004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_out));
642b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
65b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_in));
66b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_out));
67b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_in));
68b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_out));
69004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_in));
70004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_out));
71004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_in));
72004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_out));
73004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
74b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
75b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
76b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
770d0321e0SJeremy L Thompson   }
782b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
790d0321e0SJeremy L Thompson 
80a835093fSnbeams   if (impl->asmb) {
81a835093fSnbeams     Ceed ceed;
82b7453713SJeremy L Thompson 
832b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
842b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
852b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
862b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
87a835093fSnbeams   }
882b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
89a835093fSnbeams 
902b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
910d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
920d0321e0SJeremy L Thompson }
930d0321e0SJeremy L Thompson 
940d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
950d0321e0SJeremy L Thompson // Setup infields or outfields
960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
97b7453713SJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool is_input, CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e,
98b7453713SJeremy L Thompson                                        CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
990d0321e0SJeremy L Thompson   Ceed                ceed;
100b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
101b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
1020d0321e0SJeremy L Thompson 
103b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
104b7453713SJeremy L Thompson   if (is_input) {
105b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
106b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1070d0321e0SJeremy L Thompson   } else {
108b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
109b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1100d0321e0SJeremy L Thompson   }
1110d0321e0SJeremy L Thompson 
1120d0321e0SJeremy L Thompson   // Loop over fields
113b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
114004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
115b7453713SJeremy L Thompson     CeedSize     q_size;
116004e4986SSebastian Grimberg     CeedInt      size;
117004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
118b7453713SJeremy L Thompson     CeedBasis    basis;
1190d0321e0SJeremy L Thompson 
120004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
121004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
122004e4986SSebastian Grimberg       CeedElemRestriction elem_rstr;
1230d0321e0SJeremy L Thompson 
1240d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
125004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
126004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1270d0321e0SJeremy L Thompson 
1280d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
129b7453713SJeremy L Thompson       if (is_input) {
130004e4986SSebastian Grimberg         CeedVector vec;
131004e4986SSebastian Grimberg 
132004e4986SSebastian Grimberg         // Check for passive input
133b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
134b7453713SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
135004e4986SSebastian Grimberg           // Check eval_mode
136004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1370d0321e0SJeremy L Thompson             // Check for strided restriction
138b7453713SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
139b7453713SJeremy L Thompson             if (is_strided) {
1400d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
141b7453713SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1420d0321e0SJeremy L Thompson             }
1430d0321e0SJeremy L Thompson           }
1440d0321e0SJeremy L Thompson         }
1450d0321e0SJeremy L Thompson       }
146b7453713SJeremy L Thompson       if (skip_restriction) {
147ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
148b7453713SJeremy L Thompson         e_vecs[i + start_e] = NULL;
1490d0321e0SJeremy L Thompson       } else {
150b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1510d0321e0SJeremy L Thompson       }
1520d0321e0SJeremy L Thompson     }
1530d0321e0SJeremy L Thompson 
154004e4986SSebastian Grimberg     switch (eval_mode) {
1550d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
156b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
157b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
158b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1590d0321e0SJeremy L Thompson         break;
1600d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1610d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
162004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
163004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
164b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
165b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
166b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1670d0321e0SJeremy L Thompson         break;
1680d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
169b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
170b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
171b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
172b7453713SJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
1730d0321e0SJeremy L Thompson         break;
1740d0321e0SJeremy L Thompson     }
1750d0321e0SJeremy L Thompson   }
1760d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1770d0321e0SJeremy L Thompson }
1780d0321e0SJeremy L Thompson 
1790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
180ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
1810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1820d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
1830d0321e0SJeremy L Thompson   Ceed                ceed;
184b7453713SJeremy L Thompson   bool                is_setup_done;
185b7453713SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
186b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1870d0321e0SJeremy L Thompson   CeedQFunction       qf;
188b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
189b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
190b7453713SJeremy L Thompson 
191b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
192b7453713SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
193b7453713SJeremy L Thompson 
194b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
195b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1962b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1972b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
198b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
199b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
200b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2010d0321e0SJeremy L Thompson 
2020d0321e0SJeremy L Thompson   // Allocate
203b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
204*c1222711SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
205b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
206b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
207b7453713SJeremy L Thompson   impl->num_inputs  = num_input_fields;
208b7453713SJeremy L Thompson   impl->num_outputs = num_output_fields;
2090d0321e0SJeremy L Thompson 
210b7453713SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
2110d0321e0SJeremy L Thompson   // Infields
212b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2130d0321e0SJeremy L Thompson   // Outfields
214b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem));
2150d0321e0SJeremy L Thompson 
2162b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2170d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2180d0321e0SJeremy L Thompson }
2190d0321e0SJeremy L Thompson 
2200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2210d0321e0SJeremy L Thompson // Setup Operator Inputs
2220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
223b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
224b7453713SJeremy L Thompson                                               CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
225b7453713SJeremy L Thompson                                               CeedOperator_Hip *impl, CeedRequest *request) {
226b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
227004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
2280d0321e0SJeremy L Thompson     CeedVector          vec;
229b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2300d0321e0SJeremy L Thompson 
2310d0321e0SJeremy L Thompson     // Get input vector
232b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2330d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
234b7453713SJeremy L Thompson       if (skip_active) continue;
235b7453713SJeremy L Thompson       else vec = in_vec;
2360d0321e0SJeremy L Thompson     }
2370d0321e0SJeremy L Thompson 
238004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
239004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2400d0321e0SJeremy L Thompson     } else {
2410d0321e0SJeremy L Thompson       // Get input vector
242b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2430d0321e0SJeremy L Thompson       // Get input element restriction
244b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
245b7453713SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
2460d0321e0SJeremy L Thompson       // Restrict, if necessary
247b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {
2480d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
249b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2500d0321e0SJeremy L Thompson       } else {
251*c1222711SJeremy L Thompson         uint64_t state;
252*c1222711SJeremy L Thompson 
253*c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
254*c1222711SJeremy L Thompson         if (state != impl->input_states[i]) {
255b7453713SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
256*c1222711SJeremy L Thompson           impl->input_states[i] = state;
257*c1222711SJeremy L Thompson         }
2580d0321e0SJeremy L Thompson         // Get evec
259b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2600d0321e0SJeremy L Thompson       }
2610d0321e0SJeremy L Thompson     }
2620d0321e0SJeremy L Thompson   }
2630d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2640d0321e0SJeremy L Thompson }
2650d0321e0SJeremy L Thompson 
2660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2670d0321e0SJeremy L Thompson // Input Basis Action
2680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
269b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
270b7453713SJeremy L Thompson                                              CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
2712b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
272b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
273b7453713SJeremy L Thompson     CeedInt             elem_size, size;
274004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
275b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2760d0321e0SJeremy L Thompson     CeedBasis           basis;
2770d0321e0SJeremy L Thompson 
2780d0321e0SJeremy L Thompson     // Skip active input
279b7453713SJeremy L Thompson     if (skip_active) {
2800d0321e0SJeremy L Thompson       CeedVector vec;
281b7453713SJeremy L Thompson 
282b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2832b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2840d0321e0SJeremy L Thompson     }
285004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
286b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
287b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
288004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
289b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
2900d0321e0SJeremy L Thompson     // Basis action
291004e4986SSebastian Grimberg     switch (eval_mode) {
2920d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
293b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
2940d0321e0SJeremy L Thompson         break;
2950d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
2960d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
297004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
298004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
299b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
300004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3010d0321e0SJeremy L Thompson         break;
3020d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
3030d0321e0SJeremy L Thompson         break;  // No action
3040d0321e0SJeremy L Thompson     }
3050d0321e0SJeremy L Thompson   }
3060d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3070d0321e0SJeremy L Thompson }
3080d0321e0SJeremy L Thompson 
3090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3100d0321e0SJeremy L Thompson // Restore Input Vectors
3110d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
312b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
313b7453713SJeremy L Thompson                                                 const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
314b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
315004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
3160d0321e0SJeremy L Thompson     CeedVector   vec;
317004e4986SSebastian Grimberg 
3180d0321e0SJeremy L Thompson     // Skip active input
319b7453713SJeremy L Thompson     if (skip_active) {
320b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3212b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3220d0321e0SJeremy L Thompson     }
323004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
324004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3250d0321e0SJeremy L Thompson     } else {
326b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
327b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
328b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
3290d0321e0SJeremy L Thompson       } else {
330b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
3310d0321e0SJeremy L Thompson       }
3320d0321e0SJeremy L Thompson     }
3330d0321e0SJeremy L Thompson   }
3340d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3350d0321e0SJeremy L Thompson }
3360d0321e0SJeremy L Thompson 
3370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3380d0321e0SJeremy L Thompson // Apply and add to output
3390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
340b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
341b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
342b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
343b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
3440d0321e0SJeremy L Thompson   CeedQFunction       qf;
345b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
346b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
347b7453713SJeremy L Thompson 
348b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3492b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3502b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
351b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
352b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
353b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
3540d0321e0SJeremy L Thompson 
3550d0321e0SJeremy L Thompson   // Setup
3562b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
3570d0321e0SJeremy L Thompson 
3580d0321e0SJeremy L Thompson   // Input Evecs and Restriction
359b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
3600d0321e0SJeremy L Thompson 
3610d0321e0SJeremy L Thompson   // Input basis apply if needed
362b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
3630d0321e0SJeremy L Thompson 
3640d0321e0SJeremy L Thompson   // Output pointers, as necessary
365b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
366004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
367b7453713SJeremy L Thompson 
368004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
369004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
3700d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
371b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
372b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
3730d0321e0SJeremy L Thompson     }
3740d0321e0SJeremy L Thompson   }
3750d0321e0SJeremy L Thompson 
3760d0321e0SJeremy L Thompson   // Q function
377b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
3780d0321e0SJeremy L Thompson 
3790d0321e0SJeremy L Thompson   // Output basis apply if needed
380b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
381004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
382b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
383b7453713SJeremy L Thompson     CeedBasis           basis;
384b7453713SJeremy L Thompson 
385004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
386b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
387b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
388004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
389b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
3900d0321e0SJeremy L Thompson     // Basis action
391004e4986SSebastian Grimberg     switch (eval_mode) {
3920d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
393004e4986SSebastian Grimberg         break;  // No action
3940d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3950d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
396004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
397004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
398b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
399004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
4000d0321e0SJeremy L Thompson         break;
4010d0321e0SJeremy L Thompson       // LCOV_EXCL_START
4020d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
4036e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
4040d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
4050d0321e0SJeremy L Thompson       }
4060d0321e0SJeremy L Thompson     }
407004e4986SSebastian Grimberg   }
4080d0321e0SJeremy L Thompson 
4090d0321e0SJeremy L Thompson   // Output restriction
410b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
411004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
412b7453713SJeremy L Thompson     CeedVector          vec;
413b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
414b7453713SJeremy L Thompson 
4150d0321e0SJeremy L Thompson     // Restore evec
416004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
417004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
418b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4190d0321e0SJeremy L Thompson     }
4200d0321e0SJeremy L Thompson     // Get output vector
421b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4220d0321e0SJeremy L Thompson     // Restrict
423b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4240d0321e0SJeremy L Thompson     // Active
425b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4260d0321e0SJeremy L Thompson 
427b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4280d0321e0SJeremy L Thompson   }
4290d0321e0SJeremy L Thompson 
4300d0321e0SJeremy L Thompson   // Restore input arrays
431b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4320d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4330d0321e0SJeremy L Thompson }
4340d0321e0SJeremy L Thompson 
4350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
436004e4986SSebastian Grimberg // Linear QFunction Assembly Core
4370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4382b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
4390d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
440b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
441b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
442b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
443004e4986SSebastian Grimberg   CeedVector         *active_inputs;
444b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
445b7453713SJeremy L Thompson   CeedQFunction       qf;
446b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
447b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
448b7453713SJeremy L Thompson 
4492b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
450b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
451e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
452e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
453b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
454004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
455b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
456b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
457004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
458004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
4590d0321e0SJeremy L Thompson 
4600d0321e0SJeremy L Thompson   // Setup
4612b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4620d0321e0SJeremy L Thompson 
4630d0321e0SJeremy L Thompson   // Input Evecs and Restriction
464b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
4650d0321e0SJeremy L Thompson 
4660d0321e0SJeremy L Thompson   // Count number of active input fields
467b7453713SJeremy L Thompson   if (!num_active_in) {
468b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
469b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
470b7453713SJeremy L Thompson       CeedVector  vec;
471b7453713SJeremy L Thompson 
4720d0321e0SJeremy L Thompson       // Get input vector
473b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4740d0321e0SJeremy L Thompson       // Check if active input
4750d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
476b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
477b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
478b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
479004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
4800d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
481004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
482004e4986SSebastian Grimberg 
483004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
484b7453713SJeremy L Thompson           CeedCallBackend(
485004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
4860d0321e0SJeremy L Thompson         }
487b7453713SJeremy L Thompson         num_active_in += size;
488b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
4890d0321e0SJeremy L Thompson       }
4900d0321e0SJeremy L Thompson     }
491b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
492004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
4930d0321e0SJeremy L Thompson   }
4940d0321e0SJeremy L Thompson 
4950d0321e0SJeremy L Thompson   // Count number of active output fields
496b7453713SJeremy L Thompson   if (!num_active_out) {
497b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
498b7453713SJeremy L Thompson       CeedVector vec;
499b7453713SJeremy L Thompson 
5000d0321e0SJeremy L Thompson       // Get output vector
501b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5020d0321e0SJeremy L Thompson       // Check if active output
5030d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
504b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
505b7453713SJeremy L Thompson         num_active_out += size;
5060d0321e0SJeremy L Thompson       }
5070d0321e0SJeremy L Thompson     }
508b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
5090d0321e0SJeremy L Thompson   }
5100d0321e0SJeremy L Thompson 
5110d0321e0SJeremy L Thompson   // Check sizes
512b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5130d0321e0SJeremy L Thompson 
5140d0321e0SJeremy L Thompson   // Build objects if needed
5150d0321e0SJeremy L Thompson   if (build_objects) {
516b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
517b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
518b7453713SJeremy L Thompson 
519004e4986SSebastian Grimberg     // Create output restriction
520b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
521b7453713SJeremy L Thompson                                                      num_active_in * num_active_out * num_elem * Q, strides, rstr));
5220d0321e0SJeremy L Thompson     // Create assembled vector
523b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
5240d0321e0SJeremy L Thompson   }
5252b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
526b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
5270d0321e0SJeremy L Thompson 
5280d0321e0SJeremy L Thompson   // Input basis apply
529b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
5300d0321e0SJeremy L Thompson 
5310d0321e0SJeremy L Thompson   // Assemble QFunction
532b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
5330d0321e0SJeremy L Thompson     // Set Inputs
534004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
535b7453713SJeremy L Thompson     if (num_active_in > 1) {
536004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
5370d0321e0SJeremy L Thompson     }
5380d0321e0SJeremy L Thompson     // Set Outputs
539b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
540b7453713SJeremy L Thompson       CeedVector vec;
541b7453713SJeremy L Thompson 
5420d0321e0SJeremy L Thompson       // Get output vector
543b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5440d0321e0SJeremy L Thompson       // Check if active output
5450d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
546b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
547b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
548b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
5490d0321e0SJeremy L Thompson       }
5500d0321e0SJeremy L Thompson     }
5510d0321e0SJeremy L Thompson     // Apply QFunction
552b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
5530d0321e0SJeremy L Thompson   }
5540d0321e0SJeremy L Thompson 
555004e4986SSebastian Grimberg   // Un-set output q_vecs to prevent accidental overwrite of Assembled
556b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
557b7453713SJeremy L Thompson     CeedVector vec;
558b7453713SJeremy L Thompson 
5590d0321e0SJeremy L Thompson     // Get output vector
560b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5610d0321e0SJeremy L Thompson     // Check if active output
5620d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
563b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
5640d0321e0SJeremy L Thompson     }
5650d0321e0SJeremy L Thompson   }
5660d0321e0SJeremy L Thompson 
5670d0321e0SJeremy L Thompson   // Restore input arrays
568b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
5690d0321e0SJeremy L Thompson 
5700d0321e0SJeremy L Thompson   // Restore output
571b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
5720d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5730d0321e0SJeremy L Thompson }
5740d0321e0SJeremy L Thompson 
5750d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5760d0321e0SJeremy L Thompson // Assemble Linear QFunction
5770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5782b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
5792b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
5800d0321e0SJeremy L Thompson }
5810d0321e0SJeremy L Thompson 
5820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
583b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
5840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5852b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
5862b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
5870d0321e0SJeremy L Thompson }
5880d0321e0SJeremy L Thompson 
5890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
590004e4986SSebastian Grimberg // Assemble Diagonal Setup
5910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
592cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
5930d0321e0SJeremy L Thompson   Ceed                ceed;
594004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
595cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
596004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
597b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
598b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
5990d0321e0SJeremy L Thompson   CeedQFunction       qf;
600b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
601b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
602b7453713SJeremy L Thompson 
603b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
6042b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
605b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
6060d0321e0SJeremy L Thompson 
6070d0321e0SJeremy L Thompson   // Determine active input basis
608b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
609b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
610b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
6110d0321e0SJeremy L Thompson     CeedVector vec;
612b7453713SJeremy L Thompson 
613b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
6140d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
615004e4986SSebastian Grimberg       CeedBasis    basis;
616004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
617b7453713SJeremy L Thompson 
618004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
619004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
620004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
621004e4986SSebastian Grimberg       basis_in = basis;
622004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
623004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
624004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
625004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
626004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
627004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
628004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
6290d0321e0SJeremy L Thompson       }
6300d0321e0SJeremy L Thompson     }
6310d0321e0SJeremy L Thompson   }
6320d0321e0SJeremy L Thompson 
6330d0321e0SJeremy L Thompson   // Determine active output basis
634b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
635b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
636b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
6370d0321e0SJeremy L Thompson     CeedVector vec;
638b7453713SJeremy L Thompson 
639b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
6400d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
641004e4986SSebastian Grimberg       CeedBasis    basis;
642004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
643b7453713SJeremy L Thompson 
644004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
645004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
646004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
647004e4986SSebastian Grimberg       basis_out = basis;
648004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
649004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
650004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
651004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
652004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
653004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
654004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
6550d0321e0SJeremy L Thompson       }
6560d0321e0SJeremy L Thompson     }
6570d0321e0SJeremy L Thompson   }
6580d0321e0SJeremy L Thompson 
6590d0321e0SJeremy L Thompson   // Operator data struct
6602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
6612b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
6620d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
663b7453713SJeremy L Thompson 
664cbfe683aSSebastian Grimberg   // Basis matrices
665004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
666004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
667004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
668004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
669004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
670004e4986SSebastian Grimberg   bool          has_eval_none    = false;
6710d0321e0SJeremy L Thompson 
6720d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
673004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
674004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
675004e4986SSebastian Grimberg   if (has_eval_none) {
6760d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
677b7453713SJeremy L Thompson 
678004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
679004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
680b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
681b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
682004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
6830d0321e0SJeremy L Thompson   }
6840d0321e0SJeremy L Thompson 
685004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
686004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
687004e4986SSebastian Grimberg     CeedFESpace fespace;
688004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
6890d0321e0SJeremy L Thompson 
690004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
691004e4986SSebastian Grimberg     switch (fespace) {
692004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
693004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
694004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
695004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
6960d0321e0SJeremy L Thompson 
697004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
698004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
6990d0321e0SJeremy L Thompson 
700004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
701004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
702004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
703004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
704004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
705004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
706004e4986SSebastian Grimberg         if (in) {
707004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
708004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
709004e4986SSebastian Grimberg         } else {
710004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
711004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
712004e4986SSebastian Grimberg         }
713004e4986SSebastian Grimberg       } break;
714004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
715004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
716004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
717004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
718004e4986SSebastian Grimberg 
719004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
720004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
721004e4986SSebastian Grimberg 
722004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
723004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
724004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
725004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
726004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
727004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
728004e4986SSebastian Grimberg         if (in) {
729004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
730004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
731004e4986SSebastian Grimberg         } else {
732004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
733004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
734004e4986SSebastian Grimberg         }
735004e4986SSebastian Grimberg       } break;
736004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
737004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
738004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
739004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
740004e4986SSebastian Grimberg 
741004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
742004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
743004e4986SSebastian Grimberg 
744004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
745004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
746004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
747004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
748004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
749004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
750004e4986SSebastian Grimberg         if (in) {
751004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
752004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
753004e4986SSebastian Grimberg         } else {
754004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
755004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
756004e4986SSebastian Grimberg         }
757004e4986SSebastian Grimberg       } break;
758004e4986SSebastian Grimberg     }
759004e4986SSebastian Grimberg   }
760004e4986SSebastian Grimberg 
761004e4986SSebastian Grimberg   // Arrays of eval_modes
762004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
763004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
764004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
765004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
766004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
767004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
7680d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7690d0321e0SJeremy L Thompson }
7700d0321e0SJeremy L Thompson 
7710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
772cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
773cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
774cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
775cbfe683aSSebastian Grimberg   Ceed                ceed;
77622070f95SJeremy L Thompson   char               *diagonal_kernel_source;
77722070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
778cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
779cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
780cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
781cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
782cbfe683aSSebastian Grimberg   CeedQFunction       qf;
783cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
784cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
785cbfe683aSSebastian Grimberg 
786cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
787cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
788cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
789cbfe683aSSebastian Grimberg 
790cbfe683aSSebastian Grimberg   // Determine active input basis
791cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
792cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
793cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
794cbfe683aSSebastian Grimberg     CeedVector vec;
795cbfe683aSSebastian Grimberg 
796cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
797cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
798cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
799cbfe683aSSebastian Grimberg 
800cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
801cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
802cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
803cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
804cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
805cbfe683aSSebastian Grimberg       }
806cbfe683aSSebastian Grimberg     }
807cbfe683aSSebastian Grimberg   }
808cbfe683aSSebastian Grimberg 
809cbfe683aSSebastian Grimberg   // Determine active output basis
810cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
811cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
812cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
813cbfe683aSSebastian Grimberg     CeedVector vec;
814cbfe683aSSebastian Grimberg 
815cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
816cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
817cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
818cbfe683aSSebastian Grimberg 
819cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
820cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
821cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
822cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
823cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
824cbfe683aSSebastian Grimberg       }
825cbfe683aSSebastian Grimberg     }
826cbfe683aSSebastian Grimberg   }
827cbfe683aSSebastian Grimberg 
828cbfe683aSSebastian Grimberg   // Operator data struct
829cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
830cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
831cbfe683aSSebastian Grimberg 
832cbfe683aSSebastian Grimberg   // Assemble kernel
833cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
834cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
835cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
836cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
837cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
838cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
839cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
840cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
841cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
842cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
843cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
844cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
845cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
846cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
847cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
848cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
849cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
850cbfe683aSSebastian Grimberg }
851cbfe683aSSebastian Grimberg 
852cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
853004e4986SSebastian Grimberg // Assemble Diagonal Core
8540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
855b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
8560d0321e0SJeremy L Thompson   Ceed                ceed;
857cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
858b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
859b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
860004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
861004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
8620d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
863b7453713SJeremy L Thompson 
864b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8652b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
8660d0321e0SJeremy L Thompson 
8670d0321e0SJeremy L Thompson   // Assemble QFunction
868004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
869004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
870004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
8710d0321e0SJeremy L Thompson 
872cbfe683aSSebastian Grimberg   // Setup
873cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
874cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
875cbfe683aSSebastian Grimberg 
876cbfe683aSSebastian Grimberg   assert(diag != NULL);
877cbfe683aSSebastian Grimberg 
878cbfe683aSSebastian Grimberg   // Assemble kernel if needed
879cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
880cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
881cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
8829330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
883b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
884b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
8859330daecSnbeams 
886cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
887cbfe683aSSebastian Grimberg   }
8880d0321e0SJeremy L Thompson 
889004e4986SSebastian Grimberg   // Restriction and diagonal vector
890004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
891004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
892004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
893004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
894004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
895004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
896004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
897004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
898004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
8990d0321e0SJeremy L Thompson   }
900004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
901004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
902b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
9030d0321e0SJeremy L Thompson 
90491db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
905004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
906004e4986SSebastian Grimberg   if (num_nodes > 0) {
9070d0321e0SJeremy L Thompson     // Assemble element operator diagonals
908b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
909b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
9100d0321e0SJeremy L Thompson 
9110d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
912004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
913004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
914004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
915004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
916004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
917b7453713SJeremy L Thompson 
918b7453713SJeremy L Thompson     if (is_point_block) {
919004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
9200d0321e0SJeremy L Thompson     } else {
921004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
9220d0321e0SJeremy L Thompson     }
9230d0321e0SJeremy L Thompson 
9240d0321e0SJeremy L Thompson     // Restore arrays
925b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
926b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
92791db28b6SZach Atkins   }
9280d0321e0SJeremy L Thompson 
9290d0321e0SJeremy L Thompson   // Assemble local operator diagonal
930b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
9310d0321e0SJeremy L Thompson 
9320d0321e0SJeremy L Thompson   // Cleanup
933b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
9340d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9350d0321e0SJeremy L Thompson }
9360d0321e0SJeremy L Thompson 
9370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9380d0321e0SJeremy L Thompson // Assemble Linear Diagonal
9390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9402b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
9412b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
9426aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9430d0321e0SJeremy L Thompson }
9440d0321e0SJeremy L Thompson 
9450d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9460d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
9470d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9482b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
9492b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
9506aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9510d0321e0SJeremy L Thompson }
9520d0321e0SJeremy L Thompson 
953a835093fSnbeams //------------------------------------------------------------------------------
954004e4986SSebastian Grimberg // Single Operator Assembly Setup
955a835093fSnbeams //------------------------------------------------------------------------------
9569330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
957a835093fSnbeams   Ceed                ceed;
95822070f95SJeremy L Thompson   char               *assembly_kernel_source;
95922070f95SJeremy L Thompson   const char         *assembly_kernel_path;
960004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
9613b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
962004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
963b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
964b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
965b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
966b7453713SJeremy L Thompson   CeedQFunction       qf;
967b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
968a835093fSnbeams   CeedOperator_Hip   *impl;
969b7453713SJeremy L Thompson 
970b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
9712b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
972a835093fSnbeams 
973a835093fSnbeams   // Get intput and output fields
9742b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
975a835093fSnbeams 
976a835093fSnbeams   // Determine active input basis eval mode
9772b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
9782b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
979a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
980a835093fSnbeams     CeedVector vec;
981b7453713SJeremy L Thompson 
9822b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
983a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
984004e4986SSebastian Grimberg       CeedBasis    basis;
985b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
986b7453713SJeremy L Thompson 
987004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
988004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
989004e4986SSebastian Grimberg       basis_in = basis;
9902b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
991004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
992004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
993004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
9942b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
995004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
996004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
997004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
998004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
999004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1000004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1001a835093fSnbeams         }
1002004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1003a835093fSnbeams       }
1004a835093fSnbeams     }
1005a835093fSnbeams   }
1006a835093fSnbeams 
1007a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
10082b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1009a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1010a835093fSnbeams     CeedVector vec;
1011b7453713SJeremy L Thompson 
10122b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1013a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1014004e4986SSebastian Grimberg       CeedBasis    basis;
1015b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1016b7453713SJeremy L Thompson 
1017004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1018004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1019004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1020004e4986SSebastian Grimberg       basis_out = basis;
10212b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1022004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1023004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1024004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1025004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1026004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
10272b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1028004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1029004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1030004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1031004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1032004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1033004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1034004e4986SSebastian Grimberg         }
1035004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1036a835093fSnbeams       }
1037a835093fSnbeams     }
1038a835093fSnbeams   }
1039004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1040a835093fSnbeams 
10412b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1042a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1043004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1044004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1045004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1046004e4986SSebastian Grimberg 
1047004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1048004e4986SSebastian Grimberg 
1049004e4986SSebastian Grimberg   if (fallback) {
1050004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1051004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1052004e4986SSebastian Grimberg   }
1053a835093fSnbeams 
1054a835093fSnbeams   // Compile kernels
1055004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1056004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
10572b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
105823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
10592b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
106023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1061004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1062004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1063004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1064cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
10659330daecSnbeams                                   use_ceedsize_idx));
1066004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
10672b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
10682b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1069a835093fSnbeams 
1070004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1071004e4986SSebastian Grimberg   {
1072004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1073004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1074004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1075004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1076004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1077a835093fSnbeams 
1078004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1079004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1080004e4986SSebastian Grimberg     }
1081004e4986SSebastian Grimberg     if (has_eval_none) {
1082004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1083004e4986SSebastian 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;
1084004e4986SSebastian Grimberg     }
1085b7453713SJeremy L Thompson 
1086b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1087004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1088004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1089004e4986SSebastian Grimberg 
1090004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1091004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1092004e4986SSebastian Grimberg       if (q_comp > 1) {
1093004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1094004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1095004e4986SSebastian Grimberg       }
1096004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1097004e4986SSebastian Grimberg 
1098004e4986SSebastian 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),
1099004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1100004e4986SSebastian Grimberg     }
1101004e4986SSebastian Grimberg 
1102004e4986SSebastian Grimberg     if (identity) {
1103004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1104a835093fSnbeams     }
1105a835093fSnbeams   }
1106a835093fSnbeams 
1107004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1108004e4986SSebastian Grimberg   {
1109004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1110004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1111004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1112004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1113004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1114b7453713SJeremy L Thompson 
1115004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1116004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1117004e4986SSebastian Grimberg     }
1118004e4986SSebastian Grimberg     if (has_eval_none) {
1119004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1120004e4986SSebastian 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;
1121a835093fSnbeams     }
1122a835093fSnbeams 
1123b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1124004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1125004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1126004e4986SSebastian Grimberg 
1127004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1128004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1129004e4986SSebastian Grimberg       if (q_comp > 1) {
1130004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1131004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1132004e4986SSebastian Grimberg       }
1133004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1134004e4986SSebastian Grimberg 
1135004e4986SSebastian 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),
1136004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1137004e4986SSebastian Grimberg     }
1138004e4986SSebastian Grimberg 
1139004e4986SSebastian Grimberg     if (identity) {
1140004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1141a835093fSnbeams     }
1142a835093fSnbeams   }
1143a835093fSnbeams   return CEED_ERROR_SUCCESS;
1144a835093fSnbeams }
1145a835093fSnbeams 
1146a835093fSnbeams //------------------------------------------------------------------------------
1147cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1148cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1149cefa2673SJeremy L Thompson //
1150ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1151cefa2673SJeremy L Thompson // modes).
1152cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1153a835093fSnbeams //------------------------------------------------------------------------------
11542b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1155a835093fSnbeams   Ceed                ceed;
1156b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1157004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1158b7453713SJeremy L Thompson   CeedScalar         *values_array;
1159004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1160b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1161004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1162004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1163004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1164004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1165a835093fSnbeams   CeedOperator_Hip   *impl;
1166b7453713SJeremy L Thompson 
1167b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11682b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1169a835093fSnbeams 
1170a835093fSnbeams   // Assemble QFunction
1171004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1172004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1173004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1174a835093fSnbeams 
11759330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
11769330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
11779330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1178004e4986SSebastian Grimberg 
11799330daecSnbeams   // Setup
1180004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1181004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1182004e4986SSebastian Grimberg 
1183004e4986SSebastian Grimberg   assert(asmb != NULL);
1184004e4986SSebastian Grimberg 
1185004e4986SSebastian Grimberg   // Assemble element operator
1186004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1187004e4986SSebastian Grimberg   values_array += offset;
1188004e4986SSebastian Grimberg 
1189004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1190004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1191004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1192004e4986SSebastian Grimberg 
1193004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1194004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1195004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1196004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1197004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1198004e4986SSebastian Grimberg   }
1199004e4986SSebastian Grimberg 
1200004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1201004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1202004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1203004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1204004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1205004e4986SSebastian Grimberg 
1206004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1207004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1208004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1209004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1210004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1211004e4986SSebastian Grimberg     }
1212004e4986SSebastian Grimberg   } else {
1213004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1214004e4986SSebastian Grimberg     orients_out      = orients_in;
1215004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
12169330daecSnbeams   }
12179330daecSnbeams 
1218a835093fSnbeams   // Compute B^T D B
1219004e4986SSebastian Grimberg   CeedInt shared_mem =
1220004e4986SSebastian 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)) *
1221004e4986SSebastian Grimberg       sizeof(CeedScalar);
1222004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1223004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1224004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1225b7453713SJeremy L Thompson 
12262b730f8bSJeremy L Thompson   CeedCallBackend(
1227004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1228a835093fSnbeams 
1229a835093fSnbeams   // Restore arrays
12302b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1231004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1232a835093fSnbeams 
1233a835093fSnbeams   // Cleanup
12342b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1235004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1236004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1237004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1238004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1239004e4986SSebastian Grimberg   }
1240004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1241004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1242004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1243004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1244004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1245004e4986SSebastian Grimberg     }
1246004e4986SSebastian Grimberg   }
1247a835093fSnbeams   return CEED_ERROR_SUCCESS;
1248a835093fSnbeams }
1249a835093fSnbeams 
1250a835093fSnbeams //------------------------------------------------------------------------------
12510d0321e0SJeremy L Thompson // Create operator
12520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12530d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
12540d0321e0SJeremy L Thompson   Ceed              ceed;
12550d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
12560d0321e0SJeremy L Thompson 
1257b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12582b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
12592b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
12602b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
12612b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
12622b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
12632b730f8bSJeremy L Thompson   CeedCallBackend(
12642b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
12652b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
12662b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
12672b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
12680d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12690d0321e0SJeremy L Thompson }
12700d0321e0SJeremy L Thompson 
12710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1272