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 30ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) { 31ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i])); 320d0321e0SJeremy L Thompson } 33ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->e_vecs)); 34c1222711SJeremy L Thompson CeedCallBackend(CeedFree(&impl->input_states)); 350d0321e0SJeremy L Thompson 36ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_inputs; i++) { 37ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i])); 380d0321e0SJeremy L Thompson } 39ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->q_vecs_in)); 400d0321e0SJeremy L Thompson 41ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_outputs; i++) { 42ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i])); 430d0321e0SJeremy L Thompson } 44ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->q_vecs_out)); 45756ca9e9SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem)); 460d0321e0SJeremy L Thompson 470d0321e0SJeremy L Thompson // QFunction assembly data 48ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_active_in; i++) { 49ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i])); 500d0321e0SJeremy L Thompson } 51ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->qf_active_in)); 520d0321e0SJeremy L Thompson 530d0321e0SJeremy L Thompson // Diag data 540d0321e0SJeremy L Thompson if (impl->diag) { 550d0321e0SJeremy L Thompson Ceed ceed; 56ca735530SJeremy L Thompson 572b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 58cbfe683aSSebastian Grimberg if (impl->diag->module) { 592b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(impl->diag->module)); 60cbfe683aSSebastian Grimberg } 61cbfe683aSSebastian Grimberg if (impl->diag->module_point_block) { 62cbfe683aSSebastian Grimberg CeedCallCuda(ceed, cuModuleUnload(impl->diag->module_point_block)); 63cbfe683aSSebastian Grimberg } 64004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_in)); 65004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_out)); 662b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_identity)); 67ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_in)); 68ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_out)); 69ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_in)); 70ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_out)); 71004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_div_in)); 72004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_div_out)); 73004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_in)); 74004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_out)); 75004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr)); 76506b1a0cSSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr)); 77ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag)); 78ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag)); 790d0321e0SJeremy L Thompson } 802b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->diag)); 810d0321e0SJeremy L Thompson 82cc132f9aSnbeams if (impl->asmb) { 83cc132f9aSnbeams Ceed ceed; 84ca735530SJeremy L Thompson 852b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 862b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(impl->asmb->module)); 872b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_in)); 882b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_out)); 89cc132f9aSnbeams } 902b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->asmb)); 91cc132f9aSnbeams 922b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 930d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 940d0321e0SJeremy L Thompson } 950d0321e0SJeremy L Thompson 960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 970d0321e0SJeremy L Thompson // Setup infields or outfields 980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 99756ca9e9SJeremy L Thompson static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, CeedVector *e_vecs, CeedVector *q_vecs, 100756ca9e9SJeremy L Thompson CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) { 1010d0321e0SJeremy L Thompson Ceed ceed; 102ca735530SJeremy L Thompson CeedQFunctionField *qf_fields; 103ca735530SJeremy L Thompson CeedOperatorField *op_fields; 1040d0321e0SJeremy L Thompson 105ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 106ca735530SJeremy L Thompson if (is_input) { 107ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 108ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 1090d0321e0SJeremy L Thompson } else { 110ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 111ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1120d0321e0SJeremy L Thompson } 1130d0321e0SJeremy L Thompson 1140d0321e0SJeremy L Thompson // Loop over fields 115ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_fields; i++) { 116004e4986SSebastian Grimberg bool is_strided = false, skip_restriction = false; 117004e4986SSebastian Grimberg CeedSize q_size; 118004e4986SSebastian Grimberg CeedInt size; 119004e4986SSebastian Grimberg CeedEvalMode eval_mode; 120ca735530SJeremy L Thompson CeedBasis basis; 1210d0321e0SJeremy L Thompson 122004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 123004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 124edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 125ca735530SJeremy L Thompson 1260d0321e0SJeremy L Thompson // Check whether this field can skip the element restriction: 127004e4986SSebastian Grimberg // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND. 128004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr)); 1290d0321e0SJeremy L Thompson 1300d0321e0SJeremy L Thompson // First, check whether the field is input or output: 131ca735530SJeremy L Thompson if (is_input) { 132ca735530SJeremy L Thompson CeedVector vec; 133ca735530SJeremy L Thompson 134004e4986SSebastian Grimberg // Check for passive input 135ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 136ca735530SJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE) { 137004e4986SSebastian Grimberg // Check eval_mode 138004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 1390d0321e0SJeremy L Thompson // Check for strided restriction 140edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided)); 141ca735530SJeremy L Thompson if (is_strided) { 1420d0321e0SJeremy L Thompson // Check if vector is already in preferred backend ordering 143edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction)); 1440d0321e0SJeremy L Thompson } 1450d0321e0SJeremy L Thompson } 1460d0321e0SJeremy L Thompson } 1470d0321e0SJeremy L Thompson } 148ca735530SJeremy L Thompson if (skip_restriction) { 149ea61e9acSJeremy L Thompson // We do not need an E-Vector, but will use the input field vector's data directly in the operator application. 150004e4986SSebastian Grimberg e_vecs[i + start_e] = NULL; 1510d0321e0SJeremy L Thompson } else { 152004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e])); 1530d0321e0SJeremy L Thompson } 1540d0321e0SJeremy L Thompson } 1550d0321e0SJeremy L Thompson 156004e4986SSebastian Grimberg switch (eval_mode) { 1570d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 158ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 159ca735530SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 160ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 1610d0321e0SJeremy L Thompson break; 1620d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 1630d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 164004e4986SSebastian Grimberg case CEED_EVAL_DIV: 165004e4986SSebastian Grimberg case CEED_EVAL_CURL: 166ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 167ca735530SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 168ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 1690d0321e0SJeremy L Thompson break; 1700d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: // Only on input fields 171ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 172ca735530SJeremy L Thompson q_size = (CeedSize)num_elem * Q; 173ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 174756ca9e9SJeremy L Thompson if (is_at_points) { 175756ca9e9SJeremy L Thompson CeedInt num_points[num_elem]; 176756ca9e9SJeremy L Thompson 177756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q; 178756ca9e9SJeremy L Thompson CeedCallBackend( 179756ca9e9SJeremy L Thompson CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i])); 180756ca9e9SJeremy L Thompson } else { 181ca735530SJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i])); 182756ca9e9SJeremy L Thompson } 1830d0321e0SJeremy L Thompson break; 1840d0321e0SJeremy L Thompson } 1850d0321e0SJeremy L Thompson } 1860d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1870d0321e0SJeremy L Thompson } 1880d0321e0SJeremy L Thompson 1890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 190ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. 1910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1920d0321e0SJeremy L Thompson static int CeedOperatorSetup_Cuda(CeedOperator op) { 1930d0321e0SJeremy L Thompson Ceed ceed; 194ca735530SJeremy L Thompson bool is_setup_done; 195ca735530SJeremy L Thompson CeedInt Q, num_elem, num_input_fields, num_output_fields; 196ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 1970d0321e0SJeremy L Thompson CeedQFunction qf; 198ca735530SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 199ca735530SJeremy L Thompson CeedOperator_Cuda *impl; 200ca735530SJeremy L Thompson 201ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 202ca735530SJeremy L Thompson if (is_setup_done) return CEED_ERROR_SUCCESS; 203ca735530SJeremy L Thompson 204ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 205ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 2062b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 2072b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 208ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 209ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 210ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 2110d0321e0SJeremy L Thompson 2120d0321e0SJeremy L Thompson // Allocate 213ca735530SJeremy L Thompson CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs)); 214c1222711SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states)); 215ca735530SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in)); 216ca735530SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out)); 217ca735530SJeremy L Thompson impl->num_inputs = num_input_fields; 218ca735530SJeremy L Thompson impl->num_outputs = num_output_fields; 2190d0321e0SJeremy L Thompson 220ca735530SJeremy L Thompson // Set up infield and outfield e_vecs and q_vecs 2210d0321e0SJeremy L Thompson // Infields 222756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, true, false, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem)); 2230d0321e0SJeremy L Thompson // Outfields 224756ca9e9SJeremy L Thompson CeedCallBackend( 225756ca9e9SJeremy L Thompson CeedOperatorSetupFields_Cuda(qf, op, false, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem)); 2260d0321e0SJeremy L Thompson 2272b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 2280d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2290d0321e0SJeremy L Thompson } 2300d0321e0SJeremy L Thompson 2310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2320d0321e0SJeremy L Thompson // Setup Operator Inputs 2330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 234ca735530SJeremy L Thompson static inline int CeedOperatorSetupInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 235004e4986SSebastian Grimberg CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], 2360d0321e0SJeremy L Thompson CeedOperator_Cuda *impl, CeedRequest *request) { 237ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 238004e4986SSebastian Grimberg CeedEvalMode eval_mode; 2390d0321e0SJeremy L Thompson CeedVector vec; 240edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 2410d0321e0SJeremy L Thompson 2420d0321e0SJeremy L Thompson // Get input vector 243ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 2440d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 245004e4986SSebastian Grimberg if (skip_active) continue; 246ca735530SJeremy L Thompson else vec = in_vec; 2470d0321e0SJeremy L Thompson } 2480d0321e0SJeremy L Thompson 249004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 250004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 2510d0321e0SJeremy L Thompson } else { 252004e4986SSebastian Grimberg // Get input vector 253004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 2540d0321e0SJeremy L Thompson // Get input element restriction 255edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 256ca735530SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = in_vec; 2570d0321e0SJeremy L Thompson // Restrict, if necessary 258ca735530SJeremy L Thompson if (!impl->e_vecs[i]) { 2590d0321e0SJeremy L Thompson // No restriction for this field; read data directly from vec. 260ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i])); 2610d0321e0SJeremy L Thompson } else { 262c1222711SJeremy L Thompson uint64_t state; 263c1222711SJeremy L Thompson 264c1222711SJeremy L Thompson CeedCallBackend(CeedVectorGetState(vec, &state)); 265c1222711SJeremy L Thompson if (state != impl->input_states[i]) { 266edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request)); 267c1222711SJeremy L Thompson impl->input_states[i] = state; 268c1222711SJeremy L Thompson } 2690d0321e0SJeremy L Thompson // Get evec 270ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i])); 2710d0321e0SJeremy L Thompson } 2720d0321e0SJeremy L Thompson } 2730d0321e0SJeremy L Thompson } 2740d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2750d0321e0SJeremy L Thompson } 2760d0321e0SJeremy L Thompson 2770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2780d0321e0SJeremy L Thompson // Input Basis Action 2790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 280ca735530SJeremy L Thompson static inline int CeedOperatorInputBasis_Cuda(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 281004e4986SSebastian Grimberg CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], 2822b730f8bSJeremy L Thompson CeedOperator_Cuda *impl) { 283ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 284ca735530SJeremy L Thompson CeedInt elem_size, size; 285004e4986SSebastian Grimberg CeedEvalMode eval_mode; 286edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 2870d0321e0SJeremy L Thompson CeedBasis basis; 2880d0321e0SJeremy L Thompson 2890d0321e0SJeremy L Thompson // Skip active input 290004e4986SSebastian Grimberg if (skip_active) { 2910d0321e0SJeremy L Thompson CeedVector vec; 292ca735530SJeremy L Thompson 293ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 2942b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 2950d0321e0SJeremy L Thompson } 296004e4986SSebastian Grimberg // Get elem_size, eval_mode, size 297edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 298edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 299004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 300ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 3010d0321e0SJeremy L Thompson // Basis action 302004e4986SSebastian Grimberg switch (eval_mode) { 3030d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 304ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i])); 3050d0321e0SJeremy L Thompson break; 3060d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 3070d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 308004e4986SSebastian Grimberg case CEED_EVAL_DIV: 309004e4986SSebastian Grimberg case CEED_EVAL_CURL: 310ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 311004e4986SSebastian Grimberg CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i])); 3120d0321e0SJeremy L Thompson break; 3130d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: 3140d0321e0SJeremy L Thompson break; // No action 3150d0321e0SJeremy L Thompson } 3160d0321e0SJeremy L Thompson } 3170d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3180d0321e0SJeremy L Thompson } 3190d0321e0SJeremy L Thompson 3200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3210d0321e0SJeremy L Thompson // Restore Input Vectors 3220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 323ca735530SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 324004e4986SSebastian Grimberg const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) { 325ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 326004e4986SSebastian Grimberg CeedEvalMode eval_mode; 3270d0321e0SJeremy L Thompson CeedVector vec; 3280d0321e0SJeremy L Thompson 3290d0321e0SJeremy L Thompson // Skip active input 330004e4986SSebastian Grimberg if (skip_active) { 331ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 3322b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 3330d0321e0SJeremy L Thompson } 334004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 335004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 3360d0321e0SJeremy L Thompson } else { 337ca735530SJeremy L Thompson if (!impl->e_vecs[i]) { // This was a skip_restriction case 338ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 339ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i])); 3400d0321e0SJeremy L Thompson } else { 341ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i])); 3420d0321e0SJeremy L Thompson } 3430d0321e0SJeremy L Thompson } 3440d0321e0SJeremy L Thompson } 3450d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3460d0321e0SJeremy L Thompson } 3470d0321e0SJeremy L Thompson 3480d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3490d0321e0SJeremy L Thompson // Apply and add to output 3500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 351ca735530SJeremy L Thompson static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) { 352ca735530SJeremy L Thompson CeedInt Q, num_elem, elem_size, num_input_fields, num_output_fields, size; 353ca735530SJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX] = {NULL}; 354ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 3550d0321e0SJeremy L Thompson CeedQFunction qf; 356004e4986SSebastian Grimberg CeedOperatorField *op_input_fields, *op_output_fields; 357004e4986SSebastian Grimberg CeedOperator_Cuda *impl; 358ca735530SJeremy L Thompson 359ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 3602b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 3612b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 362ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 363ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 364ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 3650d0321e0SJeremy L Thompson 3660d0321e0SJeremy L Thompson // Setup 3672b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetup_Cuda(op)); 3680d0321e0SJeremy L Thompson 369004e4986SSebastian Grimberg // Input Evecs and Restriction 370ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request)); 3710d0321e0SJeremy L Thompson 3720d0321e0SJeremy L Thompson // Input basis apply if needed 373ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl)); 3740d0321e0SJeremy L Thompson 3750d0321e0SJeremy L Thompson // Output pointers, as necessary 376ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 377004e4986SSebastian Grimberg CeedEvalMode eval_mode; 378004e4986SSebastian Grimberg 379004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 380004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 3810d0321e0SJeremy L Thompson // Set the output Q-Vector to use the E-Vector data directly. 382ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields])); 383ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields])); 3840d0321e0SJeremy L Thompson } 3850d0321e0SJeremy L Thompson } 3860d0321e0SJeremy L Thompson 3870d0321e0SJeremy L Thompson // Q function 388ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out)); 3890d0321e0SJeremy L Thompson 3900d0321e0SJeremy L Thompson // Output basis apply if needed 391ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 392004e4986SSebastian Grimberg CeedEvalMode eval_mode; 393edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 394ca735530SJeremy L Thompson CeedBasis basis; 395ca735530SJeremy L Thompson 396004e4986SSebastian Grimberg // Get elem_size, eval_mode, size 397edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 398edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 399004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 400ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 4010d0321e0SJeremy L Thompson // Basis action 402004e4986SSebastian Grimberg switch (eval_mode) { 4030d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 404004e4986SSebastian Grimberg break; // No action 4050d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 4060d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 407004e4986SSebastian Grimberg case CEED_EVAL_DIV: 408004e4986SSebastian Grimberg case CEED_EVAL_CURL: 409ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 410004e4986SSebastian Grimberg CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs])); 4110d0321e0SJeremy L Thompson break; 4120d0321e0SJeremy L Thompson // LCOV_EXCL_START 4130d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: { 4146e536b99SJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 4150d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 4160d0321e0SJeremy L Thompson } 4170d0321e0SJeremy L Thompson } 418004e4986SSebastian Grimberg } 4190d0321e0SJeremy L Thompson 4200d0321e0SJeremy L Thompson // Output restriction 421ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 422004e4986SSebastian Grimberg CeedEvalMode eval_mode; 423ca735530SJeremy L Thompson CeedVector vec; 424edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 425ca735530SJeremy L Thompson 4260d0321e0SJeremy L Thompson // Restore evec 427004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 428004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 429ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields])); 4300d0321e0SJeremy L Thompson } 4310d0321e0SJeremy L Thompson // Get output vector 432ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 4330d0321e0SJeremy L Thompson // Restrict 434edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 4350d0321e0SJeremy L Thompson // Active 436ca735530SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = out_vec; 4370d0321e0SJeremy L Thompson 438edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request)); 4390d0321e0SJeremy L Thompson } 4400d0321e0SJeremy L Thompson 4410d0321e0SJeremy L Thompson // Restore input arrays 442ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl)); 4430d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4440d0321e0SJeremy L Thompson } 4450d0321e0SJeremy L Thompson 4460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 447756ca9e9SJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. 448756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 449756ca9e9SJeremy L Thompson static int CeedOperatorSetupAtPoints_Cuda(CeedOperator op) { 450756ca9e9SJeremy L Thompson Ceed ceed; 451756ca9e9SJeremy L Thompson bool is_setup_done; 452756ca9e9SJeremy L Thompson CeedInt max_num_points = -1, num_elem, num_input_fields, num_output_fields; 453756ca9e9SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 454756ca9e9SJeremy L Thompson CeedQFunction qf; 455756ca9e9SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 456756ca9e9SJeremy L Thompson CeedOperator_Cuda *impl; 457756ca9e9SJeremy L Thompson 458756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 459756ca9e9SJeremy L Thompson if (is_setup_done) return CEED_ERROR_SUCCESS; 460756ca9e9SJeremy L Thompson 461756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 462756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 463756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 464756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 465756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 466756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 467756ca9e9SJeremy L Thompson { 468756ca9e9SJeremy L Thompson CeedElemRestriction elem_rstr = NULL; 469756ca9e9SJeremy L Thompson 470756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL)); 471756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points)); 472756ca9e9SJeremy L Thompson } 473756ca9e9SJeremy L Thompson impl->max_num_points = max_num_points; 474756ca9e9SJeremy L Thompson 475756ca9e9SJeremy L Thompson // Allocate 476756ca9e9SJeremy L Thompson CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs)); 477756ca9e9SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states)); 478756ca9e9SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in)); 479756ca9e9SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out)); 480756ca9e9SJeremy L Thompson impl->num_inputs = num_input_fields; 481756ca9e9SJeremy L Thompson impl->num_outputs = num_output_fields; 482756ca9e9SJeremy L Thompson 483756ca9e9SJeremy L Thompson // Set up infield and outfield e_vecs and q_vecs 484756ca9e9SJeremy L Thompson // Infields 485756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, true, true, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, max_num_points, num_elem)); 486756ca9e9SJeremy L Thompson // Outfields 487756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, false, true, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, 488756ca9e9SJeremy L Thompson max_num_points, num_elem)); 489756ca9e9SJeremy L Thompson 490756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 491756ca9e9SJeremy L Thompson return CEED_ERROR_SUCCESS; 492756ca9e9SJeremy L Thompson } 493756ca9e9SJeremy L Thompson 494756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 495*67d9480aSJeremy L Thompson // Input Basis Action AtPoints 496756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 497756ca9e9SJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Cuda(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields, 498756ca9e9SJeremy L Thompson CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active, 499756ca9e9SJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) { 500756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 501756ca9e9SJeremy L Thompson CeedInt elem_size, size; 502756ca9e9SJeremy L Thompson CeedEvalMode eval_mode; 503756ca9e9SJeremy L Thompson CeedElemRestriction elem_rstr; 504756ca9e9SJeremy L Thompson CeedBasis basis; 505756ca9e9SJeremy L Thompson 506756ca9e9SJeremy L Thompson // Skip active input 507756ca9e9SJeremy L Thompson if (skip_active) { 508756ca9e9SJeremy L Thompson CeedVector vec; 509756ca9e9SJeremy L Thompson 510756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 511756ca9e9SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 512756ca9e9SJeremy L Thompson } 513756ca9e9SJeremy L Thompson // Get elem_size, eval_mode, size 514756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 515756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 516756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 517756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 518756ca9e9SJeremy L Thompson // Basis action 519756ca9e9SJeremy L Thompson switch (eval_mode) { 520756ca9e9SJeremy L Thompson case CEED_EVAL_NONE: 521756ca9e9SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i])); 522756ca9e9SJeremy L Thompson break; 523756ca9e9SJeremy L Thompson case CEED_EVAL_INTERP: 524756ca9e9SJeremy L Thompson case CEED_EVAL_GRAD: 525756ca9e9SJeremy L Thompson case CEED_EVAL_DIV: 526756ca9e9SJeremy L Thompson case CEED_EVAL_CURL: 527756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 528756ca9e9SJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i], 529756ca9e9SJeremy L Thompson impl->q_vecs_in[i])); 530756ca9e9SJeremy L Thompson break; 531756ca9e9SJeremy L Thompson case CEED_EVAL_WEIGHT: 532756ca9e9SJeremy L Thompson break; // No action 533756ca9e9SJeremy L Thompson } 534756ca9e9SJeremy L Thompson } 535756ca9e9SJeremy L Thompson return CEED_ERROR_SUCCESS; 536756ca9e9SJeremy L Thompson } 537756ca9e9SJeremy L Thompson 538756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 539*67d9480aSJeremy L Thompson // Apply and add to output AtPoints 540756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 541756ca9e9SJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) { 542756ca9e9SJeremy L Thompson CeedInt max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size; 543756ca9e9SJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX] = {NULL}; 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(CeedOperatorGetData(op, &impl)); 550756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 551756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 552756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 553756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 554756ca9e9SJeremy L Thompson CeedInt num_points[num_elem]; 555756ca9e9SJeremy L Thompson 556756ca9e9SJeremy L Thompson // Setup 557756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op)); 558756ca9e9SJeremy L Thompson max_num_points = impl->max_num_points; 559756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points; 560756ca9e9SJeremy L Thompson 561756ca9e9SJeremy L Thompson // Input Evecs and Restriction 562756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request)); 563756ca9e9SJeremy L Thompson 564756ca9e9SJeremy L Thompson // Get point coordinates 565756ca9e9SJeremy L Thompson if (!impl->point_coords_elem) { 566756ca9e9SJeremy L Thompson CeedVector point_coords = NULL; 567756ca9e9SJeremy L Thompson CeedElemRestriction rstr_points = NULL; 568756ca9e9SJeremy L Thompson 569756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords)); 570756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem)); 571756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request)); 572756ca9e9SJeremy L Thompson } 573756ca9e9SJeremy L Thompson 574756ca9e9SJeremy L Thompson // Input basis apply if needed 575756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl)); 576756ca9e9SJeremy L Thompson 577756ca9e9SJeremy L Thompson // Output pointers, as necessary 578756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 579756ca9e9SJeremy L Thompson CeedEvalMode eval_mode; 580756ca9e9SJeremy L Thompson 581756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 582756ca9e9SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) { 583756ca9e9SJeremy L Thompson // Set the output Q-Vector to use the E-Vector data directly. 584756ca9e9SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields])); 585756ca9e9SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields])); 586756ca9e9SJeremy L Thompson } 587756ca9e9SJeremy L Thompson } 588756ca9e9SJeremy L Thompson 589756ca9e9SJeremy L Thompson // Q function 590756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out)); 591756ca9e9SJeremy L Thompson 592756ca9e9SJeremy L Thompson // Output basis apply if needed 593756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 594756ca9e9SJeremy L Thompson CeedEvalMode eval_mode; 595756ca9e9SJeremy L Thompson CeedElemRestriction elem_rstr; 596756ca9e9SJeremy L Thompson CeedBasis basis; 597756ca9e9SJeremy L Thompson 598756ca9e9SJeremy L Thompson // Get elem_size, eval_mode, size 599756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 600756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 601756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 602756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 603756ca9e9SJeremy L Thompson // Basis action 604756ca9e9SJeremy L Thompson switch (eval_mode) { 605756ca9e9SJeremy L Thompson case CEED_EVAL_NONE: 606756ca9e9SJeremy L Thompson break; // No action 607756ca9e9SJeremy L Thompson case CEED_EVAL_INTERP: 608756ca9e9SJeremy L Thompson case CEED_EVAL_GRAD: 609756ca9e9SJeremy L Thompson case CEED_EVAL_DIV: 610756ca9e9SJeremy L Thompson case CEED_EVAL_CURL: 611756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 612756ca9e9SJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i], 613756ca9e9SJeremy L Thompson impl->e_vecs[i + impl->num_inputs])); 614756ca9e9SJeremy L Thompson break; 615756ca9e9SJeremy L Thompson // LCOV_EXCL_START 616756ca9e9SJeremy L Thompson case CEED_EVAL_WEIGHT: { 617756ca9e9SJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 618756ca9e9SJeremy L Thompson // LCOV_EXCL_STOP 619756ca9e9SJeremy L Thompson } 620756ca9e9SJeremy L Thompson } 621756ca9e9SJeremy L Thompson } 622756ca9e9SJeremy L Thompson 623756ca9e9SJeremy L Thompson // Output restriction 624756ca9e9SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 625756ca9e9SJeremy L Thompson CeedEvalMode eval_mode; 626756ca9e9SJeremy L Thompson CeedVector vec; 627756ca9e9SJeremy L Thompson CeedElemRestriction elem_rstr; 628756ca9e9SJeremy L Thompson 629756ca9e9SJeremy L Thompson // Restore evec 630756ca9e9SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 631756ca9e9SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) { 632756ca9e9SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields])); 633756ca9e9SJeremy L Thompson } 634756ca9e9SJeremy L Thompson // Get output vector 635756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 636756ca9e9SJeremy L Thompson // Restrict 637756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 638756ca9e9SJeremy L Thompson // Active 639756ca9e9SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = out_vec; 640756ca9e9SJeremy L Thompson 641756ca9e9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request)); 642756ca9e9SJeremy L Thompson } 643756ca9e9SJeremy L Thompson 644756ca9e9SJeremy L Thompson // Restore input arrays 645756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl)); 646756ca9e9SJeremy L Thompson return CEED_ERROR_SUCCESS; 647756ca9e9SJeremy L Thompson } 648756ca9e9SJeremy L Thompson 649756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 650004e4986SSebastian Grimberg // Linear QFunction Assembly Core 6510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6522b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr, 6530d0321e0SJeremy L Thompson CeedRequest *request) { 654ca735530SJeremy L Thompson Ceed ceed, ceed_parent; 655ca735530SJeremy L Thompson CeedInt num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size; 656ca735530SJeremy L Thompson CeedScalar *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL}; 657ca735530SJeremy L Thompson CeedVector *active_inputs; 658ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 659ca735530SJeremy L Thompson CeedQFunction qf; 660ca735530SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 661ca735530SJeremy L Thompson CeedOperator_Cuda *impl; 662ca735530SJeremy L Thompson 6632b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 664ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); 665e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 666e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 667ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 668e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 669ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 670ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 671ca735530SJeremy L Thompson active_inputs = impl->qf_active_in; 672ca735530SJeremy L Thompson num_active_in = impl->num_active_in, num_active_out = impl->num_active_out; 6730d0321e0SJeremy L Thompson 6740d0321e0SJeremy L Thompson // Setup 6752b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetup_Cuda(op)); 6760d0321e0SJeremy L Thompson 677004e4986SSebastian Grimberg // Input Evecs and Restriction 678ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request)); 6790d0321e0SJeremy L Thompson 6800d0321e0SJeremy L Thompson // Count number of active input fields 681ca735530SJeremy L Thompson if (!num_active_in) { 682ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 683ca735530SJeremy L Thompson CeedScalar *q_vec_array; 684ca735530SJeremy L Thompson CeedVector vec; 685ca735530SJeremy L Thompson 6860d0321e0SJeremy L Thompson // Get input vector 687ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 6880d0321e0SJeremy L Thompson // Check if active input 6890d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 690ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 691ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0)); 692ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array)); 693ca735530SJeremy L Thompson CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs)); 6940d0321e0SJeremy L Thompson for (CeedInt field = 0; field < size; field++) { 695004e4986SSebastian Grimberg CeedSize q_size = (CeedSize)Q * num_elem; 696004e4986SSebastian Grimberg 697ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field])); 698ca735530SJeremy L Thompson CeedCallBackend( 699ca735530SJeremy L Thompson CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem])); 7000d0321e0SJeremy L Thompson } 701ca735530SJeremy L Thompson num_active_in += size; 702ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array)); 7030d0321e0SJeremy L Thompson } 7040d0321e0SJeremy L Thompson } 705ca735530SJeremy L Thompson impl->num_active_in = num_active_in; 706ca735530SJeremy L Thompson impl->qf_active_in = active_inputs; 7070d0321e0SJeremy L Thompson } 7080d0321e0SJeremy L Thompson 7090d0321e0SJeremy L Thompson // Count number of active output fields 710ca735530SJeremy L Thompson if (!num_active_out) { 711ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 712ca735530SJeremy L Thompson CeedVector vec; 713ca735530SJeremy L Thompson 7140d0321e0SJeremy L Thompson // Get output vector 715ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 7160d0321e0SJeremy L Thompson // Check if active output 7170d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 718ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 719ca735530SJeremy L Thompson num_active_out += size; 7200d0321e0SJeremy L Thompson } 7210d0321e0SJeremy L Thompson } 722ca735530SJeremy L Thompson impl->num_active_out = num_active_out; 7230d0321e0SJeremy L Thompson } 7240d0321e0SJeremy L Thompson 7250d0321e0SJeremy L Thompson // Check sizes 726ca735530SJeremy L Thompson CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); 7270d0321e0SJeremy L Thompson 7280d0321e0SJeremy L Thompson // Build objects if needed 7290d0321e0SJeremy L Thompson if (build_objects) { 730004e4986SSebastian Grimberg CeedSize l_size = (CeedSize)num_elem * Q * num_active_in * num_active_out; 731ca735530SJeremy L Thompson CeedInt strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */ 732004e4986SSebastian Grimberg 733004e4986SSebastian Grimberg // Create output restriction 734ca735530SJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out, 735ca735530SJeremy L Thompson num_active_in * num_active_out * num_elem * Q, strides, rstr)); 7360d0321e0SJeremy L Thompson // Create assembled vector 737ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled)); 7380d0321e0SJeremy L Thompson } 7392b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(*assembled, 0.0)); 740ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array)); 7410d0321e0SJeremy L Thompson 7420d0321e0SJeremy L Thompson // Input basis apply 743ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl)); 7440d0321e0SJeremy L Thompson 7450d0321e0SJeremy L Thompson // Assemble QFunction 746ca735530SJeremy L Thompson for (CeedInt in = 0; in < num_active_in; in++) { 7470d0321e0SJeremy L Thompson // Set Inputs 748ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0)); 749ca735530SJeremy L Thompson if (num_active_in > 1) { 750ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0)); 7510d0321e0SJeremy L Thompson } 7520d0321e0SJeremy L Thompson // Set Outputs 753ca735530SJeremy L Thompson for (CeedInt out = 0; out < num_output_fields; out++) { 754ca735530SJeremy L Thompson CeedVector vec; 755ca735530SJeremy L Thompson 7560d0321e0SJeremy L Thompson // Get output vector 757ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec)); 7580d0321e0SJeremy L Thompson // Check if active output 7590d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 760ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array)); 761ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size)); 762ca735530SJeremy L Thompson assembled_array += size * Q * num_elem; // Advance the pointer by the size of the output 7630d0321e0SJeremy L Thompson } 7640d0321e0SJeremy L Thompson } 7650d0321e0SJeremy L Thompson // Apply QFunction 766ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out)); 7670d0321e0SJeremy L Thompson } 7680d0321e0SJeremy L Thompson 769ca735530SJeremy L Thompson // Un-set output q_vecs to prevent accidental overwrite of Assembled 770ca735530SJeremy L Thompson for (CeedInt out = 0; out < num_output_fields; out++) { 771ca735530SJeremy L Thompson CeedVector vec; 772ca735530SJeremy L Thompson 7730d0321e0SJeremy L Thompson // Get output vector 774ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec)); 7750d0321e0SJeremy L Thompson // Check if active output 7760d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 777ca735530SJeremy L Thompson CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL)); 7780d0321e0SJeremy L Thompson } 7790d0321e0SJeremy L Thompson } 7800d0321e0SJeremy L Thompson 7810d0321e0SJeremy L Thompson // Restore input arrays 782ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl)); 7830d0321e0SJeremy L Thompson 7840d0321e0SJeremy L Thompson // Restore output 785ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); 7860d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7870d0321e0SJeremy L Thompson } 7880d0321e0SJeremy L Thompson 7890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7900d0321e0SJeremy L Thompson // Assemble Linear QFunction 7910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7922b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 7932b730f8bSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, true, assembled, rstr, request); 7940d0321e0SJeremy L Thompson } 7950d0321e0SJeremy L Thompson 7960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7970d0321e0SJeremy L Thompson // Update Assembled Linear QFunction 7980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7992b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) { 8002b730f8bSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, false, &assembled, &rstr, request); 8010d0321e0SJeremy L Thompson } 8020d0321e0SJeremy L Thompson 8030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 804004e4986SSebastian Grimberg // Assemble Diagonal Setup 8050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 806cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op) { 8070d0321e0SJeremy L Thompson Ceed ceed; 808004e4986SSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 809cbfe683aSSebastian Grimberg CeedInt q_comp, num_nodes, num_qpts; 810004e4986SSebastian Grimberg CeedEvalMode *eval_modes_in = NULL, *eval_modes_out = NULL; 811ca735530SJeremy L Thompson CeedBasis basis_in = NULL, basis_out = NULL; 812ca735530SJeremy L Thompson CeedQFunctionField *qf_fields; 8130d0321e0SJeremy L Thompson CeedQFunction qf; 814ca735530SJeremy L Thompson CeedOperatorField *op_fields; 815ca735530SJeremy L Thompson CeedOperator_Cuda *impl; 816ca735530SJeremy L Thompson 817ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 8182b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 819ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields)); 8200d0321e0SJeremy L Thompson 8210d0321e0SJeremy L Thompson // Determine active input basis 822ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 823ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 824ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 8250d0321e0SJeremy L Thompson CeedVector vec; 826ca735530SJeremy L Thompson 827ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 8280d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 829004e4986SSebastian Grimberg CeedBasis basis; 830004e4986SSebastian Grimberg CeedEvalMode eval_mode; 831ca735530SJeremy L Thompson 832004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 833004e4986SSebastian Grimberg CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, 834004e4986SSebastian Grimberg "Backend does not implement operator diagonal assembly with multiple active bases"); 835004e4986SSebastian Grimberg basis_in = basis; 836004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 837004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 838004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 839004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly 840004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in)); 841004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode; 842004e4986SSebastian Grimberg num_eval_modes_in += q_comp; 8430d0321e0SJeremy L Thompson } 8440d0321e0SJeremy L Thompson } 8450d0321e0SJeremy L Thompson } 8460d0321e0SJeremy L Thompson 8470d0321e0SJeremy L Thompson // Determine active output basis 848ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 849ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 850ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 8510d0321e0SJeremy L Thompson CeedVector vec; 852ca735530SJeremy L Thompson 853ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 8540d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 855004e4986SSebastian Grimberg CeedBasis basis; 856004e4986SSebastian Grimberg CeedEvalMode eval_mode; 857ca735530SJeremy L Thompson 858004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 859004e4986SSebastian Grimberg CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND, 860004e4986SSebastian Grimberg "Backend does not implement operator diagonal assembly with multiple active bases"); 861004e4986SSebastian Grimberg basis_out = basis; 862004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 863004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 864004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 865004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly 866004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out)); 867004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode; 868004e4986SSebastian Grimberg num_eval_modes_out += q_comp; 8690d0321e0SJeremy L Thompson } 8700d0321e0SJeremy L Thompson } 8710d0321e0SJeremy L Thompson } 8720d0321e0SJeremy L Thompson 8730d0321e0SJeremy L Thompson // Operator data struct 8742b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 8752b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl->diag)); 8760d0321e0SJeremy L Thompson CeedOperatorDiag_Cuda *diag = impl->diag; 877ca735530SJeremy L Thompson 878cbfe683aSSebastian Grimberg // Basis matrices 879004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes)); 880004e4986SSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes; 881004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts)); 882004e4986SSebastian Grimberg const CeedInt interp_bytes = num_nodes * num_qpts * sizeof(CeedScalar); 883004e4986SSebastian Grimberg const CeedInt eval_modes_bytes = sizeof(CeedEvalMode); 884004e4986SSebastian Grimberg bool has_eval_none = false; 8850d0321e0SJeremy L Thompson 8860d0321e0SJeremy L Thompson // CEED_EVAL_NONE 887004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE); 888004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE); 889004e4986SSebastian Grimberg if (has_eval_none) { 8900d0321e0SJeremy L Thompson CeedScalar *identity = NULL; 891ca735530SJeremy L Thompson 892004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity)); 893ca735530SJeremy L Thompson for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0; 894ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_identity, interp_bytes)); 895ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(diag->d_identity, identity, interp_bytes, cudaMemcpyHostToDevice)); 896004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 8970d0321e0SJeremy L Thompson } 8980d0321e0SJeremy L Thompson 899004e4986SSebastian Grimberg // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL 900004e4986SSebastian Grimberg for (CeedInt in = 0; in < 2; in++) { 901004e4986SSebastian Grimberg CeedFESpace fespace; 902004e4986SSebastian Grimberg CeedBasis basis = in ? basis_in : basis_out; 9030d0321e0SJeremy L Thompson 904004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetFESpace(basis, &fespace)); 905004e4986SSebastian Grimberg switch (fespace) { 906004e4986SSebastian Grimberg case CEED_FE_SPACE_H1: { 907004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_grad; 908004e4986SSebastian Grimberg const CeedScalar *interp, *grad; 909004e4986SSebastian Grimberg CeedScalar *d_interp, *d_grad; 9100d0321e0SJeremy L Thompson 911004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 912004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad)); 9130d0321e0SJeremy L Thompson 914004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 915004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 916004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice)); 917004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetGrad(basis, &grad)); 918004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_grad, interp_bytes * q_comp_grad)); 919004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_grad, grad, interp_bytes * q_comp_grad, cudaMemcpyHostToDevice)); 920004e4986SSebastian Grimberg if (in) { 921004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 922004e4986SSebastian Grimberg diag->d_grad_in = d_grad; 923004e4986SSebastian Grimberg } else { 924004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 925004e4986SSebastian Grimberg diag->d_grad_out = d_grad; 926004e4986SSebastian Grimberg } 927004e4986SSebastian Grimberg } break; 928004e4986SSebastian Grimberg case CEED_FE_SPACE_HDIV: { 929004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_div; 930004e4986SSebastian Grimberg const CeedScalar *interp, *div; 931004e4986SSebastian Grimberg CeedScalar *d_interp, *d_div; 932004e4986SSebastian Grimberg 933004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 934004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div)); 935004e4986SSebastian Grimberg 936004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 937004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 938004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice)); 939004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetDiv(basis, &div)); 940004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_div, interp_bytes * q_comp_div)); 941004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_div, div, interp_bytes * q_comp_div, cudaMemcpyHostToDevice)); 942004e4986SSebastian Grimberg if (in) { 943004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 944004e4986SSebastian Grimberg diag->d_div_in = d_div; 945004e4986SSebastian Grimberg } else { 946004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 947004e4986SSebastian Grimberg diag->d_div_out = d_div; 948004e4986SSebastian Grimberg } 949004e4986SSebastian Grimberg } break; 950004e4986SSebastian Grimberg case CEED_FE_SPACE_HCURL: { 951004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_curl; 952004e4986SSebastian Grimberg const CeedScalar *interp, *curl; 953004e4986SSebastian Grimberg CeedScalar *d_interp, *d_curl; 954004e4986SSebastian Grimberg 955004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 956004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl)); 957004e4986SSebastian Grimberg 958004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 959004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 960004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice)); 961004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetCurl(basis, &curl)); 962004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_curl, interp_bytes * q_comp_curl)); 963004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_curl, curl, interp_bytes * q_comp_curl, cudaMemcpyHostToDevice)); 964004e4986SSebastian Grimberg if (in) { 965004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 966004e4986SSebastian Grimberg diag->d_curl_in = d_curl; 967004e4986SSebastian Grimberg } else { 968004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 969004e4986SSebastian Grimberg diag->d_curl_out = d_curl; 970004e4986SSebastian Grimberg } 971004e4986SSebastian Grimberg } break; 972004e4986SSebastian Grimberg } 973004e4986SSebastian Grimberg } 974004e4986SSebastian Grimberg 975004e4986SSebastian Grimberg // Arrays of eval_modes 976004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes)); 977004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, cudaMemcpyHostToDevice)); 978004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes)); 979004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, cudaMemcpyHostToDevice)); 980004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&eval_modes_in)); 981004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&eval_modes_out)); 9820d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9830d0321e0SJeremy L Thompson } 9840d0321e0SJeremy L Thompson 9850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 986cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation) 987cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------ 988cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Cuda(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) { 989cbfe683aSSebastian Grimberg Ceed ceed; 99022070f95SJeremy L Thompson char *diagonal_kernel_source; 99122070f95SJeremy L Thompson const char *diagonal_kernel_path; 992cbfe683aSSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 993cbfe683aSSebastian Grimberg CeedInt num_comp, q_comp, num_nodes, num_qpts; 994cbfe683aSSebastian Grimberg CeedBasis basis_in = NULL, basis_out = NULL; 995cbfe683aSSebastian Grimberg CeedQFunctionField *qf_fields; 996cbfe683aSSebastian Grimberg CeedQFunction qf; 997cbfe683aSSebastian Grimberg CeedOperatorField *op_fields; 998cbfe683aSSebastian Grimberg CeedOperator_Cuda *impl; 999cbfe683aSSebastian Grimberg 1000cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1001cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1002cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields)); 1003cbfe683aSSebastian Grimberg 1004cbfe683aSSebastian Grimberg // Determine active input basis 1005cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 1006cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 1007cbfe683aSSebastian Grimberg for (CeedInt i = 0; i < num_input_fields; i++) { 1008cbfe683aSSebastian Grimberg CeedVector vec; 1009cbfe683aSSebastian Grimberg 1010cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1011cbfe683aSSebastian Grimberg if (vec == CEED_VECTOR_ACTIVE) { 1012cbfe683aSSebastian Grimberg CeedEvalMode eval_mode; 1013cbfe683aSSebastian Grimberg 1014cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in)); 1015cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1016cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 1017cbfe683aSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1018cbfe683aSSebastian Grimberg num_eval_modes_in += q_comp; 1019cbfe683aSSebastian Grimberg } 1020cbfe683aSSebastian Grimberg } 1021cbfe683aSSebastian Grimberg } 1022cbfe683aSSebastian Grimberg 1023cbfe683aSSebastian Grimberg // Determine active output basis 1024cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 1025cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1026cbfe683aSSebastian Grimberg for (CeedInt i = 0; i < num_output_fields; i++) { 1027cbfe683aSSebastian Grimberg CeedVector vec; 1028cbfe683aSSebastian Grimberg 1029cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1030cbfe683aSSebastian Grimberg if (vec == CEED_VECTOR_ACTIVE) { 1031cbfe683aSSebastian Grimberg CeedEvalMode eval_mode; 1032cbfe683aSSebastian Grimberg 1033cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out)); 1034cbfe683aSSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1035cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 1036cbfe683aSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1037cbfe683aSSebastian Grimberg num_eval_modes_out += q_comp; 1038cbfe683aSSebastian Grimberg } 1039cbfe683aSSebastian Grimberg } 1040cbfe683aSSebastian Grimberg } 1041cbfe683aSSebastian Grimberg 1042cbfe683aSSebastian Grimberg // Operator data struct 1043cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorGetData(op, &impl)); 1044cbfe683aSSebastian Grimberg CeedOperatorDiag_Cuda *diag = impl->diag; 1045cbfe683aSSebastian Grimberg 1046cbfe683aSSebastian Grimberg // Assemble kernel 1047cbfe683aSSebastian Grimberg CUmodule *module = is_point_block ? &diag->module_point_block : &diag->module; 1048cbfe683aSSebastian Grimberg CeedInt elems_per_block = 1; 1049cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes)); 1050cbfe683aSSebastian Grimberg CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp)); 1051cbfe683aSSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes; 1052cbfe683aSSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts)); 1053cbfe683aSSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble-diagonal.h", &diagonal_kernel_path)); 1054cbfe683aSSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n"); 1055cbfe683aSSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source)); 1056cbfe683aSSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n"); 1057cbfe683aSSebastian Grimberg CeedCallCuda(ceed, CeedCompile_Cuda(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT", 1058cbfe683aSSebastian Grimberg num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE", 1059cbfe683aSSebastian Grimberg use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block)); 1060cbfe683aSSebastian Grimberg CeedCallCuda(ceed, CeedGetKernel_Cuda(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal)); 1061cbfe683aSSebastian Grimberg CeedCallBackend(CeedFree(&diagonal_kernel_path)); 1062cbfe683aSSebastian Grimberg CeedCallBackend(CeedFree(&diagonal_kernel_source)); 1063cbfe683aSSebastian Grimberg return CEED_ERROR_SUCCESS; 1064cbfe683aSSebastian Grimberg } 1065cbfe683aSSebastian Grimberg 1066cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------ 1067004e4986SSebastian Grimberg // Assemble Diagonal Core 10680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1069ca735530SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) { 10700d0321e0SJeremy L Thompson Ceed ceed; 1071cbfe683aSSebastian Grimberg CeedInt num_elem, num_nodes; 1072ca735530SJeremy L Thompson CeedScalar *elem_diag_array; 1073ca735530SJeremy L Thompson const CeedScalar *assembled_qf_array; 1074004e4986SSebastian Grimberg CeedVector assembled_qf = NULL, elem_diag; 1075004e4986SSebastian Grimberg CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr; 10760d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 1077ca735530SJeremy L Thompson 1078ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 10792b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 10800d0321e0SJeremy L Thompson 10810d0321e0SJeremy L Thompson // Assemble QFunction 1082004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request)); 1083004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr)); 1084004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array)); 10850d0321e0SJeremy L Thompson 1086cbfe683aSSebastian Grimberg // Setup 1087cbfe683aSSebastian Grimberg if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Cuda(op)); 1088cbfe683aSSebastian Grimberg CeedOperatorDiag_Cuda *diag = impl->diag; 1089cbfe683aSSebastian Grimberg 1090cbfe683aSSebastian Grimberg assert(diag != NULL); 1091cbfe683aSSebastian Grimberg 1092cbfe683aSSebastian Grimberg // Assemble kernel if needed 1093cbfe683aSSebastian Grimberg if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) { 1094cbfe683aSSebastian Grimberg CeedSize assembled_length, assembled_qf_length; 1095cbfe683aSSebastian Grimberg CeedInt use_ceedsize_idx = 0; 1096f7c1b517Snbeams CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length)); 1097ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length)); 1098ca735530SJeremy L Thompson if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1; 1099f7c1b517Snbeams 1100cbfe683aSSebastian Grimberg CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Cuda(op, use_ceedsize_idx, is_point_block)); 1101cbfe683aSSebastian Grimberg } 11020d0321e0SJeremy L Thompson 1103004e4986SSebastian Grimberg // Restriction and diagonal vector 1104004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 1105004e4986SSebastian Grimberg CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND, 1106004e4986SSebastian Grimberg "Cannot assemble operator diagonal with different input and output active element restrictions"); 1107004e4986SSebastian Grimberg if (!is_point_block && !diag->diag_rstr) { 1108004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr)); 1109004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag)); 1110004e4986SSebastian Grimberg } else if (is_point_block && !diag->point_block_diag_rstr) { 1111004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr)); 1112004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag)); 11130d0321e0SJeremy L Thompson } 1114004e4986SSebastian Grimberg diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr; 1115004e4986SSebastian Grimberg elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag; 1116ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0)); 11170d0321e0SJeremy L Thompson 111891db28b6SZach Atkins // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers 1119004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes)); 1120004e4986SSebastian Grimberg if (num_nodes > 0) { 11210d0321e0SJeremy L Thompson // Assemble element operator diagonals 1122ca735530SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem)); 1123004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array)); 11240d0321e0SJeremy L Thompson 11250d0321e0SJeremy L Thompson // Compute the diagonal of B^T D B 1126004e4986SSebastian Grimberg CeedInt elems_per_block = 1; 1127004e4986SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem, elems_per_block); 1128004e4986SSebastian Grimberg void *args[] = {(void *)&num_elem, &diag->d_identity, &diag->d_interp_in, &diag->d_grad_in, &diag->d_div_in, 1129004e4986SSebastian Grimberg &diag->d_curl_in, &diag->d_interp_out, &diag->d_grad_out, &diag->d_div_out, &diag->d_curl_out, 1130004e4986SSebastian Grimberg &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array}; 1131004e4986SSebastian Grimberg 1132ca735530SJeremy L Thompson if (is_point_block) { 1133004e4986SSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args)); 11340d0321e0SJeremy L Thompson } else { 1135004e4986SSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args)); 11360d0321e0SJeremy L Thompson } 11370d0321e0SJeremy L Thompson 11380d0321e0SJeremy L Thompson // Restore arrays 1139ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array)); 1140ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 114191db28b6SZach Atkins } 11420d0321e0SJeremy L Thompson 11430d0321e0SJeremy L Thompson // Assemble local operator diagonal 1144ca735530SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); 11450d0321e0SJeremy L Thompson 11460d0321e0SJeremy L Thompson // Cleanup 1147ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 11480d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11490d0321e0SJeremy L Thompson } 11500d0321e0SJeremy L Thompson 11510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 11520d0321e0SJeremy L Thompson // Assemble Linear Diagonal 11530d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 11542b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) { 11552b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, false)); 11566aa95790SJeremy L Thompson return CEED_ERROR_SUCCESS; 11570d0321e0SJeremy L Thompson } 11580d0321e0SJeremy L Thompson 11590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 11600d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal 11610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 11622b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) { 11632b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, true)); 11646aa95790SJeremy L Thompson return CEED_ERROR_SUCCESS; 11650d0321e0SJeremy L Thompson } 11660d0321e0SJeremy L Thompson 11670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1168004e4986SSebastian Grimberg // Single Operator Assembly Setup 1169cc132f9aSnbeams //------------------------------------------------------------------------------ 1170f7c1b517Snbeams static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op, CeedInt use_ceedsize_idx) { 1171cc132f9aSnbeams Ceed ceed; 1172004e4986SSebastian Grimberg Ceed_Cuda *cuda_data; 117322070f95SJeremy L Thompson char *assembly_kernel_source; 117422070f95SJeremy L Thompson const char *assembly_kernel_path; 1175004e4986SSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 11763b38d1dfSJeremy L Thompson CeedInt elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp; 1177004e4986SSebastian Grimberg CeedEvalMode *eval_modes_in = NULL, *eval_modes_out = NULL; 1178ca735530SJeremy L Thompson CeedElemRestriction rstr_in = NULL, rstr_out = NULL; 1179ca735530SJeremy L Thompson CeedBasis basis_in = NULL, basis_out = NULL; 1180ca735530SJeremy L Thompson CeedQFunctionField *qf_fields; 1181ca735530SJeremy L Thompson CeedQFunction qf; 1182ca735530SJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 1183cc132f9aSnbeams CeedOperator_Cuda *impl; 1184ca735530SJeremy L Thompson 1185ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 11862b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1187cc132f9aSnbeams 1188cc132f9aSnbeams // Get intput and output fields 11892b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 1190cc132f9aSnbeams 1191cc132f9aSnbeams // Determine active input basis eval mode 11922b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 11932b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 1194cc132f9aSnbeams for (CeedInt i = 0; i < num_input_fields; i++) { 1195cc132f9aSnbeams CeedVector vec; 1196ca735530SJeremy L Thompson 11972b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec)); 1198cc132f9aSnbeams if (vec == CEED_VECTOR_ACTIVE) { 1199004e4986SSebastian Grimberg CeedBasis basis; 1200ca735530SJeremy L Thompson CeedEvalMode eval_mode; 1201ca735530SJeremy L Thompson 1202004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis)); 1203004e4986SSebastian Grimberg CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases"); 1204004e4986SSebastian Grimberg basis_in = basis; 12052b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in)); 1206004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 1207004e4986SSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in; 1208004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in)); 12092b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1210004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 1211004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1212004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1213004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in)); 1214004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1215004e4986SSebastian Grimberg eval_modes_in[num_eval_modes_in + d] = eval_mode; 1216cc132f9aSnbeams } 1217004e4986SSebastian Grimberg num_eval_modes_in += q_comp; 1218cc132f9aSnbeams } 1219cc132f9aSnbeams } 1220cc132f9aSnbeams } 1221cc132f9aSnbeams 1222cc132f9aSnbeams // Determine active output basis; basis_out and rstr_out only used if same as input, TODO 12232b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1224cc132f9aSnbeams for (CeedInt i = 0; i < num_output_fields; i++) { 1225cc132f9aSnbeams CeedVector vec; 1226ca735530SJeremy L Thompson 12272b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec)); 1228cc132f9aSnbeams if (vec == CEED_VECTOR_ACTIVE) { 1229004e4986SSebastian Grimberg CeedBasis basis; 1230ca735530SJeremy L Thompson CeedEvalMode eval_mode; 1231ca735530SJeremy L Thompson 1232004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis)); 1233004e4986SSebastian Grimberg CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND, 1234004e4986SSebastian Grimberg "Backend does not implement operator assembly with multiple active bases"); 1235004e4986SSebastian Grimberg basis_out = basis; 12362b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out)); 1237004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 1238004e4986SSebastian Grimberg if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out; 1239004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out)); 1240004e4986SSebastian Grimberg CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED, 1241004e4986SSebastian Grimberg "Active input and output bases must have the same number of quadrature points"); 12422b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1243004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 1244004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1245004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1246004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out)); 1247004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1248004e4986SSebastian Grimberg eval_modes_out[num_eval_modes_out + d] = eval_mode; 1249004e4986SSebastian Grimberg } 1250004e4986SSebastian Grimberg num_eval_modes_out += q_comp; 1251cc132f9aSnbeams } 1252cc132f9aSnbeams } 1253cc132f9aSnbeams } 1254004e4986SSebastian Grimberg CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs"); 1255cc132f9aSnbeams 12562b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl->asmb)); 1257cc132f9aSnbeams CeedOperatorAssemble_Cuda *asmb = impl->asmb; 1258004e4986SSebastian Grimberg asmb->elems_per_block = 1; 1259004e4986SSebastian Grimberg asmb->block_size_x = elem_size_in; 1260004e4986SSebastian Grimberg asmb->block_size_y = elem_size_out; 1261ca735530SJeremy L Thompson 12622b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &cuda_data)); 1263004e4986SSebastian Grimberg bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > cuda_data->device_prop.maxThreadsPerBlock; 1264004e4986SSebastian Grimberg 1265004e4986SSebastian Grimberg if (fallback) { 1266004e4986SSebastian Grimberg // Use fallback kernel with 1D threadblock 1267004e4986SSebastian Grimberg asmb->block_size_y = 1; 1268004e4986SSebastian Grimberg } 1269004e4986SSebastian Grimberg 1270004e4986SSebastian Grimberg // Compile kernels 1271004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in)); 1272004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out)); 12732b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble.h", &assembly_kernel_path)); 127423d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n"); 12752b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source)); 127623d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n"); 1277004e4986SSebastian Grimberg CeedCallBackend(CeedCompile_Cuda(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT", 1278004e4986SSebastian Grimberg num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in, 1279004e4986SSebastian Grimberg "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE", 1280cbfe683aSSebastian Grimberg asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, 1281cbfe683aSSebastian Grimberg "USE_CEEDSIZE", use_ceedsize_idx)); 1282004e4986SSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble)); 12832b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&assembly_kernel_path)); 12842b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&assembly_kernel_source)); 1285cc132f9aSnbeams 1286004e4986SSebastian Grimberg // Load into B_in, in order that they will be used in eval_modes_in 1287004e4986SSebastian Grimberg { 1288004e4986SSebastian Grimberg const CeedInt in_bytes = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar); 1289004e4986SSebastian Grimberg CeedInt d_in = 0; 1290004e4986SSebastian Grimberg CeedEvalMode eval_modes_in_prev = CEED_EVAL_NONE; 1291004e4986SSebastian Grimberg bool has_eval_none = false; 1292004e4986SSebastian Grimberg CeedScalar *identity = NULL; 1293ca735530SJeremy L Thompson 1294004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) { 1295004e4986SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE); 1296004e4986SSebastian Grimberg } 1297004e4986SSebastian Grimberg if (has_eval_none) { 1298004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity)); 1299004e4986SSebastian 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; 1300004e4986SSebastian Grimberg } 1301cc132f9aSnbeams 1302004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_in, in_bytes)); 1303004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) { 1304004e4986SSebastian Grimberg const CeedScalar *h_B_in; 1305ca735530SJeremy L Thompson 1306004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in)); 1307004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp)); 1308004e4986SSebastian Grimberg if (q_comp > 1) { 1309004e4986SSebastian Grimberg if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0; 1310004e4986SSebastian Grimberg else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in]; 1311004e4986SSebastian Grimberg } 1312004e4986SSebastian Grimberg eval_modes_in_prev = eval_modes_in[i]; 1313ca735530SJeremy L Thompson 1314004e4986SSebastian 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), 1315004e4986SSebastian Grimberg cudaMemcpyHostToDevice)); 1316004e4986SSebastian Grimberg } 1317004e4986SSebastian Grimberg 1318004e4986SSebastian Grimberg if (identity) { 1319004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 1320cc132f9aSnbeams } 1321cc132f9aSnbeams } 1322cc132f9aSnbeams 1323004e4986SSebastian Grimberg // Load into B_out, in order that they will be used in eval_modes_out 1324004e4986SSebastian Grimberg { 1325004e4986SSebastian Grimberg const CeedInt out_bytes = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar); 1326004e4986SSebastian Grimberg CeedInt d_out = 0; 1327004e4986SSebastian Grimberg CeedEvalMode eval_modes_out_prev = CEED_EVAL_NONE; 1328004e4986SSebastian Grimberg bool has_eval_none = false; 1329004e4986SSebastian Grimberg CeedScalar *identity = NULL; 1330ca735530SJeremy L Thompson 1331004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) { 1332004e4986SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE); 1333004e4986SSebastian Grimberg } 1334004e4986SSebastian Grimberg if (has_eval_none) { 1335004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity)); 1336004e4986SSebastian 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; 1337cc132f9aSnbeams } 1338cc132f9aSnbeams 1339004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_out, out_bytes)); 1340004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) { 1341004e4986SSebastian Grimberg const CeedScalar *h_B_out; 1342ca735530SJeremy L Thompson 1343004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out)); 1344004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp)); 1345004e4986SSebastian Grimberg if (q_comp > 1) { 1346004e4986SSebastian Grimberg if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0; 1347004e4986SSebastian Grimberg else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out]; 1348004e4986SSebastian Grimberg } 1349004e4986SSebastian Grimberg eval_modes_out_prev = eval_modes_out[i]; 1350ca735530SJeremy L Thompson 1351004e4986SSebastian 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), 1352004e4986SSebastian Grimberg cudaMemcpyHostToDevice)); 1353004e4986SSebastian Grimberg } 1354004e4986SSebastian Grimberg 1355004e4986SSebastian Grimberg if (identity) { 1356004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 1357cc132f9aSnbeams } 1358cc132f9aSnbeams } 1359cc132f9aSnbeams return CEED_ERROR_SUCCESS; 1360cc132f9aSnbeams } 1361cc132f9aSnbeams 1362cc132f9aSnbeams //------------------------------------------------------------------------------ 1363cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator. 1364cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic. 1365cefa2673SJeremy L Thompson // 1366ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval 1367cefa2673SJeremy L Thompson // modes). 1368cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects 1369cc132f9aSnbeams //------------------------------------------------------------------------------ 13702b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Cuda(CeedOperator op, CeedInt offset, CeedVector values) { 1371cc132f9aSnbeams Ceed ceed; 1372ca735530SJeremy L Thompson CeedSize values_length = 0, assembled_qf_length = 0; 1373004e4986SSebastian Grimberg CeedInt use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out; 1374ca735530SJeremy L Thompson CeedScalar *values_array; 1375004e4986SSebastian Grimberg const CeedScalar *assembled_qf_array; 1376ca735530SJeremy L Thompson CeedVector assembled_qf = NULL; 1377004e4986SSebastian Grimberg CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out; 1378004e4986SSebastian Grimberg CeedRestrictionType rstr_type_in, rstr_type_out; 1379004e4986SSebastian Grimberg const bool *orients_in = NULL, *orients_out = NULL; 1380004e4986SSebastian Grimberg const CeedInt8 *curl_orients_in = NULL, *curl_orients_out = NULL; 1381cc132f9aSnbeams CeedOperator_Cuda *impl; 1382ca735530SJeremy L Thompson 1383ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 13842b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1385cc132f9aSnbeams 1386cc132f9aSnbeams // Assemble QFunction 1387004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE)); 1388004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr)); 1389004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array)); 1390cc132f9aSnbeams 1391f7c1b517Snbeams CeedCallBackend(CeedVectorGetLength(values, &values_length)); 1392f7c1b517Snbeams CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length)); 1393f7c1b517Snbeams if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1; 1394004e4986SSebastian Grimberg 1395f7c1b517Snbeams // Setup 1396004e4986SSebastian Grimberg if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Cuda(op, use_ceedsize_idx)); 1397004e4986SSebastian Grimberg CeedOperatorAssemble_Cuda *asmb = impl->asmb; 1398004e4986SSebastian Grimberg 1399004e4986SSebastian Grimberg assert(asmb != NULL); 1400004e4986SSebastian Grimberg 1401004e4986SSebastian Grimberg // Assemble element operator 1402004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array)); 1403004e4986SSebastian Grimberg values_array += offset; 1404004e4986SSebastian Grimberg 1405004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 1406004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in)); 1407004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 1408004e4986SSebastian Grimberg 1409004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in)); 1410004e4986SSebastian Grimberg if (rstr_type_in == CEED_RESTRICTION_ORIENTED) { 1411004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in)); 1412004e4986SSebastian Grimberg } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 1413004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in)); 1414004e4986SSebastian Grimberg } 1415004e4986SSebastian Grimberg 1416004e4986SSebastian Grimberg if (rstr_in != rstr_out) { 1417004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out)); 1418004e4986SSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 1419004e4986SSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 1420004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 1421004e4986SSebastian Grimberg 1422004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out)); 1423004e4986SSebastian Grimberg if (rstr_type_out == CEED_RESTRICTION_ORIENTED) { 1424004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out)); 1425004e4986SSebastian Grimberg } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 1426004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out)); 1427004e4986SSebastian Grimberg } 1428004e4986SSebastian Grimberg } else { 1429004e4986SSebastian Grimberg elem_size_out = elem_size_in; 1430004e4986SSebastian Grimberg orients_out = orients_in; 1431004e4986SSebastian Grimberg curl_orients_out = curl_orients_in; 1432f7c1b517Snbeams } 1433f7c1b517Snbeams 1434cc132f9aSnbeams // Compute B^T D B 1435004e4986SSebastian Grimberg CeedInt shared_mem = 1436004e4986SSebastian 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)) * 1437004e4986SSebastian Grimberg sizeof(CeedScalar); 1438004e4986SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem_in, asmb->elems_per_block); 1439004e4986SSebastian Grimberg void *args[] = {(void *)&num_elem_in, &asmb->d_B_in, &asmb->d_B_out, &orients_in, &curl_orients_in, 1440004e4986SSebastian Grimberg &orients_out, &curl_orients_out, &assembled_qf_array, &values_array}; 1441ca735530SJeremy L Thompson 14422b730f8bSJeremy L Thompson CeedCallBackend( 1443004e4986SSebastian Grimberg CeedRunKernelDimShared_Cuda(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args)); 1444cc132f9aSnbeams 1445cc132f9aSnbeams // Restore arrays 14462b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(values, &values_array)); 1447004e4986SSebastian Grimberg CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 1448cc132f9aSnbeams 1449cc132f9aSnbeams // Cleanup 14502b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 1451004e4986SSebastian Grimberg if (rstr_type_in == CEED_RESTRICTION_ORIENTED) { 1452004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in)); 1453004e4986SSebastian Grimberg } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 1454004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in)); 1455004e4986SSebastian Grimberg } 1456004e4986SSebastian Grimberg if (rstr_in != rstr_out) { 1457004e4986SSebastian Grimberg if (rstr_type_out == CEED_RESTRICTION_ORIENTED) { 1458004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out)); 1459004e4986SSebastian Grimberg } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 1460004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out)); 1461004e4986SSebastian Grimberg } 1462004e4986SSebastian Grimberg } 1463cc132f9aSnbeams return CEED_ERROR_SUCCESS; 1464cc132f9aSnbeams } 1465cc132f9aSnbeams 1466cc132f9aSnbeams //------------------------------------------------------------------------------ 1467756ca9e9SJeremy L Thompson // Assemble Linear QFunction AtPoints 1468756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 1469756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 1470756ca9e9SJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction"); 1471756ca9e9SJeremy L Thompson } 1472756ca9e9SJeremy L Thompson 1473756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 1474756ca9e9SJeremy L Thompson // Assemble Linear Diagonal AtPoints 1475756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 1476756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) { 1477756ca9e9SJeremy L Thompson return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedSingleOperatorLinearAssembleAddDiagonal"); 1478756ca9e9SJeremy L Thompson } 1479756ca9e9SJeremy L Thompson 1480756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 14810d0321e0SJeremy L Thompson // Create operator 14820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 14830d0321e0SJeremy L Thompson int CeedOperatorCreate_Cuda(CeedOperator op) { 14840d0321e0SJeremy L Thompson Ceed ceed; 14850d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 14860d0321e0SJeremy L Thompson 1487ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 14882b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 14892b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl)); 14900d0321e0SJeremy L Thompson 14912b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda)); 14922b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Cuda)); 14932b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Cuda)); 14942b730f8bSJeremy L Thompson CeedCallBackend( 14952b730f8bSJeremy L Thompson CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda)); 14962b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Cuda)); 14972b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda)); 14982b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda)); 14990d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15000d0321e0SJeremy L Thompson } 15010d0321e0SJeremy L Thompson 15020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1503*67d9480aSJeremy L Thompson // Create operator AtPoints 1504756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 1505756ca9e9SJeremy L Thompson int CeedOperatorCreateAtPoints_Cuda(CeedOperator op) { 1506756ca9e9SJeremy L Thompson Ceed ceed; 1507756ca9e9SJeremy L Thompson CeedOperator_Cuda *impl; 1508756ca9e9SJeremy L Thompson 1509756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1510756ca9e9SJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 1511756ca9e9SJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl)); 1512756ca9e9SJeremy L Thompson 1513756ca9e9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Cuda)); 1514756ca9e9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda)); 1515756ca9e9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Cuda)); 1516756ca9e9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda)); 1517756ca9e9SJeremy L Thompson return CEED_ERROR_SUCCESS; 1518756ca9e9SJeremy L Thompson } 1519756ca9e9SJeremy L Thompson 1520756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------ 1521