13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 30d0321e0SJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 50d0321e0SJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 70d0321e0SJeremy L Thompson 849aac155SJeremy L Thompson #include <ceed.h> 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)); 340d0321e0SJeremy L Thompson 35ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_inputs; i++) { 36ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i])); 370d0321e0SJeremy L Thompson } 38ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->q_vecs_in)); 390d0321e0SJeremy L Thompson 40ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_outputs; i++) { 41ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i])); 420d0321e0SJeremy L Thompson } 43ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->q_vecs_out)); 440d0321e0SJeremy L Thompson 450d0321e0SJeremy L Thompson // QFunction assembly data 46ca735530SJeremy L Thompson for (CeedInt i = 0; i < impl->num_active_in; i++) { 47ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i])); 480d0321e0SJeremy L Thompson } 49ca735530SJeremy L Thompson CeedCallBackend(CeedFree(&impl->qf_active_in)); 500d0321e0SJeremy L Thompson 510d0321e0SJeremy L Thompson // Diag data 520d0321e0SJeremy L Thompson if (impl->diag) { 530d0321e0SJeremy L Thompson Ceed ceed; 54ca735530SJeremy L Thompson 552b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 562b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(impl->diag->module)); 57*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_in)); 58*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_out)); 592b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_identity)); 60ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_in)); 61ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_out)); 62ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_in)); 63ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_out)); 64*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_div_in)); 65*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_div_out)); 66*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_in)); 67*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_out)); 68*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr)); 69506b1a0cSSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr)); 70ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag)); 71ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag)); 720d0321e0SJeremy L Thompson } 732b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->diag)); 740d0321e0SJeremy L Thompson 75cc132f9aSnbeams if (impl->asmb) { 76cc132f9aSnbeams Ceed ceed; 77ca735530SJeremy L Thompson 782b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 792b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(impl->asmb->module)); 802b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_in)); 812b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_out)); 82cc132f9aSnbeams } 832b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->asmb)); 84cc132f9aSnbeams 852b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 860d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 870d0321e0SJeremy L Thompson } 880d0321e0SJeremy L Thompson 890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 900d0321e0SJeremy L Thompson // Setup infields or outfields 910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 92*004e4986SSebastian Grimberg static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool is_input, CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e, 93ca735530SJeremy L Thompson CeedInt num_fields, CeedInt Q, CeedInt num_elem) { 940d0321e0SJeremy L Thompson Ceed ceed; 95ca735530SJeremy L Thompson CeedQFunctionField *qf_fields; 96ca735530SJeremy L Thompson CeedOperatorField *op_fields; 970d0321e0SJeremy L Thompson 98ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 99ca735530SJeremy L Thompson if (is_input) { 100ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 101ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 1020d0321e0SJeremy L Thompson } else { 103ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 104ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1050d0321e0SJeremy L Thompson } 1060d0321e0SJeremy L Thompson 1070d0321e0SJeremy L Thompson // Loop over fields 108ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_fields; i++) { 109*004e4986SSebastian Grimberg bool is_strided = false, skip_restriction = false; 110*004e4986SSebastian Grimberg CeedSize q_size; 111*004e4986SSebastian Grimberg CeedInt size; 112*004e4986SSebastian Grimberg CeedEvalMode eval_mode; 113ca735530SJeremy L Thompson CeedBasis basis; 1140d0321e0SJeremy L Thompson 115*004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 116*004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 117edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 118ca735530SJeremy L Thompson 1190d0321e0SJeremy L Thompson // Check whether this field can skip the element restriction: 120*004e4986SSebastian Grimberg // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND. 121*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr)); 1220d0321e0SJeremy L Thompson 1230d0321e0SJeremy L Thompson // First, check whether the field is input or output: 124ca735530SJeremy L Thompson if (is_input) { 125ca735530SJeremy L Thompson CeedVector vec; 126ca735530SJeremy L Thompson 127*004e4986SSebastian Grimberg // Check for passive input 128ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 129ca735530SJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE) { 130*004e4986SSebastian Grimberg // Check eval_mode 131*004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 1320d0321e0SJeremy L Thompson // Check for strided restriction 133edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided)); 134ca735530SJeremy L Thompson if (is_strided) { 1350d0321e0SJeremy L Thompson // Check if vector is already in preferred backend ordering 136edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction)); 1370d0321e0SJeremy L Thompson } 1380d0321e0SJeremy L Thompson } 1390d0321e0SJeremy L Thompson } 1400d0321e0SJeremy L Thompson } 141ca735530SJeremy L Thompson if (skip_restriction) { 142ea61e9acSJeremy L Thompson // We do not need an E-Vector, but will use the input field vector's data directly in the operator application. 143*004e4986SSebastian Grimberg e_vecs[i + start_e] = NULL; 1440d0321e0SJeremy L Thompson } else { 145*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e])); 1460d0321e0SJeremy L Thompson } 1470d0321e0SJeremy L Thompson } 1480d0321e0SJeremy L Thompson 149*004e4986SSebastian Grimberg switch (eval_mode) { 1500d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 151ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 152ca735530SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 153ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 1540d0321e0SJeremy L Thompson break; 1550d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 1560d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 157*004e4986SSebastian Grimberg case CEED_EVAL_DIV: 158*004e4986SSebastian Grimberg case CEED_EVAL_CURL: 159ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 160ca735530SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 161ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 1620d0321e0SJeremy L Thompson break; 1630d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: // Only on input fields 164ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 165ca735530SJeremy L Thompson q_size = (CeedSize)num_elem * Q; 166ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 167ca735530SJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i])); 1680d0321e0SJeremy L Thompson break; 1690d0321e0SJeremy L Thompson } 1700d0321e0SJeremy L Thompson } 1710d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1720d0321e0SJeremy L Thompson } 1730d0321e0SJeremy L Thompson 1740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 175ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. 1760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1770d0321e0SJeremy L Thompson static int CeedOperatorSetup_Cuda(CeedOperator op) { 1780d0321e0SJeremy L Thompson Ceed ceed; 179ca735530SJeremy L Thompson bool is_setup_done; 180ca735530SJeremy L Thompson CeedInt Q, num_elem, num_input_fields, num_output_fields; 181ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 1820d0321e0SJeremy L Thompson CeedQFunction qf; 183ca735530SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 184ca735530SJeremy L Thompson CeedOperator_Cuda *impl; 185ca735530SJeremy L Thompson 186ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 187ca735530SJeremy L Thompson if (is_setup_done) return CEED_ERROR_SUCCESS; 188ca735530SJeremy L Thompson 189ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 190ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1912b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1922b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 193ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 194ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 195ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 1960d0321e0SJeremy L Thompson 1970d0321e0SJeremy L Thompson // Allocate 198ca735530SJeremy L Thompson CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs)); 199ca735530SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in)); 200ca735530SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out)); 201ca735530SJeremy L Thompson impl->num_inputs = num_input_fields; 202ca735530SJeremy L Thompson impl->num_outputs = num_output_fields; 2030d0321e0SJeremy L Thompson 204ca735530SJeremy L Thompson // Set up infield and outfield e_vecs and q_vecs 2050d0321e0SJeremy L Thompson // Infields 206ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, true, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem)); 2070d0321e0SJeremy L Thompson // Outfields 208ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem)); 2090d0321e0SJeremy L Thompson 2102b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 2110d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2120d0321e0SJeremy L Thompson } 2130d0321e0SJeremy L Thompson 2140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2150d0321e0SJeremy L Thompson // Setup Operator Inputs 2160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 217ca735530SJeremy L Thompson static inline int CeedOperatorSetupInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 218*004e4986SSebastian Grimberg CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], 2190d0321e0SJeremy L Thompson CeedOperator_Cuda *impl, CeedRequest *request) { 220ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 221*004e4986SSebastian Grimberg CeedEvalMode eval_mode; 2220d0321e0SJeremy L Thompson CeedVector vec; 223edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 2240d0321e0SJeremy L Thompson 2250d0321e0SJeremy L Thompson // Get input vector 226ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 2270d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 228*004e4986SSebastian Grimberg if (skip_active) continue; 229ca735530SJeremy L Thompson else vec = in_vec; 2300d0321e0SJeremy L Thompson } 2310d0321e0SJeremy L Thompson 232*004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 233*004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 2340d0321e0SJeremy L Thompson } else { 235*004e4986SSebastian Grimberg // Get input vector 236*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 2370d0321e0SJeremy L Thompson // Get input element restriction 238edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 239ca735530SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = in_vec; 2400d0321e0SJeremy L Thompson // Restrict, if necessary 241ca735530SJeremy L Thompson if (!impl->e_vecs[i]) { 2420d0321e0SJeremy L Thompson // No restriction for this field; read data directly from vec. 243ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i])); 2440d0321e0SJeremy L Thompson } else { 245edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request)); 2460d0321e0SJeremy L Thompson // Get evec 247ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i])); 2480d0321e0SJeremy L Thompson } 2490d0321e0SJeremy L Thompson } 2500d0321e0SJeremy L Thompson } 2510d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2520d0321e0SJeremy L Thompson } 2530d0321e0SJeremy L Thompson 2540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2550d0321e0SJeremy L Thompson // Input Basis Action 2560d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 257ca735530SJeremy L Thompson static inline int CeedOperatorInputBasis_Cuda(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 258*004e4986SSebastian Grimberg CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], 2592b730f8bSJeremy L Thompson CeedOperator_Cuda *impl) { 260ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 261ca735530SJeremy L Thompson CeedInt elem_size, size; 262*004e4986SSebastian Grimberg CeedEvalMode eval_mode; 263edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 2640d0321e0SJeremy L Thompson CeedBasis basis; 2650d0321e0SJeremy L Thompson 2660d0321e0SJeremy L Thompson // Skip active input 267*004e4986SSebastian Grimberg if (skip_active) { 2680d0321e0SJeremy L Thompson CeedVector vec; 269ca735530SJeremy L Thompson 270ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 2712b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 2720d0321e0SJeremy L Thompson } 273*004e4986SSebastian Grimberg // Get elem_size, eval_mode, size 274edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 275edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 276*004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 277ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 2780d0321e0SJeremy L Thompson // Basis action 279*004e4986SSebastian Grimberg switch (eval_mode) { 2800d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 281ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i])); 2820d0321e0SJeremy L Thompson break; 2830d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 2840d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 285*004e4986SSebastian Grimberg case CEED_EVAL_DIV: 286*004e4986SSebastian Grimberg case CEED_EVAL_CURL: 287ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 288*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i])); 2890d0321e0SJeremy L Thompson break; 2900d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: 2910d0321e0SJeremy L Thompson break; // No action 2920d0321e0SJeremy L Thompson } 2930d0321e0SJeremy L Thompson } 2940d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2950d0321e0SJeremy L Thompson } 2960d0321e0SJeremy L Thompson 2970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2980d0321e0SJeremy L Thompson // Restore Input Vectors 2990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 300ca735530SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 301*004e4986SSebastian Grimberg const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) { 302ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 303*004e4986SSebastian Grimberg CeedEvalMode eval_mode; 3040d0321e0SJeremy L Thompson CeedVector vec; 3050d0321e0SJeremy L Thompson 3060d0321e0SJeremy L Thompson // Skip active input 307*004e4986SSebastian Grimberg if (skip_active) { 308ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 3092b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) continue; 3100d0321e0SJeremy L Thompson } 311*004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 312*004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_WEIGHT) { // Skip 3130d0321e0SJeremy L Thompson } else { 314ca735530SJeremy L Thompson if (!impl->e_vecs[i]) { // This was a skip_restriction case 315ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 316ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i])); 3170d0321e0SJeremy L Thompson } else { 318ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i])); 3190d0321e0SJeremy L Thompson } 3200d0321e0SJeremy L Thompson } 3210d0321e0SJeremy L Thompson } 3220d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3230d0321e0SJeremy L Thompson } 3240d0321e0SJeremy L Thompson 3250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3260d0321e0SJeremy L Thompson // Apply and add to output 3270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 328ca735530SJeremy L Thompson static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) { 329ca735530SJeremy L Thompson CeedInt Q, num_elem, elem_size, num_input_fields, num_output_fields, size; 330ca735530SJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX] = {NULL}; 331ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 3320d0321e0SJeremy L Thompson CeedQFunction qf; 333*004e4986SSebastian Grimberg CeedOperatorField *op_input_fields, *op_output_fields; 334*004e4986SSebastian Grimberg CeedOperator_Cuda *impl; 335ca735530SJeremy L Thompson 336ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 3372b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 3382b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 339ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 340ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 341ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 3420d0321e0SJeremy L Thompson 3430d0321e0SJeremy L Thompson // Setup 3442b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetup_Cuda(op)); 3450d0321e0SJeremy L Thompson 346*004e4986SSebastian Grimberg // Input Evecs and Restriction 347ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request)); 3480d0321e0SJeremy L Thompson 3490d0321e0SJeremy L Thompson // Input basis apply if needed 350ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl)); 3510d0321e0SJeremy L Thompson 3520d0321e0SJeremy L Thompson // Output pointers, as necessary 353ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 354*004e4986SSebastian Grimberg CeedEvalMode eval_mode; 355*004e4986SSebastian Grimberg 356*004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 357*004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 3580d0321e0SJeremy L Thompson // Set the output Q-Vector to use the E-Vector data directly. 359ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields])); 360ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields])); 3610d0321e0SJeremy L Thompson } 3620d0321e0SJeremy L Thompson } 3630d0321e0SJeremy L Thompson 3640d0321e0SJeremy L Thompson // Q function 365ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out)); 3660d0321e0SJeremy L Thompson 3670d0321e0SJeremy L Thompson // Output basis apply if needed 368ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 369*004e4986SSebastian Grimberg CeedEvalMode eval_mode; 370edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 371ca735530SJeremy L Thompson CeedBasis basis; 372ca735530SJeremy L Thompson 373*004e4986SSebastian Grimberg // Get elem_size, eval_mode, size 374edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 375edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 376*004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 377ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 3780d0321e0SJeremy L Thompson // Basis action 379*004e4986SSebastian Grimberg switch (eval_mode) { 3800d0321e0SJeremy L Thompson case CEED_EVAL_NONE: 381*004e4986SSebastian Grimberg break; // No action 3820d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: 3830d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: 384*004e4986SSebastian Grimberg case CEED_EVAL_DIV: 385*004e4986SSebastian Grimberg case CEED_EVAL_CURL: 386ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 387*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs])); 3880d0321e0SJeremy L Thompson break; 3890d0321e0SJeremy L Thompson // LCOV_EXCL_START 3900d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: { 3910d0321e0SJeremy L Thompson Ceed ceed; 392*004e4986SSebastian Grimberg 3932b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 3942b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 3950d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 3960d0321e0SJeremy L Thompson } 3970d0321e0SJeremy L Thompson } 398*004e4986SSebastian Grimberg } 3990d0321e0SJeremy L Thompson 4000d0321e0SJeremy L Thompson // Output restriction 401ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 402*004e4986SSebastian Grimberg CeedEvalMode eval_mode; 403ca735530SJeremy L Thompson CeedVector vec; 404edb2538eSJeremy L Thompson CeedElemRestriction elem_rstr; 405ca735530SJeremy L Thompson 4060d0321e0SJeremy L Thompson // Restore evec 407*004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 408*004e4986SSebastian Grimberg if (eval_mode == CEED_EVAL_NONE) { 409ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields])); 4100d0321e0SJeremy L Thompson } 4110d0321e0SJeremy L Thompson // Get output vector 412ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 4130d0321e0SJeremy L Thompson // Restrict 414edb2538eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 4150d0321e0SJeremy L Thompson // Active 416ca735530SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = out_vec; 4170d0321e0SJeremy L Thompson 418edb2538eSJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request)); 4190d0321e0SJeremy L Thompson } 4200d0321e0SJeremy L Thompson 4210d0321e0SJeremy L Thompson // Restore input arrays 422ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl)); 4230d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4240d0321e0SJeremy L Thompson } 4250d0321e0SJeremy L Thompson 4260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 427*004e4986SSebastian Grimberg // Linear QFunction Assembly Core 4280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4292b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr, 4300d0321e0SJeremy L Thompson CeedRequest *request) { 431ca735530SJeremy L Thompson Ceed ceed, ceed_parent; 432ca735530SJeremy L Thompson CeedInt num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size; 433ca735530SJeremy L Thompson CeedScalar *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL}; 434ca735530SJeremy L Thompson CeedVector *active_inputs; 435ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 436ca735530SJeremy L Thompson CeedQFunction qf; 437ca735530SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 438ca735530SJeremy L Thompson CeedOperator_Cuda *impl; 439ca735530SJeremy L Thompson 4402b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 441ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); 442e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 443e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 444ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 445e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 446ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 447ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 448ca735530SJeremy L Thompson active_inputs = impl->qf_active_in; 449ca735530SJeremy L Thompson num_active_in = impl->num_active_in, num_active_out = impl->num_active_out; 4500d0321e0SJeremy L Thompson 4510d0321e0SJeremy L Thompson // Setup 4522b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetup_Cuda(op)); 4530d0321e0SJeremy L Thompson 454*004e4986SSebastian Grimberg // Input Evecs and Restriction 455ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request)); 4560d0321e0SJeremy L Thompson 4570d0321e0SJeremy L Thompson // Count number of active input fields 458ca735530SJeremy L Thompson if (!num_active_in) { 459ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 460ca735530SJeremy L Thompson CeedScalar *q_vec_array; 461ca735530SJeremy L Thompson CeedVector vec; 462ca735530SJeremy L Thompson 4630d0321e0SJeremy L Thompson // Get input vector 464ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 4650d0321e0SJeremy L Thompson // Check if active input 4660d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 467ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 468ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0)); 469ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array)); 470ca735530SJeremy L Thompson CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs)); 4710d0321e0SJeremy L Thompson for (CeedInt field = 0; field < size; field++) { 472*004e4986SSebastian Grimberg CeedSize q_size = (CeedSize)Q * num_elem; 473*004e4986SSebastian Grimberg 474ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field])); 475ca735530SJeremy L Thompson CeedCallBackend( 476ca735530SJeremy L Thompson CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem])); 4770d0321e0SJeremy L Thompson } 478ca735530SJeremy L Thompson num_active_in += size; 479ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array)); 4800d0321e0SJeremy L Thompson } 4810d0321e0SJeremy L Thompson } 482ca735530SJeremy L Thompson impl->num_active_in = num_active_in; 483ca735530SJeremy L Thompson impl->qf_active_in = active_inputs; 4840d0321e0SJeremy L Thompson } 4850d0321e0SJeremy L Thompson 4860d0321e0SJeremy L Thompson // Count number of active output fields 487ca735530SJeremy L Thompson if (!num_active_out) { 488ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 489ca735530SJeremy L Thompson CeedVector vec; 490ca735530SJeremy L Thompson 4910d0321e0SJeremy L Thompson // Get output vector 492ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 4930d0321e0SJeremy L Thompson // Check if active output 4940d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 495ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 496ca735530SJeremy L Thompson num_active_out += size; 4970d0321e0SJeremy L Thompson } 4980d0321e0SJeremy L Thompson } 499ca735530SJeremy L Thompson impl->num_active_out = num_active_out; 5000d0321e0SJeremy L Thompson } 5010d0321e0SJeremy L Thompson 5020d0321e0SJeremy L Thompson // Check sizes 503ca735530SJeremy L Thompson CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); 5040d0321e0SJeremy L Thompson 5050d0321e0SJeremy L Thompson // Build objects if needed 5060d0321e0SJeremy L Thompson if (build_objects) { 507*004e4986SSebastian Grimberg CeedSize l_size = (CeedSize)num_elem * Q * num_active_in * num_active_out; 508ca735530SJeremy L Thompson CeedInt strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */ 509*004e4986SSebastian Grimberg 510*004e4986SSebastian Grimberg // Create output restriction 511ca735530SJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out, 512ca735530SJeremy L Thompson num_active_in * num_active_out * num_elem * Q, strides, rstr)); 5130d0321e0SJeremy L Thompson // Create assembled vector 514ca735530SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled)); 5150d0321e0SJeremy L Thompson } 5162b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(*assembled, 0.0)); 517ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array)); 5180d0321e0SJeremy L Thompson 5190d0321e0SJeremy L Thompson // Input basis apply 520ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl)); 5210d0321e0SJeremy L Thompson 5220d0321e0SJeremy L Thompson // Assemble QFunction 523ca735530SJeremy L Thompson for (CeedInt in = 0; in < num_active_in; in++) { 5240d0321e0SJeremy L Thompson // Set Inputs 525ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0)); 526ca735530SJeremy L Thompson if (num_active_in > 1) { 527ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0)); 5280d0321e0SJeremy L Thompson } 5290d0321e0SJeremy L Thompson // Set Outputs 530ca735530SJeremy L Thompson for (CeedInt out = 0; out < num_output_fields; out++) { 531ca735530SJeremy L Thompson CeedVector vec; 532ca735530SJeremy L Thompson 5330d0321e0SJeremy L Thompson // Get output vector 534ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec)); 5350d0321e0SJeremy L Thompson // Check if active output 5360d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 537ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array)); 538ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size)); 539ca735530SJeremy L Thompson assembled_array += size * Q * num_elem; // Advance the pointer by the size of the output 5400d0321e0SJeremy L Thompson } 5410d0321e0SJeremy L Thompson } 5420d0321e0SJeremy L Thompson // Apply QFunction 543ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out)); 5440d0321e0SJeremy L Thompson } 5450d0321e0SJeremy L Thompson 546ca735530SJeremy L Thompson // Un-set output q_vecs to prevent accidental overwrite of Assembled 547ca735530SJeremy L Thompson for (CeedInt out = 0; out < num_output_fields; out++) { 548ca735530SJeremy L Thompson CeedVector vec; 549ca735530SJeremy L Thompson 5500d0321e0SJeremy L Thompson // Get output vector 551ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec)); 5520d0321e0SJeremy L Thompson // Check if active output 5530d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 554ca735530SJeremy L Thompson CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL)); 5550d0321e0SJeremy L Thompson } 5560d0321e0SJeremy L Thompson } 5570d0321e0SJeremy L Thompson 5580d0321e0SJeremy L Thompson // Restore input arrays 559ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl)); 5600d0321e0SJeremy L Thompson 5610d0321e0SJeremy L Thompson // Restore output 562ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); 5630d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5640d0321e0SJeremy L Thompson } 5650d0321e0SJeremy L Thompson 5660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5670d0321e0SJeremy L Thompson // Assemble Linear QFunction 5680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5692b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 5702b730f8bSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, true, assembled, rstr, request); 5710d0321e0SJeremy L Thompson } 5720d0321e0SJeremy L Thompson 5730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5740d0321e0SJeremy L Thompson // Update Assembled Linear QFunction 5750d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5762b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) { 5772b730f8bSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, false, &assembled, &rstr, request); 5780d0321e0SJeremy L Thompson } 5790d0321e0SJeremy L Thompson 5800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 581*004e4986SSebastian Grimberg // Assemble Diagonal Setup 5820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 583506b1a0cSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op, CeedInt use_ceedsize_idx) { 5840d0321e0SJeremy L Thompson Ceed ceed; 585ca735530SJeremy L Thompson char *diagonal_kernel_path, *diagonal_kernel_source; 586*004e4986SSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 587*004e4986SSebastian Grimberg CeedInt num_comp, q_comp, num_nodes, num_qpts; 588*004e4986SSebastian Grimberg CeedEvalMode *eval_modes_in = NULL, *eval_modes_out = NULL; 589ca735530SJeremy L Thompson CeedBasis basis_in = NULL, basis_out = NULL; 590ca735530SJeremy L Thompson CeedQFunctionField *qf_fields; 5910d0321e0SJeremy L Thompson CeedQFunction qf; 592ca735530SJeremy L Thompson CeedOperatorField *op_fields; 593ca735530SJeremy L Thompson CeedOperator_Cuda *impl; 594ca735530SJeremy L Thompson 595ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 5962b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 597ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields)); 5980d0321e0SJeremy L Thompson 5990d0321e0SJeremy L Thompson // Determine active input basis 600ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 601ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 602ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 6030d0321e0SJeremy L Thompson CeedVector vec; 604ca735530SJeremy L Thompson 605ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 6060d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 607*004e4986SSebastian Grimberg CeedBasis basis; 608*004e4986SSebastian Grimberg CeedEvalMode eval_mode; 609ca735530SJeremy L Thompson 610*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 611*004e4986SSebastian Grimberg CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, 612*004e4986SSebastian Grimberg "Backend does not implement operator diagonal assembly with multiple active bases"); 613*004e4986SSebastian Grimberg basis_in = basis; 614*004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 615*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 616*004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 617*004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly 618*004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in)); 619*004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode; 620*004e4986SSebastian Grimberg num_eval_modes_in += q_comp; 6210d0321e0SJeremy L Thompson } 6220d0321e0SJeremy L Thompson } 6230d0321e0SJeremy L Thompson } 6240d0321e0SJeremy L Thompson 6250d0321e0SJeremy L Thompson // Determine active output basis 626ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 627ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 628ca735530SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 6290d0321e0SJeremy L Thompson CeedVector vec; 630ca735530SJeremy L Thompson 631ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 6320d0321e0SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 633*004e4986SSebastian Grimberg CeedBasis basis; 634*004e4986SSebastian Grimberg CeedEvalMode eval_mode; 635ca735530SJeremy L Thompson 636*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 637*004e4986SSebastian Grimberg CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND, 638*004e4986SSebastian Grimberg "Backend does not implement operator diagonal assembly with multiple active bases"); 639*004e4986SSebastian Grimberg basis_out = basis; 640*004e4986SSebastian Grimberg CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 641*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 642*004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 643*004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly 644*004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out)); 645*004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode; 646*004e4986SSebastian Grimberg num_eval_modes_out += q_comp; 6470d0321e0SJeremy L Thompson } 6480d0321e0SJeremy L Thompson } 6490d0321e0SJeremy L Thompson } 6500d0321e0SJeremy L Thompson 6510d0321e0SJeremy L Thompson // Operator data struct 6522b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 6532b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl->diag)); 6540d0321e0SJeremy L Thompson CeedOperatorDiag_Cuda *diag = impl->diag; 655ca735530SJeremy L Thompson 6560d0321e0SJeremy L Thompson // Assemble kernel 657*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes)); 658*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp)); 659*004e4986SSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes; 660*004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts)); 6612b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble-diagonal.h", &diagonal_kernel_path)); 66223d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n"); 6632b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source)); 66423d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n"); 665*004e4986SSebastian Grimberg CeedCallCuda( 666*004e4986SSebastian Grimberg ceed, CeedCompile_Cuda(ceed, diagonal_kernel_source, &diag->module, 6, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT", 667*004e4986SSebastian Grimberg num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "CEED_SIZE", use_ceedsize_idx)); 668*004e4986SSebastian Grimberg CeedCallCuda(ceed, CeedGetKernel_Cuda(ceed, diag->module, "LinearDiagonal", &diag->LinearDiagonal)); 669*004e4986SSebastian Grimberg CeedCallCuda(ceed, CeedGetKernel_Cuda(ceed, diag->module, "LinearPointBlockDiagonal", &diag->LinearPointBlock)); 6702b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&diagonal_kernel_path)); 6712b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&diagonal_kernel_source)); 6720d0321e0SJeremy L Thompson 6730d0321e0SJeremy L Thompson // Basis matrices 674*004e4986SSebastian Grimberg const CeedInt interp_bytes = num_nodes * num_qpts * sizeof(CeedScalar); 675*004e4986SSebastian Grimberg const CeedInt eval_modes_bytes = sizeof(CeedEvalMode); 676*004e4986SSebastian Grimberg bool has_eval_none = false; 6770d0321e0SJeremy L Thompson 6780d0321e0SJeremy L Thompson // CEED_EVAL_NONE 679*004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE); 680*004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE); 681*004e4986SSebastian Grimberg if (has_eval_none) { 6820d0321e0SJeremy L Thompson CeedScalar *identity = NULL; 683ca735530SJeremy L Thompson 684*004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity)); 685ca735530SJeremy L Thompson for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0; 686ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_identity, interp_bytes)); 687ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(diag->d_identity, identity, interp_bytes, cudaMemcpyHostToDevice)); 688*004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 6890d0321e0SJeremy L Thompson } 6900d0321e0SJeremy L Thompson 691*004e4986SSebastian Grimberg // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL 692*004e4986SSebastian Grimberg for (CeedInt in = 0; in < 2; in++) { 693*004e4986SSebastian Grimberg CeedFESpace fespace; 694*004e4986SSebastian Grimberg CeedBasis basis = in ? basis_in : basis_out; 6950d0321e0SJeremy L Thompson 696*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetFESpace(basis, &fespace)); 697*004e4986SSebastian Grimberg switch (fespace) { 698*004e4986SSebastian Grimberg case CEED_FE_SPACE_H1: { 699*004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_grad; 700*004e4986SSebastian Grimberg const CeedScalar *interp, *grad; 701*004e4986SSebastian Grimberg CeedScalar *d_interp, *d_grad; 7020d0321e0SJeremy L Thompson 703*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 704*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad)); 7050d0321e0SJeremy L Thompson 706*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 707*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 708*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice)); 709*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetGrad(basis, &grad)); 710*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_grad, interp_bytes * q_comp_grad)); 711*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_grad, grad, interp_bytes * q_comp_grad, cudaMemcpyHostToDevice)); 712*004e4986SSebastian Grimberg if (in) { 713*004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 714*004e4986SSebastian Grimberg diag->d_grad_in = d_grad; 715*004e4986SSebastian Grimberg } else { 716*004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 717*004e4986SSebastian Grimberg diag->d_grad_out = d_grad; 718*004e4986SSebastian Grimberg } 719*004e4986SSebastian Grimberg } break; 720*004e4986SSebastian Grimberg case CEED_FE_SPACE_HDIV: { 721*004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_div; 722*004e4986SSebastian Grimberg const CeedScalar *interp, *div; 723*004e4986SSebastian Grimberg CeedScalar *d_interp, *d_div; 724*004e4986SSebastian Grimberg 725*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 726*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div)); 727*004e4986SSebastian Grimberg 728*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 729*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 730*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice)); 731*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetDiv(basis, &div)); 732*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_div, interp_bytes * q_comp_div)); 733*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_div, div, interp_bytes * q_comp_div, cudaMemcpyHostToDevice)); 734*004e4986SSebastian Grimberg if (in) { 735*004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 736*004e4986SSebastian Grimberg diag->d_div_in = d_div; 737*004e4986SSebastian Grimberg } else { 738*004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 739*004e4986SSebastian Grimberg diag->d_div_out = d_div; 740*004e4986SSebastian Grimberg } 741*004e4986SSebastian Grimberg } break; 742*004e4986SSebastian Grimberg case CEED_FE_SPACE_HCURL: { 743*004e4986SSebastian Grimberg CeedInt q_comp_interp, q_comp_curl; 744*004e4986SSebastian Grimberg const CeedScalar *interp, *curl; 745*004e4986SSebastian Grimberg CeedScalar *d_interp, *d_curl; 746*004e4986SSebastian Grimberg 747*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 748*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl)); 749*004e4986SSebastian Grimberg 750*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetInterp(basis, &interp)); 751*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp)); 752*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice)); 753*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetCurl(basis, &curl)); 754*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&d_curl, interp_bytes * q_comp_curl)); 755*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(d_curl, curl, interp_bytes * q_comp_curl, cudaMemcpyHostToDevice)); 756*004e4986SSebastian Grimberg if (in) { 757*004e4986SSebastian Grimberg diag->d_interp_in = d_interp; 758*004e4986SSebastian Grimberg diag->d_curl_in = d_curl; 759*004e4986SSebastian Grimberg } else { 760*004e4986SSebastian Grimberg diag->d_interp_out = d_interp; 761*004e4986SSebastian Grimberg diag->d_curl_out = d_curl; 762*004e4986SSebastian Grimberg } 763*004e4986SSebastian Grimberg } break; 764*004e4986SSebastian Grimberg } 765*004e4986SSebastian Grimberg } 766*004e4986SSebastian Grimberg 767*004e4986SSebastian Grimberg // Arrays of eval_modes 768*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes)); 769*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, cudaMemcpyHostToDevice)); 770*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes)); 771*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, cudaMemcpyHostToDevice)); 772*004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&eval_modes_in)); 773*004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&eval_modes_out)); 7740d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7750d0321e0SJeremy L Thompson } 7760d0321e0SJeremy L Thompson 7770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 778*004e4986SSebastian Grimberg // Assemble Diagonal Core 7790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 780ca735530SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) { 7810d0321e0SJeremy L Thompson Ceed ceed; 782*004e4986SSebastian Grimberg CeedSize assembled_length, assembled_qf_length; 783*004e4986SSebastian Grimberg CeedInt use_ceedsize_idx = 0, num_elem, num_nodes; 784ca735530SJeremy L Thompson CeedScalar *elem_diag_array; 785ca735530SJeremy L Thompson const CeedScalar *assembled_qf_array; 786*004e4986SSebastian Grimberg CeedVector assembled_qf = NULL, elem_diag; 787*004e4986SSebastian Grimberg CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr; 7880d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 789ca735530SJeremy L Thompson 790ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 7912b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 7920d0321e0SJeremy L Thompson 7930d0321e0SJeremy L Thompson // Assemble QFunction 794*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request)); 795*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr)); 796*004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array)); 7970d0321e0SJeremy L Thompson 798f7c1b517Snbeams CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length)); 799ca735530SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length)); 800ca735530SJeremy L Thompson if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1; 801f7c1b517Snbeams 8020d0321e0SJeremy L Thompson // Setup 803506b1a0cSSebastian Grimberg if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Cuda(op, use_ceedsize_idx)); 8040d0321e0SJeremy L Thompson CeedOperatorDiag_Cuda *diag = impl->diag; 805ca735530SJeremy L Thompson 8060d0321e0SJeremy L Thompson assert(diag != NULL); 8070d0321e0SJeremy L Thompson 808*004e4986SSebastian Grimberg // Restriction and diagonal vector 809*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 810*004e4986SSebastian Grimberg CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND, 811*004e4986SSebastian Grimberg "Cannot assemble operator diagonal with different input and output active element restrictions"); 812*004e4986SSebastian Grimberg if (!is_point_block && !diag->diag_rstr) { 813*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr)); 814*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag)); 815*004e4986SSebastian Grimberg } else if (is_point_block && !diag->point_block_diag_rstr) { 816*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr)); 817*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag)); 8180d0321e0SJeremy L Thompson } 819*004e4986SSebastian Grimberg diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr; 820*004e4986SSebastian Grimberg elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag; 821ca735530SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0)); 8220d0321e0SJeremy L Thompson 82391db28b6SZach Atkins // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers 824*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes)); 825*004e4986SSebastian Grimberg if (num_nodes > 0) { 8260d0321e0SJeremy L Thompson // Assemble element operator diagonals 827ca735530SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem)); 828*004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array)); 8290d0321e0SJeremy L Thompson 8300d0321e0SJeremy L Thompson // Compute the diagonal of B^T D B 831*004e4986SSebastian Grimberg CeedInt elems_per_block = 1; 832*004e4986SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem, elems_per_block); 833*004e4986SSebastian Grimberg void *args[] = {(void *)&num_elem, &diag->d_identity, &diag->d_interp_in, &diag->d_grad_in, &diag->d_div_in, 834*004e4986SSebastian Grimberg &diag->d_curl_in, &diag->d_interp_out, &diag->d_grad_out, &diag->d_div_out, &diag->d_curl_out, 835*004e4986SSebastian Grimberg &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array}; 836*004e4986SSebastian Grimberg 837ca735530SJeremy L Thompson if (is_point_block) { 838*004e4986SSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args)); 8390d0321e0SJeremy L Thompson } else { 840*004e4986SSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args)); 8410d0321e0SJeremy L Thompson } 8420d0321e0SJeremy L Thompson 8430d0321e0SJeremy L Thompson // Restore arrays 844ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array)); 845ca735530SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 84691db28b6SZach Atkins } 8470d0321e0SJeremy L Thompson 8480d0321e0SJeremy L Thompson // Assemble local operator diagonal 849ca735530SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); 8500d0321e0SJeremy L Thompson 8510d0321e0SJeremy L Thompson // Cleanup 852ca735530SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 8530d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8540d0321e0SJeremy L Thompson } 8550d0321e0SJeremy L Thompson 8560d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 8570d0321e0SJeremy L Thompson // Assemble Linear Diagonal 8580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 8592b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) { 8602b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, false)); 8616aa95790SJeremy L Thompson return CEED_ERROR_SUCCESS; 8620d0321e0SJeremy L Thompson } 8630d0321e0SJeremy L Thompson 8640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 8650d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal 8660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 8672b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) { 8682b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, true)); 8696aa95790SJeremy L Thompson return CEED_ERROR_SUCCESS; 8700d0321e0SJeremy L Thompson } 8710d0321e0SJeremy L Thompson 8720d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 873*004e4986SSebastian Grimberg // Single Operator Assembly Setup 874cc132f9aSnbeams //------------------------------------------------------------------------------ 875f7c1b517Snbeams static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op, CeedInt use_ceedsize_idx) { 876cc132f9aSnbeams Ceed ceed; 877*004e4986SSebastian Grimberg Ceed_Cuda *cuda_data; 878ca735530SJeremy L Thompson char *assembly_kernel_path, *assembly_kernel_source; 879*004e4986SSebastian Grimberg CeedInt num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0; 880*004e4986SSebastian Grimberg CeedInt elem_size_in, num_qpts_in, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp; 881*004e4986SSebastian Grimberg CeedEvalMode *eval_modes_in = NULL, *eval_modes_out = NULL; 882ca735530SJeremy L Thompson CeedElemRestriction rstr_in = NULL, rstr_out = NULL; 883ca735530SJeremy L Thompson CeedBasis basis_in = NULL, basis_out = NULL; 884ca735530SJeremy L Thompson CeedQFunctionField *qf_fields; 885ca735530SJeremy L Thompson CeedQFunction qf; 886ca735530SJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 887cc132f9aSnbeams CeedOperator_Cuda *impl; 888ca735530SJeremy L Thompson 889ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 8902b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 891cc132f9aSnbeams 892cc132f9aSnbeams // Get intput and output fields 8932b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 894cc132f9aSnbeams 895cc132f9aSnbeams // Determine active input basis eval mode 8962b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 8972b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 898cc132f9aSnbeams for (CeedInt i = 0; i < num_input_fields; i++) { 899cc132f9aSnbeams CeedVector vec; 900ca735530SJeremy L Thompson 9012b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec)); 902cc132f9aSnbeams if (vec == CEED_VECTOR_ACTIVE) { 903*004e4986SSebastian Grimberg CeedBasis basis; 904ca735530SJeremy L Thompson CeedEvalMode eval_mode; 905ca735530SJeremy L Thompson 906*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis)); 907*004e4986SSebastian Grimberg CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases"); 908*004e4986SSebastian Grimberg basis_in = basis; 9092b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in)); 910*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 911*004e4986SSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in; 912*004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in)); 9132b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 914*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 915*004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 916*004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 917*004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in)); 918*004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 919*004e4986SSebastian Grimberg eval_modes_in[num_eval_modes_in + d] = eval_mode; 920cc132f9aSnbeams } 921*004e4986SSebastian Grimberg num_eval_modes_in += q_comp; 922cc132f9aSnbeams } 923cc132f9aSnbeams } 924cc132f9aSnbeams } 925cc132f9aSnbeams 926cc132f9aSnbeams // Determine active output basis; basis_out and rstr_out only used if same as input, TODO 9272b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 928cc132f9aSnbeams for (CeedInt i = 0; i < num_output_fields; i++) { 929cc132f9aSnbeams CeedVector vec; 930ca735530SJeremy L Thompson 9312b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec)); 932cc132f9aSnbeams if (vec == CEED_VECTOR_ACTIVE) { 933*004e4986SSebastian Grimberg CeedBasis basis; 934ca735530SJeremy L Thompson CeedEvalMode eval_mode; 935ca735530SJeremy L Thompson 936*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis)); 937*004e4986SSebastian Grimberg CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND, 938*004e4986SSebastian Grimberg "Backend does not implement operator assembly with multiple active bases"); 939*004e4986SSebastian Grimberg basis_out = basis; 9402b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out)); 941*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 942*004e4986SSebastian Grimberg if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out; 943*004e4986SSebastian Grimberg else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out)); 944*004e4986SSebastian Grimberg CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED, 945*004e4986SSebastian Grimberg "Active input and output bases must have the same number of quadrature points"); 9462b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 947*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 948*004e4986SSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 949*004e4986SSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 950*004e4986SSebastian Grimberg CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out)); 951*004e4986SSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 952*004e4986SSebastian Grimberg eval_modes_out[num_eval_modes_out + d] = eval_mode; 953*004e4986SSebastian Grimberg } 954*004e4986SSebastian Grimberg num_eval_modes_out += q_comp; 955cc132f9aSnbeams } 956cc132f9aSnbeams } 957cc132f9aSnbeams } 958*004e4986SSebastian Grimberg CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs"); 959cc132f9aSnbeams 9602b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl->asmb)); 961cc132f9aSnbeams CeedOperatorAssemble_Cuda *asmb = impl->asmb; 962*004e4986SSebastian Grimberg asmb->elems_per_block = 1; 963*004e4986SSebastian Grimberg asmb->block_size_x = elem_size_in; 964*004e4986SSebastian Grimberg asmb->block_size_y = elem_size_out; 965ca735530SJeremy L Thompson 9662b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &cuda_data)); 967*004e4986SSebastian Grimberg bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > cuda_data->device_prop.maxThreadsPerBlock; 968*004e4986SSebastian Grimberg 969*004e4986SSebastian Grimberg if (fallback) { 970*004e4986SSebastian Grimberg // Use fallback kernel with 1D threadblock 971*004e4986SSebastian Grimberg asmb->block_size_y = 1; 972*004e4986SSebastian Grimberg } 973*004e4986SSebastian Grimberg 974*004e4986SSebastian Grimberg // Compile kernels 975*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in)); 976*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out)); 9772b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble.h", &assembly_kernel_path)); 97823d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n"); 9792b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source)); 98023d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n"); 981*004e4986SSebastian Grimberg CeedCallBackend(CeedCompile_Cuda(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT", 982*004e4986SSebastian Grimberg num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in, 983*004e4986SSebastian Grimberg "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE", 984*004e4986SSebastian Grimberg asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "CEED_SIZE", 985*004e4986SSebastian Grimberg use_ceedsize_idx)); 986*004e4986SSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble)); 9872b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&assembly_kernel_path)); 9882b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&assembly_kernel_source)); 989cc132f9aSnbeams 990*004e4986SSebastian Grimberg // Load into B_in, in order that they will be used in eval_modes_in 991*004e4986SSebastian Grimberg { 992*004e4986SSebastian Grimberg const CeedInt in_bytes = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar); 993*004e4986SSebastian Grimberg CeedInt d_in = 0; 994*004e4986SSebastian Grimberg CeedEvalMode eval_modes_in_prev = CEED_EVAL_NONE; 995*004e4986SSebastian Grimberg bool has_eval_none = false; 996*004e4986SSebastian Grimberg CeedScalar *identity = NULL; 997ca735530SJeremy L Thompson 998*004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) { 999*004e4986SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE); 1000*004e4986SSebastian Grimberg } 1001*004e4986SSebastian Grimberg if (has_eval_none) { 1002*004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity)); 1003*004e4986SSebastian Grimberg for (CeedInt i = 0; i < (elem_size_in < num_qpts_in ? elem_size_in : num_qpts_in); i++) identity[i * elem_size_in + i] = 1.0; 1004*004e4986SSebastian Grimberg } 1005cc132f9aSnbeams 1006*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_in, in_bytes)); 1007*004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in; i++) { 1008*004e4986SSebastian Grimberg const CeedScalar *h_B_in; 1009ca735530SJeremy L Thompson 1010*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in)); 1011*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp)); 1012*004e4986SSebastian Grimberg if (q_comp > 1) { 1013*004e4986SSebastian Grimberg if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0; 1014*004e4986SSebastian Grimberg else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in]; 1015*004e4986SSebastian Grimberg } 1016*004e4986SSebastian Grimberg eval_modes_in_prev = eval_modes_in[i]; 1017ca735530SJeremy L Thompson 1018*004e4986SSebastian 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), 1019*004e4986SSebastian Grimberg cudaMemcpyHostToDevice)); 1020*004e4986SSebastian Grimberg } 1021*004e4986SSebastian Grimberg 1022*004e4986SSebastian Grimberg if (identity) { 1023*004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 1024cc132f9aSnbeams } 1025cc132f9aSnbeams } 1026cc132f9aSnbeams 1027*004e4986SSebastian Grimberg // Load into B_out, in order that they will be used in eval_modes_out 1028*004e4986SSebastian Grimberg { 1029*004e4986SSebastian Grimberg const CeedInt out_bytes = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar); 1030*004e4986SSebastian Grimberg CeedInt d_out = 0; 1031*004e4986SSebastian Grimberg CeedEvalMode eval_modes_out_prev = CEED_EVAL_NONE; 1032*004e4986SSebastian Grimberg bool has_eval_none = false; 1033*004e4986SSebastian Grimberg CeedScalar *identity = NULL; 1034ca735530SJeremy L Thompson 1035*004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) { 1036*004e4986SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE); 1037*004e4986SSebastian Grimberg } 1038*004e4986SSebastian Grimberg if (has_eval_none) { 1039*004e4986SSebastian Grimberg CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity)); 1040*004e4986SSebastian Grimberg for (CeedInt i = 0; i < (elem_size_out < num_qpts_out ? elem_size_out : num_qpts_out); i++) identity[i * elem_size_out + i] = 1.0; 1041cc132f9aSnbeams } 1042cc132f9aSnbeams 1043*004e4986SSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_out, out_bytes)); 1044*004e4986SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out; i++) { 1045*004e4986SSebastian Grimberg const CeedScalar *h_B_out; 1046ca735530SJeremy L Thompson 1047*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out)); 1048*004e4986SSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp)); 1049*004e4986SSebastian Grimberg if (q_comp > 1) { 1050*004e4986SSebastian Grimberg if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0; 1051*004e4986SSebastian Grimberg else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out]; 1052*004e4986SSebastian Grimberg } 1053*004e4986SSebastian Grimberg eval_modes_out_prev = eval_modes_out[i]; 1054ca735530SJeremy L Thompson 1055*004e4986SSebastian 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), 1056*004e4986SSebastian Grimberg cudaMemcpyHostToDevice)); 1057*004e4986SSebastian Grimberg } 1058*004e4986SSebastian Grimberg 1059*004e4986SSebastian Grimberg if (identity) { 1060*004e4986SSebastian Grimberg CeedCallBackend(CeedFree(&identity)); 1061cc132f9aSnbeams } 1062cc132f9aSnbeams } 1063cc132f9aSnbeams return CEED_ERROR_SUCCESS; 1064cc132f9aSnbeams } 1065cc132f9aSnbeams 1066cc132f9aSnbeams //------------------------------------------------------------------------------ 1067cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator. 1068cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic. 1069cefa2673SJeremy L Thompson // 1070ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval 1071cefa2673SJeremy L Thompson // modes). 1072cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects 1073cc132f9aSnbeams //------------------------------------------------------------------------------ 10742b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Cuda(CeedOperator op, CeedInt offset, CeedVector values) { 1075cc132f9aSnbeams Ceed ceed; 1076ca735530SJeremy L Thompson CeedSize values_length = 0, assembled_qf_length = 0; 1077*004e4986SSebastian Grimberg CeedInt use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out; 1078ca735530SJeremy L Thompson CeedScalar *values_array; 1079*004e4986SSebastian Grimberg const CeedScalar *assembled_qf_array; 1080ca735530SJeremy L Thompson CeedVector assembled_qf = NULL; 1081*004e4986SSebastian Grimberg CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out; 1082*004e4986SSebastian Grimberg CeedRestrictionType rstr_type_in, rstr_type_out; 1083*004e4986SSebastian Grimberg const bool *orients_in = NULL, *orients_out = NULL; 1084*004e4986SSebastian Grimberg const CeedInt8 *curl_orients_in = NULL, *curl_orients_out = NULL; 1085cc132f9aSnbeams CeedOperator_Cuda *impl; 1086ca735530SJeremy L Thompson 1087ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 10882b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1089cc132f9aSnbeams 1090cc132f9aSnbeams // Assemble QFunction 1091*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE)); 1092*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr)); 1093*004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array)); 1094cc132f9aSnbeams 1095f7c1b517Snbeams CeedCallBackend(CeedVectorGetLength(values, &values_length)); 1096f7c1b517Snbeams CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length)); 1097f7c1b517Snbeams if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1; 1098*004e4986SSebastian Grimberg 1099f7c1b517Snbeams // Setup 1100*004e4986SSebastian Grimberg if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Cuda(op, use_ceedsize_idx)); 1101*004e4986SSebastian Grimberg CeedOperatorAssemble_Cuda *asmb = impl->asmb; 1102*004e4986SSebastian Grimberg 1103*004e4986SSebastian Grimberg assert(asmb != NULL); 1104*004e4986SSebastian Grimberg 1105*004e4986SSebastian Grimberg // Assemble element operator 1106*004e4986SSebastian Grimberg CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array)); 1107*004e4986SSebastian Grimberg values_array += offset; 1108*004e4986SSebastian Grimberg 1109*004e4986SSebastian Grimberg CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 1110*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in)); 1111*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 1112*004e4986SSebastian Grimberg 1113*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in)); 1114*004e4986SSebastian Grimberg if (rstr_type_in == CEED_RESTRICTION_ORIENTED) { 1115*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in)); 1116*004e4986SSebastian Grimberg } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 1117*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in)); 1118*004e4986SSebastian Grimberg } 1119*004e4986SSebastian Grimberg 1120*004e4986SSebastian Grimberg if (rstr_in != rstr_out) { 1121*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out)); 1122*004e4986SSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 1123*004e4986SSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 1124*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 1125*004e4986SSebastian Grimberg 1126*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out)); 1127*004e4986SSebastian Grimberg if (rstr_type_out == CEED_RESTRICTION_ORIENTED) { 1128*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out)); 1129*004e4986SSebastian Grimberg } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 1130*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out)); 1131*004e4986SSebastian Grimberg } 1132*004e4986SSebastian Grimberg } else { 1133*004e4986SSebastian Grimberg elem_size_out = elem_size_in; 1134*004e4986SSebastian Grimberg orients_out = orients_in; 1135*004e4986SSebastian Grimberg curl_orients_out = curl_orients_in; 1136f7c1b517Snbeams } 1137f7c1b517Snbeams 1138cc132f9aSnbeams // Compute B^T D B 1139*004e4986SSebastian Grimberg CeedInt shared_mem = 1140*004e4986SSebastian Grimberg ((curl_orients_in || curl_orients_out ? elem_size_in * elem_size_out : 0) + (curl_orients_in ? elem_size_in * asmb->block_size_y : 0)) * 1141*004e4986SSebastian Grimberg sizeof(CeedScalar); 1142*004e4986SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem_in, asmb->elems_per_block); 1143*004e4986SSebastian Grimberg void *args[] = {(void *)&num_elem_in, &asmb->d_B_in, &asmb->d_B_out, &orients_in, &curl_orients_in, 1144*004e4986SSebastian Grimberg &orients_out, &curl_orients_out, &assembled_qf_array, &values_array}; 1145ca735530SJeremy L Thompson 11462b730f8bSJeremy L Thompson CeedCallBackend( 1147*004e4986SSebastian Grimberg CeedRunKernelDimShared_Cuda(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args)); 1148cc132f9aSnbeams 1149cc132f9aSnbeams // Restore arrays 11502b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(values, &values_array)); 1151*004e4986SSebastian Grimberg CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 1152cc132f9aSnbeams 1153cc132f9aSnbeams // Cleanup 11542b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 1155*004e4986SSebastian Grimberg if (rstr_type_in == CEED_RESTRICTION_ORIENTED) { 1156*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in)); 1157*004e4986SSebastian Grimberg } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 1158*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in)); 1159*004e4986SSebastian Grimberg } 1160*004e4986SSebastian Grimberg if (rstr_in != rstr_out) { 1161*004e4986SSebastian Grimberg if (rstr_type_out == CEED_RESTRICTION_ORIENTED) { 1162*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out)); 1163*004e4986SSebastian Grimberg } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 1164*004e4986SSebastian Grimberg CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out)); 1165*004e4986SSebastian Grimberg } 1166*004e4986SSebastian Grimberg } 1167cc132f9aSnbeams return CEED_ERROR_SUCCESS; 1168cc132f9aSnbeams } 1169cc132f9aSnbeams 1170cc132f9aSnbeams //------------------------------------------------------------------------------ 11710d0321e0SJeremy L Thompson // Create operator 11720d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 11730d0321e0SJeremy L Thompson int CeedOperatorCreate_Cuda(CeedOperator op) { 11740d0321e0SJeremy L Thompson Ceed ceed; 11750d0321e0SJeremy L Thompson CeedOperator_Cuda *impl; 11760d0321e0SJeremy L Thompson 1177ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 11782b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 11792b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetData(op, impl)); 11800d0321e0SJeremy L Thompson 11812b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda)); 11822b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Cuda)); 11832b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Cuda)); 11842b730f8bSJeremy L Thompson CeedCallBackend( 11852b730f8bSJeremy L Thompson CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda)); 11862b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Cuda)); 11872b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda)); 11882b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda)); 11890d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11900d0321e0SJeremy L Thompson } 11910d0321e0SJeremy L Thompson 11920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1193