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> 92b730f8bSJeremy L Thompson #include <ceed/backend.h> 102b730f8bSJeremy L Thompson #include <ceed/jit-tools.h> 11c85e8640SSebastian Grimberg #include <assert.h> 120d0321e0SJeremy L Thompson #include <cuda.h> 130d0321e0SJeremy L Thompson #include <cuda_runtime.h> 140d0321e0SJeremy L Thompson #include <stdbool.h> 150d0321e0SJeremy L Thompson #include <string.h> 162b730f8bSJeremy L Thompson 1749aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h" 180d0321e0SJeremy L Thompson #include "../cuda/ceed-cuda-compile.h" 192b730f8bSJeremy L Thompson #include "ceed-cuda-ref.h" 200d0321e0SJeremy L Thompson 210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 220d0321e0SJeremy L Thompson // Destroy operator 230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 240d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Cuda(CeedOperator op) { 250d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 26ca735530SJeremy L Thompson 272b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 280d0321e0SJeremy L Thompson 290d0321e0SJeremy L Thompson // Apply data 303aab95c0SJeremy L Thompson CeedCallBackend(CeedFree(&impl->skip_rstr_in)); 31f8a0df59SJeremy L Thompson CeedCallBackend(CeedFree(&impl->skip_rstr_out)); 32f8a0df59SJeremy L Thompson CeedCallBackend(CeedFree(&impl->apply_add_basis_out)); 33ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) { 34ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i])); 350d0321e0SJeremy L Thompson } 36ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->e_vecs)); 37c1222711SJeremy L Thompson CeedCallBackend(CeedFree(&impl->input_states)); 380d0321e0SJeremy L Thompson 39ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_inputs; i++) { 40ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i])); 410d0321e0SJeremy L Thompson } 42ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->q_vecs_in)); 430d0321e0SJeremy L Thompson 44ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_outputs; i++) { 45ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i])); 460d0321e0SJeremy L Thompson } 47ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->q_vecs_out)); 48756ca9e9SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem)); 490d0321e0SJeremy L Thompson 500d0321e0SJeremy L Thompson // QFunction assembly data 51ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_active_in; i++) { 52ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i])); 530d0321e0SJeremy L Thompson } 54ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->qf_active_in)); 550d0321e0SJeremy L Thompson 560d0321e0SJeremy L Thompson // Diag data 570d0321e0SJeremy L Thompson if (impl->diag) { 580d0321e0SJeremy L Thompson Ceed ceed; 59ca735530SJeremy L Thompson 602b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 61cbfe683aSSebastian Grimberg if (impl->diag->module) { 622b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(impl->diag->module)); 63cbfe683aSSebastian Grimberg } 64cbfe683aSSebastian Grimberg if (impl->diag->module_point_block) { 65cbfe683aSSebastian Grimberg CeedCallCuda(ceed, cuModuleUnload(impl->diag->module_point_block)); 66cbfe683aSSebastian Grimberg } 67004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_in)); 68004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_out)); 692b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_identity)); 70ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_in)); 71ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_out)); 72ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_in)); 73ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_out)); 74004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_div_in)); 75004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_div_out)); 76004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_in)); 77004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_out)); 78004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr)); 79506b1a0cSSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr)); 80ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag)); 81ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag)); 820d0321e0SJeremy L Thompson } 832b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->diag)); 840d0321e0SJeremy L Thompson 85cc132f9aSnbeams if (impl->asmb) { 86cc132f9aSnbeams Ceed ceed; 87ca735530SJeremy L Thompson 882b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 892b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(impl->asmb->module)); 902b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_in)); 912b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_out)); 92cc132f9aSnbeams } 932b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->asmb)); 94cc132f9aSnbeams 952b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 960d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 970d0321e0SJeremy L Thompson } 980d0321e0SJeremy L Thompson 990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1000d0321e0SJeremy L Thompson // Setup infields or outfields 1010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 102f8a0df59SJeremy L Thompson static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, bool *skip_rstr, bool *apply_add_basis, 103f8a0df59SJeremy L Thompson CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) { 1040d0321e0SJeremy L Thompson Ceed ceed; 105ca735530SJeremy L Thompson CeedQFunctionField *qf_fields; 106ca735530SJeremy L Thompson CeedOperatorField *op_fields; 1070d0321e0SJeremy L Thompson 108ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 109ca735530SJeremy L Thompson if (is_input) { 110ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 111ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 1120d0321e0SJeremy L Thompson } else { 113ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 114ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1150d0321e0SJeremy L Thompson } 1160d0321e0SJeremy L Thompson 1170d0321e0SJeremy L Thompson // Loop over fields 118ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_fields; i++) { 119004e4986SSebastian Grimberg bool is_strided = false, skip_restriction = false; 120004e4986SSebastian Grimberg CeedSize q_size; 121004e4986SSebastian Grimberg CeedInt size; 122004e4986SSebastian Grimberg CeedEvalMode eval_mode; 123ca735530SJeremy L Thompson CeedBasis basis; 1240d0321e0SJeremy L Thompson 125004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 126004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 127edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 128ca735530SJeremy L Thompson 1290d0321e0SJeremy L Thompson // Check whether this field can skip the element restriction: 130004e4986SSebastian Grimberg // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND. 131004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr)); 1320d0321e0SJeremy L Thompson 1330d0321e0SJeremy L Thompson // First, check whether the field is input or output: 134ca735530SJeremy L Thompson if (is_input) { 135ca735530SJeremy L Thompson CeedVector vec; 136ca735530SJeremy L Thompson 137004e4986SSebastian Grimberg // Check for passive input 138ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 139ca735530SJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE) { 140004e4986SSebastian Grimberg // Check eval_mode 141004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 1420d0321e0SJeremy L Thompson // Check for strided restriction 143edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided)); 144ca735530SJeremy L Thompson if (is_strided) { 1450d0321e0SJeremy L Thompson // Check if vector is already in preferred backend ordering 146edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction)); 1470d0321e0SJeremy L Thompson } 1480d0321e0SJeremy L Thompson } 1490d0321e0SJeremy L Thompson } 1500d0321e0SJeremy L Thompson } 151ca735530SJeremy L Thompson if (skip_restriction) { 152ea61e9acSJeremy L Thompson // We do not need an E-Vector, but will use the input field vector's data directly in the operator application. 153004e4986SSebastian Grimberg e_vecs[i + start_e] = NULL; 1540d0321e0SJeremy L Thompson } else { 155004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e])); 1560d0321e0SJeremy L Thompson } 1570d0321e0SJeremy L Thompson } 1580d0321e0SJeremy L Thompson 159004e4986SSebastian Grimberg switch (eval_mode) { 1600d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 161ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 162ca735530SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 163ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 1640d0321e0SJeremy L Thompson break; 1650d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 1660d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 167004e4986SSebastian Grimberg case CEED_EVAL_DIV: 168004e4986SSebastian Grimberg case CEED_EVAL_CURL: 169ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 170ca735530SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 171ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 1720d0321e0SJeremy L Thompson break; 1730d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: // Only on input fields 174ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 175ca735530SJeremy L Thompson q_size = (CeedSize)num_elem * Q; 176ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 177756ca9e9SJeremy L Thompson if (is_at_points) { 178756ca9e9SJeremy L Thompson CeedInt num_points[num_elem]; 179756ca9e9SJeremy L Thompson 180756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q; 181756ca9e9SJeremy L Thompson CeedCallBackend( 182756ca9e9SJeremy L Thompson CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i])); 183756ca9e9SJeremy L Thompson } else { 184ca735530SJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i])); 185756ca9e9SJeremy L Thompson } 1860d0321e0SJeremy L Thompson break; 1870d0321e0SJeremy L Thompson } 1880d0321e0SJeremy L Thompson } 189f8a0df59SJeremy L Thompson // Drop duplicate restrictions 1903aab95c0SJeremy L Thompson if (is_input) { 1913aab95c0SJeremy L Thompson for (CeedInt i = 0; i < num_fields; i++) { 1923aab95c0SJeremy L Thompson CeedVector vec_i; 1933aab95c0SJeremy L Thompson CeedElemRestriction rstr_i; 1943aab95c0SJeremy L Thompson 1953aab95c0SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i)); 1963aab95c0SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i)); 1973aab95c0SJeremy L Thompson for (CeedInt j = i + 1; j < num_fields; j++) { 1983aab95c0SJeremy L Thompson CeedVector vec_j; 1993aab95c0SJeremy L Thompson CeedElemRestriction rstr_j; 2003aab95c0SJeremy L Thompson 2013aab95c0SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j)); 2023aab95c0SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j)); 2033aab95c0SJeremy L Thompson if (vec_i == vec_j && rstr_i == rstr_j) { 204f8a0df59SJeremy L Thompson CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e])); 2053aab95c0SJeremy L Thompson skip_rstr[j] = true; 2063aab95c0SJeremy L Thompson } 2073aab95c0SJeremy L Thompson } 2083aab95c0SJeremy L Thompson } 209f8a0df59SJeremy L Thompson } else { 210f8a0df59SJeremy L Thompson for (CeedInt i = num_fields - 1; i >= 0; i--) { 211f8a0df59SJeremy L Thompson CeedVector vec_i; 212f8a0df59SJeremy L Thompson CeedElemRestriction rstr_i; 213f8a0df59SJeremy L Thompson 214f8a0df59SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i)); 215f8a0df59SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i)); 216f8a0df59SJeremy L Thompson for (CeedInt j = i - 1; j >= 0; j--) { 217f8a0df59SJeremy L Thompson CeedVector vec_j; 218f8a0df59SJeremy L Thompson CeedElemRestriction rstr_j; 219f8a0df59SJeremy L Thompson 220f8a0df59SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j)); 221f8a0df59SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j)); 222f8a0df59SJeremy L Thompson if (vec_i == vec_j && rstr_i == rstr_j) { 223f8a0df59SJeremy L Thompson CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e])); 224f8a0df59SJeremy L Thompson skip_rstr[j] = true; 225f8a0df59SJeremy L Thompson apply_add_basis[i] = true; 226f8a0df59SJeremy L Thompson } 227f8a0df59SJeremy L Thompson } 228f8a0df59SJeremy L Thompson } 2293aab95c0SJeremy L Thompson } 2300d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2310d0321e0SJeremy L Thompson } 2320d0321e0SJeremy L Thompson 2330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 234ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. 2350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2360d0321e0SJeremy L Thompson static int CeedOperatorSetup_Cuda(CeedOperator op) { 2370d0321e0SJeremy L Thompson Ceed ceed; 238ca735530SJeremy L Thompson bool is_setup_done; 239ca735530SJeremy L Thompson CeedInt Q, num_elem, num_input_fields, num_output_fields; 240ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 2410d0321e0SJeremy L Thompson CeedQFunction qf; 242ca735530SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 243ca735530SJeremy L Thompson CeedOperator_Cuda *impl; 244ca735530SJeremy L Thompson 245ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 246ca735530SJeremy L Thompson if (is_setup_done) return CEED_ERROR_SUCCESS; 247ca735530SJeremy L Thompson 248ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 249ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 2502b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 2512b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 252ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 253ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 254ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 2550d0321e0SJeremy L Thompson 2560d0321e0SJeremy L Thompson // Allocate 257ca735530SJeremy L Thompson CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs)); 2583aab95c0SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in)); 259f8a0df59SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out)); 260f8a0df59SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out)); 261c1222711SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states)); 262ca735530SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in)); 263ca735530SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out)); 264ca735530SJeremy L Thompson impl->num_inputs = num_input_fields; 265ca735530SJeremy L Thompson impl->num_outputs = num_output_fields; 2660d0321e0SJeremy L Thompson 267*41655a23SJeremy L Thompson // Set up infield and outfield e-vecs and q-vecs 2680d0321e0SJeremy L Thompson // Infields 2693aab95c0SJeremy L Thompson CeedCallBackend( 270f8a0df59SJeremy L Thompson CeedOperatorSetupFields_Cuda(qf, op, true, false, impl->skip_rstr_in, NULL, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem)); 2710d0321e0SJeremy L Thompson // Outfields 272f8a0df59SJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, false, false, impl->skip_rstr_out, impl->apply_add_basis_out, impl->e_vecs, impl->q_vecs_out, 273f8a0df59SJeremy L Thompson num_input_fields, num_output_fields, Q, num_elem)); 2740d0321e0SJeremy L Thompson 275*41655a23SJeremy L Thompson // Reuse active e-vecs where able 276*41655a23SJeremy L Thompson { 277*41655a23SJeremy L Thompson CeedInt num_used = 0; 278*41655a23SJeremy L Thompson CeedElemRestriction *rstr_used = NULL; 279*41655a23SJeremy L Thompson 280*41655a23SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 281*41655a23SJeremy L Thompson bool is_used = false; 282*41655a23SJeremy L Thompson CeedVector vec_i; 283*41655a23SJeremy L Thompson CeedElemRestriction rstr_i; 284*41655a23SJeremy L Thompson 285*41655a23SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i)); 286*41655a23SJeremy L Thompson if (vec_i != CEED_VECTOR_ACTIVE) continue; 287*41655a23SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i)); 288*41655a23SJeremy L Thompson for (CeedInt j = 0; j < num_used; j++) { 289*41655a23SJeremy L Thompson if (rstr_i == rstr_used[i]) is_used = true; 290*41655a23SJeremy L Thompson } 291*41655a23SJeremy L Thompson if (is_used) continue; 292*41655a23SJeremy L Thompson num_used++; 293*41655a23SJeremy L Thompson if (num_used == 1) CeedCallBackend(CeedCalloc(num_used, &rstr_used)); 294*41655a23SJeremy L Thompson else CeedCallBackend(CeedRealloc(num_used, &rstr_used)); 295*41655a23SJeremy L Thompson rstr_used[num_used - 1] = rstr_i; 296*41655a23SJeremy L Thompson for (CeedInt j = num_output_fields - 1; j >= 0; j--) { 297*41655a23SJeremy L Thompson CeedEvalMode eval_mode; 298*41655a23SJeremy L Thompson CeedVector vec_j; 299*41655a23SJeremy L Thompson CeedElemRestriction rstr_j; 300*41655a23SJeremy L Thompson 301*41655a23SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec_j)); 302*41655a23SJeremy L Thompson if (vec_j != CEED_VECTOR_ACTIVE) continue; 303*41655a23SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode)); 304*41655a23SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) continue; 305*41655a23SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &rstr_j)); 306*41655a23SJeremy L Thompson if (rstr_i == rstr_j) { 307*41655a23SJeremy L Thompson CeedCallBackend(CeedVectorReferenceCopy(impl->e_vecs[i], &impl->e_vecs[j + impl->num_inputs])); 308*41655a23SJeremy L Thompson } 309*41655a23SJeremy L Thompson } 310*41655a23SJeremy L Thompson } 311*41655a23SJeremy L Thompson CeedCallBackend(CeedFree(&rstr_used)); 312*41655a23SJeremy L Thompson } 3132b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 3140d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3150d0321e0SJeremy L Thompson } 3160d0321e0SJeremy L Thompson 3170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3180d0321e0SJeremy L Thompson // Setup Operator Inputs 3190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 320ca735530SJeremy L Thompson static inline int CeedOperatorSetupInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 321004e4986SSebastian Grimberg CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], 3220d0321e0SJeremy L Thompson CeedOperator_Cuda *impl, CeedRequest *request) { 323ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 324004e4986SSebastian Grimberg CeedEvalMode eval_mode; 3250d0321e0SJeremy L Thompson CeedVector vec; 326edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 3270d0321e0SJeremy L Thompson 3280d0321e0SJeremy L Thompson // Get input vector 329ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 3300d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 331004e4986SSebastian Grimberg if (skip_active) continue; 332ca735530SJeremy L Thompson else vec = in_vec; 3330d0321e0SJeremy L Thompson } 3340d0321e0SJeremy L Thompson 335004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 336004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 3370d0321e0SJeremy L Thompson } else { 338004e4986SSebastian Grimberg // Get input vector 339004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 3400d0321e0SJeremy L Thompson // Get input element restriction 341edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 342ca735530SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = in_vec; 3430d0321e0SJeremy L Thompson // Restrict, if necessary 344ca735530SJeremy L Thompson if (!impl->e_vecs[i]) { 3450d0321e0SJeremy L Thompson // No restriction for this field; read data directly from vec. 346ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i])); 3470d0321e0SJeremy L Thompson } else { 348c1222711SJeremy L Thompson uint64_t state; 349c1222711SJeremy L Thompson 350c1222711SJeremy L Thompson CeedCallBackend(CeedVectorGetState(vec, &state)); 351*41655a23SJeremy L Thompson if ((state != impl->input_states[i] || vec == in_vec) && !impl->skip_rstr_in[i]) { 352edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request)); 353c1222711SJeremy L Thompson } 3543aab95c0SJeremy L Thompson impl->input_states[i] = state; 3550d0321e0SJeremy L Thompson // Get evec 356ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i])); 3570d0321e0SJeremy L Thompson } 3580d0321e0SJeremy L Thompson } 3590d0321e0SJeremy L Thompson } 3600d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3610d0321e0SJeremy L Thompson } 3620d0321e0SJeremy L Thompson 3630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3640d0321e0SJeremy L Thompson // Input Basis Action 3650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 366ca735530SJeremy L Thompson static inline int CeedOperatorInputBasis_Cuda(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 367004e4986SSebastian Grimberg CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], 3682b730f8bSJeremy L Thompson CeedOperator_Cuda *impl) { 369ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 370ca735530SJeremy L Thompson CeedInt elem_size, size; 371004e4986SSebastian Grimberg CeedEvalMode eval_mode; 372edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 3730d0321e0SJeremy L Thompson CeedBasis basis; 3740d0321e0SJeremy L Thompson 3750d0321e0SJeremy L Thompson // Skip active input 376004e4986SSebastian Grimberg if (skip_active) { 3770d0321e0SJeremy L Thompson CeedVector vec; 378ca735530SJeremy L Thompson 379ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 3802b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 3810d0321e0SJeremy L Thompson } 382004e4986SSebastian Grimberg // Get elem_size, eval_mode, size 383edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 384edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 385004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 386ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 3870d0321e0SJeremy L Thompson // Basis action 388004e4986SSebastian Grimberg switch (eval_mode) { 3890d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 390ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i])); 3910d0321e0SJeremy L Thompson break; 3920d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 3930d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 394004e4986SSebastian Grimberg case CEED_EVAL_DIV: 395004e4986SSebastian Grimberg case CEED_EVAL_CURL: 396ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 397004e4986SSebastian Grimberg CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i])); 3980d0321e0SJeremy L Thompson break; 3990d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: 4000d0321e0SJeremy L Thompson break; // No action 4010d0321e0SJeremy L Thompson } 4020d0321e0SJeremy L Thompson } 4030d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4040d0321e0SJeremy L Thompson } 4050d0321e0SJeremy L Thompson 4060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4070d0321e0SJeremy L Thompson // Restore Input Vectors 4080d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 409ca735530SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 410004e4986SSebastian Grimberg const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) { 411ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 412004e4986SSebastian Grimberg CeedEvalMode eval_mode; 4130d0321e0SJeremy L Thompson CeedVector vec; 4140d0321e0SJeremy L Thompson 4150d0321e0SJeremy L Thompson // Skip active input 416004e4986SSebastian Grimberg if (skip_active) { 417ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 4182b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 4190d0321e0SJeremy L Thompson } 420004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 421004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 4220d0321e0SJeremy L Thompson } else { 423ca735530SJeremy L Thompson if (!impl->e_vecs[i]) { // This was a skip_restriction case 424ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 425ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i])); 4260d0321e0SJeremy L Thompson } else { 427ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i])); 4280d0321e0SJeremy L Thompson } 4290d0321e0SJeremy L Thompson } 4300d0321e0SJeremy L Thompson } 4310d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4320d0321e0SJeremy L Thompson } 4330d0321e0SJeremy L Thompson 4340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4350d0321e0SJeremy L Thompson // Apply and add to output 4360d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 437ca735530SJeremy L Thompson static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) { 438ca735530SJeremy L Thompson CeedInt Q, num_elem, elem_size, num_input_fields, num_output_fields, size; 439ca735530SJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX] = {NULL}; 440ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 4410d0321e0SJeremy L Thompson CeedQFunction qf; 442004e4986SSebastian Grimberg CeedOperatorField *op_input_fields, *op_output_fields; 443004e4986SSebastian Grimberg CeedOperator_Cuda *impl; 444ca735530SJeremy L Thompson 445ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 4462b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 4472b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 448ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 449ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 450ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 4510d0321e0SJeremy L Thompson 4520d0321e0SJeremy L Thompson // Setup 4532b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetup_Cuda(op)); 4540d0321e0SJeremy L Thompson 455004e4986SSebastian Grimberg // Input Evecs and Restriction 456ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request)); 4570d0321e0SJeremy L Thompson 4580d0321e0SJeremy L Thompson // Input basis apply if needed 459ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl)); 4600d0321e0SJeremy L Thompson 4610d0321e0SJeremy L Thompson // Output pointers, as necessary 462ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 463004e4986SSebastian Grimberg CeedEvalMode eval_mode; 464004e4986SSebastian Grimberg 465004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 466004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 4670d0321e0SJeremy L Thompson // Set the output Q-Vector to use the E-Vector data directly. 468ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields])); 469ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields])); 4700d0321e0SJeremy L Thompson } 4710d0321e0SJeremy L Thompson } 4720d0321e0SJeremy L Thompson 4730d0321e0SJeremy L Thompson // Q function 474ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out)); 4750d0321e0SJeremy L Thompson 476*41655a23SJeremy L Thompson // Restore input arrays 477*41655a23SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl)); 478*41655a23SJeremy L Thompson 4790d0321e0SJeremy L Thompson // Output basis apply if needed 480ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 481004e4986SSebastian Grimberg CeedEvalMode eval_mode; 482edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 483ca735530SJeremy L Thompson CeedBasis basis; 484ca735530SJeremy L Thompson 485004e4986SSebastian Grimberg // Get elem_size, eval_mode, size 486edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 487edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 488004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 489ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 4900d0321e0SJeremy L Thompson // Basis action 491004e4986SSebastian Grimberg switch (eval_mode) { 4920d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 493004e4986SSebastian Grimberg break; // No action 4940d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 4950d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 496004e4986SSebastian Grimberg case CEED_EVAL_DIV: 497004e4986SSebastian Grimberg case CEED_EVAL_CURL: 498ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 499f8a0df59SJeremy L Thompson if (impl->apply_add_basis_out[i]) { 500f8a0df59SJeremy L Thompson CeedCallBackend(CeedBasisApplyAdd(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs])); 501f8a0df59SJeremy L Thompson } else { 502004e4986SSebastian Grimberg CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs])); 503f8a0df59SJeremy L Thompson } 5040d0321e0SJeremy L Thompson break; 5050d0321e0SJeremy L Thompson // LCOV_EXCL_START 5060d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: { 5076e536b99SJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 5080d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 5090d0321e0SJeremy L Thompson } 5100d0321e0SJeremy L Thompson } 511004e4986SSebastian Grimberg } 5120d0321e0SJeremy L Thompson 5130d0321e0SJeremy L Thompson // Output restriction 514ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 515004e4986SSebastian Grimberg CeedEvalMode eval_mode; 516ca735530SJeremy L Thompson CeedVector vec; 517edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 518ca735530SJeremy L Thompson 5190d0321e0SJeremy L Thompson // Restore evec 520004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 521004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 522ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields])); 5230d0321e0SJeremy L Thompson } 524f8a0df59SJeremy L Thompson if (impl->skip_rstr_out[i]) continue; 5250d0321e0SJeremy L Thompson // Get output vector 526ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 5270d0321e0SJeremy L Thompson // Restrict 528edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 5290d0321e0SJeremy L Thompson // Active 530ca735530SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = out_vec; 5310d0321e0SJeremy L Thompson 532edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request)); 5330d0321e0SJeremy L Thompson } 5340d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5350d0321e0SJeremy L Thompson } 5360d0321e0SJeremy L Thompson 5370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 538756ca9e9SJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. 539756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 540756ca9e9SJeremy L Thompson static int CeedOperatorSetupAtPoints_Cuda(CeedOperator op) { 541756ca9e9SJeremy L Thompson Ceed ceed; 542756ca9e9SJeremy L Thompson bool is_setup_done; 543756ca9e9SJeremy L Thompson CeedInt max_num_points = -1, num_elem, num_input_fields, num_output_fields; 544756ca9e9SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 545756ca9e9SJeremy L Thompson CeedQFunction qf; 546756ca9e9SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 547756ca9e9SJeremy L Thompson CeedOperator_Cuda *impl; 548756ca9e9SJeremy L Thompson 549756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 550756ca9e9SJeremy L Thompson if (is_setup_done) return CEED_ERROR_SUCCESS; 551756ca9e9SJeremy L Thompson 552756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 553756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 554756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 555756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 556756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 557756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 558756ca9e9SJeremy L Thompson { 559756ca9e9SJeremy L Thompson CeedElemRestriction elem_rstr = NULL; 560756ca9e9SJeremy L Thompson 561756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL)); 562756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points)); 563756ca9e9SJeremy L Thompson } 564756ca9e9SJeremy L Thompson impl->max_num_points = max_num_points; 565756ca9e9SJeremy L Thompson 566756ca9e9SJeremy L Thompson // Allocate 567756ca9e9SJeremy L Thompson CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs)); 5683aab95c0SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in)); 569f8a0df59SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out)); 570f8a0df59SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out)); 571756ca9e9SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states)); 572756ca9e9SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in)); 573756ca9e9SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out)); 574756ca9e9SJeremy L Thompson impl->num_inputs = num_input_fields; 575756ca9e9SJeremy L Thompson impl->num_outputs = num_output_fields; 576756ca9e9SJeremy L Thompson 577756ca9e9SJeremy L Thompson // Set up infield and outfield e_vecs and q_vecs 578756ca9e9SJeremy L Thompson // Infields 579f8a0df59SJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, true, true, impl->skip_rstr_in, NULL, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, 5803aab95c0SJeremy L Thompson max_num_points, num_elem)); 581756ca9e9SJeremy L Thompson // Outfields 582f8a0df59SJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, false, true, impl->skip_rstr_out, impl->apply_add_basis_out, impl->e_vecs, impl->q_vecs_out, 583f8a0df59SJeremy L Thompson num_input_fields, num_output_fields, max_num_points, num_elem)); 584756ca9e9SJeremy L Thompson 585756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 586756ca9e9SJeremy L Thompson return CEED_ERROR_SUCCESS; 587756ca9e9SJeremy L Thompson } 588756ca9e9SJeremy L Thompson 589756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 59067d9480aSJeremy L Thompson // Input Basis Action AtPoints 591756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 592756ca9e9SJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Cuda(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields, 593756ca9e9SJeremy L Thompson CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active, 594756ca9e9SJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) { 595756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 596756ca9e9SJeremy L Thompson CeedInt elem_size, size; 597756ca9e9SJeremy L Thompson CeedEvalMode eval_mode; 598756ca9e9SJeremy L Thompson CeedElemRestriction elem_rstr; 599756ca9e9SJeremy L Thompson CeedBasis basis; 600756ca9e9SJeremy L Thompson 601756ca9e9SJeremy L Thompson // Skip active input 602756ca9e9SJeremy L Thompson if (skip_active) { 603756ca9e9SJeremy L Thompson CeedVector vec; 604756ca9e9SJeremy L Thompson 605756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 606756ca9e9SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 607756ca9e9SJeremy L Thompson } 608756ca9e9SJeremy L Thompson // Get elem_size, eval_mode, size 609756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 610756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 611756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 612756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 613756ca9e9SJeremy L Thompson // Basis action 614756ca9e9SJeremy L Thompson switch (eval_mode) { 615756ca9e9SJeremy L Thompson case CEED_EVAL_NONE: 616756ca9e9SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i])); 617756ca9e9SJeremy L Thompson break; 618756ca9e9SJeremy L Thompson case CEED_EVAL_INTERP: 619756ca9e9SJeremy L Thompson case CEED_EVAL_GRAD: 620756ca9e9SJeremy L Thompson case CEED_EVAL_DIV: 621756ca9e9SJeremy L Thompson case CEED_EVAL_CURL: 622756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 623756ca9e9SJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i], 624756ca9e9SJeremy L Thompson impl->q_vecs_in[i])); 625756ca9e9SJeremy L Thompson break; 626756ca9e9SJeremy L Thompson case CEED_EVAL_WEIGHT: 627756ca9e9SJeremy L Thompson break; // No action 628756ca9e9SJeremy L Thompson } 629756ca9e9SJeremy L Thompson } 630756ca9e9SJeremy L Thompson return CEED_ERROR_SUCCESS; 631756ca9e9SJeremy L Thompson } 632756ca9e9SJeremy L Thompson 633756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 63467d9480aSJeremy L Thompson // Apply and add to output AtPoints 635756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 636756ca9e9SJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) { 637756ca9e9SJeremy L Thompson CeedInt max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size; 638756ca9e9SJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX] = {NULL}; 639756ca9e9SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 640756ca9e9SJeremy L Thompson CeedQFunction qf; 641756ca9e9SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 642756ca9e9SJeremy L Thompson CeedOperator_Cuda *impl; 643756ca9e9SJeremy L Thompson 644756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 645756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 646756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 647756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 648756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 649756ca9e9SJeremy L Thompson CeedInt num_points[num_elem]; 650756ca9e9SJeremy L Thompson 651756ca9e9SJeremy L Thompson // Setup 652756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op)); 653756ca9e9SJeremy L Thompson max_num_points = impl->max_num_points; 654756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points; 655756ca9e9SJeremy L Thompson 656756ca9e9SJeremy L Thompson // Input Evecs and Restriction 657756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request)); 658756ca9e9SJeremy L Thompson 659756ca9e9SJeremy L Thompson // Get point coordinates 660756ca9e9SJeremy L Thompson if (!impl->point_coords_elem) { 661756ca9e9SJeremy L Thompson CeedVector point_coords = NULL; 662756ca9e9SJeremy L Thompson CeedElemRestriction rstr_points = NULL; 663756ca9e9SJeremy L Thompson 664756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords)); 665756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem)); 666756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request)); 667756ca9e9SJeremy L Thompson } 668756ca9e9SJeremy L Thompson 669756ca9e9SJeremy L Thompson // Input basis apply if needed 670756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl)); 671756ca9e9SJeremy L Thompson 672756ca9e9SJeremy L Thompson // Output pointers, as necessary 673756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 674756ca9e9SJeremy L Thompson CeedEvalMode eval_mode; 675756ca9e9SJeremy L Thompson 676756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 677756ca9e9SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) { 678756ca9e9SJeremy L Thompson // Set the output Q-Vector to use the E-Vector data directly. 679756ca9e9SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields])); 680756ca9e9SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields])); 681756ca9e9SJeremy L Thompson } 682756ca9e9SJeremy L Thompson } 683756ca9e9SJeremy L Thompson 684756ca9e9SJeremy L Thompson // Q function 685756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out)); 686756ca9e9SJeremy L Thompson 687756ca9e9SJeremy L Thompson // Output basis apply if needed 688756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 689756ca9e9SJeremy L Thompson CeedEvalMode eval_mode; 690756ca9e9SJeremy L Thompson CeedElemRestriction elem_rstr; 691756ca9e9SJeremy L Thompson CeedBasis basis; 692756ca9e9SJeremy L Thompson 693756ca9e9SJeremy L Thompson // Get elem_size, eval_mode, size 694756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 695756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 696756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 697756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 698756ca9e9SJeremy L Thompson // Basis action 699756ca9e9SJeremy L Thompson switch (eval_mode) { 700756ca9e9SJeremy L Thompson case CEED_EVAL_NONE: 701756ca9e9SJeremy L Thompson break; // No action 702756ca9e9SJeremy L Thompson case CEED_EVAL_INTERP: 703756ca9e9SJeremy L Thompson case CEED_EVAL_GRAD: 704756ca9e9SJeremy L Thompson case CEED_EVAL_DIV: 705756ca9e9SJeremy L Thompson case CEED_EVAL_CURL: 706756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 707f8a0df59SJeremy L Thompson if (impl->apply_add_basis_out[i]) { 708f8a0df59SJeremy L Thompson CeedCallBackend(CeedBasisApplyAddAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, 709f8a0df59SJeremy L Thompson impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs])); 710f8a0df59SJeremy L Thompson } else { 711756ca9e9SJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i], 712756ca9e9SJeremy L Thompson impl->e_vecs[i + impl->num_inputs])); 713f8a0df59SJeremy L Thompson } 714756ca9e9SJeremy L Thompson break; 715756ca9e9SJeremy L Thompson // LCOV_EXCL_START 716756ca9e9SJeremy L Thompson case CEED_EVAL_WEIGHT: { 717756ca9e9SJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 718756ca9e9SJeremy L Thompson // LCOV_EXCL_STOP 719756ca9e9SJeremy L Thompson } 720756ca9e9SJeremy L Thompson } 721756ca9e9SJeremy L Thompson } 722756ca9e9SJeremy L Thompson 723756ca9e9SJeremy L Thompson // Output restriction 724756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 725756ca9e9SJeremy L Thompson CeedEvalMode eval_mode; 726756ca9e9SJeremy L Thompson CeedVector vec; 727756ca9e9SJeremy L Thompson CeedElemRestriction elem_rstr; 728756ca9e9SJeremy L Thompson 729756ca9e9SJeremy L Thompson // Restore evec 730756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 731756ca9e9SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) { 732756ca9e9SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields])); 733756ca9e9SJeremy L Thompson } 734f8a0df59SJeremy L Thompson if (impl->skip_rstr_out[i]) continue; 735756ca9e9SJeremy L Thompson // Get output vector 736756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 737756ca9e9SJeremy L Thompson // Restrict 738756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 739756ca9e9SJeremy L Thompson // Active 740756ca9e9SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = out_vec; 741756ca9e9SJeremy L Thompson 742756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request)); 743756ca9e9SJeremy L Thompson } 744756ca9e9SJeremy L Thompson 745756ca9e9SJeremy L Thompson // Restore input arrays 746756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl)); 747756ca9e9SJeremy L Thompson return CEED_ERROR_SUCCESS; 748756ca9e9SJeremy L Thompson } 749756ca9e9SJeremy L Thompson 750756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 751004e4986SSebastian Grimberg // Linear QFunction Assembly Core 7520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7532b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr, 7540d0321e0SJeremy L Thompson CeedRequest *request) { 755ca735530SJeremy L Thompson Ceed ceed, ceed_parent; 756ca735530SJeremy L Thompson CeedInt num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size; 757ca735530SJeremy L Thompson CeedScalar *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL}; 758ca735530SJeremy L Thompson CeedVector *active_inputs; 759ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 760ca735530SJeremy L Thompson CeedQFunction qf; 761ca735530SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 762ca735530SJeremy L Thompson CeedOperator_Cuda *impl; 763ca735530SJeremy L Thompson 7642b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 765ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); 766e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 767e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 768ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 769e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 770ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 771ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 772ca735530SJeremy L Thompson active_inputs = impl->qf_active_in; 773ca735530SJeremy L Thompson num_active_in = impl->num_active_in, num_active_out = impl->num_active_out; 7740d0321e0SJeremy L Thompson 7750d0321e0SJeremy L Thompson // Setup 7762b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetup_Cuda(op)); 7770d0321e0SJeremy L Thompson 778004e4986SSebastian Grimberg // Input Evecs and Restriction 779ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request)); 7800d0321e0SJeremy L Thompson 7810d0321e0SJeremy L Thompson // Count number of active input fields 782ca735530SJeremy L Thompson if (!num_active_in) { 783ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 784ca735530SJeremy L Thompson CeedScalar *q_vec_array; 785ca735530SJeremy L Thompson CeedVector vec; 786ca735530SJeremy L Thompson 7870d0321e0SJeremy L Thompson // Get input vector 788ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 7890d0321e0SJeremy L Thompson // Check if active input 7900d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 791ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 792ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0)); 793ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array)); 794ca735530SJeremy L Thompson CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs)); 7950d0321e0SJeremy L Thompson for (CeedInt field = 0; field < size; field++) { 796004e4986SSebastian Grimberg CeedSize q_size = (CeedSize)Q * num_elem; 797004e4986SSebastian Grimberg 798ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field])); 799ca735530SJeremy L Thompson CeedCallBackend( 800ca735530SJeremy L Thompson CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem])); 8010d0321e0SJeremy L Thompson } 802ca735530SJeremy L Thompson num_active_in += size; 803ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array)); 8040d0321e0SJeremy L Thompson } 8050d0321e0SJeremy L Thompson } 806ca735530SJeremy L Thompson impl->num_active_in = num_active_in; 807ca735530SJeremy L Thompson impl->qf_active_in = active_inputs; 8080d0321e0SJeremy L Thompson } 8090d0321e0SJeremy L Thompson 8100d0321e0SJeremy L Thompson // Count number of active output fields 811ca735530SJeremy L Thompson if (!num_active_out) { 812ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 813ca735530SJeremy L Thompson CeedVector vec; 814ca735530SJeremy L Thompson 8150d0321e0SJeremy L Thompson // Get output vector 816ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 8170d0321e0SJeremy L Thompson // Check if active output 8180d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 819ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 820ca735530SJeremy L Thompson num_active_out += size; 8210d0321e0SJeremy L Thompson } 8220d0321e0SJeremy L Thompson } 823ca735530SJeremy L Thompson impl->num_active_out = num_active_out; 8240d0321e0SJeremy L Thompson } 8250d0321e0SJeremy L Thompson 8260d0321e0SJeremy L Thompson // Check sizes 827ca735530SJeremy L Thompson CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); 8280d0321e0SJeremy L Thompson 8290d0321e0SJeremy L Thompson // Build objects if needed 8300d0321e0SJeremy L Thompson if (build_objects) { 831004e4986SSebastian Grimberg CeedSize l_size = (CeedSize)num_elem * Q * num_active_in * num_active_out; 832ca735530SJeremy L Thompson CeedInt strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */ 833004e4986SSebastian Grimberg 834004e4986SSebastian Grimberg // Create output restriction 835ca735530SJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out, 8360a5597ceSJeremy L Thompson (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides, 8370a5597ceSJeremy L Thompson rstr)); 8380d0321e0SJeremy L Thompson // Create assembled vector 839ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled)); 8400d0321e0SJeremy L Thompson } 8412b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(*assembled, 0.0)); 842ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array)); 8430d0321e0SJeremy L Thompson 8440d0321e0SJeremy L Thompson // Input basis apply 845ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl)); 8460d0321e0SJeremy L Thompson 8470d0321e0SJeremy L Thompson // Assemble QFunction 848ca735530SJeremy L Thompson for (CeedInt in = 0; in < num_active_in; in++) { 8490d0321e0SJeremy L Thompson // Set Inputs 850ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0)); 851ca735530SJeremy L Thompson if (num_active_in > 1) { 852ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0)); 8530d0321e0SJeremy L Thompson } 8540d0321e0SJeremy L Thompson // Set Outputs 855ca735530SJeremy L Thompson for (CeedInt out = 0; out < num_output_fields; out++) { 856ca735530SJeremy L Thompson CeedVector vec; 857ca735530SJeremy L Thompson 8580d0321e0SJeremy L Thompson // Get output vector 859ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec)); 8600d0321e0SJeremy L Thompson // Check if active output 8610d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 862ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array)); 863ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size)); 864ca735530SJeremy L Thompson assembled_array += size * Q * num_elem; // Advance the pointer by the size of the output 8650d0321e0SJeremy L Thompson } 8660d0321e0SJeremy L Thompson } 8670d0321e0SJeremy L Thompson // Apply QFunction 868ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out)); 8690d0321e0SJeremy L Thompson } 8700d0321e0SJeremy L Thompson 871ca735530SJeremy L Thompson // Un-set output q_vecs to prevent accidental overwrite of Assembled 872ca735530SJeremy L Thompson for (CeedInt out = 0; out < num_output_fields; out++) { 873ca735530SJeremy L Thompson CeedVector vec; 874ca735530SJeremy L Thompson 8750d0321e0SJeremy L Thompson // Get output vector 876ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec)); 8770d0321e0SJeremy L Thompson // Check if active output 8780d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 879ca735530SJeremy L Thompson CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL)); 8800d0321e0SJeremy L Thompson } 8810d0321e0SJeremy L Thompson } 8820d0321e0SJeremy L Thompson 8830d0321e0SJeremy L Thompson // Restore input arrays 884ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl)); 8850d0321e0SJeremy L Thompson 8860d0321e0SJeremy L Thompson // Restore output 887ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); 8880d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8890d0321e0SJeremy L Thompson } 8900d0321e0SJeremy L Thompson 8910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 8920d0321e0SJeremy L Thompson // Assemble Linear QFunction 8930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 8942b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 8952b730f8bSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, true, assembled, rstr, request); 8960d0321e0SJeremy L Thompson } 8970d0321e0SJeremy L Thompson 8980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 8990d0321e0SJeremy L Thompson // Update Assembled Linear QFunction 9000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 9012b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) { 9022b730f8bSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, false, &assembled, &rstr, request); 9030d0321e0SJeremy L Thompson } 9040d0321e0SJeremy L Thompson 9050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 906004e4986SSebastian Grimberg // Assemble Diagonal Setup 9070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 908cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op) { 9090d0321e0SJeremy L Thompson Ceed ceed; 910004e4986SSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 911cbfe683aSSebastian Grimberg CeedInt q_comp, num_nodes, num_qpts; 912004e4986SSebastian Grimberg CeedEvalMode *eval_modes_in = NULL, *eval_modes_out = NULL; 913ca735530SJeremy L Thompson CeedBasis basis_in = NULL, basis_out = NULL; 914ca735530SJeremy L Thompson CeedQFunctionField *qf_fields; 9150d0321e0SJeremy L Thompson CeedQFunction qf; 916ca735530SJeremy L Thompson CeedOperatorField *op_fields; 917ca735530SJeremy L Thompson CeedOperator_Cuda *impl; 918ca735530SJeremy L Thompson 919ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 9202b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 921ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields)); 9220d0321e0SJeremy L Thompson 9230d0321e0SJeremy L Thompson // Determine active input basis 924ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 925ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 926ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 9270d0321e0SJeremy L Thompson CeedVector vec; 928ca735530SJeremy L Thompson 929ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 9300d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 931004e4986SSebastian Grimberg CeedBasis basis; 932004e4986SSebastian Grimberg CeedEvalMode eval_mode; 933ca735530SJeremy L Thompson 934004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 935004e4986SSebastian Grimberg CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, 936004e4986SSebastian Grimberg "Backend does not implement operator diagonal assembly with multiple active bases"); 937004e4986SSebastian Grimberg basis_in = basis; 938004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 939004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 940004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 941004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly 942004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in)); 943004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode; 944004e4986SSebastian Grimberg num_eval_modes_in += q_comp; 9450d0321e0SJeremy L Thompson } 9460d0321e0SJeremy L Thompson } 9470d0321e0SJeremy L Thompson } 9480d0321e0SJeremy L Thompson 9490d0321e0SJeremy L Thompson // Determine active output basis 950ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 951ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 952ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 9530d0321e0SJeremy L Thompson CeedVector vec; 954ca735530SJeremy L Thompson 955ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 9560d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 957004e4986SSebastian Grimberg CeedBasis basis; 958004e4986SSebastian Grimberg CeedEvalMode eval_mode; 959ca735530SJeremy L Thompson 960004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 961004e4986SSebastian Grimberg CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND, 962004e4986SSebastian Grimberg "Backend does not implement operator diagonal assembly with multiple active bases"); 963004e4986SSebastian Grimberg basis_out = basis; 964004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 965004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 966004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 967004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly 968004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out)); 969004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode; 970004e4986SSebastian Grimberg num_eval_modes_out += q_comp; 9710d0321e0SJeremy L Thompson } 9720d0321e0SJeremy L Thompson } 9730d0321e0SJeremy L Thompson } 9740d0321e0SJeremy L Thompson 9750d0321e0SJeremy L Thompson // Operator data struct 9762b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 9772b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl->diag)); 9780d0321e0SJeremy L Thompson CeedOperatorDiag_Cuda *diag = impl->diag; 979ca735530SJeremy L Thompson 980cbfe683aSSebastian Grimberg // Basis matrices 981004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes)); 982004e4986SSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes; 983004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts)); 984004e4986SSebastian Grimberg const CeedInt interp_bytes = num_nodes * num_qpts * sizeof(CeedScalar); 985004e4986SSebastian Grimberg const CeedInt eval_modes_bytes = sizeof(CeedEvalMode); 986004e4986SSebastian Grimberg bool has_eval_none = false; 9870d0321e0SJeremy L Thompson 9880d0321e0SJeremy L Thompson // CEED_EVAL_NONE 989004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE); 990004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE); 991004e4986SSebastian Grimberg if (has_eval_none) { 9920d0321e0SJeremy L Thompson CeedScalar *identity = NULL; 993ca735530SJeremy L Thompson 994004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity)); 995ca735530SJeremy L Thompson for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0; 996ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_identity, interp_bytes)); 997ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(diag->d_identity, identity, interp_bytes, cudaMemcpyHostToDevice)); 998004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 9990d0321e0SJeremy L Thompson } 10000d0321e0SJeremy L Thompson 1001004e4986SSebastian Grimberg // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL 1002004e4986SSebastian Grimberg for (CeedInt in = 0; in < 2; in++) { 1003004e4986SSebastian Grimberg CeedFESpace fespace; 1004004e4986SSebastian Grimberg CeedBasis basis = in ? basis_in : basis_out; 10050d0321e0SJeremy L Thompson 1006004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetFESpace(basis, &fespace)); 1007004e4986SSebastian Grimberg switch (fespace) { 1008004e4986SSebastian Grimberg case CEED_FE_SPACE_H1: { 1009004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_grad; 1010004e4986SSebastian Grimberg const CeedScalar *interp, *grad; 1011004e4986SSebastian Grimberg CeedScalar *d_interp, *d_grad; 10120d0321e0SJeremy L Thompson 1013004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 1014004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad)); 10150d0321e0SJeremy L Thompson 1016004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 1017004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 1018004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice)); 1019004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetGrad(basis, &grad)); 1020004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_grad, interp_bytes * q_comp_grad)); 1021004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_grad, grad, interp_bytes * q_comp_grad, cudaMemcpyHostToDevice)); 1022004e4986SSebastian Grimberg if (in) { 1023004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 1024004e4986SSebastian Grimberg diag->d_grad_in = d_grad; 1025004e4986SSebastian Grimberg } else { 1026004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 1027004e4986SSebastian Grimberg diag->d_grad_out = d_grad; 1028004e4986SSebastian Grimberg } 1029004e4986SSebastian Grimberg } break; 1030004e4986SSebastian Grimberg case CEED_FE_SPACE_HDIV: { 1031004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_div; 1032004e4986SSebastian Grimberg const CeedScalar *interp, *div; 1033004e4986SSebastian Grimberg CeedScalar *d_interp, *d_div; 1034004e4986SSebastian Grimberg 1035004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 1036004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div)); 1037004e4986SSebastian Grimberg 1038004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 1039004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 1040004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice)); 1041004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetDiv(basis, &div)); 1042004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_div, interp_bytes * q_comp_div)); 1043004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_div, div, interp_bytes * q_comp_div, cudaMemcpyHostToDevice)); 1044004e4986SSebastian Grimberg if (in) { 1045004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 1046004e4986SSebastian Grimberg diag->d_div_in = d_div; 1047004e4986SSebastian Grimberg } else { 1048004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 1049004e4986SSebastian Grimberg diag->d_div_out = d_div; 1050004e4986SSebastian Grimberg } 1051004e4986SSebastian Grimberg } break; 1052004e4986SSebastian Grimberg case CEED_FE_SPACE_HCURL: { 1053004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_curl; 1054004e4986SSebastian Grimberg const CeedScalar *interp, *curl; 1055004e4986SSebastian Grimberg CeedScalar *d_interp, *d_curl; 1056004e4986SSebastian Grimberg 1057004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 1058004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl)); 1059004e4986SSebastian Grimberg 1060004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 1061004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 1062004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice)); 1063004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetCurl(basis, &curl)); 1064004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_curl, interp_bytes * q_comp_curl)); 1065004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_curl, curl, interp_bytes * q_comp_curl, cudaMemcpyHostToDevice)); 1066004e4986SSebastian Grimberg if (in) { 1067004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 1068004e4986SSebastian Grimberg diag->d_curl_in = d_curl; 1069004e4986SSebastian Grimberg } else { 1070004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 1071004e4986SSebastian Grimberg diag->d_curl_out = d_curl; 1072004e4986SSebastian Grimberg } 1073004e4986SSebastian Grimberg } break; 1074004e4986SSebastian Grimberg } 1075004e4986SSebastian Grimberg } 1076004e4986SSebastian Grimberg 1077004e4986SSebastian Grimberg // Arrays of eval_modes 1078004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes)); 1079004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, cudaMemcpyHostToDevice)); 1080004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes)); 1081004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, cudaMemcpyHostToDevice)); 1082004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&eval_modes_in)); 1083004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&eval_modes_out)); 10840d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10850d0321e0SJeremy L Thompson } 10860d0321e0SJeremy L Thompson 10870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1088cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation) 1089cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------ 1090cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Cuda(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) { 1091cbfe683aSSebastian Grimberg Ceed ceed; 109222070f95SJeremy L Thompson char *diagonal_kernel_source; 109322070f95SJeremy L Thompson const char *diagonal_kernel_path; 1094cbfe683aSSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 1095cbfe683aSSebastian Grimberg CeedInt num_comp, q_comp, num_nodes, num_qpts; 1096cbfe683aSSebastian Grimberg CeedBasis basis_in = NULL, basis_out = NULL; 1097cbfe683aSSebastian Grimberg CeedQFunctionField *qf_fields; 1098cbfe683aSSebastian Grimberg CeedQFunction qf; 1099cbfe683aSSebastian Grimberg CeedOperatorField *op_fields; 1100cbfe683aSSebastian Grimberg CeedOperator_Cuda *impl; 1101cbfe683aSSebastian Grimberg 1102cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1103cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1104cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields)); 1105cbfe683aSSebastian Grimberg 1106cbfe683aSSebastian Grimberg // Determine active input basis 1107cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 1108cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 1109cbfe683aSSebastian Grimberg for (CeedInt i = 0; i < num_input_fields; i++) { 1110cbfe683aSSebastian Grimberg CeedVector vec; 1111cbfe683aSSebastian Grimberg 1112cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1113cbfe683aSSebastian Grimberg if (vec == CEED_VECTOR_ACTIVE) { 1114cbfe683aSSebastian Grimberg CeedEvalMode eval_mode; 1115cbfe683aSSebastian Grimberg 1116cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in)); 1117cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1118cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 1119cbfe683aSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1120cbfe683aSSebastian Grimberg num_eval_modes_in += q_comp; 1121cbfe683aSSebastian Grimberg } 1122cbfe683aSSebastian Grimberg } 1123cbfe683aSSebastian Grimberg } 1124cbfe683aSSebastian Grimberg 1125cbfe683aSSebastian Grimberg // Determine active output basis 1126cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 1127cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1128cbfe683aSSebastian Grimberg for (CeedInt i = 0; i < num_output_fields; i++) { 1129cbfe683aSSebastian Grimberg CeedVector vec; 1130cbfe683aSSebastian Grimberg 1131cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1132cbfe683aSSebastian Grimberg if (vec == CEED_VECTOR_ACTIVE) { 1133cbfe683aSSebastian Grimberg CeedEvalMode eval_mode; 1134cbfe683aSSebastian Grimberg 1135cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out)); 1136cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1137cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 1138cbfe683aSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1139cbfe683aSSebastian Grimberg num_eval_modes_out += q_comp; 1140cbfe683aSSebastian Grimberg } 1141cbfe683aSSebastian Grimberg } 1142cbfe683aSSebastian Grimberg } 1143cbfe683aSSebastian Grimberg 1144cbfe683aSSebastian Grimberg // Operator data struct 1145cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetData(op, &impl)); 1146cbfe683aSSebastian Grimberg CeedOperatorDiag_Cuda *diag = impl->diag; 1147cbfe683aSSebastian Grimberg 1148cbfe683aSSebastian Grimberg // Assemble kernel 1149cbfe683aSSebastian Grimberg CUmodule *module = is_point_block ? &diag->module_point_block : &diag->module; 1150cbfe683aSSebastian Grimberg CeedInt elems_per_block = 1; 1151cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes)); 1152cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp)); 1153cbfe683aSSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes; 1154cbfe683aSSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts)); 1155cbfe683aSSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble-diagonal.h", &diagonal_kernel_path)); 1156cbfe683aSSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n"); 1157cbfe683aSSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source)); 1158cbfe683aSSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n"); 1159cbfe683aSSebastian Grimberg CeedCallCuda(ceed, CeedCompile_Cuda(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT", 1160cbfe683aSSebastian Grimberg num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE", 1161cbfe683aSSebastian Grimberg use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block)); 1162cbfe683aSSebastian Grimberg CeedCallCuda(ceed, CeedGetKernel_Cuda(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal)); 1163cbfe683aSSebastian Grimberg CeedCallBackend(CeedFree(&diagonal_kernel_path)); 1164cbfe683aSSebastian Grimberg CeedCallBackend(CeedFree(&diagonal_kernel_source)); 1165cbfe683aSSebastian Grimberg return CEED_ERROR_SUCCESS; 1166cbfe683aSSebastian Grimberg } 1167cbfe683aSSebastian Grimberg 1168cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------ 1169004e4986SSebastian Grimberg // Assemble Diagonal Core 11700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1171ca735530SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) { 11720d0321e0SJeremy L Thompson Ceed ceed; 1173cbfe683aSSebastian Grimberg CeedInt num_elem, num_nodes; 1174ca735530SJeremy L Thompson CeedScalar *elem_diag_array; 1175ca735530SJeremy L Thompson const CeedScalar *assembled_qf_array; 1176004e4986SSebastian Grimberg CeedVector assembled_qf = NULL, elem_diag; 1177004e4986SSebastian Grimberg CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr; 11780d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 1179ca735530SJeremy L Thompson 1180ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 11812b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 11820d0321e0SJeremy L Thompson 11830d0321e0SJeremy L Thompson // Assemble QFunction 1184004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request)); 1185004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr)); 1186004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array)); 11870d0321e0SJeremy L Thompson 1188cbfe683aSSebastian Grimberg // Setup 1189cbfe683aSSebastian Grimberg if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Cuda(op)); 1190cbfe683aSSebastian Grimberg CeedOperatorDiag_Cuda *diag = impl->diag; 1191cbfe683aSSebastian Grimberg 1192cbfe683aSSebastian Grimberg assert(diag != NULL); 1193cbfe683aSSebastian Grimberg 1194cbfe683aSSebastian Grimberg // Assemble kernel if needed 1195cbfe683aSSebastian Grimberg if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) { 1196cbfe683aSSebastian Grimberg CeedSize assembled_length, assembled_qf_length; 1197cbfe683aSSebastian Grimberg CeedInt use_ceedsize_idx = 0; 1198f7c1b517Snbeams CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length)); 1199ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length)); 1200ca735530SJeremy L Thompson if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1; 1201f7c1b517Snbeams 1202cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Cuda(op, use_ceedsize_idx, is_point_block)); 1203cbfe683aSSebastian Grimberg } 12040d0321e0SJeremy L Thompson 1205004e4986SSebastian Grimberg // Restriction and diagonal vector 1206004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 1207004e4986SSebastian Grimberg CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND, 1208004e4986SSebastian Grimberg "Cannot assemble operator diagonal with different input and output active element restrictions"); 1209004e4986SSebastian Grimberg if (!is_point_block && !diag->diag_rstr) { 1210004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr)); 1211004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag)); 1212004e4986SSebastian Grimberg } else if (is_point_block && !diag->point_block_diag_rstr) { 1213004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr)); 1214004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag)); 12150d0321e0SJeremy L Thompson } 1216004e4986SSebastian Grimberg diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr; 1217004e4986SSebastian Grimberg elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag; 1218ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0)); 12190d0321e0SJeremy L Thompson 122091db28b6SZach Atkins // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers 1221004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes)); 1222004e4986SSebastian Grimberg if (num_nodes > 0) { 12230d0321e0SJeremy L Thompson // Assemble element operator diagonals 1224ca735530SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem)); 1225004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array)); 12260d0321e0SJeremy L Thompson 12270d0321e0SJeremy L Thompson // Compute the diagonal of B^T D B 1228004e4986SSebastian Grimberg CeedInt elems_per_block = 1; 1229004e4986SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem, elems_per_block); 1230004e4986SSebastian Grimberg void *args[] = {(void *)&num_elem, &diag->d_identity, &diag->d_interp_in, &diag->d_grad_in, &diag->d_div_in, 1231004e4986SSebastian Grimberg &diag->d_curl_in, &diag->d_interp_out, &diag->d_grad_out, &diag->d_div_out, &diag->d_curl_out, 1232004e4986SSebastian Grimberg &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array}; 1233004e4986SSebastian Grimberg 1234ca735530SJeremy L Thompson if (is_point_block) { 1235004e4986SSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args)); 12360d0321e0SJeremy L Thompson } else { 1237004e4986SSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args)); 12380d0321e0SJeremy L Thompson } 12390d0321e0SJeremy L Thompson 12400d0321e0SJeremy L Thompson // Restore arrays 1241ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array)); 1242ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 124391db28b6SZach Atkins } 12440d0321e0SJeremy L Thompson 12450d0321e0SJeremy L Thompson // Assemble local operator diagonal 1246ca735530SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); 12470d0321e0SJeremy L Thompson 12480d0321e0SJeremy L Thompson // Cleanup 1249ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 12500d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 12510d0321e0SJeremy L Thompson } 12520d0321e0SJeremy L Thompson 12530d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 12540d0321e0SJeremy L Thompson // Assemble Linear Diagonal 12550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 12562b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) { 12572b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, false)); 12586aa95790SJeremy L Thompson return CEED_ERROR_SUCCESS; 12590d0321e0SJeremy L Thompson } 12600d0321e0SJeremy L Thompson 12610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 12620d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal 12630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 12642b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) { 12652b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, true)); 12666aa95790SJeremy L Thompson return CEED_ERROR_SUCCESS; 12670d0321e0SJeremy L Thompson } 12680d0321e0SJeremy L Thompson 12690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1270004e4986SSebastian Grimberg // Single Operator Assembly Setup 1271cc132f9aSnbeams //------------------------------------------------------------------------------ 1272f7c1b517Snbeams static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op, CeedInt use_ceedsize_idx) { 1273cc132f9aSnbeams Ceed ceed; 1274004e4986SSebastian Grimberg Ceed_Cuda *cuda_data; 127522070f95SJeremy L Thompson char *assembly_kernel_source; 127622070f95SJeremy L Thompson const char *assembly_kernel_path; 1277004e4986SSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 12783b38d1dfSJeremy L Thompson CeedInt elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp; 1279004e4986SSebastian Grimberg CeedEvalMode *eval_modes_in = NULL, *eval_modes_out = NULL; 1280ca735530SJeremy L Thompson CeedElemRestriction rstr_in = NULL, rstr_out = NULL; 1281ca735530SJeremy L Thompson CeedBasis basis_in = NULL, basis_out = NULL; 1282ca735530SJeremy L Thompson CeedQFunctionField *qf_fields; 1283ca735530SJeremy L Thompson CeedQFunction qf; 1284ca735530SJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 1285cc132f9aSnbeams CeedOperator_Cuda *impl; 1286ca735530SJeremy L Thompson 1287ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 12882b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1289cc132f9aSnbeams 1290cc132f9aSnbeams // Get intput and output fields 12912b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 1292cc132f9aSnbeams 1293cc132f9aSnbeams // Determine active input basis eval mode 12942b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 12952b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 1296cc132f9aSnbeams for (CeedInt i = 0; i < num_input_fields; i++) { 1297cc132f9aSnbeams CeedVector vec; 1298ca735530SJeremy L Thompson 12992b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec)); 1300cc132f9aSnbeams if (vec == CEED_VECTOR_ACTIVE) { 1301004e4986SSebastian Grimberg CeedBasis basis; 1302ca735530SJeremy L Thompson CeedEvalMode eval_mode; 1303ca735530SJeremy L Thompson 1304004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis)); 1305004e4986SSebastian Grimberg CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases"); 1306004e4986SSebastian Grimberg basis_in = basis; 13072b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in)); 1308004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 1309004e4986SSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in; 1310004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in)); 13112b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1312004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 1313004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1314004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1315004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in)); 1316004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1317004e4986SSebastian Grimberg eval_modes_in[num_eval_modes_in + d] = eval_mode; 1318cc132f9aSnbeams } 1319004e4986SSebastian Grimberg num_eval_modes_in += q_comp; 1320cc132f9aSnbeams } 1321cc132f9aSnbeams } 1322cc132f9aSnbeams } 1323cc132f9aSnbeams 1324cc132f9aSnbeams // Determine active output basis; basis_out and rstr_out only used if same as input, TODO 13252b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1326cc132f9aSnbeams for (CeedInt i = 0; i < num_output_fields; i++) { 1327cc132f9aSnbeams CeedVector vec; 1328ca735530SJeremy L Thompson 13292b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec)); 1330cc132f9aSnbeams if (vec == CEED_VECTOR_ACTIVE) { 1331004e4986SSebastian Grimberg CeedBasis basis; 1332ca735530SJeremy L Thompson CeedEvalMode eval_mode; 1333ca735530SJeremy L Thompson 1334004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis)); 1335004e4986SSebastian Grimberg CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND, 1336004e4986SSebastian Grimberg "Backend does not implement operator assembly with multiple active bases"); 1337004e4986SSebastian Grimberg basis_out = basis; 13382b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out)); 1339004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 1340004e4986SSebastian Grimberg if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out; 1341004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out)); 1342004e4986SSebastian Grimberg CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED, 1343004e4986SSebastian Grimberg "Active input and output bases must have the same number of quadrature points"); 13442b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1345004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 1346004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1347004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1348004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out)); 1349004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1350004e4986SSebastian Grimberg eval_modes_out[num_eval_modes_out + d] = eval_mode; 1351004e4986SSebastian Grimberg } 1352004e4986SSebastian Grimberg num_eval_modes_out += q_comp; 1353cc132f9aSnbeams } 1354cc132f9aSnbeams } 1355cc132f9aSnbeams } 1356004e4986SSebastian Grimberg CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs"); 1357cc132f9aSnbeams 13582b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl->asmb)); 1359cc132f9aSnbeams CeedOperatorAssemble_Cuda *asmb = impl->asmb; 1360004e4986SSebastian Grimberg asmb->elems_per_block = 1; 1361004e4986SSebastian Grimberg asmb->block_size_x = elem_size_in; 1362004e4986SSebastian Grimberg asmb->block_size_y = elem_size_out; 1363ca735530SJeremy L Thompson 13642b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &cuda_data)); 1365004e4986SSebastian Grimberg bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > cuda_data->device_prop.maxThreadsPerBlock; 1366004e4986SSebastian Grimberg 1367004e4986SSebastian Grimberg if (fallback) { 1368004e4986SSebastian Grimberg // Use fallback kernel with 1D threadblock 1369004e4986SSebastian Grimberg asmb->block_size_y = 1; 1370004e4986SSebastian Grimberg } 1371004e4986SSebastian Grimberg 1372004e4986SSebastian Grimberg // Compile kernels 1373004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in)); 1374004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out)); 13752b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble.h", &assembly_kernel_path)); 137623d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n"); 13772b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source)); 137823d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n"); 1379004e4986SSebastian Grimberg CeedCallBackend(CeedCompile_Cuda(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT", 1380004e4986SSebastian Grimberg num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in, 1381004e4986SSebastian Grimberg "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE", 1382cbfe683aSSebastian Grimberg asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, 1383cbfe683aSSebastian Grimberg "USE_CEEDSIZE", use_ceedsize_idx)); 1384004e4986SSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble)); 13852b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&assembly_kernel_path)); 13862b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&assembly_kernel_source)); 1387cc132f9aSnbeams 1388004e4986SSebastian Grimberg // Load into B_in, in order that they will be used in eval_modes_in 1389004e4986SSebastian Grimberg { 1390004e4986SSebastian Grimberg const CeedInt in_bytes = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar); 1391004e4986SSebastian Grimberg CeedInt d_in = 0; 1392004e4986SSebastian Grimberg CeedEvalMode eval_modes_in_prev = CEED_EVAL_NONE; 1393004e4986SSebastian Grimberg bool has_eval_none = false; 1394004e4986SSebastian Grimberg CeedScalar *identity = NULL; 1395ca735530SJeremy L Thompson 1396004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) { 1397004e4986SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE); 1398004e4986SSebastian Grimberg } 1399004e4986SSebastian Grimberg if (has_eval_none) { 1400004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity)); 1401004e4986SSebastian 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; 1402004e4986SSebastian Grimberg } 1403cc132f9aSnbeams 1404004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_in, in_bytes)); 1405004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) { 1406004e4986SSebastian Grimberg const CeedScalar *h_B_in; 1407ca735530SJeremy L Thompson 1408004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in)); 1409004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp)); 1410004e4986SSebastian Grimberg if (q_comp > 1) { 1411004e4986SSebastian Grimberg if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0; 1412004e4986SSebastian Grimberg else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in]; 1413004e4986SSebastian Grimberg } 1414004e4986SSebastian Grimberg eval_modes_in_prev = eval_modes_in[i]; 1415ca735530SJeremy L Thompson 1416004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_in[i * elem_size_in * num_qpts_in], h_B_in, elem_size_in * num_qpts_in * sizeof(CeedScalar), 1417004e4986SSebastian Grimberg cudaMemcpyHostToDevice)); 1418004e4986SSebastian Grimberg } 1419004e4986SSebastian Grimberg 1420004e4986SSebastian Grimberg if (identity) { 1421004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 1422cc132f9aSnbeams } 1423cc132f9aSnbeams } 1424cc132f9aSnbeams 1425004e4986SSebastian Grimberg // Load into B_out, in order that they will be used in eval_modes_out 1426004e4986SSebastian Grimberg { 1427004e4986SSebastian Grimberg const CeedInt out_bytes = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar); 1428004e4986SSebastian Grimberg CeedInt d_out = 0; 1429004e4986SSebastian Grimberg CeedEvalMode eval_modes_out_prev = CEED_EVAL_NONE; 1430004e4986SSebastian Grimberg bool has_eval_none = false; 1431004e4986SSebastian Grimberg CeedScalar *identity = NULL; 1432ca735530SJeremy L Thompson 1433004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) { 1434004e4986SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE); 1435004e4986SSebastian Grimberg } 1436004e4986SSebastian Grimberg if (has_eval_none) { 1437004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity)); 1438004e4986SSebastian 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; 1439cc132f9aSnbeams } 1440cc132f9aSnbeams 1441004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_out, out_bytes)); 1442004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) { 1443004e4986SSebastian Grimberg const CeedScalar *h_B_out; 1444ca735530SJeremy L Thompson 1445004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out)); 1446004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp)); 1447004e4986SSebastian Grimberg if (q_comp > 1) { 1448004e4986SSebastian Grimberg if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0; 1449004e4986SSebastian Grimberg else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out]; 1450004e4986SSebastian Grimberg } 1451004e4986SSebastian Grimberg eval_modes_out_prev = eval_modes_out[i]; 1452ca735530SJeremy L Thompson 1453004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_out[i * elem_size_out * num_qpts_out], h_B_out, elem_size_out * num_qpts_out * sizeof(CeedScalar), 1454004e4986SSebastian Grimberg cudaMemcpyHostToDevice)); 1455004e4986SSebastian Grimberg } 1456004e4986SSebastian Grimberg 1457004e4986SSebastian Grimberg if (identity) { 1458004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 1459cc132f9aSnbeams } 1460cc132f9aSnbeams } 1461cc132f9aSnbeams return CEED_ERROR_SUCCESS; 1462cc132f9aSnbeams } 1463cc132f9aSnbeams 1464cc132f9aSnbeams //------------------------------------------------------------------------------ 1465cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator. 1466cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic. 1467cefa2673SJeremy L Thompson // 1468ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval 1469cefa2673SJeremy L Thompson // modes). 1470cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects 1471cc132f9aSnbeams //------------------------------------------------------------------------------ 14722b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Cuda(CeedOperator op, CeedInt offset, CeedVector values) { 1473cc132f9aSnbeams Ceed ceed; 1474ca735530SJeremy L Thompson CeedSize values_length = 0, assembled_qf_length = 0; 1475004e4986SSebastian Grimberg CeedInt use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out; 1476ca735530SJeremy L Thompson CeedScalar *values_array; 1477004e4986SSebastian Grimberg const CeedScalar *assembled_qf_array; 1478ca735530SJeremy L Thompson CeedVector assembled_qf = NULL; 1479004e4986SSebastian Grimberg CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out; 1480004e4986SSebastian Grimberg CeedRestrictionType rstr_type_in, rstr_type_out; 1481004e4986SSebastian Grimberg const bool *orients_in = NULL, *orients_out = NULL; 1482004e4986SSebastian Grimberg const CeedInt8 *curl_orients_in = NULL, *curl_orients_out = NULL; 1483cc132f9aSnbeams CeedOperator_Cuda *impl; 1484ca735530SJeremy L Thompson 1485ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 14862b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1487cc132f9aSnbeams 1488cc132f9aSnbeams // Assemble QFunction 1489004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE)); 1490004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr)); 1491004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array)); 1492cc132f9aSnbeams 1493f7c1b517Snbeams CeedCallBackend(CeedVectorGetLength(values, &values_length)); 1494f7c1b517Snbeams CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length)); 1495f7c1b517Snbeams if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1; 1496004e4986SSebastian Grimberg 1497f7c1b517Snbeams // Setup 1498004e4986SSebastian Grimberg if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Cuda(op, use_ceedsize_idx)); 1499004e4986SSebastian Grimberg CeedOperatorAssemble_Cuda *asmb = impl->asmb; 1500004e4986SSebastian Grimberg 1501004e4986SSebastian Grimberg assert(asmb != NULL); 1502004e4986SSebastian Grimberg 1503004e4986SSebastian Grimberg // Assemble element operator 1504004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array)); 1505004e4986SSebastian Grimberg values_array += offset; 1506004e4986SSebastian Grimberg 1507004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 1508004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in)); 1509004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 1510004e4986SSebastian Grimberg 1511004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in)); 1512004e4986SSebastian Grimberg if (rstr_type_in == CEED_RESTRICTION_ORIENTED) { 1513004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in)); 1514004e4986SSebastian Grimberg } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 1515004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in)); 1516004e4986SSebastian Grimberg } 1517004e4986SSebastian Grimberg 1518004e4986SSebastian Grimberg if (rstr_in != rstr_out) { 1519004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out)); 1520004e4986SSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 1521004e4986SSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 1522004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 1523004e4986SSebastian Grimberg 1524004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out)); 1525004e4986SSebastian Grimberg if (rstr_type_out == CEED_RESTRICTION_ORIENTED) { 1526004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out)); 1527004e4986SSebastian Grimberg } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 1528004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out)); 1529004e4986SSebastian Grimberg } 1530004e4986SSebastian Grimberg } else { 1531004e4986SSebastian Grimberg elem_size_out = elem_size_in; 1532004e4986SSebastian Grimberg orients_out = orients_in; 1533004e4986SSebastian Grimberg curl_orients_out = curl_orients_in; 1534f7c1b517Snbeams } 1535f7c1b517Snbeams 1536cc132f9aSnbeams // Compute B^T D B 1537004e4986SSebastian Grimberg CeedInt shared_mem = 1538004e4986SSebastian 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)) * 1539004e4986SSebastian Grimberg sizeof(CeedScalar); 1540004e4986SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem_in, asmb->elems_per_block); 1541004e4986SSebastian Grimberg void *args[] = {(void *)&num_elem_in, &asmb->d_B_in, &asmb->d_B_out, &orients_in, &curl_orients_in, 1542004e4986SSebastian Grimberg &orients_out, &curl_orients_out, &assembled_qf_array, &values_array}; 1543ca735530SJeremy L Thompson 15442b730f8bSJeremy L Thompson CeedCallBackend( 1545004e4986SSebastian Grimberg CeedRunKernelDimShared_Cuda(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args)); 1546cc132f9aSnbeams 1547cc132f9aSnbeams // Restore arrays 15482b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(values, &values_array)); 1549004e4986SSebastian Grimberg CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 1550cc132f9aSnbeams 1551cc132f9aSnbeams // Cleanup 15522b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 1553004e4986SSebastian Grimberg if (rstr_type_in == CEED_RESTRICTION_ORIENTED) { 1554004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in)); 1555004e4986SSebastian Grimberg } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 1556004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in)); 1557004e4986SSebastian Grimberg } 1558004e4986SSebastian Grimberg if (rstr_in != rstr_out) { 1559004e4986SSebastian Grimberg if (rstr_type_out == CEED_RESTRICTION_ORIENTED) { 1560004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out)); 1561004e4986SSebastian Grimberg } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 1562004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out)); 1563004e4986SSebastian Grimberg } 1564004e4986SSebastian Grimberg } 1565cc132f9aSnbeams return CEED_ERROR_SUCCESS; 1566cc132f9aSnbeams } 1567cc132f9aSnbeams 1568cc132f9aSnbeams //------------------------------------------------------------------------------ 1569756ca9e9SJeremy L Thompson // Assemble Linear QFunction AtPoints 1570756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 1571756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 1572756ca9e9SJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction"); 1573756ca9e9SJeremy L Thompson } 1574756ca9e9SJeremy L Thompson 1575756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 1576756ca9e9SJeremy L Thompson // Assemble Linear Diagonal AtPoints 1577756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 1578756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) { 1579382e9c83SJeremy L Thompson CeedInt max_num_points, num_elem, num_input_fields, num_output_fields; 1580349fb27dSJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX] = {NULL}; 1581349fb27dSJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 1582349fb27dSJeremy L Thompson CeedQFunction qf; 1583349fb27dSJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 1584349fb27dSJeremy L Thompson CeedOperator_Cuda *impl; 1585349fb27dSJeremy L Thompson 1586349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1587349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1588349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 1589349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 1590349fb27dSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 1591349fb27dSJeremy L Thompson CeedInt num_points[num_elem]; 1592349fb27dSJeremy L Thompson 1593349fb27dSJeremy L Thompson // Setup 1594349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op)); 1595349fb27dSJeremy L Thompson max_num_points = impl->max_num_points; 1596349fb27dSJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points; 1597349fb27dSJeremy L Thompson 1598349fb27dSJeremy L Thompson // Input Evecs and Restriction 1599349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request)); 1600349fb27dSJeremy L Thompson 1601349fb27dSJeremy L Thompson // Get point coordinates 1602349fb27dSJeremy L Thompson if (!impl->point_coords_elem) { 1603349fb27dSJeremy L Thompson CeedVector point_coords = NULL; 1604349fb27dSJeremy L Thompson CeedElemRestriction rstr_points = NULL; 1605349fb27dSJeremy L Thompson 1606349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords)); 1607349fb27dSJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem)); 1608349fb27dSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request)); 1609349fb27dSJeremy L Thompson } 1610349fb27dSJeremy L Thompson 1611382e9c83SJeremy L Thompson // Clear active input Qvecs 1612382e9c83SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1613382e9c83SJeremy L Thompson CeedVector vec; 1614382e9c83SJeremy L Thompson 1615382e9c83SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 1616382e9c83SJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE) continue; 1617382e9c83SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0)); 1618382e9c83SJeremy L Thompson } 1619382e9c83SJeremy L Thompson 1620349fb27dSJeremy L Thompson // Input basis apply if needed 1621349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl)); 1622349fb27dSJeremy L Thompson 1623349fb27dSJeremy L Thompson // Output pointers, as necessary 1624349fb27dSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1625349fb27dSJeremy L Thompson CeedEvalMode eval_mode; 1626349fb27dSJeremy L Thompson 1627349fb27dSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1628349fb27dSJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) { 1629349fb27dSJeremy L Thompson // Set the output Q-Vector to use the E-Vector data directly. 1630349fb27dSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields])); 1631349fb27dSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields])); 1632349fb27dSJeremy L Thompson } 1633349fb27dSJeremy L Thompson } 1634349fb27dSJeremy L Thompson 1635349fb27dSJeremy L Thompson // Loop over active fields 1636349fb27dSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1637382e9c83SJeremy L Thompson bool is_active_at_points = true; 1638382e9c83SJeremy L Thompson CeedInt elem_size = 1, num_comp_active = 1, e_vec_size = 0; 1639382e9c83SJeremy L Thompson CeedRestrictionType rstr_type; 1640382e9c83SJeremy L Thompson CeedVector vec; 1641382e9c83SJeremy L Thompson CeedElemRestriction elem_rstr; 1642382e9c83SJeremy L Thompson 1643382e9c83SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 1644382e9c83SJeremy L Thompson // -- Skip non-active input 1645382e9c83SJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE) continue; 1646382e9c83SJeremy L Thompson 1647382e9c83SJeremy L Thompson // -- Get active restriction type 1648382e9c83SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 1649382e9c83SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type)); 1650382e9c83SJeremy L Thompson is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS; 1651382e9c83SJeremy L Thompson if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 1652382e9c83SJeremy L Thompson else elem_size = max_num_points; 1653382e9c83SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active)); 1654382e9c83SJeremy L Thompson 1655382e9c83SJeremy L Thompson e_vec_size = elem_size * num_comp_active; 1656382e9c83SJeremy L Thompson for (CeedInt s = 0; s < e_vec_size; s++) { 1657349fb27dSJeremy L Thompson bool is_active_input = false; 1658349fb27dSJeremy L Thompson CeedEvalMode eval_mode; 1659349fb27dSJeremy L Thompson CeedVector vec; 1660349fb27dSJeremy L Thompson CeedBasis basis; 1661349fb27dSJeremy L Thompson 1662349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 1663349fb27dSJeremy L Thompson // Skip non-active input 1664349fb27dSJeremy L Thompson is_active_input = vec == CEED_VECTOR_ACTIVE; 1665349fb27dSJeremy L Thompson if (!is_active_input) continue; 1666349fb27dSJeremy L Thompson 1667349fb27dSJeremy L Thompson // Update unit vector 166813062808SJeremy L Thompson if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0)); 1669349fb27dSJeremy L Thompson else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0)); 1670349fb27dSJeremy L Thompson CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0)); 1671349fb27dSJeremy L Thompson 1672349fb27dSJeremy L Thompson // Basis action 1673349fb27dSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1674349fb27dSJeremy L Thompson switch (eval_mode) { 1675349fb27dSJeremy L Thompson case CEED_EVAL_NONE: 1676349fb27dSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i])); 1677349fb27dSJeremy L Thompson break; 1678349fb27dSJeremy L Thompson case CEED_EVAL_INTERP: 1679349fb27dSJeremy L Thompson case CEED_EVAL_GRAD: 1680349fb27dSJeremy L Thompson case CEED_EVAL_DIV: 1681349fb27dSJeremy L Thompson case CEED_EVAL_CURL: 1682349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1683349fb27dSJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i], 1684349fb27dSJeremy L Thompson impl->q_vecs_in[i])); 1685349fb27dSJeremy L Thompson break; 1686349fb27dSJeremy L Thompson case CEED_EVAL_WEIGHT: 1687349fb27dSJeremy L Thompson break; // No action 1688349fb27dSJeremy L Thompson } 1689349fb27dSJeremy L Thompson 1690349fb27dSJeremy L Thompson // Q function 1691349fb27dSJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out)); 1692349fb27dSJeremy L Thompson 1693349fb27dSJeremy L Thompson // Output basis apply if needed 1694382e9c83SJeremy L Thompson for (CeedInt j = 0; j < num_output_fields; j++) { 1695349fb27dSJeremy L Thompson bool is_active_output = false; 1696382e9c83SJeremy L Thompson CeedInt elem_size = 0; 1697382e9c83SJeremy L Thompson CeedRestrictionType rstr_type; 1698349fb27dSJeremy L Thompson CeedEvalMode eval_mode; 1699349fb27dSJeremy L Thompson CeedVector vec; 1700349fb27dSJeremy L Thompson CeedElemRestriction elem_rstr; 1701349fb27dSJeremy L Thompson CeedBasis basis; 1702349fb27dSJeremy L Thompson 1703382e9c83SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec)); 1704382e9c83SJeremy L Thompson // ---- Skip non-active output 1705349fb27dSJeremy L Thompson is_active_output = vec == CEED_VECTOR_ACTIVE; 1706349fb27dSJeremy L Thompson if (!is_active_output) continue; 1707349fb27dSJeremy L Thompson 1708382e9c83SJeremy L Thompson // ---- Check if elem size matches 1709382e9c83SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr)); 1710382e9c83SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type)); 1711382e9c83SJeremy L Thompson if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue; 1712382e9c83SJeremy L Thompson if (rstr_type == CEED_RESTRICTION_POINTS) { 1713382e9c83SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size)); 1714382e9c83SJeremy L Thompson } else { 1715382e9c83SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 1716382e9c83SJeremy L Thompson } 1717382e9c83SJeremy L Thompson { 1718382e9c83SJeremy L Thompson CeedInt num_comp = 0; 1719382e9c83SJeremy L Thompson 1720382e9c83SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1721382e9c83SJeremy L Thompson if (e_vec_size != num_comp * elem_size) continue; 1722382e9c83SJeremy L Thompson } 1723382e9c83SJeremy L Thompson 1724349fb27dSJeremy L Thompson // Basis action 1725382e9c83SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode)); 1726349fb27dSJeremy L Thompson switch (eval_mode) { 1727349fb27dSJeremy L Thompson case CEED_EVAL_NONE: 1728382e9c83SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields])); 1729349fb27dSJeremy L Thompson break; 1730349fb27dSJeremy L Thompson case CEED_EVAL_INTERP: 1731349fb27dSJeremy L Thompson case CEED_EVAL_GRAD: 1732349fb27dSJeremy L Thompson case CEED_EVAL_DIV: 1733349fb27dSJeremy L Thompson case CEED_EVAL_CURL: 1734382e9c83SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis)); 1735382e9c83SJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, 1736382e9c83SJeremy L Thompson impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs])); 1737349fb27dSJeremy L Thompson break; 1738349fb27dSJeremy L Thompson // LCOV_EXCL_START 1739349fb27dSJeremy L Thompson case CEED_EVAL_WEIGHT: { 1740349fb27dSJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 1741349fb27dSJeremy L Thompson // LCOV_EXCL_STOP 1742349fb27dSJeremy L Thompson } 1743349fb27dSJeremy L Thompson } 1744349fb27dSJeremy L Thompson 1745349fb27dSJeremy L Thompson // Mask output e-vec 1746382e9c83SJeremy L Thompson CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs])); 1747349fb27dSJeremy L Thompson 1748349fb27dSJeremy L Thompson // Restrict 1749382e9c83SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr)); 1750382e9c83SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request)); 1751349fb27dSJeremy L Thompson 1752349fb27dSJeremy L Thompson // Reset q_vec for 1753349fb27dSJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) { 1754382e9c83SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields])); 1755382e9c83SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields])); 1756349fb27dSJeremy L Thompson } 1757349fb27dSJeremy L Thompson } 1758382e9c83SJeremy L Thompson 1759382e9c83SJeremy L Thompson // Reset vec 176013062808SJeremy L Thompson if (s == e_vec_size - 1 && i != num_input_fields - 1) CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0)); 176186e10729SJeremy L Thompson } 1762349fb27dSJeremy L Thompson } 1763349fb27dSJeremy L Thompson 1764349fb27dSJeremy L Thompson // Restore CEED_EVAL_NONE 1765349fb27dSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1766349fb27dSJeremy L Thompson CeedEvalMode eval_mode; 1767349fb27dSJeremy L Thompson 1768349fb27dSJeremy L Thompson // Get eval_mode 1769349fb27dSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1770349fb27dSJeremy L Thompson 1771349fb27dSJeremy L Thompson // Restore evec 1772349fb27dSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1773349fb27dSJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) { 1774349fb27dSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields])); 1775349fb27dSJeremy L Thompson } 1776349fb27dSJeremy L Thompson } 1777349fb27dSJeremy L Thompson 1778349fb27dSJeremy L Thompson // Restore input arrays 1779349fb27dSJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl)); 1780349fb27dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1781756ca9e9SJeremy L Thompson } 1782756ca9e9SJeremy L Thompson 1783756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 17840d0321e0SJeremy L Thompson // Create operator 17850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 17860d0321e0SJeremy L Thompson int CeedOperatorCreate_Cuda(CeedOperator op) { 17870d0321e0SJeremy L Thompson Ceed ceed; 17880d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 17890d0321e0SJeremy L Thompson 1790ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 17912b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 17922b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl)); 17930d0321e0SJeremy L Thompson 17942b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda)); 17952b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Cuda)); 17962b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Cuda)); 17972b730f8bSJeremy L Thompson CeedCallBackend( 17982b730f8bSJeremy L Thompson CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda)); 17992b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Cuda)); 18002b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda)); 18012b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda)); 18020d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 18030d0321e0SJeremy L Thompson } 18040d0321e0SJeremy L Thompson 18050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 180667d9480aSJeremy L Thompson // Create operator AtPoints 1807756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 1808756ca9e9SJeremy L Thompson int CeedOperatorCreateAtPoints_Cuda(CeedOperator op) { 1809756ca9e9SJeremy L Thompson Ceed ceed; 1810756ca9e9SJeremy L Thompson CeedOperator_Cuda *impl; 1811756ca9e9SJeremy L Thompson 1812756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1813756ca9e9SJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 1814756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl)); 1815756ca9e9SJeremy L Thompson 1816756ca9e9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Cuda)); 1817756ca9e9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda)); 1818756ca9e9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Cuda)); 1819756ca9e9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda)); 1820756ca9e9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1821756ca9e9SJeremy L Thompson } 1822756ca9e9SJeremy L Thompson 1823756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 1824