15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 30d0321e0SJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 50d0321e0SJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 70d0321e0SJeremy L Thompson 849aac155SJeremy L Thompson #include <ceed.h> 90d0321e0SJeremy L Thompson #include <ceed/backend.h> 1007b31e0eSJeremy L Thompson #include <ceed/jit-tools.h> 11c85e8640SSebastian Grimberg #include <assert.h> 120d0321e0SJeremy L Thompson #include <stdbool.h> 130d0321e0SJeremy L Thompson #include <string.h> 14c85e8640SSebastian Grimberg #include <hip/hip_runtime.h> 152b730f8bSJeremy L Thompson 1649aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h" 170d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h" 182b730f8bSJeremy L Thompson #include "ceed-hip-ref.h" 190d0321e0SJeremy L Thompson 200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 210d0321e0SJeremy L Thompson // Destroy operator 220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 230d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Hip(CeedOperator op) { 240d0321e0SJeremy L Thompson CeedOperator_Hip *impl; 25b7453713SJeremy L Thompson 262b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 270d0321e0SJeremy L Thompson 280d0321e0SJeremy L Thompson // Apply data 29b7453713SJeremy L Thompson for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) { 30b7453713SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i])); 310d0321e0SJeremy L Thompson } 32b7453713SJeremy L Thompson CeedCallBackend(CeedFree(&impl->e_vecs)); 33c1222711SJeremy L Thompson CeedCallBackend(CeedFree(&impl->input_states)); 340d0321e0SJeremy L Thompson 35b7453713SJeremy L Thompson for (CeedInt i = 0; i < impl->num_inputs; i++) { 36b7453713SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i])); 370d0321e0SJeremy L Thompson } 38b7453713SJeremy L Thompson CeedCallBackend(CeedFree(&impl->q_vecs_in)); 390d0321e0SJeremy L Thompson 40b7453713SJeremy L Thompson for (CeedInt i = 0; i < impl->num_outputs; i++) { 41b7453713SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i])); 420d0321e0SJeremy L Thompson } 43b7453713SJeremy L Thompson CeedCallBackend(CeedFree(&impl->q_vecs_out)); 44*67d9480aSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem)); 450d0321e0SJeremy L Thompson 46b2165e7aSSebastian Grimberg // QFunction assembly data 47b7453713SJeremy L Thompson for (CeedInt i = 0; i < impl->num_active_in; i++) { 48b7453713SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i])); 490d0321e0SJeremy L Thompson } 50b7453713SJeremy L Thompson CeedCallBackend(CeedFree(&impl->qf_active_in)); 510d0321e0SJeremy L Thompson 520d0321e0SJeremy L Thompson // Diag data 530d0321e0SJeremy L Thompson if (impl->diag) { 540d0321e0SJeremy L Thompson Ceed ceed; 55b7453713SJeremy L Thompson 562b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 57cbfe683aSSebastian Grimberg if (impl->diag->module) { 582b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleUnload(impl->diag->module)); 59cbfe683aSSebastian Grimberg } 60cbfe683aSSebastian Grimberg if (impl->diag->module_point_block) { 61cbfe683aSSebastian Grimberg CeedCallHip(ceed, hipModuleUnload(impl->diag->module_point_block)); 62cbfe683aSSebastian Grimberg } 63004e4986SSebastian Grimberg CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_in)); 64004e4986SSebastian Grimberg CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_out)); 652b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->diag->d_identity)); 66b7453713SJeremy L Thompson CeedCallHip(ceed, hipFree(impl->diag->d_interp_in)); 67b7453713SJeremy L Thompson CeedCallHip(ceed, hipFree(impl->diag->d_interp_out)); 68b7453713SJeremy L Thompson CeedCallHip(ceed, hipFree(impl->diag->d_grad_in)); 69b7453713SJeremy L Thompson CeedCallHip(ceed, hipFree(impl->diag->d_grad_out)); 70004e4986SSebastian Grimberg CeedCallHip(ceed, hipFree(impl->diag->d_div_in)); 71004e4986SSebastian Grimberg CeedCallHip(ceed, hipFree(impl->diag->d_div_out)); 72004e4986SSebastian Grimberg CeedCallHip(ceed, hipFree(impl->diag->d_curl_in)); 73004e4986SSebastian Grimberg CeedCallHip(ceed, hipFree(impl->diag->d_curl_out)); 74004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr)); 75b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr)); 76b7453713SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag)); 77b7453713SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag)); 780d0321e0SJeremy L Thompson } 792b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->diag)); 800d0321e0SJeremy L Thompson 81a835093fSnbeams if (impl->asmb) { 82a835093fSnbeams Ceed ceed; 83b7453713SJeremy L Thompson 842b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 852b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleUnload(impl->asmb->module)); 862b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->asmb->d_B_in)); 872b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->asmb->d_B_out)); 88a835093fSnbeams } 892b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->asmb)); 90a835093fSnbeams 912b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 920d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 930d0321e0SJeremy L Thompson } 940d0321e0SJeremy L Thompson 950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 960d0321e0SJeremy L Thompson // Setup infields or outfields 970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 98*67d9480aSJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, CeedVector *e_vecs, CeedVector *q_vecs, 99*67d9480aSJeremy L Thompson CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) { 1000d0321e0SJeremy L Thompson Ceed ceed; 101b7453713SJeremy L Thompson CeedQFunctionField *qf_fields; 102b7453713SJeremy L Thompson CeedOperatorField *op_fields; 1030d0321e0SJeremy L Thompson 104b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 105b7453713SJeremy L Thompson if (is_input) { 106b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 107b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 1080d0321e0SJeremy L Thompson } else { 109b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 110b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1110d0321e0SJeremy L Thompson } 1120d0321e0SJeremy L Thompson 1130d0321e0SJeremy L Thompson // Loop over fields 114b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_fields; i++) { 115004e4986SSebastian Grimberg bool is_strided = false, skip_restriction = false; 116b7453713SJeremy L Thompson CeedSize q_size; 117004e4986SSebastian Grimberg CeedInt size; 118004e4986SSebastian Grimberg CeedEvalMode eval_mode; 119b7453713SJeremy L Thompson CeedBasis basis; 1200d0321e0SJeremy L Thompson 121004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 122004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 123004e4986SSebastian Grimberg CeedElemRestriction elem_rstr; 1240d0321e0SJeremy L Thompson 1250d0321e0SJeremy L Thompson // Check whether this field can skip the element restriction: 126004e4986SSebastian Grimberg // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND. 127004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr)); 1280d0321e0SJeremy L Thompson 1290d0321e0SJeremy L Thompson // First, check whether the field is input or output: 130b7453713SJeremy L Thompson if (is_input) { 131004e4986SSebastian Grimberg CeedVector vec; 132004e4986SSebastian Grimberg 133004e4986SSebastian Grimberg // Check for passive input 134b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 135b7453713SJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE) { 136004e4986SSebastian Grimberg // Check eval_mode 137004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 1380d0321e0SJeremy L Thompson // Check for strided restriction 139b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided)); 140b7453713SJeremy L Thompson if (is_strided) { 1410d0321e0SJeremy L Thompson // Check if vector is already in preferred backend ordering 142b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction)); 1430d0321e0SJeremy L Thompson } 1440d0321e0SJeremy L Thompson } 1450d0321e0SJeremy L Thompson } 1460d0321e0SJeremy L Thompson } 147b7453713SJeremy L Thompson if (skip_restriction) { 148ea61e9acSJeremy L Thompson // We do not need an E-Vector, but will use the input field vector's data directly in the operator application. 149b7453713SJeremy L Thompson e_vecs[i + start_e] = NULL; 1500d0321e0SJeremy L Thompson } else { 151b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e])); 1520d0321e0SJeremy L Thompson } 1530d0321e0SJeremy L Thompson } 1540d0321e0SJeremy L Thompson 155004e4986SSebastian Grimberg switch (eval_mode) { 1560d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 157b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 158b7453713SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 159b7453713SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 1600d0321e0SJeremy L Thompson break; 1610d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 1620d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 163004e4986SSebastian Grimberg case CEED_EVAL_DIV: 164004e4986SSebastian Grimberg case CEED_EVAL_CURL: 165b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 166b7453713SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 167b7453713SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 1680d0321e0SJeremy L Thompson break; 1690d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: // Only on input fields 170b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 171b7453713SJeremy L Thompson q_size = (CeedSize)num_elem * Q; 172b7453713SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 173*67d9480aSJeremy L Thompson if (is_at_points) { 174*67d9480aSJeremy L Thompson CeedInt num_points[num_elem]; 175*67d9480aSJeremy L Thompson 176*67d9480aSJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q; 177*67d9480aSJeremy L Thompson CeedCallBackend( 178*67d9480aSJeremy L Thompson CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i])); 179*67d9480aSJeremy L Thompson } else { 180b7453713SJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i])); 181*67d9480aSJeremy L Thompson } 1820d0321e0SJeremy L Thompson break; 1830d0321e0SJeremy L Thompson } 1840d0321e0SJeremy L Thompson } 1850d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1860d0321e0SJeremy L Thompson } 1870d0321e0SJeremy L Thompson 1880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 189ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. 1900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1910d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) { 1920d0321e0SJeremy L Thompson Ceed ceed; 193b7453713SJeremy L Thompson bool is_setup_done; 194b7453713SJeremy L Thompson CeedInt Q, num_elem, num_input_fields, num_output_fields; 195b7453713SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 1960d0321e0SJeremy L Thompson CeedQFunction qf; 197b7453713SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 198b7453713SJeremy L Thompson CeedOperator_Hip *impl; 199b7453713SJeremy L Thompson 200b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 201b7453713SJeremy L Thompson if (is_setup_done) return CEED_ERROR_SUCCESS; 202b7453713SJeremy L Thompson 203b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 204b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 2052b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 2062b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 207b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 208b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 209b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 2100d0321e0SJeremy L Thompson 2110d0321e0SJeremy L Thompson // Allocate 212b7453713SJeremy L Thompson CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs)); 213c1222711SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states)); 214b7453713SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in)); 215b7453713SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out)); 216b7453713SJeremy L Thompson impl->num_inputs = num_input_fields; 217b7453713SJeremy L Thompson impl->num_outputs = num_output_fields; 2180d0321e0SJeremy L Thompson 219b7453713SJeremy L Thompson // Set up infield and outfield e_vecs and q_vecs 2200d0321e0SJeremy L Thompson // Infields 221*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, false, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem)); 2220d0321e0SJeremy L Thompson // Outfields 223*67d9480aSJeremy L Thompson CeedCallBackend( 224*67d9480aSJeremy L Thompson CeedOperatorSetupFields_Hip(qf, op, false, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem)); 2250d0321e0SJeremy L Thompson 2262b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 2270d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2280d0321e0SJeremy L Thompson } 2290d0321e0SJeremy L Thompson 2300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2310d0321e0SJeremy L Thompson // Setup Operator Inputs 2320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 233b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 234b7453713SJeremy L Thompson CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], 235b7453713SJeremy L Thompson CeedOperator_Hip *impl, CeedRequest *request) { 236b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 237004e4986SSebastian Grimberg CeedEvalMode eval_mode; 2380d0321e0SJeremy L Thompson CeedVector vec; 239b7453713SJeremy L Thompson CeedElemRestriction elem_rstr; 2400d0321e0SJeremy L Thompson 2410d0321e0SJeremy L Thompson // Get input vector 242b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 2430d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 244b7453713SJeremy L Thompson if (skip_active) continue; 245b7453713SJeremy L Thompson else vec = in_vec; 2460d0321e0SJeremy L Thompson } 2470d0321e0SJeremy L Thompson 248004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 249004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 2500d0321e0SJeremy L Thompson } else { 2510d0321e0SJeremy L Thompson // Get input vector 252b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 2530d0321e0SJeremy L Thompson // Get input element restriction 254b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 255b7453713SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = in_vec; 2560d0321e0SJeremy L Thompson // Restrict, if necessary 257b7453713SJeremy L Thompson if (!impl->e_vecs[i]) { 2580d0321e0SJeremy L Thompson // No restriction for this field; read data directly from vec. 259b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i])); 2600d0321e0SJeremy L Thompson } else { 261c1222711SJeremy L Thompson uint64_t state; 262c1222711SJeremy L Thompson 263c1222711SJeremy L Thompson CeedCallBackend(CeedVectorGetState(vec, &state)); 264c1222711SJeremy L Thompson if (state != impl->input_states[i]) { 265b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request)); 266c1222711SJeremy L Thompson impl->input_states[i] = state; 267c1222711SJeremy L Thompson } 2680d0321e0SJeremy L Thompson // Get evec 269b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i])); 2700d0321e0SJeremy L Thompson } 2710d0321e0SJeremy L Thompson } 2720d0321e0SJeremy L Thompson } 2730d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2740d0321e0SJeremy L Thompson } 2750d0321e0SJeremy L Thompson 2760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2770d0321e0SJeremy L Thompson // Input Basis Action 2780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 279b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 280b7453713SJeremy L Thompson CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], 2812b730f8bSJeremy L Thompson CeedOperator_Hip *impl) { 282b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 283b7453713SJeremy L Thompson CeedInt elem_size, size; 284004e4986SSebastian Grimberg CeedEvalMode eval_mode; 285b7453713SJeremy L Thompson CeedElemRestriction elem_rstr; 2860d0321e0SJeremy L Thompson CeedBasis basis; 2870d0321e0SJeremy L Thompson 2880d0321e0SJeremy L Thompson // Skip active input 289b7453713SJeremy L Thompson if (skip_active) { 2900d0321e0SJeremy L Thompson CeedVector vec; 291b7453713SJeremy L Thompson 292b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 2932b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 2940d0321e0SJeremy L Thompson } 295004e4986SSebastian Grimberg // Get elem_size, eval_mode, size 296b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 297b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 298004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 299b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 3000d0321e0SJeremy L Thompson // Basis action 301004e4986SSebastian Grimberg switch (eval_mode) { 3020d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 303b7453713SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i])); 3040d0321e0SJeremy L Thompson break; 3050d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 3060d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 307004e4986SSebastian Grimberg case CEED_EVAL_DIV: 308004e4986SSebastian Grimberg case CEED_EVAL_CURL: 309b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 310004e4986SSebastian Grimberg CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i])); 3110d0321e0SJeremy L Thompson break; 3120d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: 3130d0321e0SJeremy L Thompson break; // No action 3140d0321e0SJeremy L Thompson } 3150d0321e0SJeremy L Thompson } 3160d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3170d0321e0SJeremy L Thompson } 3180d0321e0SJeremy L Thompson 3190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3200d0321e0SJeremy L Thompson // Restore Input Vectors 3210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 322b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 323b7453713SJeremy L Thompson const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) { 324b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 325004e4986SSebastian Grimberg CeedEvalMode eval_mode; 3260d0321e0SJeremy L Thompson CeedVector vec; 327004e4986SSebastian Grimberg 3280d0321e0SJeremy L Thompson // Skip active input 329b7453713SJeremy L Thompson if (skip_active) { 330b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 3312b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 3320d0321e0SJeremy L Thompson } 333004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 334004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 3350d0321e0SJeremy L Thompson } else { 336b7453713SJeremy L Thompson if (!impl->e_vecs[i]) { // This was a skip_restriction case 337b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 338b7453713SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i])); 3390d0321e0SJeremy L Thompson } else { 340b7453713SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i])); 3410d0321e0SJeremy L Thompson } 3420d0321e0SJeremy L Thompson } 3430d0321e0SJeremy L Thompson } 3440d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3450d0321e0SJeremy L Thompson } 3460d0321e0SJeremy L Thompson 3470d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3480d0321e0SJeremy L Thompson // Apply and add to output 3490d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 350b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) { 351b7453713SJeremy L Thompson CeedInt Q, num_elem, elem_size, num_input_fields, num_output_fields, size; 352b7453713SJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX] = {NULL}; 353b7453713SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 3540d0321e0SJeremy L Thompson CeedQFunction qf; 355b7453713SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 356b7453713SJeremy L Thompson CeedOperator_Hip *impl; 357b7453713SJeremy L Thompson 358b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 3592b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 3602b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 361b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 362b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 363b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 3640d0321e0SJeremy L Thompson 3650d0321e0SJeremy L Thompson // Setup 3662b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetup_Hip(op)); 3670d0321e0SJeremy L Thompson 3680d0321e0SJeremy L Thompson // Input Evecs and Restriction 369b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request)); 3700d0321e0SJeremy L Thompson 3710d0321e0SJeremy L Thompson // Input basis apply if needed 372b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl)); 3730d0321e0SJeremy L Thompson 3740d0321e0SJeremy L Thompson // Output pointers, as necessary 375b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 376004e4986SSebastian Grimberg CeedEvalMode eval_mode; 377b7453713SJeremy L Thompson 378004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 379004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 3800d0321e0SJeremy L Thompson // Set the output Q-Vector to use the E-Vector data directly. 381b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields])); 382b7453713SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields])); 3830d0321e0SJeremy L Thompson } 3840d0321e0SJeremy L Thompson } 3850d0321e0SJeremy L Thompson 3860d0321e0SJeremy L Thompson // Q function 387b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out)); 3880d0321e0SJeremy L Thompson 3890d0321e0SJeremy L Thompson // Output basis apply if needed 390b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 391004e4986SSebastian Grimberg CeedEvalMode eval_mode; 392b7453713SJeremy L Thompson CeedElemRestriction elem_rstr; 393b7453713SJeremy L Thompson CeedBasis basis; 394b7453713SJeremy L Thompson 395004e4986SSebastian Grimberg // Get elem_size, eval_mode, size 396b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 397b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 398004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 399b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 4000d0321e0SJeremy L Thompson // Basis action 401004e4986SSebastian Grimberg switch (eval_mode) { 4020d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 403004e4986SSebastian Grimberg break; // No action 4040d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 4050d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 406004e4986SSebastian Grimberg case CEED_EVAL_DIV: 407004e4986SSebastian Grimberg case CEED_EVAL_CURL: 408b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 409004e4986SSebastian Grimberg CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs])); 4100d0321e0SJeremy L Thompson break; 4110d0321e0SJeremy L Thompson // LCOV_EXCL_START 4120d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: { 4136e536b99SJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 4140d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 4150d0321e0SJeremy L Thompson } 4160d0321e0SJeremy L Thompson } 417004e4986SSebastian Grimberg } 4180d0321e0SJeremy L Thompson 4190d0321e0SJeremy L Thompson // Output restriction 420b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 421004e4986SSebastian Grimberg CeedEvalMode eval_mode; 422b7453713SJeremy L Thompson CeedVector vec; 423b7453713SJeremy L Thompson CeedElemRestriction elem_rstr; 424b7453713SJeremy L Thompson 4250d0321e0SJeremy L Thompson // Restore evec 426004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 427004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 428b7453713SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields])); 4290d0321e0SJeremy L Thompson } 4300d0321e0SJeremy L Thompson // Get output vector 431b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 4320d0321e0SJeremy L Thompson // Restrict 433b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 4340d0321e0SJeremy L Thompson // Active 435b7453713SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = out_vec; 4360d0321e0SJeremy L Thompson 437b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request)); 4380d0321e0SJeremy L Thompson } 4390d0321e0SJeremy L Thompson 4400d0321e0SJeremy L Thompson // Restore input arrays 441b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl)); 4420d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4430d0321e0SJeremy L Thompson } 4440d0321e0SJeremy L Thompson 4450d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 446*67d9480aSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. 447*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 448*67d9480aSJeremy L Thompson static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) { 449*67d9480aSJeremy L Thompson Ceed ceed; 450*67d9480aSJeremy L Thompson bool is_setup_done; 451*67d9480aSJeremy L Thompson CeedInt max_num_points = -1, num_elem, num_input_fields, num_output_fields; 452*67d9480aSJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 453*67d9480aSJeremy L Thompson CeedQFunction qf; 454*67d9480aSJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 455*67d9480aSJeremy L Thompson CeedOperator_Hip *impl; 456*67d9480aSJeremy L Thompson 457*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 458*67d9480aSJeremy L Thompson if (is_setup_done) return CEED_ERROR_SUCCESS; 459*67d9480aSJeremy L Thompson 460*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 461*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 462*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 463*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 464*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 465*67d9480aSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 466*67d9480aSJeremy L Thompson { 467*67d9480aSJeremy L Thompson CeedElemRestriction elem_rstr = NULL; 468*67d9480aSJeremy L Thompson 469*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL)); 470*67d9480aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points)); 471*67d9480aSJeremy L Thompson } 472*67d9480aSJeremy L Thompson impl->max_num_points = max_num_points; 473*67d9480aSJeremy L Thompson 474*67d9480aSJeremy L Thompson // Allocate 475*67d9480aSJeremy L Thompson CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs)); 476*67d9480aSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states)); 477*67d9480aSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in)); 478*67d9480aSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out)); 479*67d9480aSJeremy L Thompson impl->num_inputs = num_input_fields; 480*67d9480aSJeremy L Thompson impl->num_outputs = num_output_fields; 481*67d9480aSJeremy L Thompson 482*67d9480aSJeremy L Thompson // Set up infield and outfield e_vecs and q_vecs 483*67d9480aSJeremy L Thompson // Infields 484*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, true, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, max_num_points, num_elem)); 485*67d9480aSJeremy L Thompson // Outfields 486*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, true, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, 487*67d9480aSJeremy L Thompson max_num_points, num_elem)); 488*67d9480aSJeremy L Thompson 489*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 490*67d9480aSJeremy L Thompson return CEED_ERROR_SUCCESS; 491*67d9480aSJeremy L Thompson } 492*67d9480aSJeremy L Thompson 493*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 494*67d9480aSJeremy L Thompson // Input Basis Action AtPoints 495*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 496*67d9480aSJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Hip(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields, 497*67d9480aSJeremy L Thompson CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active, 498*67d9480aSJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) { 499*67d9480aSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 500*67d9480aSJeremy L Thompson CeedInt elem_size, size; 501*67d9480aSJeremy L Thompson CeedEvalMode eval_mode; 502*67d9480aSJeremy L Thompson CeedElemRestriction elem_rstr; 503*67d9480aSJeremy L Thompson CeedBasis basis; 504*67d9480aSJeremy L Thompson 505*67d9480aSJeremy L Thompson // Skip active input 506*67d9480aSJeremy L Thompson if (skip_active) { 507*67d9480aSJeremy L Thompson CeedVector vec; 508*67d9480aSJeremy L Thompson 509*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 510*67d9480aSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 511*67d9480aSJeremy L Thompson } 512*67d9480aSJeremy L Thompson // Get elem_size, eval_mode, size 513*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 514*67d9480aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 515*67d9480aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 516*67d9480aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 517*67d9480aSJeremy L Thompson // Basis action 518*67d9480aSJeremy L Thompson switch (eval_mode) { 519*67d9480aSJeremy L Thompson case CEED_EVAL_NONE: 520*67d9480aSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i])); 521*67d9480aSJeremy L Thompson break; 522*67d9480aSJeremy L Thompson case CEED_EVAL_INTERP: 523*67d9480aSJeremy L Thompson case CEED_EVAL_GRAD: 524*67d9480aSJeremy L Thompson case CEED_EVAL_DIV: 525*67d9480aSJeremy L Thompson case CEED_EVAL_CURL: 526*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 527*67d9480aSJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i], 528*67d9480aSJeremy L Thompson impl->q_vecs_in[i])); 529*67d9480aSJeremy L Thompson break; 530*67d9480aSJeremy L Thompson case CEED_EVAL_WEIGHT: 531*67d9480aSJeremy L Thompson break; // No action 532*67d9480aSJeremy L Thompson } 533*67d9480aSJeremy L Thompson } 534*67d9480aSJeremy L Thompson return CEED_ERROR_SUCCESS; 535*67d9480aSJeremy L Thompson } 536*67d9480aSJeremy L Thompson 537*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 538*67d9480aSJeremy L Thompson // Apply and add to output AtPoints 539*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 540*67d9480aSJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) { 541*67d9480aSJeremy L Thompson CeedInt max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size; 542*67d9480aSJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX] = {NULL}; 543*67d9480aSJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 544*67d9480aSJeremy L Thompson CeedQFunction qf; 545*67d9480aSJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 546*67d9480aSJeremy L Thompson CeedOperator_Hip *impl; 547*67d9480aSJeremy L Thompson 548*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 549*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 550*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 551*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 552*67d9480aSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 553*67d9480aSJeremy L Thompson CeedInt num_points[num_elem]; 554*67d9480aSJeremy L Thompson 555*67d9480aSJeremy L Thompson // Setup 556*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op)); 557*67d9480aSJeremy L Thompson max_num_points = impl->max_num_points; 558*67d9480aSJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points; 559*67d9480aSJeremy L Thompson 560*67d9480aSJeremy L Thompson // Input Evecs and Restriction 561*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request)); 562*67d9480aSJeremy L Thompson 563*67d9480aSJeremy L Thompson // Get point coordinates 564*67d9480aSJeremy L Thompson if (!impl->point_coords_elem) { 565*67d9480aSJeremy L Thompson CeedVector point_coords = NULL; 566*67d9480aSJeremy L Thompson CeedElemRestriction rstr_points = NULL; 567*67d9480aSJeremy L Thompson 568*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords)); 569*67d9480aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem)); 570*67d9480aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request)); 571*67d9480aSJeremy L Thompson } 572*67d9480aSJeremy L Thompson 573*67d9480aSJeremy L Thompson // Input basis apply if needed 574*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl)); 575*67d9480aSJeremy L Thompson 576*67d9480aSJeremy L Thompson // Output pointers, as necessary 577*67d9480aSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 578*67d9480aSJeremy L Thompson CeedEvalMode eval_mode; 579*67d9480aSJeremy L Thompson 580*67d9480aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 581*67d9480aSJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) { 582*67d9480aSJeremy L Thompson // Set the output Q-Vector to use the E-Vector data directly. 583*67d9480aSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields])); 584*67d9480aSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields])); 585*67d9480aSJeremy L Thompson } 586*67d9480aSJeremy L Thompson } 587*67d9480aSJeremy L Thompson 588*67d9480aSJeremy L Thompson // Q function 589*67d9480aSJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out)); 590*67d9480aSJeremy L Thompson 591*67d9480aSJeremy L Thompson // Output basis apply if needed 592*67d9480aSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 593*67d9480aSJeremy L Thompson CeedEvalMode eval_mode; 594*67d9480aSJeremy L Thompson CeedElemRestriction elem_rstr; 595*67d9480aSJeremy L Thompson CeedBasis basis; 596*67d9480aSJeremy L Thompson 597*67d9480aSJeremy L Thompson // Get elem_size, eval_mode, size 598*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 599*67d9480aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 600*67d9480aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 601*67d9480aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 602*67d9480aSJeremy L Thompson // Basis action 603*67d9480aSJeremy L Thompson switch (eval_mode) { 604*67d9480aSJeremy L Thompson case CEED_EVAL_NONE: 605*67d9480aSJeremy L Thompson break; // No action 606*67d9480aSJeremy L Thompson case CEED_EVAL_INTERP: 607*67d9480aSJeremy L Thompson case CEED_EVAL_GRAD: 608*67d9480aSJeremy L Thompson case CEED_EVAL_DIV: 609*67d9480aSJeremy L Thompson case CEED_EVAL_CURL: 610*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 611*67d9480aSJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i], 612*67d9480aSJeremy L Thompson impl->e_vecs[i + impl->num_inputs])); 613*67d9480aSJeremy L Thompson break; 614*67d9480aSJeremy L Thompson // LCOV_EXCL_START 615*67d9480aSJeremy L Thompson case CEED_EVAL_WEIGHT: { 616*67d9480aSJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 617*67d9480aSJeremy L Thompson // LCOV_EXCL_STOP 618*67d9480aSJeremy L Thompson } 619*67d9480aSJeremy L Thompson } 620*67d9480aSJeremy L Thompson } 621*67d9480aSJeremy L Thompson 622*67d9480aSJeremy L Thompson // Output restriction 623*67d9480aSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 624*67d9480aSJeremy L Thompson CeedEvalMode eval_mode; 625*67d9480aSJeremy L Thompson CeedVector vec; 626*67d9480aSJeremy L Thompson CeedElemRestriction elem_rstr; 627*67d9480aSJeremy L Thompson 628*67d9480aSJeremy L Thompson // Restore evec 629*67d9480aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 630*67d9480aSJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) { 631*67d9480aSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields])); 632*67d9480aSJeremy L Thompson } 633*67d9480aSJeremy L Thompson // Get output vector 634*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 635*67d9480aSJeremy L Thompson // Restrict 636*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 637*67d9480aSJeremy L Thompson // Active 638*67d9480aSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = out_vec; 639*67d9480aSJeremy L Thompson 640*67d9480aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request)); 641*67d9480aSJeremy L Thompson } 642*67d9480aSJeremy L Thompson 643*67d9480aSJeremy L Thompson // Restore input arrays 644*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl)); 645*67d9480aSJeremy L Thompson return CEED_ERROR_SUCCESS; 646*67d9480aSJeremy L Thompson } 647*67d9480aSJeremy L Thompson 648*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 649004e4986SSebastian Grimberg // Linear QFunction Assembly Core 6500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6512b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr, 6520d0321e0SJeremy L Thompson CeedRequest *request) { 653b7453713SJeremy L Thompson Ceed ceed, ceed_parent; 654b7453713SJeremy L Thompson CeedInt num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size; 655b7453713SJeremy L Thompson CeedScalar *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL}; 656004e4986SSebastian Grimberg CeedVector *active_inputs; 657b7453713SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 658b7453713SJeremy L Thompson CeedQFunction qf; 659b7453713SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 660b7453713SJeremy L Thompson CeedOperator_Hip *impl; 661b7453713SJeremy L Thompson 6622b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 663b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); 664e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 665e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 666b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 667004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 668b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 669b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 670004e4986SSebastian Grimberg active_inputs = impl->qf_active_in; 671004e4986SSebastian Grimberg num_active_in = impl->num_active_in, num_active_out = impl->num_active_out; 6720d0321e0SJeremy L Thompson 6730d0321e0SJeremy L Thompson // Setup 6742b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetup_Hip(op)); 6750d0321e0SJeremy L Thompson 6760d0321e0SJeremy L Thompson // Input Evecs and Restriction 677b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request)); 6780d0321e0SJeremy L Thompson 6790d0321e0SJeremy L Thompson // Count number of active input fields 680b7453713SJeremy L Thompson if (!num_active_in) { 681b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 682b7453713SJeremy L Thompson CeedScalar *q_vec_array; 683b7453713SJeremy L Thompson CeedVector vec; 684b7453713SJeremy L Thompson 6850d0321e0SJeremy L Thompson // Get input vector 686b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 6870d0321e0SJeremy L Thompson // Check if active input 6880d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 689b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 690b7453713SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0)); 691b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array)); 692004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs)); 6930d0321e0SJeremy L Thompson for (CeedInt field = 0; field < size; field++) { 694004e4986SSebastian Grimberg CeedSize q_size = (CeedSize)Q * num_elem; 695004e4986SSebastian Grimberg 696004e4986SSebastian Grimberg CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field])); 697b7453713SJeremy L Thompson CeedCallBackend( 698004e4986SSebastian Grimberg CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem])); 6990d0321e0SJeremy L Thompson } 700b7453713SJeremy L Thompson num_active_in += size; 701b7453713SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array)); 7020d0321e0SJeremy L Thompson } 7030d0321e0SJeremy L Thompson } 704b7453713SJeremy L Thompson impl->num_active_in = num_active_in; 705004e4986SSebastian Grimberg impl->qf_active_in = active_inputs; 7060d0321e0SJeremy L Thompson } 7070d0321e0SJeremy L Thompson 7080d0321e0SJeremy L Thompson // Count number of active output fields 709b7453713SJeremy L Thompson if (!num_active_out) { 710b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 711b7453713SJeremy L Thompson CeedVector vec; 712b7453713SJeremy L Thompson 7130d0321e0SJeremy L Thompson // Get output vector 714b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 7150d0321e0SJeremy L Thompson // Check if active output 7160d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 717b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 718b7453713SJeremy L Thompson num_active_out += size; 7190d0321e0SJeremy L Thompson } 7200d0321e0SJeremy L Thompson } 721b7453713SJeremy L Thompson impl->num_active_out = num_active_out; 7220d0321e0SJeremy L Thompson } 7230d0321e0SJeremy L Thompson 7240d0321e0SJeremy L Thompson // Check sizes 725b7453713SJeremy L Thompson CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); 7260d0321e0SJeremy L Thompson 7270d0321e0SJeremy L Thompson // Build objects if needed 7280d0321e0SJeremy L Thompson if (build_objects) { 729b7453713SJeremy L Thompson CeedSize l_size = (CeedSize)num_elem * Q * num_active_in * num_active_out; 730b7453713SJeremy L Thompson CeedInt strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */ 731b7453713SJeremy L Thompson 732004e4986SSebastian Grimberg // Create output restriction 733b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out, 734b7453713SJeremy L Thompson num_active_in * num_active_out * num_elem * Q, strides, rstr)); 7350d0321e0SJeremy L Thompson // Create assembled vector 736b7453713SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled)); 7370d0321e0SJeremy L Thompson } 7382b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(*assembled, 0.0)); 739b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array)); 7400d0321e0SJeremy L Thompson 7410d0321e0SJeremy L Thompson // Input basis apply 742b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl)); 7430d0321e0SJeremy L Thompson 7440d0321e0SJeremy L Thompson // Assemble QFunction 745b7453713SJeremy L Thompson for (CeedInt in = 0; in < num_active_in; in++) { 7460d0321e0SJeremy L Thompson // Set Inputs 747004e4986SSebastian Grimberg CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0)); 748b7453713SJeremy L Thompson if (num_active_in > 1) { 749004e4986SSebastian Grimberg CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0)); 7500d0321e0SJeremy L Thompson } 7510d0321e0SJeremy L Thompson // Set Outputs 752b7453713SJeremy L Thompson for (CeedInt out = 0; out < num_output_fields; out++) { 753b7453713SJeremy L Thompson CeedVector vec; 754b7453713SJeremy L Thompson 7550d0321e0SJeremy L Thompson // Get output vector 756b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec)); 7570d0321e0SJeremy L Thompson // Check if active output 7580d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 759b7453713SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array)); 760b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size)); 761b7453713SJeremy L Thompson assembled_array += size * Q * num_elem; // Advance the pointer by the size of the output 7620d0321e0SJeremy L Thompson } 7630d0321e0SJeremy L Thompson } 7640d0321e0SJeremy L Thompson // Apply QFunction 765b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out)); 7660d0321e0SJeremy L Thompson } 7670d0321e0SJeremy L Thompson 768004e4986SSebastian Grimberg // Un-set output q_vecs to prevent accidental overwrite of Assembled 769b7453713SJeremy L Thompson for (CeedInt out = 0; out < num_output_fields; out++) { 770b7453713SJeremy L Thompson CeedVector vec; 771b7453713SJeremy L Thompson 7720d0321e0SJeremy L Thompson // Get output vector 773b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec)); 7740d0321e0SJeremy L Thompson // Check if active output 7750d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 776b7453713SJeremy L Thompson CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL)); 7770d0321e0SJeremy L Thompson } 7780d0321e0SJeremy L Thompson } 7790d0321e0SJeremy L Thompson 7800d0321e0SJeremy L Thompson // Restore input arrays 781b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl)); 7820d0321e0SJeremy L Thompson 7830d0321e0SJeremy L Thompson // Restore output 784b7453713SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); 7850d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7860d0321e0SJeremy L Thompson } 7870d0321e0SJeremy L Thompson 7880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7890d0321e0SJeremy L Thompson // Assemble Linear QFunction 7900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7912b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 7922b730f8bSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request); 7930d0321e0SJeremy L Thompson } 7940d0321e0SJeremy L Thompson 7950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 796b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction 7970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7982b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) { 7992b730f8bSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request); 8000d0321e0SJeremy L Thompson } 8010d0321e0SJeremy L Thompson 8020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 803004e4986SSebastian Grimberg // Assemble Diagonal Setup 8040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 805cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) { 8060d0321e0SJeremy L Thompson Ceed ceed; 807004e4986SSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 808cbfe683aSSebastian Grimberg CeedInt q_comp, num_nodes, num_qpts; 809004e4986SSebastian Grimberg CeedEvalMode *eval_modes_in = NULL, *eval_modes_out = NULL; 810b7453713SJeremy L Thompson CeedBasis basis_in = NULL, basis_out = NULL; 811b7453713SJeremy L Thompson CeedQFunctionField *qf_fields; 8120d0321e0SJeremy L Thompson CeedQFunction qf; 813b7453713SJeremy L Thompson CeedOperatorField *op_fields; 814b7453713SJeremy L Thompson CeedOperator_Hip *impl; 815b7453713SJeremy L Thompson 816b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 8172b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 818b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields)); 8190d0321e0SJeremy L Thompson 8200d0321e0SJeremy L Thompson // Determine active input basis 821b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 822b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 823b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 8240d0321e0SJeremy L Thompson CeedVector vec; 825b7453713SJeremy L Thompson 826b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 8270d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 828004e4986SSebastian Grimberg CeedBasis basis; 829004e4986SSebastian Grimberg CeedEvalMode eval_mode; 830b7453713SJeremy L Thompson 831004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 832004e4986SSebastian Grimberg CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, 833004e4986SSebastian Grimberg "Backend does not implement operator diagonal assembly with multiple active bases"); 834004e4986SSebastian Grimberg basis_in = basis; 835004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 836004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 837004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 838004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly 839004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in)); 840004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode; 841004e4986SSebastian Grimberg num_eval_modes_in += q_comp; 8420d0321e0SJeremy L Thompson } 8430d0321e0SJeremy L Thompson } 8440d0321e0SJeremy L Thompson } 8450d0321e0SJeremy L Thompson 8460d0321e0SJeremy L Thompson // Determine active output basis 847b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 848b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 849b7453713SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 8500d0321e0SJeremy L Thompson CeedVector vec; 851b7453713SJeremy L Thompson 852b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 8530d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 854004e4986SSebastian Grimberg CeedBasis basis; 855004e4986SSebastian Grimberg CeedEvalMode eval_mode; 856b7453713SJeremy L Thompson 857004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 858004e4986SSebastian Grimberg CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND, 859004e4986SSebastian Grimberg "Backend does not implement operator diagonal assembly with multiple active bases"); 860004e4986SSebastian Grimberg basis_out = basis; 861004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 862004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 863004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 864004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly 865004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out)); 866004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode; 867004e4986SSebastian Grimberg num_eval_modes_out += q_comp; 8680d0321e0SJeremy L Thompson } 8690d0321e0SJeremy L Thompson } 8700d0321e0SJeremy L Thompson } 8710d0321e0SJeremy L Thompson 8720d0321e0SJeremy L Thompson // Operator data struct 8732b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 8742b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl->diag)); 8750d0321e0SJeremy L Thompson CeedOperatorDiag_Hip *diag = impl->diag; 876b7453713SJeremy L Thompson 877cbfe683aSSebastian Grimberg // Basis matrices 878004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes)); 879004e4986SSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes; 880004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts)); 881004e4986SSebastian Grimberg const CeedInt interp_bytes = num_nodes * num_qpts * sizeof(CeedScalar); 882004e4986SSebastian Grimberg const CeedInt eval_modes_bytes = sizeof(CeedEvalMode); 883004e4986SSebastian Grimberg bool has_eval_none = false; 8840d0321e0SJeremy L Thompson 8850d0321e0SJeremy L Thompson // CEED_EVAL_NONE 886004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE); 887004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE); 888004e4986SSebastian Grimberg if (has_eval_none) { 8890d0321e0SJeremy L Thompson CeedScalar *identity = NULL; 890b7453713SJeremy L Thompson 891004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity)); 892004e4986SSebastian Grimberg for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0; 893b7453713SJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes)); 894b7453713SJeremy L Thompson CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice)); 895004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 8960d0321e0SJeremy L Thompson } 8970d0321e0SJeremy L Thompson 898004e4986SSebastian Grimberg // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL 899004e4986SSebastian Grimberg for (CeedInt in = 0; in < 2; in++) { 900004e4986SSebastian Grimberg CeedFESpace fespace; 901004e4986SSebastian Grimberg CeedBasis basis = in ? basis_in : basis_out; 9020d0321e0SJeremy L Thompson 903004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetFESpace(basis, &fespace)); 904004e4986SSebastian Grimberg switch (fespace) { 905004e4986SSebastian Grimberg case CEED_FE_SPACE_H1: { 906004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_grad; 907004e4986SSebastian Grimberg const CeedScalar *interp, *grad; 908004e4986SSebastian Grimberg CeedScalar *d_interp, *d_grad; 9090d0321e0SJeremy L Thompson 910004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 911004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad)); 9120d0321e0SJeremy L Thompson 913004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 914004e4986SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 915004e4986SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice)); 916004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetGrad(basis, &grad)); 917004e4986SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad)); 918004e4986SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice)); 919004e4986SSebastian Grimberg if (in) { 920004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 921004e4986SSebastian Grimberg diag->d_grad_in = d_grad; 922004e4986SSebastian Grimberg } else { 923004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 924004e4986SSebastian Grimberg diag->d_grad_out = d_grad; 925004e4986SSebastian Grimberg } 926004e4986SSebastian Grimberg } break; 927004e4986SSebastian Grimberg case CEED_FE_SPACE_HDIV: { 928004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_div; 929004e4986SSebastian Grimberg const CeedScalar *interp, *div; 930004e4986SSebastian Grimberg CeedScalar *d_interp, *d_div; 931004e4986SSebastian Grimberg 932004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 933004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div)); 934004e4986SSebastian Grimberg 935004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 936004e4986SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 937004e4986SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice)); 938004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetDiv(basis, &div)); 939004e4986SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div)); 940004e4986SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice)); 941004e4986SSebastian Grimberg if (in) { 942004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 943004e4986SSebastian Grimberg diag->d_div_in = d_div; 944004e4986SSebastian Grimberg } else { 945004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 946004e4986SSebastian Grimberg diag->d_div_out = d_div; 947004e4986SSebastian Grimberg } 948004e4986SSebastian Grimberg } break; 949004e4986SSebastian Grimberg case CEED_FE_SPACE_HCURL: { 950004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_curl; 951004e4986SSebastian Grimberg const CeedScalar *interp, *curl; 952004e4986SSebastian Grimberg CeedScalar *d_interp, *d_curl; 953004e4986SSebastian Grimberg 954004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 955004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl)); 956004e4986SSebastian Grimberg 957004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 958004e4986SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 959004e4986SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice)); 960004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetCurl(basis, &curl)); 961004e4986SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl)); 962004e4986SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice)); 963004e4986SSebastian Grimberg if (in) { 964004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 965004e4986SSebastian Grimberg diag->d_curl_in = d_curl; 966004e4986SSebastian Grimberg } else { 967004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 968004e4986SSebastian Grimberg diag->d_curl_out = d_curl; 969004e4986SSebastian Grimberg } 970004e4986SSebastian Grimberg } break; 971004e4986SSebastian Grimberg } 972004e4986SSebastian Grimberg } 973004e4986SSebastian Grimberg 974004e4986SSebastian Grimberg // Arrays of eval_modes 975004e4986SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes)); 976004e4986SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice)); 977004e4986SSebastian Grimberg CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes)); 978004e4986SSebastian Grimberg CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice)); 979004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&eval_modes_in)); 980004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&eval_modes_out)); 9810d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9820d0321e0SJeremy L Thompson } 9830d0321e0SJeremy L Thompson 9840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 985cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation) 986cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------ 987cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) { 988cbfe683aSSebastian Grimberg Ceed ceed; 98922070f95SJeremy L Thompson char *diagonal_kernel_source; 99022070f95SJeremy L Thompson const char *diagonal_kernel_path; 991cbfe683aSSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 992cbfe683aSSebastian Grimberg CeedInt num_comp, q_comp, num_nodes, num_qpts; 993cbfe683aSSebastian Grimberg CeedBasis basis_in = NULL, basis_out = NULL; 994cbfe683aSSebastian Grimberg CeedQFunctionField *qf_fields; 995cbfe683aSSebastian Grimberg CeedQFunction qf; 996cbfe683aSSebastian Grimberg CeedOperatorField *op_fields; 997cbfe683aSSebastian Grimberg CeedOperator_Hip *impl; 998cbfe683aSSebastian Grimberg 999cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1000cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1001cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields)); 1002cbfe683aSSebastian Grimberg 1003cbfe683aSSebastian Grimberg // Determine active input basis 1004cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 1005cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 1006cbfe683aSSebastian Grimberg for (CeedInt i = 0; i < num_input_fields; i++) { 1007cbfe683aSSebastian Grimberg CeedVector vec; 1008cbfe683aSSebastian Grimberg 1009cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1010cbfe683aSSebastian Grimberg if (vec == CEED_VECTOR_ACTIVE) { 1011cbfe683aSSebastian Grimberg CeedEvalMode eval_mode; 1012cbfe683aSSebastian Grimberg 1013cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in)); 1014cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1015cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 1016cbfe683aSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1017cbfe683aSSebastian Grimberg num_eval_modes_in += q_comp; 1018cbfe683aSSebastian Grimberg } 1019cbfe683aSSebastian Grimberg } 1020cbfe683aSSebastian Grimberg } 1021cbfe683aSSebastian Grimberg 1022cbfe683aSSebastian Grimberg // Determine active output basis 1023cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 1024cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1025cbfe683aSSebastian Grimberg for (CeedInt i = 0; i < num_output_fields; i++) { 1026cbfe683aSSebastian Grimberg CeedVector vec; 1027cbfe683aSSebastian Grimberg 1028cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1029cbfe683aSSebastian Grimberg if (vec == CEED_VECTOR_ACTIVE) { 1030cbfe683aSSebastian Grimberg CeedEvalMode eval_mode; 1031cbfe683aSSebastian Grimberg 1032cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out)); 1033cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1034cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 1035cbfe683aSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1036cbfe683aSSebastian Grimberg num_eval_modes_out += q_comp; 1037cbfe683aSSebastian Grimberg } 1038cbfe683aSSebastian Grimberg } 1039cbfe683aSSebastian Grimberg } 1040cbfe683aSSebastian Grimberg 1041cbfe683aSSebastian Grimberg // Operator data struct 1042cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetData(op, &impl)); 1043cbfe683aSSebastian Grimberg CeedOperatorDiag_Hip *diag = impl->diag; 1044cbfe683aSSebastian Grimberg 1045cbfe683aSSebastian Grimberg // Assemble kernel 1046cbfe683aSSebastian Grimberg hipModule_t *module = is_point_block ? &diag->module_point_block : &diag->module; 1047cbfe683aSSebastian Grimberg CeedInt elems_per_block = 1; 1048cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes)); 1049cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp)); 1050cbfe683aSSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes; 1051cbfe683aSSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts)); 1052cbfe683aSSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path)); 1053cbfe683aSSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n"); 1054cbfe683aSSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source)); 1055cbfe683aSSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n"); 1056cbfe683aSSebastian Grimberg CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT", 1057cbfe683aSSebastian Grimberg num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE", 1058cbfe683aSSebastian Grimberg use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block)); 1059cbfe683aSSebastian Grimberg CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal)); 1060cbfe683aSSebastian Grimberg CeedCallBackend(CeedFree(&diagonal_kernel_path)); 1061cbfe683aSSebastian Grimberg CeedCallBackend(CeedFree(&diagonal_kernel_source)); 1062cbfe683aSSebastian Grimberg return CEED_ERROR_SUCCESS; 1063cbfe683aSSebastian Grimberg } 1064cbfe683aSSebastian Grimberg 1065cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------ 1066004e4986SSebastian Grimberg // Assemble Diagonal Core 10670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1068b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) { 10690d0321e0SJeremy L Thompson Ceed ceed; 1070cbfe683aSSebastian Grimberg CeedInt num_elem, num_nodes; 1071b7453713SJeremy L Thompson CeedScalar *elem_diag_array; 1072b7453713SJeremy L Thompson const CeedScalar *assembled_qf_array; 1073004e4986SSebastian Grimberg CeedVector assembled_qf = NULL, elem_diag; 1074004e4986SSebastian Grimberg CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr; 10750d0321e0SJeremy L Thompson CeedOperator_Hip *impl; 1076b7453713SJeremy L Thompson 1077b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 10782b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 10790d0321e0SJeremy L Thompson 10800d0321e0SJeremy L Thompson // Assemble QFunction 1081004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request)); 1082004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr)); 1083004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array)); 10840d0321e0SJeremy L Thompson 1085cbfe683aSSebastian Grimberg // Setup 1086cf8cbdd6SSebastian Grimberg if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op)); 1087cbfe683aSSebastian Grimberg CeedOperatorDiag_Hip *diag = impl->diag; 1088cbfe683aSSebastian Grimberg 1089cbfe683aSSebastian Grimberg assert(diag != NULL); 1090cbfe683aSSebastian Grimberg 1091cbfe683aSSebastian Grimberg // Assemble kernel if needed 1092cbfe683aSSebastian Grimberg if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) { 1093cbfe683aSSebastian Grimberg CeedSize assembled_length, assembled_qf_length; 1094cbfe683aSSebastian Grimberg CeedInt use_ceedsize_idx = 0; 10959330daecSnbeams CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length)); 1096b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length)); 1097b7453713SJeremy L Thompson if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1; 10989330daecSnbeams 1099cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block)); 1100cbfe683aSSebastian Grimberg } 11010d0321e0SJeremy L Thompson 1102004e4986SSebastian Grimberg // Restriction and diagonal vector 1103004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 1104004e4986SSebastian Grimberg CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND, 1105004e4986SSebastian Grimberg "Cannot assemble operator diagonal with different input and output active element restrictions"); 1106004e4986SSebastian Grimberg if (!is_point_block && !diag->diag_rstr) { 1107004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr)); 1108004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag)); 1109004e4986SSebastian Grimberg } else if (is_point_block && !diag->point_block_diag_rstr) { 1110004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr)); 1111004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag)); 11120d0321e0SJeremy L Thompson } 1113004e4986SSebastian Grimberg diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr; 1114004e4986SSebastian Grimberg elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag; 1115b7453713SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0)); 11160d0321e0SJeremy L Thompson 111791db28b6SZach Atkins // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers 1118004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes)); 1119004e4986SSebastian Grimberg if (num_nodes > 0) { 11200d0321e0SJeremy L Thompson // Assemble element operator diagonals 1121b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array)); 1122b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem)); 11230d0321e0SJeremy L Thompson 11240d0321e0SJeremy L Thompson // Compute the diagonal of B^T D B 1125004e4986SSebastian Grimberg CeedInt elems_per_block = 1; 1126004e4986SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem, elems_per_block); 1127004e4986SSebastian Grimberg void *args[] = {(void *)&num_elem, &diag->d_identity, &diag->d_interp_in, &diag->d_grad_in, &diag->d_div_in, 1128004e4986SSebastian Grimberg &diag->d_curl_in, &diag->d_interp_out, &diag->d_grad_out, &diag->d_div_out, &diag->d_curl_out, 1129004e4986SSebastian Grimberg &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array}; 1130b7453713SJeremy L Thompson 1131b7453713SJeremy L Thompson if (is_point_block) { 1132004e4986SSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args)); 11330d0321e0SJeremy L Thompson } else { 1134004e4986SSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args)); 11350d0321e0SJeremy L Thompson } 11360d0321e0SJeremy L Thompson 11370d0321e0SJeremy L Thompson // Restore arrays 1138b7453713SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array)); 1139b7453713SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 114091db28b6SZach Atkins } 11410d0321e0SJeremy L Thompson 11420d0321e0SJeremy L Thompson // Assemble local operator diagonal 1143b7453713SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); 11440d0321e0SJeremy L Thompson 11450d0321e0SJeremy L Thompson // Cleanup 1146b7453713SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 11470d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11480d0321e0SJeremy L Thompson } 11490d0321e0SJeremy L Thompson 11500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 11510d0321e0SJeremy L Thompson // Assemble Linear Diagonal 11520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 11532b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) { 11542b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false)); 11556aa95790SJeremy L Thompson return CEED_ERROR_SUCCESS; 11560d0321e0SJeremy L Thompson } 11570d0321e0SJeremy L Thompson 11580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 11590d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal 11600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 11612b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) { 11622b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true)); 11636aa95790SJeremy L Thompson return CEED_ERROR_SUCCESS; 11640d0321e0SJeremy L Thompson } 11650d0321e0SJeremy L Thompson 1166a835093fSnbeams //------------------------------------------------------------------------------ 1167004e4986SSebastian Grimberg // Single Operator Assembly Setup 1168a835093fSnbeams //------------------------------------------------------------------------------ 11699330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) { 1170a835093fSnbeams Ceed ceed; 117122070f95SJeremy L Thompson char *assembly_kernel_source; 117222070f95SJeremy L Thompson const char *assembly_kernel_path; 1173004e4986SSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 11743b38d1dfSJeremy L Thompson CeedInt elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp; 1175004e4986SSebastian Grimberg CeedEvalMode *eval_modes_in = NULL, *eval_modes_out = NULL; 1176b7453713SJeremy L Thompson CeedElemRestriction rstr_in = NULL, rstr_out = NULL; 1177b7453713SJeremy L Thompson CeedBasis basis_in = NULL, basis_out = NULL; 1178b7453713SJeremy L Thompson CeedQFunctionField *qf_fields; 1179b7453713SJeremy L Thompson CeedQFunction qf; 1180b7453713SJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 1181a835093fSnbeams CeedOperator_Hip *impl; 1182b7453713SJeremy L Thompson 1183b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 11842b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1185a835093fSnbeams 1186a835093fSnbeams // Get intput and output fields 11872b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 1188a835093fSnbeams 1189a835093fSnbeams // Determine active input basis eval mode 11902b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 11912b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 1192a835093fSnbeams for (CeedInt i = 0; i < num_input_fields; i++) { 1193a835093fSnbeams CeedVector vec; 1194b7453713SJeremy L Thompson 11952b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec)); 1196a835093fSnbeams if (vec == CEED_VECTOR_ACTIVE) { 1197004e4986SSebastian Grimberg CeedBasis basis; 1198b7453713SJeremy L Thompson CeedEvalMode eval_mode; 1199b7453713SJeremy L Thompson 1200004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis)); 1201004e4986SSebastian Grimberg CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases"); 1202004e4986SSebastian Grimberg basis_in = basis; 12032b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in)); 1204004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 1205004e4986SSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in; 1206004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in)); 12072b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1208004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 1209004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1210004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1211004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in)); 1212004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1213004e4986SSebastian Grimberg eval_modes_in[num_eval_modes_in + d] = eval_mode; 1214a835093fSnbeams } 1215004e4986SSebastian Grimberg num_eval_modes_in += q_comp; 1216a835093fSnbeams } 1217a835093fSnbeams } 1218a835093fSnbeams } 1219a835093fSnbeams 1220a835093fSnbeams // Determine active output basis; basis_out and rstr_out only used if same as input, TODO 12212b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1222a835093fSnbeams for (CeedInt i = 0; i < num_output_fields; i++) { 1223a835093fSnbeams CeedVector vec; 1224b7453713SJeremy L Thompson 12252b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec)); 1226a835093fSnbeams if (vec == CEED_VECTOR_ACTIVE) { 1227004e4986SSebastian Grimberg CeedBasis basis; 1228b7453713SJeremy L Thompson CeedEvalMode eval_mode; 1229b7453713SJeremy L Thompson 1230004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis)); 1231004e4986SSebastian Grimberg CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND, 1232004e4986SSebastian Grimberg "Backend does not implement operator assembly with multiple active bases"); 1233004e4986SSebastian Grimberg basis_out = basis; 12342b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out)); 1235004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 1236004e4986SSebastian Grimberg if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out; 1237004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out)); 1238004e4986SSebastian Grimberg CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED, 1239004e4986SSebastian Grimberg "Active input and output bases must have the same number of quadrature points"); 12402b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1241004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 1242004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1243004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1244004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out)); 1245004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1246004e4986SSebastian Grimberg eval_modes_out[num_eval_modes_out + d] = eval_mode; 1247004e4986SSebastian Grimberg } 1248004e4986SSebastian Grimberg num_eval_modes_out += q_comp; 1249a835093fSnbeams } 1250a835093fSnbeams } 1251a835093fSnbeams } 1252004e4986SSebastian Grimberg CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs"); 1253a835093fSnbeams 12542b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl->asmb)); 1255a835093fSnbeams CeedOperatorAssemble_Hip *asmb = impl->asmb; 1256004e4986SSebastian Grimberg asmb->elems_per_block = 1; 1257004e4986SSebastian Grimberg asmb->block_size_x = elem_size_in; 1258004e4986SSebastian Grimberg asmb->block_size_y = elem_size_out; 1259004e4986SSebastian Grimberg 1260004e4986SSebastian Grimberg bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024; 1261004e4986SSebastian Grimberg 1262004e4986SSebastian Grimberg if (fallback) { 1263004e4986SSebastian Grimberg // Use fallback kernel with 1D threadblock 1264004e4986SSebastian Grimberg asmb->block_size_y = 1; 1265004e4986SSebastian Grimberg } 1266a835093fSnbeams 1267a835093fSnbeams // Compile kernels 1268004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in)); 1269004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out)); 12702b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path)); 127123d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n"); 12722b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source)); 127323d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n"); 1274004e4986SSebastian Grimberg CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT", 1275004e4986SSebastian Grimberg num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in, 1276004e4986SSebastian Grimberg "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE", 1277cbfe683aSSebastian Grimberg asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE", 12789330daecSnbeams use_ceedsize_idx)); 1279004e4986SSebastian Grimberg CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble)); 12802b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&assembly_kernel_path)); 12812b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&assembly_kernel_source)); 1282a835093fSnbeams 1283004e4986SSebastian Grimberg // Load into B_in, in order that they will be used in eval_modes_in 1284004e4986SSebastian Grimberg { 1285004e4986SSebastian Grimberg const CeedInt in_bytes = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar); 1286004e4986SSebastian Grimberg CeedInt d_in = 0; 1287004e4986SSebastian Grimberg CeedEvalMode eval_modes_in_prev = CEED_EVAL_NONE; 1288004e4986SSebastian Grimberg bool has_eval_none = false; 1289004e4986SSebastian Grimberg CeedScalar *identity = NULL; 1290a835093fSnbeams 1291004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) { 1292004e4986SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE); 1293004e4986SSebastian Grimberg } 1294004e4986SSebastian Grimberg if (has_eval_none) { 1295004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity)); 1296004e4986SSebastian 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; 1297004e4986SSebastian Grimberg } 1298b7453713SJeremy L Thompson 1299b7453713SJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes)); 1300004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) { 1301004e4986SSebastian Grimberg const CeedScalar *h_B_in; 1302004e4986SSebastian Grimberg 1303004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in)); 1304004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp)); 1305004e4986SSebastian Grimberg if (q_comp > 1) { 1306004e4986SSebastian Grimberg if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0; 1307004e4986SSebastian Grimberg else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in]; 1308004e4986SSebastian Grimberg } 1309004e4986SSebastian Grimberg eval_modes_in_prev = eval_modes_in[i]; 1310004e4986SSebastian Grimberg 1311004e4986SSebastian 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), 1312004e4986SSebastian Grimberg hipMemcpyHostToDevice)); 1313004e4986SSebastian Grimberg } 1314004e4986SSebastian Grimberg 1315004e4986SSebastian Grimberg if (identity) { 1316004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 1317a835093fSnbeams } 1318a835093fSnbeams } 1319a835093fSnbeams 1320004e4986SSebastian Grimberg // Load into B_out, in order that they will be used in eval_modes_out 1321004e4986SSebastian Grimberg { 1322004e4986SSebastian Grimberg const CeedInt out_bytes = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar); 1323004e4986SSebastian Grimberg CeedInt d_out = 0; 1324004e4986SSebastian Grimberg CeedEvalMode eval_modes_out_prev = CEED_EVAL_NONE; 1325004e4986SSebastian Grimberg bool has_eval_none = false; 1326004e4986SSebastian Grimberg CeedScalar *identity = NULL; 1327b7453713SJeremy L Thompson 1328004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) { 1329004e4986SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE); 1330004e4986SSebastian Grimberg } 1331004e4986SSebastian Grimberg if (has_eval_none) { 1332004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity)); 1333004e4986SSebastian 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; 1334a835093fSnbeams } 1335a835093fSnbeams 1336b7453713SJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes)); 1337004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) { 1338004e4986SSebastian Grimberg const CeedScalar *h_B_out; 1339004e4986SSebastian Grimberg 1340004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out)); 1341004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp)); 1342004e4986SSebastian Grimberg if (q_comp > 1) { 1343004e4986SSebastian Grimberg if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0; 1344004e4986SSebastian Grimberg else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out]; 1345004e4986SSebastian Grimberg } 1346004e4986SSebastian Grimberg eval_modes_out_prev = eval_modes_out[i]; 1347004e4986SSebastian Grimberg 1348004e4986SSebastian 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), 1349004e4986SSebastian Grimberg hipMemcpyHostToDevice)); 1350004e4986SSebastian Grimberg } 1351004e4986SSebastian Grimberg 1352004e4986SSebastian Grimberg if (identity) { 1353004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 1354a835093fSnbeams } 1355a835093fSnbeams } 1356a835093fSnbeams return CEED_ERROR_SUCCESS; 1357a835093fSnbeams } 1358a835093fSnbeams 1359a835093fSnbeams //------------------------------------------------------------------------------ 1360cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator. 1361cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic. 1362cefa2673SJeremy L Thompson // 1363ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval 1364cefa2673SJeremy L Thompson // modes). 1365cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects 1366a835093fSnbeams //------------------------------------------------------------------------------ 13672b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) { 1368a835093fSnbeams Ceed ceed; 1369b7453713SJeremy L Thompson CeedSize values_length = 0, assembled_qf_length = 0; 1370004e4986SSebastian Grimberg CeedInt use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out; 1371b7453713SJeremy L Thompson CeedScalar *values_array; 1372004e4986SSebastian Grimberg const CeedScalar *assembled_qf_array; 1373b7453713SJeremy L Thompson CeedVector assembled_qf = NULL; 1374004e4986SSebastian Grimberg CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out; 1375004e4986SSebastian Grimberg CeedRestrictionType rstr_type_in, rstr_type_out; 1376004e4986SSebastian Grimberg const bool *orients_in = NULL, *orients_out = NULL; 1377004e4986SSebastian Grimberg const CeedInt8 *curl_orients_in = NULL, *curl_orients_out = NULL; 1378a835093fSnbeams CeedOperator_Hip *impl; 1379b7453713SJeremy L Thompson 1380b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 13812b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1382a835093fSnbeams 1383a835093fSnbeams // Assemble QFunction 1384004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE)); 1385004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr)); 1386004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array)); 1387a835093fSnbeams 13889330daecSnbeams CeedCallBackend(CeedVectorGetLength(values, &values_length)); 13899330daecSnbeams CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length)); 13909330daecSnbeams if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1; 1391004e4986SSebastian Grimberg 13929330daecSnbeams // Setup 1393004e4986SSebastian Grimberg if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx)); 1394004e4986SSebastian Grimberg CeedOperatorAssemble_Hip *asmb = impl->asmb; 1395004e4986SSebastian Grimberg 1396004e4986SSebastian Grimberg assert(asmb != NULL); 1397004e4986SSebastian Grimberg 1398004e4986SSebastian Grimberg // Assemble element operator 1399004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array)); 1400004e4986SSebastian Grimberg values_array += offset; 1401004e4986SSebastian Grimberg 1402004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 1403004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in)); 1404004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 1405004e4986SSebastian Grimberg 1406004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in)); 1407004e4986SSebastian Grimberg if (rstr_type_in == CEED_RESTRICTION_ORIENTED) { 1408004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in)); 1409004e4986SSebastian Grimberg } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 1410004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in)); 1411004e4986SSebastian Grimberg } 1412004e4986SSebastian Grimberg 1413004e4986SSebastian Grimberg if (rstr_in != rstr_out) { 1414004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out)); 1415004e4986SSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 1416004e4986SSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 1417004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 1418004e4986SSebastian Grimberg 1419004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out)); 1420004e4986SSebastian Grimberg if (rstr_type_out == CEED_RESTRICTION_ORIENTED) { 1421004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out)); 1422004e4986SSebastian Grimberg } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 1423004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out)); 1424004e4986SSebastian Grimberg } 1425004e4986SSebastian Grimberg } else { 1426004e4986SSebastian Grimberg elem_size_out = elem_size_in; 1427004e4986SSebastian Grimberg orients_out = orients_in; 1428004e4986SSebastian Grimberg curl_orients_out = curl_orients_in; 14299330daecSnbeams } 14309330daecSnbeams 1431a835093fSnbeams // Compute B^T D B 1432004e4986SSebastian Grimberg CeedInt shared_mem = 1433004e4986SSebastian 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)) * 1434004e4986SSebastian Grimberg sizeof(CeedScalar); 1435004e4986SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem_in, asmb->elems_per_block); 1436004e4986SSebastian Grimberg void *args[] = {(void *)&num_elem_in, &asmb->d_B_in, &asmb->d_B_out, &orients_in, &curl_orients_in, 1437004e4986SSebastian Grimberg &orients_out, &curl_orients_out, &assembled_qf_array, &values_array}; 1438b7453713SJeremy L Thompson 14392b730f8bSJeremy L Thompson CeedCallBackend( 1440004e4986SSebastian Grimberg CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args)); 1441a835093fSnbeams 1442a835093fSnbeams // Restore arrays 14432b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(values, &values_array)); 1444004e4986SSebastian Grimberg CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 1445a835093fSnbeams 1446a835093fSnbeams // Cleanup 14472b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 1448004e4986SSebastian Grimberg if (rstr_type_in == CEED_RESTRICTION_ORIENTED) { 1449004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in)); 1450004e4986SSebastian Grimberg } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 1451004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in)); 1452004e4986SSebastian Grimberg } 1453004e4986SSebastian Grimberg if (rstr_in != rstr_out) { 1454004e4986SSebastian Grimberg if (rstr_type_out == CEED_RESTRICTION_ORIENTED) { 1455004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out)); 1456004e4986SSebastian Grimberg } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 1457004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out)); 1458004e4986SSebastian Grimberg } 1459004e4986SSebastian Grimberg } 1460a835093fSnbeams return CEED_ERROR_SUCCESS; 1461a835093fSnbeams } 1462a835093fSnbeams 1463a835093fSnbeams //------------------------------------------------------------------------------ 1464*67d9480aSJeremy L Thompson // Assemble Linear QFunction AtPoints 1465*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 1466*67d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 1467*67d9480aSJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction"); 1468*67d9480aSJeremy L Thompson } 1469*67d9480aSJeremy L Thompson 1470*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 1471*67d9480aSJeremy L Thompson // Assemble Linear Diagonal AtPoints 1472*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 1473*67d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) { 1474*67d9480aSJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedSingleOperatorLinearAssembleAddDiagonal"); 1475*67d9480aSJeremy L Thompson } 1476*67d9480aSJeremy L Thompson 1477*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 14780d0321e0SJeremy L Thompson // Create operator 14790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 14800d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) { 14810d0321e0SJeremy L Thompson Ceed ceed; 14820d0321e0SJeremy L Thompson CeedOperator_Hip *impl; 14830d0321e0SJeremy L Thompson 1484b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 14852b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 14862b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl)); 14872b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip)); 14882b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip)); 14892b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip)); 14902b730f8bSJeremy L Thompson CeedCallBackend( 14912b730f8bSJeremy L Thompson CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip)); 14922b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip)); 14932b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip)); 14942b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip)); 14950d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14960d0321e0SJeremy L Thompson } 14970d0321e0SJeremy L Thompson 14980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1499*67d9480aSJeremy L Thompson // Create operator AtPoints 1500*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 1501*67d9480aSJeremy L Thompson int CeedOperatorCreateAtPoints_Hip(CeedOperator op) { 1502*67d9480aSJeremy L Thompson Ceed ceed; 1503*67d9480aSJeremy L Thompson CeedOperator_Hip *impl; 1504*67d9480aSJeremy L Thompson 1505*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1506*67d9480aSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 1507*67d9480aSJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl)); 1508*67d9480aSJeremy L Thompson 1509*67d9480aSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Hip)); 1510*67d9480aSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip)); 1511*67d9480aSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Hip)); 1512*67d9480aSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip)); 1513*67d9480aSJeremy L Thompson return CEED_ERROR_SUCCESS; 1514*67d9480aSJeremy L Thompson } 1515*67d9480aSJeremy L Thompson 1516*67d9480aSJeremy L Thompson //------------------------------------------------------------------------------ 1517