xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-operator.c (revision 004e49868906b3e3ec4a252ac682c88f9414881a)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
849aac155SJeremy L Thompson #include <ceed.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
1007b31e0eSJeremy L Thompson #include <ceed/jit-tools.h>
11c85e8640SSebastian Grimberg #include <assert.h>
120d0321e0SJeremy L Thompson #include <stdbool.h>
130d0321e0SJeremy L Thompson #include <string.h>
14c85e8640SSebastian Grimberg #include <hip/hip_runtime.h>
152b730f8bSJeremy L Thompson 
1649aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
170d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
182b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
190d0321e0SJeremy L Thompson 
200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
210d0321e0SJeremy L Thompson // Destroy operator
220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
230d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Hip(CeedOperator op) {
240d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
25b7453713SJeremy L Thompson 
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
270d0321e0SJeremy L Thompson 
280d0321e0SJeremy L Thompson   // Apply data
29b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
30b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
310d0321e0SJeremy L Thompson   }
32b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
330d0321e0SJeremy L Thompson 
34b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
35b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
360d0321e0SJeremy L Thompson   }
37b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
380d0321e0SJeremy L Thompson 
39b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
40b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
410d0321e0SJeremy L Thompson   }
42b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
430d0321e0SJeremy L Thompson 
44b2165e7aSSebastian Grimberg   // QFunction assembly data
45b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
46b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
470d0321e0SJeremy L Thompson   }
48b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
490d0321e0SJeremy L Thompson 
500d0321e0SJeremy L Thompson   // Diag data
510d0321e0SJeremy L Thompson   if (impl->diag) {
520d0321e0SJeremy L Thompson     Ceed ceed;
53b7453713SJeremy L Thompson 
542b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
552b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
56*004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_in));
57*004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_out));
582b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
59b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_in));
60b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_out));
61b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_in));
62b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_out));
63*004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_in));
64*004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_out));
65*004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_in));
66*004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_out));
67*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
68b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
69b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
70b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
710d0321e0SJeremy L Thompson   }
722b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
730d0321e0SJeremy L Thompson 
74a835093fSnbeams   if (impl->asmb) {
75a835093fSnbeams     Ceed ceed;
76b7453713SJeremy L Thompson 
772b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
782b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
792b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
802b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
81a835093fSnbeams   }
822b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
83a835093fSnbeams 
842b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
850d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
860d0321e0SJeremy L Thompson }
870d0321e0SJeremy L Thompson 
880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
890d0321e0SJeremy L Thompson // Setup infields or outfields
900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
91b7453713SJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool is_input, CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e,
92b7453713SJeremy L Thompson                                        CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
930d0321e0SJeremy L Thompson   Ceed                ceed;
94b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
95b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
960d0321e0SJeremy L Thompson 
97b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
98b7453713SJeremy L Thompson   if (is_input) {
99b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
100b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1010d0321e0SJeremy L Thompson   } else {
102b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
103b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1040d0321e0SJeremy L Thompson   }
1050d0321e0SJeremy L Thompson 
1060d0321e0SJeremy L Thompson   // Loop over fields
107b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
108*004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
109b7453713SJeremy L Thompson     CeedSize     q_size;
110*004e4986SSebastian Grimberg     CeedInt      size;
111*004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
112b7453713SJeremy L Thompson     CeedBasis    basis;
1130d0321e0SJeremy L Thompson 
114*004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
115*004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
116*004e4986SSebastian Grimberg       CeedElemRestriction elem_rstr;
1170d0321e0SJeremy L Thompson 
1180d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
119*004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
120*004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1210d0321e0SJeremy L Thompson 
1220d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
123b7453713SJeremy L Thompson       if (is_input) {
124*004e4986SSebastian Grimberg         CeedVector vec;
125*004e4986SSebastian Grimberg 
126*004e4986SSebastian Grimberg         // Check for passive input
127b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
128b7453713SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
129*004e4986SSebastian Grimberg           // Check eval_mode
130*004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1310d0321e0SJeremy L Thompson             // Check for strided restriction
132b7453713SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
133b7453713SJeremy L Thompson             if (is_strided) {
1340d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
135b7453713SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1360d0321e0SJeremy L Thompson             }
1370d0321e0SJeremy L Thompson           }
1380d0321e0SJeremy L Thompson         }
1390d0321e0SJeremy L Thompson       }
140b7453713SJeremy L Thompson       if (skip_restriction) {
141ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
142b7453713SJeremy L Thompson         e_vecs[i + start_e] = NULL;
1430d0321e0SJeremy L Thompson       } else {
144b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1450d0321e0SJeremy L Thompson       }
1460d0321e0SJeremy L Thompson     }
1470d0321e0SJeremy L Thompson 
148*004e4986SSebastian Grimberg     switch (eval_mode) {
1490d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
150b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
151b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
152b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1530d0321e0SJeremy L Thompson         break;
1540d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1550d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
156*004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
157*004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
158b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
159b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
160b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1610d0321e0SJeremy L Thompson         break;
1620d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
163b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
164b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
165b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
166b7453713SJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
1670d0321e0SJeremy L Thompson         break;
1680d0321e0SJeremy L Thompson     }
1690d0321e0SJeremy L Thompson   }
1700d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1710d0321e0SJeremy L Thompson }
1720d0321e0SJeremy L Thompson 
1730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
174ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
1750d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1760d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
1770d0321e0SJeremy L Thompson   Ceed                ceed;
178b7453713SJeremy L Thompson   bool                is_setup_done;
179b7453713SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
180b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1810d0321e0SJeremy L Thompson   CeedQFunction       qf;
182b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
183b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
184b7453713SJeremy L Thompson 
185b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
186b7453713SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
187b7453713SJeremy L Thompson 
188b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
189b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1902b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1912b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
192b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
193b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
194b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1950d0321e0SJeremy L Thompson 
1960d0321e0SJeremy L Thompson   // Allocate
197b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
198b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
199b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
200b7453713SJeremy L Thompson   impl->num_inputs  = num_input_fields;
201b7453713SJeremy L Thompson   impl->num_outputs = num_output_fields;
2020d0321e0SJeremy L Thompson 
203b7453713SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
2040d0321e0SJeremy L Thompson   // Infields
205b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2060d0321e0SJeremy L Thompson   // Outfields
207b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem));
2080d0321e0SJeremy L Thompson 
2092b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2100d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2110d0321e0SJeremy L Thompson }
2120d0321e0SJeremy L Thompson 
2130d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2140d0321e0SJeremy L Thompson // Setup Operator Inputs
2150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
216b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
217b7453713SJeremy L Thompson                                               CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
218b7453713SJeremy L Thompson                                               CeedOperator_Hip *impl, CeedRequest *request) {
219b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
220*004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
2210d0321e0SJeremy L Thompson     CeedVector          vec;
222b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2230d0321e0SJeremy L Thompson 
2240d0321e0SJeremy L Thompson     // Get input vector
225b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2260d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
227b7453713SJeremy L Thompson       if (skip_active) continue;
228b7453713SJeremy L Thompson       else vec = in_vec;
2290d0321e0SJeremy L Thompson     }
2300d0321e0SJeremy L Thompson 
231*004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
232*004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2330d0321e0SJeremy L Thompson     } else {
2340d0321e0SJeremy L Thompson       // Get input vector
235b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2360d0321e0SJeremy L Thompson       // Get input element restriction
237b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
238b7453713SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
2390d0321e0SJeremy L Thompson       // Restrict, if necessary
240b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {
2410d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
242b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2430d0321e0SJeremy L Thompson       } else {
244b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
2450d0321e0SJeremy L Thompson         // Get evec
246b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2470d0321e0SJeremy L Thompson       }
2480d0321e0SJeremy L Thompson     }
2490d0321e0SJeremy L Thompson   }
2500d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2510d0321e0SJeremy L Thompson }
2520d0321e0SJeremy L Thompson 
2530d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2540d0321e0SJeremy L Thompson // Input Basis Action
2550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
256b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
257b7453713SJeremy L Thompson                                              CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
2582b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
259b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
260b7453713SJeremy L Thompson     CeedInt             elem_size, size;
261*004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
262b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2630d0321e0SJeremy L Thompson     CeedBasis           basis;
2640d0321e0SJeremy L Thompson 
2650d0321e0SJeremy L Thompson     // Skip active input
266b7453713SJeremy L Thompson     if (skip_active) {
2670d0321e0SJeremy L Thompson       CeedVector vec;
268b7453713SJeremy L Thompson 
269b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2702b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2710d0321e0SJeremy L Thompson     }
272*004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
273b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
274b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
275*004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
276b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
2770d0321e0SJeremy L Thompson     // Basis action
278*004e4986SSebastian Grimberg     switch (eval_mode) {
2790d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
280b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
2810d0321e0SJeremy L Thompson         break;
2820d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
2830d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
284*004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
285*004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
286b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
287*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
2880d0321e0SJeremy L Thompson         break;
2890d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
2900d0321e0SJeremy L Thompson         break;  // No action
2910d0321e0SJeremy L Thompson     }
2920d0321e0SJeremy L Thompson   }
2930d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2940d0321e0SJeremy L Thompson }
2950d0321e0SJeremy L Thompson 
2960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2970d0321e0SJeremy L Thompson // Restore Input Vectors
2980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
299b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
300b7453713SJeremy L Thompson                                                 const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
301b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
302*004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
3030d0321e0SJeremy L Thompson     CeedVector   vec;
304*004e4986SSebastian Grimberg 
3050d0321e0SJeremy L Thompson     // Skip active input
306b7453713SJeremy L Thompson     if (skip_active) {
307b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3082b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3090d0321e0SJeremy L Thompson     }
310*004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
311*004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3120d0321e0SJeremy L Thompson     } else {
313b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
314b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
315b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
3160d0321e0SJeremy L Thompson       } else {
317b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
3180d0321e0SJeremy L Thompson       }
3190d0321e0SJeremy L Thompson     }
3200d0321e0SJeremy L Thompson   }
3210d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3220d0321e0SJeremy L Thompson }
3230d0321e0SJeremy L Thompson 
3240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3250d0321e0SJeremy L Thompson // Apply and add to output
3260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
327b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
328b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
329b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
330b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
3310d0321e0SJeremy L Thompson   CeedQFunction       qf;
332b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
333b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
334b7453713SJeremy L Thompson 
335b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3362b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3372b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
338b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
339b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
340b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
3410d0321e0SJeremy L Thompson 
3420d0321e0SJeremy L Thompson   // Setup
3432b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
3440d0321e0SJeremy L Thompson 
3450d0321e0SJeremy L Thompson   // Input Evecs and Restriction
346b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
3470d0321e0SJeremy L Thompson 
3480d0321e0SJeremy L Thompson   // Input basis apply if needed
349b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
3500d0321e0SJeremy L Thompson 
3510d0321e0SJeremy L Thompson   // Output pointers, as necessary
352b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
353*004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
354b7453713SJeremy L Thompson 
355*004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
356*004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
3570d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
358b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
359b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
3600d0321e0SJeremy L Thompson     }
3610d0321e0SJeremy L Thompson   }
3620d0321e0SJeremy L Thompson 
3630d0321e0SJeremy L Thompson   // Q function
364b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
3650d0321e0SJeremy L Thompson 
3660d0321e0SJeremy L Thompson   // Output basis apply if needed
367b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
368*004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
369b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
370b7453713SJeremy L Thompson     CeedBasis           basis;
371b7453713SJeremy L Thompson 
372*004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
373b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
374b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
375*004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
376b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
3770d0321e0SJeremy L Thompson     // Basis action
378*004e4986SSebastian Grimberg     switch (eval_mode) {
3790d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
380*004e4986SSebastian Grimberg         break;  // No action
3810d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3820d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
383*004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
384*004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
385b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
386*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
3870d0321e0SJeremy L Thompson         break;
3880d0321e0SJeremy L Thompson       // LCOV_EXCL_START
3890d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
3900d0321e0SJeremy L Thompson         Ceed ceed;
391b7453713SJeremy L Thompson 
3922b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
3932b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
3940d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
3950d0321e0SJeremy L Thompson       }
3960d0321e0SJeremy L Thompson     }
397*004e4986SSebastian Grimberg   }
3980d0321e0SJeremy L Thompson 
3990d0321e0SJeremy L Thompson   // Output restriction
400b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
401*004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
402b7453713SJeremy L Thompson     CeedVector          vec;
403b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
404b7453713SJeremy L Thompson 
4050d0321e0SJeremy L Thompson     // Restore evec
406*004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
407*004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
408b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4090d0321e0SJeremy L Thompson     }
4100d0321e0SJeremy L Thompson     // Get output vector
411b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4120d0321e0SJeremy L Thompson     // Restrict
413b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4140d0321e0SJeremy L Thompson     // Active
415b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4160d0321e0SJeremy L Thompson 
417b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4180d0321e0SJeremy L Thompson   }
4190d0321e0SJeremy L Thompson 
4200d0321e0SJeremy L Thompson   // Restore input arrays
421b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4220d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4230d0321e0SJeremy L Thompson }
4240d0321e0SJeremy L Thompson 
4250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
426*004e4986SSebastian Grimberg // Linear QFunction Assembly Core
4270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4282b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
4290d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
430b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
431b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
432b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
433*004e4986SSebastian Grimberg   CeedVector         *active_inputs;
434b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
435b7453713SJeremy L Thompson   CeedQFunction       qf;
436b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
437b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
438b7453713SJeremy L Thompson 
4392b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
440b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
441e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
442e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
443b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
444*004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
445b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
446b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
447*004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
448*004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
4490d0321e0SJeremy L Thompson 
4500d0321e0SJeremy L Thompson   // Setup
4512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4520d0321e0SJeremy L Thompson 
4530d0321e0SJeremy L Thompson   // Input Evecs and Restriction
454b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
4550d0321e0SJeremy L Thompson 
4560d0321e0SJeremy L Thompson   // Count number of active input fields
457b7453713SJeremy L Thompson   if (!num_active_in) {
458b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
459b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
460b7453713SJeremy L Thompson       CeedVector  vec;
461b7453713SJeremy L Thompson 
4620d0321e0SJeremy L Thompson       // Get input vector
463b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4640d0321e0SJeremy L Thompson       // Check if active input
4650d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
466b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
467b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
468b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
469*004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
4700d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
471*004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
472*004e4986SSebastian Grimberg 
473*004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
474b7453713SJeremy L Thompson           CeedCallBackend(
475*004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
4760d0321e0SJeremy L Thompson         }
477b7453713SJeremy L Thompson         num_active_in += size;
478b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
4790d0321e0SJeremy L Thompson       }
4800d0321e0SJeremy L Thompson     }
481b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
482*004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
4830d0321e0SJeremy L Thompson   }
4840d0321e0SJeremy L Thompson 
4850d0321e0SJeremy L Thompson   // Count number of active output fields
486b7453713SJeremy L Thompson   if (!num_active_out) {
487b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
488b7453713SJeremy L Thompson       CeedVector vec;
489b7453713SJeremy L Thompson 
4900d0321e0SJeremy L Thompson       // Get output vector
491b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4920d0321e0SJeremy L Thompson       // Check if active output
4930d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
494b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
495b7453713SJeremy L Thompson         num_active_out += size;
4960d0321e0SJeremy L Thompson       }
4970d0321e0SJeremy L Thompson     }
498b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
4990d0321e0SJeremy L Thompson   }
5000d0321e0SJeremy L Thompson 
5010d0321e0SJeremy L Thompson   // Check sizes
502b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5030d0321e0SJeremy L Thompson 
5040d0321e0SJeremy L Thompson   // Build objects if needed
5050d0321e0SJeremy L Thompson   if (build_objects) {
506b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
507b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
508b7453713SJeremy L Thompson 
509*004e4986SSebastian Grimberg     // Create output restriction
510b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
511b7453713SJeremy L Thompson                                                      num_active_in * num_active_out * num_elem * Q, strides, rstr));
5120d0321e0SJeremy L Thompson     // Create assembled vector
513b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
5140d0321e0SJeremy L Thompson   }
5152b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
516b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
5170d0321e0SJeremy L Thompson 
5180d0321e0SJeremy L Thompson   // Input basis apply
519b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
5200d0321e0SJeremy L Thompson 
5210d0321e0SJeremy L Thompson   // Assemble QFunction
522b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
5230d0321e0SJeremy L Thompson     // Set Inputs
524*004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
525b7453713SJeremy L Thompson     if (num_active_in > 1) {
526*004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
5270d0321e0SJeremy L Thompson     }
5280d0321e0SJeremy L Thompson     // Set Outputs
529b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
530b7453713SJeremy L Thompson       CeedVector vec;
531b7453713SJeremy L Thompson 
5320d0321e0SJeremy L Thompson       // Get output vector
533b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5340d0321e0SJeremy L Thompson       // Check if active output
5350d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
536b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
537b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
538b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
5390d0321e0SJeremy L Thompson       }
5400d0321e0SJeremy L Thompson     }
5410d0321e0SJeremy L Thompson     // Apply QFunction
542b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
5430d0321e0SJeremy L Thompson   }
5440d0321e0SJeremy L Thompson 
545*004e4986SSebastian Grimberg   // Un-set output q_vecs to prevent accidental overwrite of Assembled
546b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
547b7453713SJeremy L Thompson     CeedVector vec;
548b7453713SJeremy L Thompson 
5490d0321e0SJeremy L Thompson     // Get output vector
550b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5510d0321e0SJeremy L Thompson     // Check if active output
5520d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
553b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
5540d0321e0SJeremy L Thompson     }
5550d0321e0SJeremy L Thompson   }
5560d0321e0SJeremy L Thompson 
5570d0321e0SJeremy L Thompson   // Restore input arrays
558b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
5590d0321e0SJeremy L Thompson 
5600d0321e0SJeremy L Thompson   // Restore output
561b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
5620d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5630d0321e0SJeremy L Thompson }
5640d0321e0SJeremy L Thompson 
5650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5660d0321e0SJeremy L Thompson // Assemble Linear QFunction
5670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5682b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
5692b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
5700d0321e0SJeremy L Thompson }
5710d0321e0SJeremy L Thompson 
5720d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
573b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
5740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5752b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
5762b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
5770d0321e0SJeremy L Thompson }
5780d0321e0SJeremy L Thompson 
5790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
580*004e4986SSebastian Grimberg // Assemble Diagonal Setup
5810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
582506b1a0cSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
5830d0321e0SJeremy L Thompson   Ceed                ceed;
584b7453713SJeremy L Thompson   char               *diagonal_kernel_path, *diagonal_kernel_source;
585*004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
586*004e4986SSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
587*004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
588b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
589b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
5900d0321e0SJeremy L Thompson   CeedQFunction       qf;
591b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
592b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
593b7453713SJeremy L Thompson 
594b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
5952b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
596b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
5970d0321e0SJeremy L Thompson 
5980d0321e0SJeremy L Thompson   // Determine active input basis
599b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
600b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
601b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
6020d0321e0SJeremy L Thompson     CeedVector vec;
603b7453713SJeremy L Thompson 
604b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
6050d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
606*004e4986SSebastian Grimberg       CeedBasis    basis;
607*004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
608b7453713SJeremy L Thompson 
609*004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
610*004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
611*004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
612*004e4986SSebastian Grimberg       basis_in = basis;
613*004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
614*004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
615*004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
616*004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
617*004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
618*004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
619*004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
6200d0321e0SJeremy L Thompson       }
6210d0321e0SJeremy L Thompson     }
6220d0321e0SJeremy L Thompson   }
6230d0321e0SJeremy L Thompson 
6240d0321e0SJeremy L Thompson   // Determine active output basis
625b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
626b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
627b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
6280d0321e0SJeremy L Thompson     CeedVector vec;
629b7453713SJeremy L Thompson 
630b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
6310d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
632*004e4986SSebastian Grimberg       CeedBasis    basis;
633*004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
634b7453713SJeremy L Thompson 
635*004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
636*004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
637*004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
638*004e4986SSebastian Grimberg       basis_out = basis;
639*004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
640*004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
641*004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
642*004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
643*004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
644*004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
645*004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
6460d0321e0SJeremy L Thompson       }
6470d0321e0SJeremy L Thompson     }
6480d0321e0SJeremy L Thompson   }
6490d0321e0SJeremy L Thompson 
6500d0321e0SJeremy L Thompson   // Operator data struct
6512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
6522b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
6530d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
654b7453713SJeremy L Thompson 
6550d0321e0SJeremy L Thompson   // Assemble kernel
656*004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
657*004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
658*004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
659*004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
6602b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
66123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
6622b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
66323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
664*004e4986SSebastian Grimberg   CeedCallHip(ceed,
665*004e4986SSebastian Grimberg               CeedCompile_Hip(ceed, diagonal_kernel_source, &diag->module, 6, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
666*004e4986SSebastian Grimberg                               num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "CEED_SIZE", use_ceedsize_idx));
667*004e4986SSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, diag->module, "LinearDiagonal", &diag->LinearDiagonal));
668*004e4986SSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, diag->module, "LinearPointBlockDiagonal", &diag->LinearPointBlock));
6692b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&diagonal_kernel_path));
6702b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&diagonal_kernel_source));
6710d0321e0SJeremy L Thompson 
6720d0321e0SJeremy L Thompson   // Basis matrices
673*004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
674*004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
675*004e4986SSebastian Grimberg   bool          has_eval_none    = false;
6760d0321e0SJeremy L Thompson 
6770d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
678*004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
679*004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
680*004e4986SSebastian Grimberg   if (has_eval_none) {
6810d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
682b7453713SJeremy L Thompson 
683*004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
684*004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
685b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
686b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
687*004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
6880d0321e0SJeremy L Thompson   }
6890d0321e0SJeremy L Thompson 
690*004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
691*004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
692*004e4986SSebastian Grimberg     CeedFESpace fespace;
693*004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
6940d0321e0SJeremy L Thompson 
695*004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
696*004e4986SSebastian Grimberg     switch (fespace) {
697*004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
698*004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
699*004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
700*004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
7010d0321e0SJeremy L Thompson 
702*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
703*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
7040d0321e0SJeremy L Thompson 
705*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
706*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
707*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
708*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
709*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
710*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
711*004e4986SSebastian Grimberg         if (in) {
712*004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
713*004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
714*004e4986SSebastian Grimberg         } else {
715*004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
716*004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
717*004e4986SSebastian Grimberg         }
718*004e4986SSebastian Grimberg       } break;
719*004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
720*004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
721*004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
722*004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
723*004e4986SSebastian Grimberg 
724*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
725*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
726*004e4986SSebastian Grimberg 
727*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
728*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
729*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
730*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
731*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
732*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
733*004e4986SSebastian Grimberg         if (in) {
734*004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
735*004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
736*004e4986SSebastian Grimberg         } else {
737*004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
738*004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
739*004e4986SSebastian Grimberg         }
740*004e4986SSebastian Grimberg       } break;
741*004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
742*004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
743*004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
744*004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
745*004e4986SSebastian Grimberg 
746*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
747*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
748*004e4986SSebastian Grimberg 
749*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
750*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
751*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
752*004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
753*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
754*004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
755*004e4986SSebastian Grimberg         if (in) {
756*004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
757*004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
758*004e4986SSebastian Grimberg         } else {
759*004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
760*004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
761*004e4986SSebastian Grimberg         }
762*004e4986SSebastian Grimberg       } break;
763*004e4986SSebastian Grimberg     }
764*004e4986SSebastian Grimberg   }
765*004e4986SSebastian Grimberg 
766*004e4986SSebastian Grimberg   // Arrays of eval_modes
767*004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
768*004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
769*004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
770*004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
771*004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
772*004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
7730d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7740d0321e0SJeremy L Thompson }
7750d0321e0SJeremy L Thompson 
7760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
777*004e4986SSebastian Grimberg // Assemble Diagonal Core
7780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
779b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
7800d0321e0SJeremy L Thompson   Ceed                ceed;
781*004e4986SSebastian Grimberg   CeedSize            assembled_length, assembled_qf_length;
782*004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem, num_nodes;
783b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
784b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
785*004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
786*004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
7870d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
788b7453713SJeremy L Thompson 
789b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
7902b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
7910d0321e0SJeremy L Thompson 
7920d0321e0SJeremy L Thompson   // Assemble QFunction
793*004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
794*004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
795*004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
7960d0321e0SJeremy L Thompson 
7979330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
798b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
799b7453713SJeremy L Thompson   if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
8009330daecSnbeams 
8010d0321e0SJeremy L Thompson   // Setup
802506b1a0cSSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op, use_ceedsize_idx));
8030d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
804506b1a0cSSebastian Grimberg 
8050d0321e0SJeremy L Thompson   assert(diag != NULL);
8060d0321e0SJeremy L Thompson 
807*004e4986SSebastian Grimberg   // Restriction and diagonal vector
808*004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
809*004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
810*004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
811*004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
812*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
813*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
814*004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
815*004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
816*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
8170d0321e0SJeremy L Thompson   }
818*004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
819*004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
820b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
8210d0321e0SJeremy L Thompson 
82291db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
823*004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
824*004e4986SSebastian Grimberg   if (num_nodes > 0) {
8250d0321e0SJeremy L Thompson     // Assemble element operator diagonals
826b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
827b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
8280d0321e0SJeremy L Thompson 
8290d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
830*004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
831*004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
832*004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
833*004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
834*004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
835b7453713SJeremy L Thompson 
836b7453713SJeremy L Thompson     if (is_point_block) {
837*004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
8380d0321e0SJeremy L Thompson     } else {
839*004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
8400d0321e0SJeremy L Thompson     }
8410d0321e0SJeremy L Thompson 
8420d0321e0SJeremy L Thompson     // Restore arrays
843b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
844b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
84591db28b6SZach Atkins   }
8460d0321e0SJeremy L Thompson 
8470d0321e0SJeremy L Thompson   // Assemble local operator diagonal
848b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
8490d0321e0SJeremy L Thompson 
8500d0321e0SJeremy L Thompson   // Cleanup
851b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
8520d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8530d0321e0SJeremy L Thompson }
8540d0321e0SJeremy L Thompson 
8550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8560d0321e0SJeremy L Thompson // Assemble Linear Diagonal
8570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8582b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
8592b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
8606aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8610d0321e0SJeremy L Thompson }
8620d0321e0SJeremy L Thompson 
8630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8640d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
8650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8662b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
8672b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
8686aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8690d0321e0SJeremy L Thompson }
8700d0321e0SJeremy L Thompson 
871a835093fSnbeams //------------------------------------------------------------------------------
872*004e4986SSebastian Grimberg // Single Operator Assembly Setup
873a835093fSnbeams //------------------------------------------------------------------------------
8749330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
875a835093fSnbeams   Ceed                ceed;
876*004e4986SSebastian Grimberg   char               *assembly_kernel_path, *assembly_kernel_source;
877*004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
878*004e4986SSebastian Grimberg   CeedInt             elem_size_in, num_qpts_in, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
879*004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
880b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
881b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
882b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
883b7453713SJeremy L Thompson   CeedQFunction       qf;
884b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
885a835093fSnbeams   CeedOperator_Hip   *impl;
886b7453713SJeremy L Thompson 
887b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8882b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
889a835093fSnbeams 
890a835093fSnbeams   // Get intput and output fields
8912b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
892a835093fSnbeams 
893a835093fSnbeams   // Determine active input basis eval mode
8942b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
8952b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
896a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
897a835093fSnbeams     CeedVector vec;
898b7453713SJeremy L Thompson 
8992b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
900a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
901*004e4986SSebastian Grimberg       CeedBasis    basis;
902b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
903b7453713SJeremy L Thompson 
904*004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
905*004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
906*004e4986SSebastian Grimberg       basis_in = basis;
9072b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
908*004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
909*004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
910*004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
9112b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
912*004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
913*004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
914*004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
915*004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
916*004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
917*004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
918a835093fSnbeams         }
919*004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
920a835093fSnbeams       }
921a835093fSnbeams     }
922a835093fSnbeams   }
923a835093fSnbeams 
924a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
9252b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
926a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
927a835093fSnbeams     CeedVector vec;
928b7453713SJeremy L Thompson 
9292b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
930a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
931*004e4986SSebastian Grimberg       CeedBasis    basis;
932b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
933b7453713SJeremy L Thompson 
934*004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
935*004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
936*004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
937*004e4986SSebastian Grimberg       basis_out = basis;
9382b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
939*004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
940*004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
941*004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
942*004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
943*004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
9442b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
945*004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
946*004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
947*004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
948*004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
949*004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
950*004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
951*004e4986SSebastian Grimberg         }
952*004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
953a835093fSnbeams       }
954a835093fSnbeams     }
955a835093fSnbeams   }
956*004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
957a835093fSnbeams 
9582b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
959a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
960*004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
961*004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
962*004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
963*004e4986SSebastian Grimberg 
964*004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
965*004e4986SSebastian Grimberg 
966*004e4986SSebastian Grimberg   if (fallback) {
967*004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
968*004e4986SSebastian Grimberg     asmb->block_size_y = 1;
969*004e4986SSebastian Grimberg   }
970a835093fSnbeams 
971a835093fSnbeams   // Compile kernels
972*004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
973*004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
9742b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
97523d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
9762b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
97723d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
978*004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
979*004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
980*004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
981*004e4986SSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "CEED_SIZE",
9829330daecSnbeams                                   use_ceedsize_idx));
983*004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
9842b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
9852b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
986a835093fSnbeams 
987*004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
988*004e4986SSebastian Grimberg   {
989*004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
990*004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
991*004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
992*004e4986SSebastian Grimberg     bool          has_eval_none      = false;
993*004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
994a835093fSnbeams 
995*004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
996*004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
997*004e4986SSebastian Grimberg     }
998*004e4986SSebastian Grimberg     if (has_eval_none) {
999*004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1000*004e4986SSebastian 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;
1001*004e4986SSebastian Grimberg     }
1002b7453713SJeremy L Thompson 
1003b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1004*004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1005*004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1006*004e4986SSebastian Grimberg 
1007*004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1008*004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1009*004e4986SSebastian Grimberg       if (q_comp > 1) {
1010*004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1011*004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1012*004e4986SSebastian Grimberg       }
1013*004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1014*004e4986SSebastian Grimberg 
1015*004e4986SSebastian 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),
1016*004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1017*004e4986SSebastian Grimberg     }
1018*004e4986SSebastian Grimberg 
1019*004e4986SSebastian Grimberg     if (identity) {
1020*004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1021a835093fSnbeams     }
1022a835093fSnbeams   }
1023a835093fSnbeams 
1024*004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1025*004e4986SSebastian Grimberg   {
1026*004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1027*004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1028*004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1029*004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1030*004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1031b7453713SJeremy L Thompson 
1032*004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1033*004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1034*004e4986SSebastian Grimberg     }
1035*004e4986SSebastian Grimberg     if (has_eval_none) {
1036*004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1037*004e4986SSebastian 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;
1038a835093fSnbeams     }
1039a835093fSnbeams 
1040b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1041*004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1042*004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1043*004e4986SSebastian Grimberg 
1044*004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1045*004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1046*004e4986SSebastian Grimberg       if (q_comp > 1) {
1047*004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1048*004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1049*004e4986SSebastian Grimberg       }
1050*004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1051*004e4986SSebastian Grimberg 
1052*004e4986SSebastian 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),
1053*004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1054*004e4986SSebastian Grimberg     }
1055*004e4986SSebastian Grimberg 
1056*004e4986SSebastian Grimberg     if (identity) {
1057*004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1058a835093fSnbeams     }
1059a835093fSnbeams   }
1060a835093fSnbeams   return CEED_ERROR_SUCCESS;
1061a835093fSnbeams }
1062a835093fSnbeams 
1063a835093fSnbeams //------------------------------------------------------------------------------
1064cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1065cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1066cefa2673SJeremy L Thompson //
1067ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1068cefa2673SJeremy L Thompson // modes).
1069cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1070a835093fSnbeams //------------------------------------------------------------------------------
10712b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1072a835093fSnbeams   Ceed                ceed;
1073b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1074*004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1075b7453713SJeremy L Thompson   CeedScalar         *values_array;
1076*004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1077b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1078*004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1079*004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1080*004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1081*004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1082a835093fSnbeams   CeedOperator_Hip   *impl;
1083b7453713SJeremy L Thompson 
1084b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
10852b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1086a835093fSnbeams 
1087a835093fSnbeams   // Assemble QFunction
1088*004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1089*004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1090*004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1091a835093fSnbeams 
10929330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
10939330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
10949330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1095*004e4986SSebastian Grimberg 
10969330daecSnbeams   // Setup
1097*004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1098*004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1099*004e4986SSebastian Grimberg 
1100*004e4986SSebastian Grimberg   assert(asmb != NULL);
1101*004e4986SSebastian Grimberg 
1102*004e4986SSebastian Grimberg   // Assemble element operator
1103*004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1104*004e4986SSebastian Grimberg   values_array += offset;
1105*004e4986SSebastian Grimberg 
1106*004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1107*004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1108*004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1109*004e4986SSebastian Grimberg 
1110*004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1111*004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1112*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1113*004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1114*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1115*004e4986SSebastian Grimberg   }
1116*004e4986SSebastian Grimberg 
1117*004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1118*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1119*004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1120*004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1121*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1122*004e4986SSebastian Grimberg 
1123*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1124*004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1125*004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1126*004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1127*004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1128*004e4986SSebastian Grimberg     }
1129*004e4986SSebastian Grimberg   } else {
1130*004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1131*004e4986SSebastian Grimberg     orients_out      = orients_in;
1132*004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
11339330daecSnbeams   }
11349330daecSnbeams 
1135a835093fSnbeams   // Compute B^T D B
1136*004e4986SSebastian Grimberg   CeedInt shared_mem =
1137*004e4986SSebastian 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)) *
1138*004e4986SSebastian Grimberg       sizeof(CeedScalar);
1139*004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1140*004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1141*004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1142b7453713SJeremy L Thompson 
11432b730f8bSJeremy L Thompson   CeedCallBackend(
1144*004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1145a835093fSnbeams 
1146a835093fSnbeams   // Restore arrays
11472b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1148*004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1149a835093fSnbeams 
1150a835093fSnbeams   // Cleanup
11512b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1152*004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1153*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1154*004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1155*004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1156*004e4986SSebastian Grimberg   }
1157*004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1158*004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1159*004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1160*004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1161*004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1162*004e4986SSebastian Grimberg     }
1163*004e4986SSebastian Grimberg   }
1164a835093fSnbeams   return CEED_ERROR_SUCCESS;
1165a835093fSnbeams }
1166a835093fSnbeams 
1167a835093fSnbeams //------------------------------------------------------------------------------
11680d0321e0SJeremy L Thompson // Create operator
11690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11700d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
11710d0321e0SJeremy L Thompson   Ceed              ceed;
11720d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
11730d0321e0SJeremy L Thompson 
1174b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11752b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
11762b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
11772b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
11782b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
11792b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
11802b730f8bSJeremy L Thompson   CeedCallBackend(
11812b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
11822b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
11832b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
11842b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
11850d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11860d0321e0SJeremy L Thompson }
11870d0321e0SJeremy L Thompson 
11880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1189