15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other 2bd882c8aSJames Wright // CEED contributors. All Rights Reserved. See the top-level LICENSE and NOTICE 3bd882c8aSJames Wright // files for details. 4bd882c8aSJames Wright // 5bd882c8aSJames Wright // SPDX-License-Identifier: BSD-2-Clause 6bd882c8aSJames Wright // 7bd882c8aSJames Wright // This file is part of CEED: http://github.com/ceed 8bd882c8aSJames Wright 9bd882c8aSJames Wright #include <ceed/backend.h> 10bd882c8aSJames Wright #include <ceed/ceed.h> 11bd882c8aSJames Wright 12bd882c8aSJames Wright #include <cassert> 13bd882c8aSJames Wright #include <string> 14bd882c8aSJames Wright #include <sycl/sycl.hpp> 15bd882c8aSJames Wright 16bd882c8aSJames Wright #include "../sycl/ceed-sycl-compile.hpp" 17bd882c8aSJames Wright #include "ceed-sycl-ref.hpp" 18bd882c8aSJames Wright 19bd882c8aSJames Wright class CeedOperatorSyclLinearDiagonal; 20bd882c8aSJames Wright class CeedOperatorSyclLinearAssemble; 21bd882c8aSJames Wright class CeedOperatorSyclLinearAssembleFallback; 22bd882c8aSJames Wright 23bd882c8aSJames Wright //------------------------------------------------------------------------------ 24bd882c8aSJames Wright // Get Basis Emode Pointer 25bd882c8aSJames Wright //------------------------------------------------------------------------------ 26dd64fc84SJeremy L Thompson void CeedOperatorGetBasisPointer_Sycl(const CeedScalar **basis_ptr, CeedEvalMode e_mode, const CeedScalar *identity, const CeedScalar *interp, 27bd882c8aSJames Wright const CeedScalar *grad) { 28dd64fc84SJeremy L Thompson switch (e_mode) { 29bd882c8aSJames Wright case CEED_EVAL_NONE: 30dd64fc84SJeremy L Thompson *basis_ptr = identity; 31bd882c8aSJames Wright break; 32bd882c8aSJames Wright case CEED_EVAL_INTERP: 33dd64fc84SJeremy L Thompson *basis_ptr = interp; 34bd882c8aSJames Wright break; 35bd882c8aSJames Wright case CEED_EVAL_GRAD: 36dd64fc84SJeremy L Thompson *basis_ptr = grad; 37bd882c8aSJames Wright break; 38bd882c8aSJames Wright case CEED_EVAL_WEIGHT: 39bd882c8aSJames Wright case CEED_EVAL_DIV: 40bd882c8aSJames Wright case CEED_EVAL_CURL: 41bd882c8aSJames Wright break; // Caught by QF Assembly 42bd882c8aSJames Wright } 43bd882c8aSJames Wright } 44bd882c8aSJames Wright 45bd882c8aSJames Wright //------------------------------------------------------------------------------ 46bd882c8aSJames Wright // Destroy operator 47bd882c8aSJames Wright //------------------------------------------------------------------------------ 48bd882c8aSJames Wright static int CeedOperatorDestroy_Sycl(CeedOperator op) { 49bd882c8aSJames Wright Ceed ceed; 50bd882c8aSJames Wright Ceed_Sycl *sycl_data; 51dd64fc84SJeremy L Thompson CeedOperator_Sycl *impl; 52dd64fc84SJeremy L Thompson 53dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 54dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 55bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &sycl_data)); 56bd882c8aSJames Wright 57bd882c8aSJames Wright // Apply data 58dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < impl->num_e_in + impl->num_e_out; i++) { 59dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i])); 60bd882c8aSJames Wright } 61dd64fc84SJeremy L Thompson CeedCallBackend(CeedFree(&impl->e_vecs)); 62bd882c8aSJames Wright 63dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < impl->num_e_in; i++) { 64dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i])); 65bd882c8aSJames Wright } 66dd64fc84SJeremy L Thompson CeedCallBackend(CeedFree(&impl->q_vecs_in)); 67bd882c8aSJames Wright 68dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < impl->num_e_out; i++) { 69dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i])); 70bd882c8aSJames Wright } 71dd64fc84SJeremy L Thompson CeedCallBackend(CeedFree(&impl->q_vecs_out)); 72bd882c8aSJames Wright 73bd882c8aSJames Wright // QFunction assembly data 74dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < impl->num_active_in; i++) { 75dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i])); 76bd882c8aSJames Wright } 77dd64fc84SJeremy L Thompson CeedCallBackend(CeedFree(&impl->qf_active_in)); 78bd882c8aSJames Wright 79bd882c8aSJames Wright // Diag data 80bd882c8aSJames Wright if (impl->diag) { 81dd64fc84SJeremy L Thompson CeedCallBackend(CeedFree(&impl->diag->h_e_mode_in)); 82dd64fc84SJeremy L Thompson CeedCallBackend(CeedFree(&impl->diag->h_e_mode_out)); 83bd882c8aSJames Wright 84bd882c8aSJames Wright CeedCallSycl(ceed, sycl_data->sycl_queue.wait_and_throw()); 85dd64fc84SJeremy L Thompson CeedCallSycl(ceed, sycl::free(impl->diag->d_e_mode_in, sycl_data->sycl_context)); 86dd64fc84SJeremy L Thompson CeedCallSycl(ceed, sycl::free(impl->diag->d_e_mode_out, sycl_data->sycl_context)); 87bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->diag->d_identity, sycl_data->sycl_context)); 88dd64fc84SJeremy L Thompson CeedCallSycl(ceed, sycl::free(impl->diag->d_interp_in, sycl_data->sycl_context)); 89dd64fc84SJeremy L Thompson CeedCallSycl(ceed, sycl::free(impl->diag->d_interp_out, sycl_data->sycl_context)); 90dd64fc84SJeremy L Thompson CeedCallSycl(ceed, sycl::free(impl->diag->d_grad_in, sycl_data->sycl_context)); 91dd64fc84SJeremy L Thompson CeedCallSycl(ceed, sycl::free(impl->diag->d_grad_out, sycl_data->sycl_context)); 92dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr)); 93bd882c8aSJames Wright 94dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag)); 95dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag)); 96bd882c8aSJames Wright } 97bd882c8aSJames Wright CeedCallBackend(CeedFree(&impl->diag)); 98bd882c8aSJames Wright 99bd882c8aSJames Wright if (impl->asmb) { 100bd882c8aSJames Wright CeedCallSycl(ceed, sycl_data->sycl_queue.wait_and_throw()); 101bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->asmb->d_B_in, sycl_data->sycl_context)); 102bd882c8aSJames Wright CeedCallSycl(ceed, sycl::free(impl->asmb->d_B_out, sycl_data->sycl_context)); 103bd882c8aSJames Wright } 104bd882c8aSJames Wright CeedCallBackend(CeedFree(&impl->asmb)); 105bd882c8aSJames Wright 106bd882c8aSJames Wright CeedCallBackend(CeedFree(&impl)); 107bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 108bd882c8aSJames Wright } 109bd882c8aSJames Wright 110bd882c8aSJames Wright //------------------------------------------------------------------------------ 111bd882c8aSJames Wright // Setup infields or outfields 112bd882c8aSJames Wright //------------------------------------------------------------------------------ 113dd64fc84SJeremy L Thompson static int CeedOperatorSetupFields_Sycl(CeedQFunction qf, CeedOperator op, bool is_input, CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e, 114dd64fc84SJeremy L Thompson CeedInt num_fields, CeedInt Q, CeedInt num_elem) { 115bd882c8aSJames Wright Ceed ceed; 116dd64fc84SJeremy L Thompson CeedSize q_size; 117dd64fc84SJeremy L Thompson bool is_strided, skip_restriction; 118dd64fc84SJeremy L Thompson CeedInt dim, size; 119dd64fc84SJeremy L Thompson CeedOperatorField *op_fields; 120dd64fc84SJeremy L Thompson CeedQFunctionField *qf_fields; 121bd882c8aSJames Wright 122dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 123dd64fc84SJeremy L Thompson if (is_input) { 124dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 125dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 126bd882c8aSJames Wright } else { 127dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 128dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 129bd882c8aSJames Wright } 130bd882c8aSJames Wright 131bd882c8aSJames Wright // Loop over fields 132dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_fields; i++) { 133dd64fc84SJeremy L Thompson CeedEvalMode e_mode; 134dd64fc84SJeremy L Thompson CeedVector vec; 135dd64fc84SJeremy L Thompson CeedElemRestriction rstr; 136dd64fc84SJeremy L Thompson CeedBasis basis; 137bd882c8aSJames Wright 138dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode)); 139dd64fc84SJeremy L Thompson 140dd64fc84SJeremy L Thompson is_strided = false; 141dd64fc84SJeremy L Thompson skip_restriction = false; 142dd64fc84SJeremy L Thompson if (e_mode != CEED_EVAL_WEIGHT) { 143dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr)); 144bd882c8aSJames Wright 145bd882c8aSJames Wright // Check whether this field can skip the element restriction: 146dd64fc84SJeremy L Thompson // must be passive input, with e_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND. 147bd882c8aSJames Wright 148bd882c8aSJames Wright // First, check whether the field is input or output: 149dd64fc84SJeremy L Thompson if (is_input) { 150bd882c8aSJames Wright // Check for passive input: 151dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 152dd64fc84SJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE) { 153dd64fc84SJeremy L Thompson // Check e_mode 154dd64fc84SJeremy L Thompson if (e_mode == CEED_EVAL_NONE) { 155dd64fc84SJeremy L Thompson // Check for is_strided restriction 156dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(rstr, &is_strided)); 157dd64fc84SJeremy L Thompson if (is_strided) { 158bd882c8aSJames Wright // Check if vector is already in preferred backend ordering 159dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &skip_restriction)); 160bd882c8aSJames Wright } 161bd882c8aSJames Wright } 162bd882c8aSJames Wright } 163bd882c8aSJames Wright } 164dd64fc84SJeremy L Thompson if (skip_restriction) { 165bd882c8aSJames Wright // We do not need an E-Vector, but will use the input field vector's data directly in the operator application 166dd64fc84SJeremy L Thompson e_vecs[i + start_e] = NULL; 167bd882c8aSJames Wright } else { 168dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateVector(rstr, NULL, &e_vecs[i + start_e])); 169bd882c8aSJames Wright } 170bd882c8aSJames Wright } 171bd882c8aSJames Wright 172dd64fc84SJeremy L Thompson switch (e_mode) { 173bd882c8aSJames Wright case CEED_EVAL_NONE: 174dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 175dd64fc84SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 176dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 177bd882c8aSJames Wright break; 178bd882c8aSJames Wright case CEED_EVAL_INTERP: 179dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 180dd64fc84SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 181dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 182bd882c8aSJames Wright break; 183bd882c8aSJames Wright case CEED_EVAL_GRAD: 184dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 185dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size)); 186bd882c8aSJames Wright CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 187dd64fc84SJeremy L Thompson q_size = (CeedSize)num_elem * Q * size; 188dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 189bd882c8aSJames Wright break; 190bd882c8aSJames Wright case CEED_EVAL_WEIGHT: // Only on input fields 191dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 192dd64fc84SJeremy L Thompson q_size = (CeedSize)num_elem * Q; 193dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i])); 1946078361eSJames Wright CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i])); 195bd882c8aSJames Wright break; 196bd882c8aSJames Wright case CEED_EVAL_DIV: 197bd882c8aSJames Wright break; // TODO: Not implemented 198bd882c8aSJames Wright case CEED_EVAL_CURL: 199bd882c8aSJames Wright break; // TODO: Not implemented 200bd882c8aSJames Wright } 201bd882c8aSJames Wright } 202bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 203bd882c8aSJames Wright } 204bd882c8aSJames Wright 205bd882c8aSJames Wright //------------------------------------------------------------------------------ 206bd882c8aSJames Wright // CeedOperator needs to connect all the named fields (be they active or 207bd882c8aSJames Wright // passive) to the named inputs and outputs of its CeedQFunction. 208bd882c8aSJames Wright //------------------------------------------------------------------------------ 209bd882c8aSJames Wright static int CeedOperatorSetup_Sycl(CeedOperator op) { 210bd882c8aSJames Wright Ceed ceed; 211dd64fc84SJeremy L Thompson bool is_setup_done; 212dd64fc84SJeremy L Thompson CeedInt Q, num_elem, num_input_fields, num_output_fields; 213dd64fc84SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 214bd882c8aSJames Wright CeedQFunction qf; 215dd64fc84SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 216dd64fc84SJeremy L Thompson CeedOperator_Sycl *impl; 217dd64fc84SJeremy L Thompson 218dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 219dd64fc84SJeremy L Thompson if (is_setup_done) return CEED_ERROR_SUCCESS; 220dd64fc84SJeremy L Thompson 221dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 222dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 223bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 224bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 225dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 226dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 227dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 228bd882c8aSJames Wright 229bd882c8aSJames Wright // Allocate 230dd64fc84SJeremy L Thompson CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs)); 231bd882c8aSJames Wright 232dd64fc84SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in)); 233dd64fc84SJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out)); 234bd882c8aSJames Wright 235dd64fc84SJeremy L Thompson impl->num_e_in = num_input_fields; 236dd64fc84SJeremy L Thompson impl->num_e_out = num_output_fields; 237bd882c8aSJames Wright 238dd64fc84SJeremy L Thompson // Set up infield and outfield e_vecs and q_vecs 239bd882c8aSJames Wright // Infields 240dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Sycl(qf, op, true, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem)); 241bd882c8aSJames Wright // Outfields 242dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorSetupFields_Sycl(qf, op, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem)); 243bd882c8aSJames Wright 244bd882c8aSJames Wright CeedCallBackend(CeedOperatorSetSetupDone(op)); 245bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 246bd882c8aSJames Wright } 247bd882c8aSJames Wright 248bd882c8aSJames Wright //------------------------------------------------------------------------------ 249bd882c8aSJames Wright // Setup Operator Inputs 250bd882c8aSJames Wright //------------------------------------------------------------------------------ 251dd64fc84SJeremy L Thompson static inline int CeedOperatorSetupInputs_Sycl(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 252dd64fc84SJeremy L Thompson CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], 253bd882c8aSJames Wright CeedOperator_Sycl *impl, CeedRequest *request) { 254dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 255dd64fc84SJeremy L Thompson CeedEvalMode e_mode; 256bd882c8aSJames Wright CeedVector vec; 257dd64fc84SJeremy L Thompson CeedElemRestriction rstr; 258bd882c8aSJames Wright 259bd882c8aSJames Wright // Get input vector 260dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 261bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) { 262dd64fc84SJeremy L Thompson if (skip_active) continue; 263dd64fc84SJeremy L Thompson else vec = in_vec; 264bd882c8aSJames Wright } 265bd882c8aSJames Wright 266dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &e_mode)); 267dd64fc84SJeremy L Thompson if (e_mode == CEED_EVAL_WEIGHT) { // Skip 268bd882c8aSJames Wright } else { 269bd882c8aSJames Wright // Get input vector 270dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 271bd882c8aSJames Wright // Get input element restriction 272dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr)); 273dd64fc84SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = in_vec; 274bd882c8aSJames Wright // Restrict, if necessary 275dd64fc84SJeremy L Thompson if (!impl->e_vecs[i]) { 276bd882c8aSJames Wright // No restriction for this field; read data directly from vec. 277dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i])); 278bd882c8aSJames Wright } else { 279dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request)); 280bd882c8aSJames Wright // Get evec 281dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i])); 282bd882c8aSJames Wright } 283bd882c8aSJames Wright } 284bd882c8aSJames Wright } 285bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 286bd882c8aSJames Wright } 287bd882c8aSJames Wright 288bd882c8aSJames Wright //------------------------------------------------------------------------------ 289bd882c8aSJames Wright // Input Basis Action 290bd882c8aSJames Wright //------------------------------------------------------------------------------ 291dd64fc84SJeremy L Thompson static inline int CeedOperatorInputBasis_Sycl(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 292dd64fc84SJeremy L Thompson CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], 293bd882c8aSJames Wright CeedOperator_Sycl *impl) { 294dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 295dd64fc84SJeremy L Thompson CeedInt elem_size, size; 296dd64fc84SJeremy L Thompson CeedElemRestriction rstr; 297dd64fc84SJeremy L Thompson CeedEvalMode e_mode; 298bd882c8aSJames Wright CeedBasis basis; 299bd882c8aSJames Wright 300bd882c8aSJames Wright // Skip active input 301dd64fc84SJeremy L Thompson if (skip_active) { 302bd882c8aSJames Wright CeedVector vec; 303dd64fc84SJeremy L Thompson 304dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 305bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) continue; 306bd882c8aSJames Wright } 307dd64fc84SJeremy L Thompson // Get elem_size, e_mode, size 308dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr)); 309dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size)); 310dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &e_mode)); 311dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 312bd882c8aSJames Wright // Basis action 313dd64fc84SJeremy L Thompson switch (e_mode) { 314bd882c8aSJames Wright case CEED_EVAL_NONE: 315dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i])); 316bd882c8aSJames Wright break; 317bd882c8aSJames Wright case CEED_EVAL_INTERP: 318dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 319dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, impl->e_vecs[i], impl->q_vecs_in[i])); 320bd882c8aSJames Wright break; 321bd882c8aSJames Wright case CEED_EVAL_GRAD: 322dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 323dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, impl->e_vecs[i], impl->q_vecs_in[i])); 324bd882c8aSJames Wright break; 325bd882c8aSJames Wright case CEED_EVAL_WEIGHT: 326bd882c8aSJames Wright break; // No action 327bd882c8aSJames Wright case CEED_EVAL_DIV: 328bd882c8aSJames Wright break; // TODO: Not implemented 329bd882c8aSJames Wright case CEED_EVAL_CURL: 330bd882c8aSJames Wright break; // TODO: Not implemented 331bd882c8aSJames Wright } 332bd882c8aSJames Wright } 333bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 334bd882c8aSJames Wright } 335bd882c8aSJames Wright 336bd882c8aSJames Wright //------------------------------------------------------------------------------ 337bd882c8aSJames Wright // Restore Input Vectors 338bd882c8aSJames Wright //------------------------------------------------------------------------------ 339dd64fc84SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Sycl(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields, 340dd64fc84SJeremy L Thompson const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Sycl *impl) { 341dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 342dd64fc84SJeremy L Thompson CeedEvalMode e_mode; 343bd882c8aSJames Wright CeedVector vec; 344bd882c8aSJames Wright 345bd882c8aSJames Wright // Skip active input 346dd64fc84SJeremy L Thompson if (skip_active) { 347dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 348bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) continue; 349bd882c8aSJames Wright } 350dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &e_mode)); 351dd64fc84SJeremy L Thompson if (e_mode == CEED_EVAL_WEIGHT) { // Skip 352bd882c8aSJames Wright } else { 353dd64fc84SJeremy L Thompson if (!impl->e_vecs[i]) { // This was a skip_restriction case 354dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 355dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i])); 356bd882c8aSJames Wright } else { 357dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i])); 358bd882c8aSJames Wright } 359bd882c8aSJames Wright } 360bd882c8aSJames Wright } 361bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 362bd882c8aSJames Wright } 363bd882c8aSJames Wright 364bd882c8aSJames Wright //------------------------------------------------------------------------------ 365bd882c8aSJames Wright // Apply and add to output 366bd882c8aSJames Wright //------------------------------------------------------------------------------ 367dd64fc84SJeremy L Thompson static int CeedOperatorApplyAdd_Sycl(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) { 368dd64fc84SJeremy L Thompson CeedInt Q, num_elem, elem_size, num_input_fields, num_output_fields, size; 3699d1bceceSJames Wright CeedEvalMode eval_mode; 370dd64fc84SJeremy L Thompson CeedScalar *e_data[2 * CEED_FIELD_MAX] = {0}; 371dd64fc84SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 372bd882c8aSJames Wright CeedQFunction qf; 373dd64fc84SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 374dd64fc84SJeremy L Thompson CeedOperator_Sycl *impl; 375dd64fc84SJeremy L Thompson 376dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 377bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 378bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 379dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 380dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 381dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 382bd882c8aSJames Wright 383bd882c8aSJames Wright // Setup 384bd882c8aSJames Wright CeedCallBackend(CeedOperatorSetup_Sycl(op)); 385bd882c8aSJames Wright 386bd882c8aSJames Wright // Input Evecs and Restriction 387dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request)); 388bd882c8aSJames Wright 389bd882c8aSJames Wright // Input basis apply if needed 390dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Sycl(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl)); 391bd882c8aSJames Wright 392bd882c8aSJames Wright // Output pointers, as necessary 393dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 3949d1bceceSJames Wright CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 3959d1bceceSJames Wright if (eval_mode == CEED_EVAL_NONE) { 396bd882c8aSJames Wright // Set the output Q-Vector to use the E-Vector data directly 397dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_e_in], CEED_MEM_DEVICE, &e_data[i + num_input_fields])); 398dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields])); 399bd882c8aSJames Wright } 400bd882c8aSJames Wright } 401bd882c8aSJames Wright 402bd882c8aSJames Wright // Q function 403dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out)); 404bd882c8aSJames Wright 405bd882c8aSJames Wright // Output basis apply if needed 406dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 407dd64fc84SJeremy L Thompson CeedElemRestriction rstr; 408dd64fc84SJeremy L Thompson CeedBasis basis; 409dd64fc84SJeremy L Thompson 4109d1bceceSJames Wright // Get elem_size, eval_mode, size 411dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr)); 412dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size)); 4139d1bceceSJames Wright CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 414dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 415bd882c8aSJames Wright // Basis action 4169d1bceceSJames Wright switch (eval_mode) { 417bd882c8aSJames Wright case CEED_EVAL_NONE: 418bd882c8aSJames Wright break; 419bd882c8aSJames Wright case CEED_EVAL_INTERP: 420dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 421dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, CEED_EVAL_INTERP, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_e_in])); 422bd882c8aSJames Wright break; 423bd882c8aSJames Wright case CEED_EVAL_GRAD: 424dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 425dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, CEED_EVAL_GRAD, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_e_in])); 426bd882c8aSJames Wright break; 427bd882c8aSJames Wright // LCOV_EXCL_START 428bd882c8aSJames Wright case CEED_EVAL_WEIGHT: 429bd882c8aSJames Wright Ceed ceed; 4304e3038a5SJeremy L Thompson 431bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 432bd882c8aSJames Wright return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 433bd882c8aSJames Wright break; // Should not occur 434bd882c8aSJames Wright case CEED_EVAL_DIV: 4354e3038a5SJeremy L Thompson case CEED_EVAL_CURL: { 4364e3038a5SJeremy L Thompson Ceed ceed; 4374e3038a5SJeremy L Thompson 4384e3038a5SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 4394e3038a5SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]); 4404e3038a5SJeremy L Thompson break; // Should not occur 4414e3038a5SJeremy L Thompson } 442bd882c8aSJames Wright // LCOV_EXCL_STOP 443bd882c8aSJames Wright } 444bd882c8aSJames Wright } 445bd882c8aSJames Wright 446bd882c8aSJames Wright // Output restriction 447dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 448dd64fc84SJeremy L Thompson CeedVector vec; 449dd64fc84SJeremy L Thompson CeedElemRestriction rstr; 450dd64fc84SJeremy L Thompson 451bd882c8aSJames Wright // Restore evec 4529d1bceceSJames Wright CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 4539d1bceceSJames Wright if (eval_mode == CEED_EVAL_NONE) { 454dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_e_in], &e_data[i + num_input_fields])); 455bd882c8aSJames Wright } 456bd882c8aSJames Wright // Get output vector 457dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 458bd882c8aSJames Wright // Restrict 459dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr)); 460bd882c8aSJames Wright // Active 461dd64fc84SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = out_vec; 462bd882c8aSJames Wright 463dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_e_in], vec, request)); 464bd882c8aSJames Wright } 465bd882c8aSJames Wright 466bd882c8aSJames Wright // Restore input arrays 467dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl)); 468bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 469bd882c8aSJames Wright } 470bd882c8aSJames Wright 471bd882c8aSJames Wright //------------------------------------------------------------------------------ 472bd882c8aSJames Wright // Core code for assembling linear QFunction 473bd882c8aSJames Wright //------------------------------------------------------------------------------ 474bd882c8aSJames Wright static inline int CeedOperatorLinearAssembleQFunctionCore_Sycl(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr, 475bd882c8aSJames Wright CeedRequest *request) { 476dd64fc84SJeremy L Thompson Ceed ceed, ceed_parent; 477e984cf9aSJeremy L Thompson CeedSize q_size; 478dd64fc84SJeremy L Thompson CeedInt num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size; 479dd64fc84SJeremy L Thompson CeedScalar *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL}; 480dd64fc84SJeremy L Thompson CeedVector *active_in; 481dd64fc84SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 482dd64fc84SJeremy L Thompson CeedQFunction qf; 483dd64fc84SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 484dd64fc84SJeremy L Thompson CeedOperator_Sycl *impl; 485dd64fc84SJeremy L Thompson 486bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 487dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); 488e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 489e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 490e984cf9aSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 491dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 492dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 493dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 494dd64fc84SJeremy L Thompson active_in = impl->qf_active_in; 495dd64fc84SJeremy L Thompson num_active_in = impl->num_active_in, num_active_out = impl->num_active_out; 496bd882c8aSJames Wright 497bd882c8aSJames Wright // Setup 498bd882c8aSJames Wright CeedCallBackend(CeedOperatorSetup_Sycl(op)); 499bd882c8aSJames Wright 500bd882c8aSJames Wright // Input Evecs and Restriction 501dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorSetupInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request)); 502bd882c8aSJames Wright 503bd882c8aSJames Wright // Count number of active input fields 504dd64fc84SJeremy L Thompson if (!num_active_in) { 505dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 506dd64fc84SJeremy L Thompson CeedScalar *q_vec_array; 507dd64fc84SJeremy L Thompson CeedVector vec; 508dd64fc84SJeremy L Thompson 509bd882c8aSJames Wright // Get input vector 510dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec)); 511bd882c8aSJames Wright // Check if active input 512bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) { 513dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size)); 514dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0)); 515dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array)); 516dd64fc84SJeremy L Thompson CeedCallBackend(CeedRealloc(num_active_in + size, &active_in)); 517bd882c8aSJames Wright for (CeedInt field = 0; field < size; field++) { 518dd64fc84SJeremy L Thompson q_size = (CeedSize)Q * num_elem; 519dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_in[num_active_in + field])); 520dd64fc84SJeremy L Thompson CeedCallBackend( 521dd64fc84SJeremy L Thompson CeedVectorSetArray(active_in[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem])); 522bd882c8aSJames Wright } 523dd64fc84SJeremy L Thompson num_active_in += size; 524dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array)); 525bd882c8aSJames Wright } 526bd882c8aSJames Wright } 527dd64fc84SJeremy L Thompson impl->num_active_in = num_active_in; 528dd64fc84SJeremy L Thompson impl->qf_active_in = active_in; 529bd882c8aSJames Wright } 530bd882c8aSJames Wright 531bd882c8aSJames Wright // Count number of active output fields 532dd64fc84SJeremy L Thompson if (!num_active_out) { 533dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 534dd64fc84SJeremy L Thompson CeedVector vec; 535dd64fc84SJeremy L Thompson 536bd882c8aSJames Wright // Get output vector 537dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec)); 538bd882c8aSJames Wright // Check if active output 539bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) { 540dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size)); 541dd64fc84SJeremy L Thompson num_active_out += size; 542bd882c8aSJames Wright } 543bd882c8aSJames Wright } 544dd64fc84SJeremy L Thompson impl->num_active_out = num_active_out; 545bd882c8aSJames Wright } 546bd882c8aSJames Wright 547bd882c8aSJames Wright // Check sizes 5484e3038a5SJeremy L Thompson CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); 549bd882c8aSJames Wright 550bd882c8aSJames Wright // Build objects if needed 551bd882c8aSJames Wright if (build_objects) { 552dd64fc84SJeremy L Thompson CeedSize l_size = (CeedSize)num_elem * Q * num_active_in * num_active_out; 553dd64fc84SJeremy L Thompson CeedInt strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */ 554dd64fc84SJeremy L Thompson 555bd882c8aSJames Wright // Create output restriction 556*22ab0487SJames Wright CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out, l_size, strides, rstr)); 557bd882c8aSJames Wright // Create assembled vector 558dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled)); 559bd882c8aSJames Wright } 560bd882c8aSJames Wright CeedCallBackend(CeedVectorSetValue(*assembled, 0.0)); 561dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array)); 562bd882c8aSJames Wright 563bd882c8aSJames Wright // Input basis apply 564dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorInputBasis_Sycl(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl)); 565bd882c8aSJames Wright 566bd882c8aSJames Wright // Assemble QFunction 567dd64fc84SJeremy L Thompson for (CeedInt in = 0; in < num_active_in; in++) { 568bd882c8aSJames Wright // Set Inputs 569dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(active_in[in], 1.0)); 570dd64fc84SJeremy L Thompson if (num_active_in > 1) { 571dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(active_in[(in + num_active_in - 1) % num_active_in], 0.0)); 572bd882c8aSJames Wright } 573bd882c8aSJames Wright // Set Outputs 574dd64fc84SJeremy L Thompson for (CeedInt out = 0; out < num_output_fields; out++) { 575dd64fc84SJeremy L Thompson CeedVector vec; 576dd64fc84SJeremy L Thompson 577bd882c8aSJames Wright // Get output vector 578dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec)); 579bd882c8aSJames Wright // Check if active output 580bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) { 581dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array)); 582dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size)); 583dd64fc84SJeremy L Thompson assembled_array += size * Q * num_elem; // Advance the pointer by the size of the output 584bd882c8aSJames Wright } 585bd882c8aSJames Wright } 586bd882c8aSJames Wright // Apply QFunction 587dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out)); 588bd882c8aSJames Wright } 589bd882c8aSJames Wright 590bd882c8aSJames Wright // Un-set output Qvecs to prevent accidental overwrite of Assembled 591dd64fc84SJeremy L Thompson for (CeedInt out = 0; out < num_output_fields; out++) { 592dd64fc84SJeremy L Thompson CeedVector vec; 593dd64fc84SJeremy L Thompson 594bd882c8aSJames Wright // Get output vector 595dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec)); 596bd882c8aSJames Wright // Check if active output 597bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) { 598dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL)); 599bd882c8aSJames Wright } 600bd882c8aSJames Wright } 601bd882c8aSJames Wright 602bd882c8aSJames Wright // Restore input arrays 603dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorRestoreInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl)); 604bd882c8aSJames Wright 605bd882c8aSJames Wright // Restore output 606dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); 607bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 608bd882c8aSJames Wright } 609bd882c8aSJames Wright 610bd882c8aSJames Wright //------------------------------------------------------------------------------ 611bd882c8aSJames Wright // Assemble Linear QFunction 612bd882c8aSJames Wright //------------------------------------------------------------------------------ 613bd882c8aSJames Wright static int CeedOperatorLinearAssembleQFunction_Sycl(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 614bd882c8aSJames Wright return CeedOperatorLinearAssembleQFunctionCore_Sycl(op, true, assembled, rstr, request); 615bd882c8aSJames Wright } 616bd882c8aSJames Wright 617bd882c8aSJames Wright //------------------------------------------------------------------------------ 618bd882c8aSJames Wright // Update Assembled Linear QFunction 619bd882c8aSJames Wright //------------------------------------------------------------------------------ 620bd882c8aSJames Wright static int CeedOperatorLinearAssembleQFunctionUpdate_Sycl(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) { 621bd882c8aSJames Wright return CeedOperatorLinearAssembleQFunctionCore_Sycl(op, false, &assembled, &rstr, request); 622bd882c8aSJames Wright } 623bd882c8aSJames Wright 624bd882c8aSJames Wright //------------------------------------------------------------------------------ 625bd882c8aSJames Wright // Assemble diagonal setup 626bd882c8aSJames Wright //------------------------------------------------------------------------------ 627506b1a0cSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Sycl(CeedOperator op) { 628bd882c8aSJames Wright Ceed ceed; 629dd64fc84SJeremy L Thompson Ceed_Sycl *sycl_data; 630dd64fc84SJeremy L Thompson CeedInt num_input_fields, num_output_fields, num_e_mode_in = 0, num_comp = 0, dim = 1, num_e_mode_out = 0; 631dd64fc84SJeremy L Thompson CeedEvalMode *e_mode_in = NULL, *e_mode_out = NULL; 632dd64fc84SJeremy L Thompson CeedBasis basis_in = NULL, basis_out = NULL; 633dd64fc84SJeremy L Thompson CeedElemRestriction rstr_in = NULL, rstr_out = NULL; 634dd64fc84SJeremy L Thompson CeedQFunctionField *qf_fields; 635bd882c8aSJames Wright CeedQFunction qf; 636dd64fc84SJeremy L Thompson CeedOperatorField *op_fields; 637dd64fc84SJeremy L Thompson CeedOperator_Sycl *impl; 638dd64fc84SJeremy L Thompson 639dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 640bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 641dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields)); 642bd882c8aSJames Wright 643bd882c8aSJames Wright // Determine active input basis 644dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 645dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 646dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 647bd882c8aSJames Wright CeedVector vec; 648dd64fc84SJeremy L Thompson 649dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 650bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) { 651dd64fc84SJeremy L Thompson CeedEvalMode e_mode; 652bd882c8aSJames Wright CeedElemRestriction rstr; 653dd64fc84SJeremy L Thompson 654dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in)); 655dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp)); 656dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis_in, &dim)); 657dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr)); 6584e3038a5SJeremy L Thompson CeedCheck(!rstr_in || rstr_in == rstr, ceed, CEED_ERROR_BACKEND, 6594e3038a5SJeremy L Thompson "Backend does not implement multi-field non-composite operator diagonal assembly"); 660dd64fc84SJeremy L Thompson rstr_in = rstr; 661dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode)); 662dd64fc84SJeremy L Thompson switch (e_mode) { 663bd882c8aSJames Wright case CEED_EVAL_NONE: 664bd882c8aSJames Wright case CEED_EVAL_INTERP: 665dd64fc84SJeremy L Thompson CeedCallBackend(CeedRealloc(num_e_mode_in + 1, &e_mode_in)); 666dd64fc84SJeremy L Thompson e_mode_in[num_e_mode_in] = e_mode; 667dd64fc84SJeremy L Thompson num_e_mode_in += 1; 668bd882c8aSJames Wright break; 669bd882c8aSJames Wright case CEED_EVAL_GRAD: 670dd64fc84SJeremy L Thompson CeedCallBackend(CeedRealloc(num_e_mode_in + dim, &e_mode_in)); 671dd64fc84SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) e_mode_in[num_e_mode_in + d] = e_mode; 672dd64fc84SJeremy L Thompson num_e_mode_in += dim; 673bd882c8aSJames Wright break; 674bd882c8aSJames Wright case CEED_EVAL_WEIGHT: 675bd882c8aSJames Wright case CEED_EVAL_DIV: 676bd882c8aSJames Wright case CEED_EVAL_CURL: 677bd882c8aSJames Wright break; // Caught by QF Assembly 678bd882c8aSJames Wright } 679bd882c8aSJames Wright } 680bd882c8aSJames Wright } 681bd882c8aSJames Wright 682bd882c8aSJames Wright // Determine active output basis 683dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 684dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 685dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 686bd882c8aSJames Wright CeedVector vec; 687dd64fc84SJeremy L Thompson 688dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec)); 689bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) { 690dd64fc84SJeremy L Thompson CeedEvalMode e_mode; 691bd882c8aSJames Wright CeedElemRestriction rstr; 692dd64fc84SJeremy L Thompson 693dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out)); 694dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr)); 6954e3038a5SJeremy L Thompson CeedCheck(!rstr_out || rstr_out == rstr, ceed, CEED_ERROR_BACKEND, 6964e3038a5SJeremy L Thompson "Backend does not implement multi-field non-composite operator diagonal assembly"); 697dd64fc84SJeremy L Thompson rstr_out = rstr; 698dd64fc84SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode)); 699dd64fc84SJeremy L Thompson switch (e_mode) { 700bd882c8aSJames Wright case CEED_EVAL_NONE: 701bd882c8aSJames Wright case CEED_EVAL_INTERP: 702dd64fc84SJeremy L Thompson CeedCallBackend(CeedRealloc(num_e_mode_out + 1, &e_mode_out)); 703dd64fc84SJeremy L Thompson e_mode_out[num_e_mode_out] = e_mode; 704dd64fc84SJeremy L Thompson num_e_mode_out += 1; 705bd882c8aSJames Wright break; 706bd882c8aSJames Wright case CEED_EVAL_GRAD: 707dd64fc84SJeremy L Thompson CeedCallBackend(CeedRealloc(num_e_mode_out + dim, &e_mode_out)); 708dd64fc84SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) e_mode_out[num_e_mode_out + d] = e_mode; 709dd64fc84SJeremy L Thompson num_e_mode_out += dim; 710bd882c8aSJames Wright break; 711bd882c8aSJames Wright case CEED_EVAL_WEIGHT: 712bd882c8aSJames Wright case CEED_EVAL_DIV: 713bd882c8aSJames Wright case CEED_EVAL_CURL: 714bd882c8aSJames Wright break; // Caught by QF Assembly 715bd882c8aSJames Wright } 716bd882c8aSJames Wright } 717bd882c8aSJames Wright } 718bd882c8aSJames Wright 719bd882c8aSJames Wright // Operator data struct 720bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetData(op, &impl)); 721bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &sycl_data)); 722bd882c8aSJames Wright CeedCallBackend(CeedCalloc(1, &impl->diag)); 723bd882c8aSJames Wright CeedOperatorDiag_Sycl *diag = impl->diag; 724dd64fc84SJeremy L Thompson 725dd64fc84SJeremy L Thompson diag->basis_in = basis_in; 726dd64fc84SJeremy L Thompson diag->basis_out = basis_out; 727dd64fc84SJeremy L Thompson diag->h_e_mode_in = e_mode_in; 728dd64fc84SJeremy L Thompson diag->h_e_mode_out = e_mode_out; 729dd64fc84SJeremy L Thompson diag->num_e_mode_in = num_e_mode_in; 730dd64fc84SJeremy L Thompson diag->num_e_mode_out = num_e_mode_out; 731bd882c8aSJames Wright 732bd882c8aSJames Wright // Kernel parameters 733dd64fc84SJeremy L Thompson CeedInt num_nodes, num_qpts; 734dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes)); 735dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts)); 736dd64fc84SJeremy L Thompson diag->num_nodes = num_nodes; 737dd64fc84SJeremy L Thompson diag->num_qpts = num_qpts; 738dd64fc84SJeremy L Thompson diag->num_comp = num_comp; 739bd882c8aSJames Wright 740bd882c8aSJames Wright // Basis matrices 741dd64fc84SJeremy L Thompson const CeedInt i_len = num_qpts * num_nodes; 742dd64fc84SJeremy L Thompson const CeedInt g_len = num_qpts * num_nodes * dim; 743dd64fc84SJeremy L Thompson const CeedScalar *interp_in, *interp_out, *grad_in, *grad_out; 744bd882c8aSJames Wright 745bd882c8aSJames Wright // CEED_EVAL_NONE 746bd882c8aSJames Wright CeedScalar *identity = NULL; 747dd64fc84SJeremy L Thompson bool has_eval_none = false; 748dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_e_mode_in; i++) has_eval_none = has_eval_none || (e_mode_in[i] == CEED_EVAL_NONE); 749dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_e_mode_out; i++) has_eval_none = has_eval_none || (e_mode_out[i] == CEED_EVAL_NONE); 750bd882c8aSJames Wright 7511f4b1b45SUmesh Unnikrishnan std::vector<sycl::event> e; 7521f4b1b45SUmesh Unnikrishnan 7531f4b1b45SUmesh Unnikrishnan if (!sycl_data->sycl_queue.is_in_order()) e = {sycl_data->sycl_queue.ext_oneapi_submit_barrier()}; 754bd882c8aSJames Wright 755bd882c8aSJames Wright std::vector<sycl::event> copy_events; 7561f4b1b45SUmesh Unnikrishnan 757dd64fc84SJeremy L Thompson if (has_eval_none) { 758dd64fc84SJeremy L Thompson CeedCallBackend(CeedCalloc(num_qpts * num_nodes, &identity)); 759dd64fc84SJeremy L Thompson for (CeedSize i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0; 760dd64fc84SJeremy L Thompson CeedCallSycl(ceed, diag->d_identity = sycl::malloc_device<CeedScalar>(i_len, sycl_data->sycl_device, sycl_data->sycl_context)); 7611f4b1b45SUmesh Unnikrishnan sycl::event identity_copy = sycl_data->sycl_queue.copy<CeedScalar>(identity, diag->d_identity, i_len, e); 762bd882c8aSJames Wright copy_events.push_back(identity_copy); 763bd882c8aSJames Wright } 764bd882c8aSJames Wright 765bd882c8aSJames Wright // CEED_EVAL_INTERP 766dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in)); 767dd64fc84SJeremy L Thompson CeedCallSycl(ceed, diag->d_interp_in = sycl::malloc_device<CeedScalar>(i_len, sycl_data->sycl_device, sycl_data->sycl_context)); 7681f4b1b45SUmesh Unnikrishnan sycl::event interp_in_copy = sycl_data->sycl_queue.copy<CeedScalar>(interp_in, diag->d_interp_in, i_len, e); 769dd64fc84SJeremy L Thompson copy_events.push_back(interp_in_copy); 770bd882c8aSJames Wright 771dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out)); 772dd64fc84SJeremy L Thompson CeedCallSycl(ceed, diag->d_interp_out = sycl::malloc_device<CeedScalar>(i_len, sycl_data->sycl_device, sycl_data->sycl_context)); 7731f4b1b45SUmesh Unnikrishnan sycl::event interp_out_copy = sycl_data->sycl_queue.copy<CeedScalar>(interp_out, diag->d_interp_out, i_len, e); 774dd64fc84SJeremy L Thompson copy_events.push_back(interp_out_copy); 775bd882c8aSJames Wright 776bd882c8aSJames Wright // CEED_EVAL_GRAD 777dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in)); 778dd64fc84SJeremy L Thompson CeedCallSycl(ceed, diag->d_grad_in = sycl::malloc_device<CeedScalar>(g_len, sycl_data->sycl_device, sycl_data->sycl_context)); 7791f4b1b45SUmesh Unnikrishnan sycl::event grad_in_copy = sycl_data->sycl_queue.copy<CeedScalar>(grad_in, diag->d_grad_in, g_len, e); 780dd64fc84SJeremy L Thompson copy_events.push_back(grad_in_copy); 781bd882c8aSJames Wright 782dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out)); 783dd64fc84SJeremy L Thompson CeedCallSycl(ceed, diag->d_grad_out = sycl::malloc_device<CeedScalar>(g_len, sycl_data->sycl_device, sycl_data->sycl_context)); 7841f4b1b45SUmesh Unnikrishnan sycl::event grad_out_copy = sycl_data->sycl_queue.copy<CeedScalar>(grad_out, diag->d_grad_out, g_len, e); 785dd64fc84SJeremy L Thompson copy_events.push_back(grad_out_copy); 786bd882c8aSJames Wright 787dd64fc84SJeremy L Thompson // Arrays of e_modes 788dd64fc84SJeremy L Thompson CeedCallSycl(ceed, diag->d_e_mode_in = sycl::malloc_device<CeedEvalMode>(num_e_mode_in, sycl_data->sycl_device, sycl_data->sycl_context)); 7891f4b1b45SUmesh Unnikrishnan sycl::event e_mode_in_copy = sycl_data->sycl_queue.copy<CeedEvalMode>(e_mode_in, diag->d_e_mode_in, num_e_mode_in, e); 790dd64fc84SJeremy L Thompson copy_events.push_back(e_mode_in_copy); 791bd882c8aSJames Wright 792dd64fc84SJeremy L Thompson CeedCallSycl(ceed, diag->d_e_mode_out = sycl::malloc_device<CeedEvalMode>(num_e_mode_out, sycl_data->sycl_device, sycl_data->sycl_context)); 7931f4b1b45SUmesh Unnikrishnan sycl::event e_mode_out_copy = sycl_data->sycl_queue.copy<CeedEvalMode>(e_mode_out, diag->d_e_mode_out, num_e_mode_out, e); 794dd64fc84SJeremy L Thompson copy_events.push_back(e_mode_out_copy); 795bd882c8aSJames Wright 796bd882c8aSJames Wright // Restriction 797dd64fc84SJeremy L Thompson diag->diag_rstr = rstr_out; 798bd882c8aSJames Wright 799bd882c8aSJames Wright // Wait for all copies to complete and handle exceptions 800bd882c8aSJames Wright CeedCallSycl(ceed, sycl::event::wait_and_throw(copy_events)); 801bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 802bd882c8aSJames Wright } 803bd882c8aSJames Wright 804bd882c8aSJames Wright //------------------------------------------------------------------------------ 805bd882c8aSJames Wright // Kernel for diagonal assembly 806bd882c8aSJames Wright //------------------------------------------------------------------------------ 807dd64fc84SJeremy L Thompson static int CeedOperatorLinearDiagonal_Sycl(sycl::queue &sycl_queue, const bool is_point_block, const CeedInt num_elem, 808dd64fc84SJeremy L Thompson const CeedOperatorDiag_Sycl *diag, const CeedScalar *assembled_qf_array, CeedScalar *elem_diag_array) { 809dd64fc84SJeremy L Thompson const CeedSize num_nodes = diag->num_nodes; 810dd64fc84SJeremy L Thompson const CeedSize num_qpts = diag->num_qpts; 811dd64fc84SJeremy L Thompson const CeedSize num_comp = diag->num_comp; 812dd64fc84SJeremy L Thompson const CeedSize num_e_mode_in = diag->num_e_mode_in; 813dd64fc84SJeremy L Thompson const CeedSize num_e_mode_out = diag->num_e_mode_out; 814bd882c8aSJames Wright const CeedScalar *identity = diag->d_identity; 815dd64fc84SJeremy L Thompson const CeedScalar *interp_in = diag->d_interp_in; 816dd64fc84SJeremy L Thompson const CeedScalar *grad_in = diag->d_grad_in; 817dd64fc84SJeremy L Thompson const CeedScalar *interp_out = diag->d_interp_out; 818dd64fc84SJeremy L Thompson const CeedScalar *grad_out = diag->d_grad_out; 819dd64fc84SJeremy L Thompson const CeedEvalMode *e_mode_in = diag->d_e_mode_in; 820dd64fc84SJeremy L Thompson const CeedEvalMode *e_mode_out = diag->d_e_mode_out; 821bd882c8aSJames Wright 822dd64fc84SJeremy L Thompson sycl::range<1> kernel_range(num_elem * num_nodes); 823bd882c8aSJames Wright 8241f4b1b45SUmesh Unnikrishnan std::vector<sycl::event> e; 8251f4b1b45SUmesh Unnikrishnan 8261f4b1b45SUmesh Unnikrishnan if (!sycl_queue.is_in_order()) e = {sycl_queue.ext_oneapi_submit_barrier()}; 8271f4b1b45SUmesh Unnikrishnan 8281f4b1b45SUmesh Unnikrishnan sycl_queue.parallel_for<CeedOperatorSyclLinearDiagonal>(kernel_range, e, [=](sycl::id<1> idx) { 829dd64fc84SJeremy L Thompson const CeedInt tid = idx % num_nodes; 830dd64fc84SJeremy L Thompson const CeedInt e = idx / num_nodes; 831bd882c8aSJames Wright 832bd882c8aSJames Wright // Compute the diagonal of B^T D B 833bd882c8aSJames Wright // Each element 834dd64fc84SJeremy L Thompson CeedInt d_out = -1; 835bd882c8aSJames Wright // Each basis eval mode pair 836dd64fc84SJeremy L Thompson for (CeedSize e_out = 0; e_out < num_e_mode_out; e_out++) { 837bd882c8aSJames Wright const CeedScalar *bt = NULL; 838dd64fc84SJeremy L Thompson 839dd64fc84SJeremy L Thompson if (e_mode_out[e_out] == CEED_EVAL_GRAD) ++d_out; 840dd64fc84SJeremy L Thompson CeedOperatorGetBasisPointer_Sycl(&bt, e_mode_out[e_out], identity, interp_out, &grad_out[d_out * num_qpts * num_nodes]); 841dd64fc84SJeremy L Thompson CeedInt d_in = -1; 842dd64fc84SJeremy L Thompson 843dd64fc84SJeremy L Thompson for (CeedSize e_in = 0; e_in < num_e_mode_in; e_in++) { 844bd882c8aSJames Wright const CeedScalar *b = NULL; 845dd64fc84SJeremy L Thompson 846dd64fc84SJeremy L Thompson if (e_mode_in[e_in] == CEED_EVAL_GRAD) ++d_in; 847dd64fc84SJeremy L Thompson CeedOperatorGetBasisPointer_Sycl(&b, e_mode_in[e_in], identity, interp_in, &grad_in[d_in * num_qpts * num_nodes]); 848bd882c8aSJames Wright // Each component 849dd64fc84SJeremy L Thompson for (CeedSize comp_out = 0; comp_out < num_comp; comp_out++) { 850bd882c8aSJames Wright // Each qpoint/node pair 851dd64fc84SJeremy L Thompson if (is_point_block) { 852bd882c8aSJames Wright // Point Block Diagonal 853dd64fc84SJeremy L Thompson for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) { 854dd64fc84SJeremy L Thompson CeedScalar e_value = 0.0; 855dd64fc84SJeremy L Thompson 856dd64fc84SJeremy L Thompson for (CeedSize q = 0; q < num_qpts; q++) { 857dd64fc84SJeremy L Thompson const CeedScalar qf_value = 858dd64fc84SJeremy L Thompson assembled_qf_array[((((e_in * num_comp + comp_in) * num_e_mode_out + e_out) * num_comp + comp_out) * num_elem + e) * num_qpts + 859dd64fc84SJeremy L Thompson q]; 860dd64fc84SJeremy L Thompson 861dd64fc84SJeremy L Thompson e_value += bt[q * num_nodes + tid] * qf_value * b[q * num_nodes + tid]; 862bd882c8aSJames Wright } 863dd64fc84SJeremy L Thompson elem_diag_array[((comp_out * num_comp + comp_in) * num_elem + e) * num_nodes + tid] += e_value; 864bd882c8aSJames Wright } 865bd882c8aSJames Wright } else { 866bd882c8aSJames Wright // Diagonal Only 867dd64fc84SJeremy L Thompson CeedScalar e_value = 0.0; 868dd64fc84SJeremy L Thompson 869dd64fc84SJeremy L Thompson for (CeedSize q = 0; q < num_qpts; q++) { 870dd64fc84SJeremy L Thompson const CeedScalar qf_value = 871dd64fc84SJeremy L Thompson assembled_qf_array[((((e_in * num_comp + comp_out) * num_e_mode_out + e_out) * num_comp + comp_out) * num_elem + e) * num_qpts + q]; 872dd64fc84SJeremy L Thompson e_value += bt[q * num_nodes + tid] * qf_value * b[q * num_nodes + tid]; 873bd882c8aSJames Wright } 874dd64fc84SJeremy L Thompson elem_diag_array[(comp_out * num_elem + e) * num_nodes + tid] += e_value; 875bd882c8aSJames Wright } 876bd882c8aSJames Wright } 877bd882c8aSJames Wright } 878bd882c8aSJames Wright } 879bd882c8aSJames Wright }); 880bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 881bd882c8aSJames Wright } 882bd882c8aSJames Wright 883bd882c8aSJames Wright //------------------------------------------------------------------------------ 884bd882c8aSJames Wright // Assemble diagonal common code 885bd882c8aSJames Wright //------------------------------------------------------------------------------ 886dd64fc84SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Sycl(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) { 887bd882c8aSJames Wright Ceed ceed; 888bd882c8aSJames Wright Ceed_Sycl *sycl_data; 889dd64fc84SJeremy L Thompson CeedInt num_elem; 890dd64fc84SJeremy L Thompson CeedScalar *elem_diag_array; 891dd64fc84SJeremy L Thompson const CeedScalar *assembled_qf_array; 892dd64fc84SJeremy L Thompson CeedVector assembled_qf = NULL; 893dd64fc84SJeremy L Thompson CeedElemRestriction rstr = NULL; 894dd64fc84SJeremy L Thompson CeedOperator_Sycl *impl; 895dd64fc84SJeremy L Thompson 896dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 897dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 898bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &sycl_data)); 899bd882c8aSJames Wright 900bd882c8aSJames Wright // Assemble QFunction 901dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr, request)); 902bd882c8aSJames Wright CeedCallBackend(CeedElemRestrictionDestroy(&rstr)); 903bd882c8aSJames Wright 904bd882c8aSJames Wright // Setup 905bd882c8aSJames Wright if (!impl->diag) { 906506b1a0cSSebastian Grimberg CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Sycl(op)); 907bd882c8aSJames Wright } 908bd882c8aSJames Wright CeedOperatorDiag_Sycl *diag = impl->diag; 909dd64fc84SJeremy L Thompson 910bd882c8aSJames Wright assert(diag != NULL); 911bd882c8aSJames Wright 912bd882c8aSJames Wright // Restriction 913dd64fc84SJeremy L Thompson if (is_point_block && !diag->point_block_diag_rstr) { 914506b1a0cSSebastian Grimberg CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(diag->diag_rstr, &diag->point_block_diag_rstr)); 915bd882c8aSJames Wright } 916dd64fc84SJeremy L Thompson CeedElemRestriction diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr; 917bd882c8aSJames Wright 918bd882c8aSJames Wright // Create diagonal vector 919dd64fc84SJeremy L Thompson CeedVector elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag; 920dd64fc84SJeremy L Thompson 921dd64fc84SJeremy L Thompson if (!elem_diag) { 922dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionCreateVector(diag_rstr, NULL, &elem_diag)); 923dd64fc84SJeremy L Thompson if (is_point_block) diag->point_block_elem_diag = elem_diag; 924dd64fc84SJeremy L Thompson else diag->elem_diag = elem_diag; 925bd882c8aSJames Wright } 926dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0)); 927bd882c8aSJames Wright 928bd882c8aSJames Wright // Assemble element operator diagonals 929dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array)); 930dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array)); 931dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem)); 932bd882c8aSJames Wright 933bd882c8aSJames Wright // Compute the diagonal of B^T D B 934dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorLinearDiagonal_Sycl(sycl_data->sycl_queue, is_point_block, num_elem, diag, assembled_qf_array, elem_diag_array)); 935bd882c8aSJames Wright 936bd882c8aSJames Wright // Wait for queue to complete and handle exceptions 937bd882c8aSJames Wright sycl_data->sycl_queue.wait_and_throw(); 938bd882c8aSJames Wright 939bd882c8aSJames Wright // Restore arrays 940dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array)); 941dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 942bd882c8aSJames Wright 943bd882c8aSJames Wright // Assemble local operator diagonal 944dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); 945bd882c8aSJames Wright 946bd882c8aSJames Wright // Cleanup 947dd64fc84SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 948bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 949bd882c8aSJames Wright } 950bd882c8aSJames Wright 951bd882c8aSJames Wright //------------------------------------------------------------------------------ 952bd882c8aSJames Wright // Assemble Linear Diagonal 953bd882c8aSJames Wright //------------------------------------------------------------------------------ 954bd882c8aSJames Wright static int CeedOperatorLinearAssembleAddDiagonal_Sycl(CeedOperator op, CeedVector assembled, CeedRequest *request) { 955bd882c8aSJames Wright CeedCallBackend(CeedOperatorAssembleDiagonalCore_Sycl(op, assembled, request, false)); 956bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 957bd882c8aSJames Wright } 958bd882c8aSJames Wright 959bd882c8aSJames Wright //------------------------------------------------------------------------------ 960bd882c8aSJames Wright // Assemble Linear Point Block Diagonal 961bd882c8aSJames Wright //------------------------------------------------------------------------------ 962bd882c8aSJames Wright static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Sycl(CeedOperator op, CeedVector assembled, CeedRequest *request) { 963bd882c8aSJames Wright CeedCallBackend(CeedOperatorAssembleDiagonalCore_Sycl(op, assembled, request, true)); 964bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 965bd882c8aSJames Wright } 966bd882c8aSJames Wright 967bd882c8aSJames Wright //------------------------------------------------------------------------------ 968bd882c8aSJames Wright // Single operator assembly setup 969bd882c8aSJames Wright //------------------------------------------------------------------------------ 970bd882c8aSJames Wright static int CeedSingleOperatorAssembleSetup_Sycl(CeedOperator op) { 971bd882c8aSJames Wright Ceed ceed; 972dd64fc84SJeremy L Thompson CeedInt num_input_fields, num_output_fields, num_e_mode_in = 0, dim = 1, num_B_in_mats_to_load = 0, size_B_in = 0, num_e_mode_out = 0, 973dd64fc84SJeremy L Thompson num_B_out_mats_to_load = 0, size_B_out = 0, num_qpts = 0, elem_size = 0, num_elem, num_comp, 974dd64fc84SJeremy L Thompson mat_start = 0; 975dd64fc84SJeremy L Thompson CeedEvalMode *eval_mode_in = NULL, *eval_mode_out = NULL; 976dd64fc84SJeremy L Thompson const CeedScalar *interp_in, *grad_in; 977dd64fc84SJeremy L Thompson CeedElemRestriction rstr_in = NULL, rstr_out = NULL; 978dd64fc84SJeremy L Thompson CeedBasis basis_in = NULL, basis_out = NULL; 979dd64fc84SJeremy L Thompson CeedQFunctionField *qf_fields; 980dd64fc84SJeremy L Thompson CeedQFunction qf; 981dd64fc84SJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 982bd882c8aSJames Wright CeedOperator_Sycl *impl; 983dd64fc84SJeremy L Thompson 984dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 985bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetData(op, &impl)); 986bd882c8aSJames Wright 987bd882c8aSJames Wright // Get input and output fields 988bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 989bd882c8aSJames Wright 990bd882c8aSJames Wright // Determine active input basis eval mode 991bd882c8aSJames Wright CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 992bd882c8aSJames Wright CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 993bd882c8aSJames Wright // Note that the kernel will treat each dimension of a gradient action separately; 994dd64fc84SJeremy L Thompson // i.e., when an active input has a CEED_EVAL_GRAD mode, num_ e_mode_in will increment by dim. 995dd64fc84SJeremy L Thompson // However, for the purposes of load_ing the B matrices, it will be treated as one mode, and we will load/copy the entire gradient matrix at once, 996dd64fc84SJeremy L Thompson // so num_B_in_mats_to_load will be incremented by 1. 997bd882c8aSJames Wright for (CeedInt i = 0; i < num_input_fields; i++) { 998dd64fc84SJeremy L Thompson CeedEvalMode eval_mode; 999bd882c8aSJames Wright CeedVector vec; 1000dd64fc84SJeremy L Thompson 1001bd882c8aSJames Wright CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec)); 1002bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) { 1003bd882c8aSJames Wright CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis_in)); 1004bd882c8aSJames Wright CeedCallBackend(CeedBasisGetDimension(basis_in, &dim)); 1005dd64fc84SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts)); 1006bd882c8aSJames Wright CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in)); 1007dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size)); 1008bd882c8aSJames Wright CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1009bd882c8aSJames Wright if (eval_mode != CEED_EVAL_NONE) { 1010bd882c8aSJames Wright CeedCallBackend(CeedRealloc(num_B_in_mats_to_load + 1, &eval_mode_in)); 1011bd882c8aSJames Wright eval_mode_in[num_B_in_mats_to_load] = eval_mode; 1012bd882c8aSJames Wright num_B_in_mats_to_load += 1; 1013bd882c8aSJames Wright if (eval_mode == CEED_EVAL_GRAD) { 1014dd64fc84SJeremy L Thompson num_e_mode_in += dim; 1015dd64fc84SJeremy L Thompson size_B_in += dim * elem_size * num_qpts; 1016bd882c8aSJames Wright } else { 1017dd64fc84SJeremy L Thompson num_e_mode_in += 1; 1018dd64fc84SJeremy L Thompson size_B_in += elem_size * num_qpts; 1019bd882c8aSJames Wright } 1020bd882c8aSJames Wright } 1021bd882c8aSJames Wright } 1022bd882c8aSJames Wright } 1023bd882c8aSJames Wright 1024bd882c8aSJames Wright // Determine active output basis; basis_out and rstr_out only used if same as input, TODO 1025bd882c8aSJames Wright CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields)); 1026bd882c8aSJames Wright for (CeedInt i = 0; i < num_output_fields; i++) { 1027dd64fc84SJeremy L Thompson CeedEvalMode eval_mode; 1028bd882c8aSJames Wright CeedVector vec; 1029dd64fc84SJeremy L Thompson 1030bd882c8aSJames Wright CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec)); 1031bd882c8aSJames Wright if (vec == CEED_VECTOR_ACTIVE) { 1032bd882c8aSJames Wright CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis_out)); 1033bd882c8aSJames Wright CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out)); 10344e3038a5SJeremy L Thompson CeedCheck(!rstr_out || rstr_out == rstr_in, ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator assembly"); 1035bd882c8aSJames Wright CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1036bd882c8aSJames Wright if (eval_mode != CEED_EVAL_NONE) { 1037bd882c8aSJames Wright CeedCallBackend(CeedRealloc(num_B_out_mats_to_load + 1, &eval_mode_out)); 1038bd882c8aSJames Wright eval_mode_out[num_B_out_mats_to_load] = eval_mode; 1039bd882c8aSJames Wright num_B_out_mats_to_load += 1; 1040bd882c8aSJames Wright if (eval_mode == CEED_EVAL_GRAD) { 1041dd64fc84SJeremy L Thompson num_e_mode_out += dim; 1042dd64fc84SJeremy L Thompson size_B_out += dim * elem_size * num_qpts; 1043bd882c8aSJames Wright } else { 1044dd64fc84SJeremy L Thompson num_e_mode_out += 1; 1045dd64fc84SJeremy L Thompson size_B_out += elem_size * num_qpts; 1046bd882c8aSJames Wright } 1047bd882c8aSJames Wright } 1048bd882c8aSJames Wright } 1049bd882c8aSJames Wright } 10504e3038a5SJeremy L Thompson CeedCheck(num_e_mode_in > 0 && num_e_mode_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs"); 1051bd882c8aSJames Wright 1052dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem)); 1053dd64fc84SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp)); 1054bd882c8aSJames Wright 1055bd882c8aSJames Wright CeedCallBackend(CeedCalloc(1, &impl->asmb)); 1056bd882c8aSJames Wright CeedOperatorAssemble_Sycl *asmb = impl->asmb; 1057dd64fc84SJeremy L Thompson asmb->num_elem = num_elem; 1058bd882c8aSJames Wright 1059bd882c8aSJames Wright Ceed_Sycl *sycl_data; 1060bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &sycl_data)); 1061bd882c8aSJames Wright 1062bd882c8aSJames Wright // Kernel setup 1063004e4986SSebastian Grimberg int elems_per_block = 1; 1064004e4986SSebastian Grimberg asmb->elems_per_block = elems_per_block; 1065dd64fc84SJeremy L Thompson asmb->block_size_x = elem_size; 1066dd64fc84SJeremy L Thompson asmb->block_size_y = elem_size; 1067dd64fc84SJeremy L Thompson asmb->num_e_mode_in = num_e_mode_in; 1068dd64fc84SJeremy L Thompson asmb->num_e_mode_out = num_e_mode_out; 1069dd64fc84SJeremy L Thompson asmb->num_qpts = num_qpts; 1070dd64fc84SJeremy L Thompson asmb->num_nodes = elem_size; 10715700671aSSebastian Grimberg asmb->block_size = elem_size * elem_size * elems_per_block; 1072dd64fc84SJeremy L Thompson asmb->num_comp = num_comp; 1073bd882c8aSJames Wright 1074bd882c8aSJames Wright // Build 'full' B matrices (not 1D arrays used for tensor-product matrices 1075bd882c8aSJames Wright CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in)); 1076bd882c8aSJames Wright CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in)); 1077bd882c8aSJames Wright 1078bd882c8aSJames Wright // Load into B_in, in order that they will be used in eval_mode 1079bd882c8aSJames Wright CeedCallSycl(ceed, asmb->d_B_in = sycl::malloc_device<CeedScalar>(size_B_in, sycl_data->sycl_device, sycl_data->sycl_context)); 1080bd882c8aSJames Wright for (int i = 0; i < num_B_in_mats_to_load; i++) { 1081bd882c8aSJames Wright CeedEvalMode eval_mode = eval_mode_in[i]; 1082dd64fc84SJeremy L Thompson 1083bd882c8aSJames Wright if (eval_mode == CEED_EVAL_INTERP) { 10841f4b1b45SUmesh Unnikrishnan std::vector<sycl::event> e; 10851f4b1b45SUmesh Unnikrishnan 10861f4b1b45SUmesh Unnikrishnan if (!sycl_data->sycl_queue.is_in_order()) e = {sycl_data->sycl_queue.ext_oneapi_submit_barrier()}; 10871f4b1b45SUmesh Unnikrishnan sycl_data->sycl_queue.copy<CeedScalar>(interp_in, &asmb->d_B_in[mat_start], elem_size * num_qpts, e); 1088dd64fc84SJeremy L Thompson mat_start += elem_size * num_qpts; 1089bd882c8aSJames Wright } else if (eval_mode == CEED_EVAL_GRAD) { 10901f4b1b45SUmesh Unnikrishnan std::vector<sycl::event> e; 10911f4b1b45SUmesh Unnikrishnan 10921f4b1b45SUmesh Unnikrishnan if (!sycl_data->sycl_queue.is_in_order()) e = {sycl_data->sycl_queue.ext_oneapi_submit_barrier()}; 10931f4b1b45SUmesh Unnikrishnan sycl_data->sycl_queue.copy<CeedScalar>(grad_in, &asmb->d_B_in[mat_start], dim * elem_size * num_qpts, e); 1094dd64fc84SJeremy L Thompson mat_start += dim * elem_size * num_qpts; 1095bd882c8aSJames Wright } 1096bd882c8aSJames Wright } 1097bd882c8aSJames Wright 1098bd882c8aSJames Wright const CeedScalar *interp_out, *grad_out; 1099bd882c8aSJames Wright // Note that this function currently assumes 1 basis, so this should always be true 1100bd882c8aSJames Wright // for now 1101bd882c8aSJames Wright if (basis_out == basis_in) { 1102bd882c8aSJames Wright interp_out = interp_in; 1103bd882c8aSJames Wright grad_out = grad_in; 1104bd882c8aSJames Wright } else { 1105bd882c8aSJames Wright CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out)); 1106bd882c8aSJames Wright CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out)); 1107bd882c8aSJames Wright } 1108bd882c8aSJames Wright 1109bd882c8aSJames Wright // Load into B_out, in order that they will be used in eval_mode 1110bd882c8aSJames Wright mat_start = 0; 1111bd882c8aSJames Wright CeedCallSycl(ceed, asmb->d_B_out = sycl::malloc_device<CeedScalar>(size_B_out, sycl_data->sycl_device, sycl_data->sycl_context)); 1112bd882c8aSJames Wright for (int i = 0; i < num_B_out_mats_to_load; i++) { 1113bd882c8aSJames Wright CeedEvalMode eval_mode = eval_mode_out[i]; 1114dd64fc84SJeremy L Thompson 1115bd882c8aSJames Wright if (eval_mode == CEED_EVAL_INTERP) { 11161f4b1b45SUmesh Unnikrishnan std::vector<sycl::event> e; 11171f4b1b45SUmesh Unnikrishnan 11181f4b1b45SUmesh Unnikrishnan if (!sycl_data->sycl_queue.is_in_order()) e = {sycl_data->sycl_queue.ext_oneapi_submit_barrier()}; 11191f4b1b45SUmesh Unnikrishnan sycl_data->sycl_queue.copy<CeedScalar>(interp_out, &asmb->d_B_out[mat_start], elem_size * num_qpts, e); 1120dd64fc84SJeremy L Thompson mat_start += elem_size * num_qpts; 1121bd882c8aSJames Wright } else if (eval_mode == CEED_EVAL_GRAD) { 11221f4b1b45SUmesh Unnikrishnan std::vector<sycl::event> e; 11231f4b1b45SUmesh Unnikrishnan 11241f4b1b45SUmesh Unnikrishnan if (!sycl_data->sycl_queue.is_in_order()) e = {sycl_data->sycl_queue.ext_oneapi_submit_barrier()}; 11251f4b1b45SUmesh Unnikrishnan sycl_data->sycl_queue.copy<CeedScalar>(grad_out, &asmb->d_B_out[mat_start], dim * elem_size * num_qpts, e); 1126dd64fc84SJeremy L Thompson mat_start += dim * elem_size * num_qpts; 1127bd882c8aSJames Wright } 1128bd882c8aSJames Wright } 1129bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 1130bd882c8aSJames Wright } 1131bd882c8aSJames Wright 1132bd882c8aSJames Wright //------------------------------------------------------------------------------ 1133bd882c8aSJames Wright // Matrix assembly kernel for low-order elements (3D thread block) 1134bd882c8aSJames Wright //------------------------------------------------------------------------------ 1135bd882c8aSJames Wright static int CeedOperatorLinearAssemble_Sycl(sycl::queue &sycl_queue, const CeedOperator_Sycl *impl, const CeedScalar *qf_array, 1136bd882c8aSJames Wright CeedScalar *values_array) { 1137bd882c8aSJames Wright // This kernels assumes B_in and B_out have the same number of quadrature points and basis points. 1138bd882c8aSJames Wright // TODO: expand to more general cases 1139bd882c8aSJames Wright CeedOperatorAssemble_Sycl *asmb = impl->asmb; 1140dd64fc84SJeremy L Thompson const CeedInt num_elem = asmb->num_elem; 1141dd64fc84SJeremy L Thompson const CeedSize num_nodes = asmb->num_nodes; 1142dd64fc84SJeremy L Thompson const CeedSize num_comp = asmb->num_comp; 1143dd64fc84SJeremy L Thompson const CeedSize num_qpts = asmb->num_qpts; 1144dd64fc84SJeremy L Thompson const CeedSize num_e_mode_in = asmb->num_e_mode_in; 1145dd64fc84SJeremy L Thompson const CeedSize num_e_mode_out = asmb->num_e_mode_out; 1146bd882c8aSJames Wright 1147bd882c8aSJames Wright // Strides for final output ordering, determined by the reference (inference) implementation of the symbolic assembly, slowest --> fastest: element, 1148bd882c8aSJames Wright // comp_in, comp_out, node_row, node_col 1149dd64fc84SJeremy L Thompson const CeedSize comp_out_stride = num_nodes * num_nodes; 1150dd64fc84SJeremy L Thompson const CeedSize comp_in_stride = comp_out_stride * num_comp; 1151dd64fc84SJeremy L Thompson const CeedSize e_stride = comp_in_stride * num_comp; 1152dd64fc84SJeremy L Thompson // Strides for QF array, slowest --> fastest: e_mode_in, comp_in, e_mode_out, comp_out, elem, qpt 1153dd64fc84SJeremy L Thompson const CeedSize q_e_stride = num_qpts; 1154dd64fc84SJeremy L Thompson const CeedSize q_comp_out_stride = num_elem * q_e_stride; 1155dd64fc84SJeremy L Thompson const CeedSize q_e_mode_out_stride = q_comp_out_stride * num_comp; 1156dd64fc84SJeremy L Thompson const CeedSize q_comp_in_stride = q_e_mode_out_stride * num_e_mode_out; 1157dd64fc84SJeremy L Thompson const CeedSize q_e_mode_in_stride = q_comp_in_stride * num_comp; 1158bd882c8aSJames Wright 1159bd882c8aSJames Wright CeedScalar *B_in, *B_out; 1160bd882c8aSJames Wright B_in = asmb->d_B_in; 1161bd882c8aSJames Wright B_out = asmb->d_B_out; 1162bd882c8aSJames Wright const CeedInt block_size_x = asmb->block_size_x; 1163bd882c8aSJames Wright const CeedInt block_size_y = asmb->block_size_y; 1164bd882c8aSJames Wright 1165dd64fc84SJeremy L Thompson sycl::range<3> kernel_range(num_elem, block_size_y, block_size_x); 1166bd882c8aSJames Wright 11671f4b1b45SUmesh Unnikrishnan std::vector<sycl::event> e; 11681f4b1b45SUmesh Unnikrishnan 11691f4b1b45SUmesh Unnikrishnan if (!sycl_queue.is_in_order()) e = {sycl_queue.ext_oneapi_submit_barrier()}; 11701f4b1b45SUmesh Unnikrishnan sycl_queue.parallel_for<CeedOperatorSyclLinearAssemble>(kernel_range, e, [=](sycl::id<3> idx) { 1171bd882c8aSJames Wright const int e = idx.get(0); // Element index 1172bd882c8aSJames Wright const int l = idx.get(1); // The output column index of each B^TDB operation 1173bd882c8aSJames Wright const int i = idx.get(2); // The output row index of each B^TDB operation 1174bd882c8aSJames Wright // such that we have (Bout^T)_ij D_jk Bin_kl = C_il 1175dd64fc84SJeremy L Thompson for (CeedSize comp_in = 0; comp_in < num_comp; comp_in++) { 1176dd64fc84SJeremy L Thompson for (CeedSize comp_out = 0; comp_out < num_comp; comp_out++) { 1177bd882c8aSJames Wright CeedScalar result = 0.0; 1178dd64fc84SJeremy L Thompson CeedSize qf_index_comp = q_comp_in_stride * comp_in + q_comp_out_stride * comp_out + q_e_stride * e; 1179dd64fc84SJeremy L Thompson 1180dd64fc84SJeremy L Thompson for (CeedSize e_mode_in = 0; e_mode_in < num_e_mode_in; e_mode_in++) { 1181dd64fc84SJeremy L Thompson CeedSize b_in_index = e_mode_in * num_qpts * num_nodes; 1182dd64fc84SJeremy L Thompson 1183dd64fc84SJeremy L Thompson for (CeedSize e_mode_out = 0; e_mode_out < num_e_mode_out; e_mode_out++) { 1184dd64fc84SJeremy L Thompson CeedSize b_out_index = e_mode_out * num_qpts * num_nodes; 1185dd64fc84SJeremy L Thompson CeedSize qf_index = qf_index_comp + q_e_mode_out_stride * e_mode_out + q_e_mode_in_stride * e_mode_in; 1186dd64fc84SJeremy L Thompson 1187bd882c8aSJames Wright // Perform the B^T D B operation for this 'chunk' of D (the qf_array) 1188dd64fc84SJeremy L Thompson for (CeedSize j = 0; j < num_qpts; j++) { 1189dd64fc84SJeremy L Thompson result += B_out[b_out_index + j * num_nodes + i] * qf_array[qf_index + j] * B_in[b_in_index + j * num_nodes + l]; 1190bd882c8aSJames Wright } 1191dd64fc84SJeremy L Thompson } // end of e_mode_out 1192dd64fc84SJeremy L Thompson } // end of e_mode_in 1193dd64fc84SJeremy L Thompson CeedSize val_index = comp_in_stride * comp_in + comp_out_stride * comp_out + e_stride * e + num_nodes * i + l; 1194dd64fc84SJeremy L Thompson 1195bd882c8aSJames Wright values_array[val_index] = result; 1196bd882c8aSJames Wright } // end of out component 1197bd882c8aSJames Wright } // end of in component 1198bd882c8aSJames Wright }); 1199bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 1200bd882c8aSJames Wright } 1201bd882c8aSJames Wright 1202bd882c8aSJames Wright //------------------------------------------------------------------------------ 1203bd882c8aSJames Wright // Fallback kernel for larger orders (1D thread block) 1204bd882c8aSJames Wright //------------------------------------------------------------------------------ 1205bd882c8aSJames Wright /* 1206bd882c8aSJames Wright static int CeedOperatorLinearAssembleFallback_Sycl(sycl::queue &sycl_queue, const CeedOperator_Sycl *impl, const CeedScalar *qf_array, 1207bd882c8aSJames Wright CeedScalar *values_array) { 1208bd882c8aSJames Wright // This kernel assumes B_in and B_out have the same number of quadrature points and basis points. 1209bd882c8aSJames Wright // TODO: expand to more general cases 1210bd882c8aSJames Wright CeedOperatorAssemble_Sycl *asmb = impl->asmb; 1211dd64fc84SJeremy L Thompson const CeedInt num_elem = asmb->num_elem; 1212dd64fc84SJeremy L Thompson const CeedInt num_nodes = asmb->num_nodes; 1213dd64fc84SJeremy L Thompson const CeedInt num_comp = asmb->num_comp; 1214dd64fc84SJeremy L Thompson const CeedInt num_qpts = asmb->num_qpts; 1215dd64fc84SJeremy L Thompson const CeedInt num_e_mode_in = asmb->num_e_mode_in; 1216dd64fc84SJeremy L Thompson const CeedInt num_e_mode_out = asmb->num_e_mode_out; 1217bd882c8aSJames Wright 1218bd882c8aSJames Wright // Strides for final output ordering, determined by the reference (interface) implementation of the symbolic assembly, slowest --> fastest: elememt, 1219bd882c8aSJames Wright // comp_in, comp_out, node_row, node_col 1220dd64fc84SJeremy L Thompson const CeedInt comp_out_stride = num_nodes * num_nodes; 1221dd64fc84SJeremy L Thompson const CeedInt comp_in_stride = comp_out_stride * num_comp; 1222dd64fc84SJeremy L Thompson const CeedInt e_stride = comp_in_stride * num_comp; 1223dd64fc84SJeremy L Thompson // Strides for QF array, slowest --> fastest: e_mode_in, comp_in, e_mode_out, comp_out, elem, qpt 1224dd64fc84SJeremy L Thompson const CeedInt q_e_stride = num_qpts; 1225dd64fc84SJeremy L Thompson const CeedInt q_comp_out_stride = num_elem * q_e_stride; 1226dd64fc84SJeremy L Thompson const CeedInt q_e_mode_out_stride = q_comp_out_stride * num_comp; 1227dd64fc84SJeremy L Thompson const CeedInt q_comp_in_stride = q_e_mode_out_stride * num_e_mode_out; 1228dd64fc84SJeremy L Thompson const CeedInt q_e_mode_in_stride = q_comp_in_stride * num_comp; 1229bd882c8aSJames Wright 1230bd882c8aSJames Wright CeedScalar *B_in, *B_out; 1231bd882c8aSJames Wright B_in = asmb->d_B_in; 1232bd882c8aSJames Wright B_out = asmb->d_B_out; 1233004e4986SSebastian Grimberg const CeedInt elems_per_block = asmb->elems_per_block; 1234bd882c8aSJames Wright const CeedInt block_size_x = asmb->block_size_x; 1235bd882c8aSJames Wright const CeedInt block_size_y = asmb->block_size_y; // This will be 1 for the fallback kernel 1236bd882c8aSJames Wright 1237004e4986SSebastian Grimberg const CeedInt grid = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0); 1238004e4986SSebastian Grimberg sycl::range<3> local_range(block_size_x, block_size_y, elems_per_block); 1239004e4986SSebastian Grimberg sycl::range<3> global_range(grid * block_size_x, block_size_y, elems_per_block); 1240bd882c8aSJames Wright sycl::nd_range<3> kernel_range(global_range, local_range); 1241bd882c8aSJames Wright 1242bd882c8aSJames Wright sycl_queue.parallel_for<CeedOperatorSyclLinearAssembleFallback>(kernel_range, [=](sycl::nd_item<3> work_item) { 1243bd882c8aSJames Wright const CeedInt blockIdx = work_item.get_group(0); 1244bd882c8aSJames Wright const CeedInt gridDimx = work_item.get_group_range(0); 1245bd882c8aSJames Wright const CeedInt threadIdx = work_item.get_local_id(0); 1246bd882c8aSJames Wright const CeedInt threadIdz = work_item.get_local_id(2); 1247bd882c8aSJames Wright const CeedInt blockDimz = work_item.get_local_range(2); 1248bd882c8aSJames Wright 1249bd882c8aSJames Wright const int l = threadIdx; // The output column index of each B^TDB operation 1250bd882c8aSJames Wright // such that we have (Bout^T)_ij D_jk Bin_kl = C_il 1251dd64fc84SJeremy L Thompson for (CeedInt e = blockIdx * blockDimz + threadIdz; e < num_elem; e += gridDimx * blockDimz) { 1252dd64fc84SJeremy L Thompson for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) { 1253dd64fc84SJeremy L Thompson for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) { 1254dd64fc84SJeremy L Thompson for (CeedInt i = 0; i < num_nodes; i++) { 1255bd882c8aSJames Wright CeedScalar result = 0.0; 1256dd64fc84SJeremy L Thompson CeedInt qf_index_comp = q_comp_in_stride * comp_in + q_comp_out_stride * comp_out + q_e_stride * e; 1257dd64fc84SJeremy L Thompson for (CeedInt e_mode_in = 0; e_mode_in < num_e_mode_in; e_mode_in++) { 1258dd64fc84SJeremy L Thompson CeedInt b_in_index = e_mode_in * num_qpts * num_nodes; 1259dd64fc84SJeremy L Thompson for (CeedInt e_mode_out = 0; e_mode_out < num_e_mode_out; e_mode_out++) { 1260dd64fc84SJeremy L Thompson CeedInt b_out_index = e_mode_out * num_qpts * num_nodes; 1261dd64fc84SJeremy L Thompson CeedInt qf_index = qf_index_comp + q_e_mode_out_stride * e_mode_out + q_e_mode_in_stride * e_mode_in; 1262bd882c8aSJames Wright // Perform the B^T D B operation for this 'chunk' of D (the qf_array) 1263dd64fc84SJeremy L Thompson for (CeedInt j = 0; j < num_qpts; j++) { 1264dd64fc84SJeremy L Thompson result += B_out[b_out_index + j * num_nodes + i] * qf_array[qf_index + j] * B_in[b_in_index + j * num_nodes + l]; 1265bd882c8aSJames Wright } 1266dd64fc84SJeremy L Thompson } // end of e_mode_out 1267dd64fc84SJeremy L Thompson } // end of e_mode_in 1268dd64fc84SJeremy L Thompson CeedInt val_index = comp_in_stride * comp_in + comp_out_stride * comp_out + e_stride * e + num_nodes * i + l; 1269bd882c8aSJames Wright values_array[val_index] = result; 1270bd882c8aSJames Wright } // end of loop over element node index, i 1271bd882c8aSJames Wright } // end of out component 1272bd882c8aSJames Wright } // end of in component 1273bd882c8aSJames Wright } // end of element loop 1274bd882c8aSJames Wright }); 1275bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 1276bd882c8aSJames Wright }*/ 1277bd882c8aSJames Wright 1278bd882c8aSJames Wright //------------------------------------------------------------------------------ 1279bd882c8aSJames Wright // Assemble matrix data for COO matrix of assembled operator. 1280bd882c8aSJames Wright // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic. 1281bd882c8aSJames Wright // 1282bd882c8aSJames Wright // Note that this (and other assembly routines) currently assume only one active 1283bd882c8aSJames Wright // input restriction/basis per operator (could have multiple basis eval modes). 1284bd882c8aSJames Wright // TODO: allow multiple active input restrictions/basis objects 1285bd882c8aSJames Wright //------------------------------------------------------------------------------ 1286bd882c8aSJames Wright static int CeedSingleOperatorAssemble_Sycl(CeedOperator op, CeedInt offset, CeedVector values) { 1287bd882c8aSJames Wright Ceed ceed; 1288bd882c8aSJames Wright Ceed_Sycl *sycl_data; 1289dd64fc84SJeremy L Thompson CeedScalar *values_array; 1290dd64fc84SJeremy L Thompson const CeedScalar *qf_array; 1291dd64fc84SJeremy L Thompson CeedVector assembled_qf = NULL; 1292dd64fc84SJeremy L Thompson CeedElemRestriction rstr_q = NULL; 1293dd64fc84SJeremy L Thompson CeedOperator_Sycl *impl; 1294dd64fc84SJeremy L Thompson 1295dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1296dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &impl)); 1297bd882c8aSJames Wright CeedCallBackend(CeedGetData(ceed, &sycl_data)); 1298bd882c8aSJames Wright 1299bd882c8aSJames Wright // Setup 1300bd882c8aSJames Wright if (!impl->asmb) { 1301bd882c8aSJames Wright CeedCallBackend(CeedSingleOperatorAssembleSetup_Sycl(op)); 1302bd882c8aSJames Wright assert(impl->asmb != NULL); 1303bd882c8aSJames Wright } 1304bd882c8aSJames Wright 1305bd882c8aSJames Wright // Assemble QFunction 1306bd882c8aSJames Wright CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE)); 1307bd882c8aSJames Wright CeedCallBackend(CeedElemRestrictionDestroy(&rstr_q)); 1308bd882c8aSJames Wright CeedCallBackend(CeedVectorGetArrayWrite(values, CEED_MEM_DEVICE, &values_array)); 1309bd882c8aSJames Wright values_array += offset; 1310bd882c8aSJames Wright CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &qf_array)); 1311bd882c8aSJames Wright 1312bd882c8aSJames Wright // Compute B^T D B 1313bd882c8aSJames Wright CeedCallBackend(CeedOperatorLinearAssemble_Sycl(sycl_data->sycl_queue, impl, qf_array, values_array)); 1314bd882c8aSJames Wright 1315bd882c8aSJames Wright // Wait for kernels to be completed 1316bd882c8aSJames Wright // Kris: Review if this is necessary -- enqueing an async barrier may be sufficient 1317bd882c8aSJames Wright sycl_data->sycl_queue.wait_and_throw(); 1318bd882c8aSJames Wright 1319bd882c8aSJames Wright // Restore arrays 1320bd882c8aSJames Wright CeedCallBackend(CeedVectorRestoreArray(values, &values_array)); 1321bd882c8aSJames Wright CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &qf_array)); 1322bd882c8aSJames Wright 1323bd882c8aSJames Wright // Cleanup 1324bd882c8aSJames Wright CeedCallBackend(CeedVectorDestroy(&assembled_qf)); 1325bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 1326bd882c8aSJames Wright } 1327bd882c8aSJames Wright 1328bd882c8aSJames Wright //------------------------------------------------------------------------------ 1329bd882c8aSJames Wright // Create operator 1330bd882c8aSJames Wright //------------------------------------------------------------------------------ 1331bd882c8aSJames Wright int CeedOperatorCreate_Sycl(CeedOperator op) { 1332bd882c8aSJames Wright Ceed ceed; 1333bd882c8aSJames Wright CeedOperator_Sycl *impl; 1334bd882c8aSJames Wright 1335dd64fc84SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1336dd64fc84SJeremy L Thompson 1337bd882c8aSJames Wright CeedCallBackend(CeedCalloc(1, &impl)); 1338bd882c8aSJames Wright CeedCallBackend(CeedOperatorSetData(op, impl)); 1339bd882c8aSJames Wright 1340bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Sycl)); 1341bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Sycl)); 1342bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Sycl)); 1343bd882c8aSJames Wright CeedCallBackend( 1344bd882c8aSJames Wright CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Sycl)); 1345bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Sycl)); 1346bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Sycl)); 1347bd882c8aSJames Wright CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Sycl)); 1348bd882c8aSJames Wright return CEED_ERROR_SUCCESS; 1349bd882c8aSJames Wright } 1350bd882c8aSJames Wright 1351bd882c8aSJames Wright //------------------------------------------------------------------------------ 1352