xref: /libCEED/rust/libceed-sys/c-src/backends/sycl-ref/ceed-sycl-ref-operator.sycl.cpp (revision dd64fc8452c2d35c954858232143719e6bb2e61d)
1bd882c8aSJames Wright // Copyright (c) 2017-2022, 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 //------------------------------------------------------------------------------
26*dd64fc84SJeremy L Thompson void CeedOperatorGetBasisPointer_Sycl(const CeedScalar **basis_ptr, CeedEvalMode e_mode, const CeedScalar *identity, const CeedScalar *interp,
27bd882c8aSJames Wright                                       const CeedScalar *grad) {
28*dd64fc84SJeremy L Thompson   switch (e_mode) {
29bd882c8aSJames Wright     case CEED_EVAL_NONE:
30*dd64fc84SJeremy L Thompson       *basis_ptr = identity;
31bd882c8aSJames Wright       break;
32bd882c8aSJames Wright     case CEED_EVAL_INTERP:
33*dd64fc84SJeremy L Thompson       *basis_ptr = interp;
34bd882c8aSJames Wright       break;
35bd882c8aSJames Wright     case CEED_EVAL_GRAD:
36*dd64fc84SJeremy 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;
51*dd64fc84SJeremy L Thompson   CeedOperator_Sycl *impl;
52*dd64fc84SJeremy L Thompson 
53*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
54*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
55bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
56bd882c8aSJames Wright 
57bd882c8aSJames Wright   // Apply data
58*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_e_in + impl->num_e_out; i++) {
59*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
60bd882c8aSJames Wright   }
61*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
62bd882c8aSJames Wright 
63*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_e_in; i++) {
64*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
65bd882c8aSJames Wright   }
66*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
67bd882c8aSJames Wright 
68*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_e_out; i++) {
69*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
70bd882c8aSJames Wright   }
71*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
72bd882c8aSJames Wright 
73bd882c8aSJames Wright   // QFunction assembly data
74*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
75*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
76bd882c8aSJames Wright   }
77*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
78bd882c8aSJames Wright 
79bd882c8aSJames Wright   // Diag data
80bd882c8aSJames Wright   if (impl->diag) {
81*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedFree(&impl->diag->h_e_mode_in));
82*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedFree(&impl->diag->h_e_mode_out));
83bd882c8aSJames Wright 
84bd882c8aSJames Wright     CeedCallSycl(ceed, sycl_data->sycl_queue.wait_and_throw());
85*dd64fc84SJeremy L Thompson     CeedCallSycl(ceed, sycl::free(impl->diag->d_e_mode_in, sycl_data->sycl_context));
86*dd64fc84SJeremy 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));
88*dd64fc84SJeremy L Thompson     CeedCallSycl(ceed, sycl::free(impl->diag->d_interp_in, sycl_data->sycl_context));
89*dd64fc84SJeremy L Thompson     CeedCallSycl(ceed, sycl::free(impl->diag->d_interp_out, sycl_data->sycl_context));
90*dd64fc84SJeremy L Thompson     CeedCallSycl(ceed, sycl::free(impl->diag->d_grad_in, sycl_data->sycl_context));
91*dd64fc84SJeremy L Thompson     CeedCallSycl(ceed, sycl::free(impl->diag->d_grad_out, sycl_data->sycl_context));
92*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
93bd882c8aSJames Wright 
94*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
95*dd64fc84SJeremy 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 //------------------------------------------------------------------------------
113*dd64fc84SJeremy L Thompson static int CeedOperatorSetupFields_Sycl(CeedQFunction qf, CeedOperator op, bool is_input, CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e,
114*dd64fc84SJeremy L Thompson                                         CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
115bd882c8aSJames Wright   Ceed                ceed;
116*dd64fc84SJeremy L Thompson   CeedSize            q_size;
117*dd64fc84SJeremy L Thompson   bool                is_strided, skip_restriction;
118*dd64fc84SJeremy L Thompson   CeedInt             dim, size;
119*dd64fc84SJeremy L Thompson   CeedOperatorField  *op_fields;
120*dd64fc84SJeremy L Thompson   CeedQFunctionField *qf_fields;
121bd882c8aSJames Wright 
122*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
123*dd64fc84SJeremy L Thompson   if (is_input) {
124*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
125*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
126bd882c8aSJames Wright   } else {
127*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
128*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
129bd882c8aSJames Wright   }
130bd882c8aSJames Wright 
131bd882c8aSJames Wright   // Loop over fields
132*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
133*dd64fc84SJeremy L Thompson     CeedEvalMode        e_mode;
134*dd64fc84SJeremy L Thompson     CeedVector          vec;
135*dd64fc84SJeremy L Thompson     CeedElemRestriction rstr;
136*dd64fc84SJeremy L Thompson     CeedBasis           basis;
137bd882c8aSJames Wright 
138*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode));
139*dd64fc84SJeremy L Thompson 
140*dd64fc84SJeremy L Thompson     is_strided       = false;
141*dd64fc84SJeremy L Thompson     skip_restriction = false;
142*dd64fc84SJeremy L Thompson     if (e_mode != CEED_EVAL_WEIGHT) {
143*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
144bd882c8aSJames Wright 
145bd882c8aSJames Wright       // Check whether this field can skip the element restriction:
146*dd64fc84SJeremy 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:
149*dd64fc84SJeremy L Thompson       if (is_input) {
150bd882c8aSJames Wright         // Check for passive input:
151*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
152*dd64fc84SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
153*dd64fc84SJeremy L Thompson           // Check  e_mode
154*dd64fc84SJeremy L Thompson           if (e_mode == CEED_EVAL_NONE) {
155*dd64fc84SJeremy L Thompson             // Check for  is_strided restriction
156*dd64fc84SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(rstr, &is_strided));
157*dd64fc84SJeremy L Thompson             if (is_strided) {
158bd882c8aSJames Wright               // Check if vector is already in preferred backend ordering
159*dd64fc84SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &skip_restriction));
160bd882c8aSJames Wright             }
161bd882c8aSJames Wright           }
162bd882c8aSJames Wright         }
163bd882c8aSJames Wright       }
164*dd64fc84SJeremy 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
166*dd64fc84SJeremy L Thompson         e_vecs[i + start_e] = NULL;
167bd882c8aSJames Wright       } else {
168*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(rstr, NULL, &e_vecs[i + start_e]));
169bd882c8aSJames Wright       }
170bd882c8aSJames Wright     }
171bd882c8aSJames Wright 
172*dd64fc84SJeremy L Thompson     switch (e_mode) {
173bd882c8aSJames Wright       case CEED_EVAL_NONE:
174*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
175*dd64fc84SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
176*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
177bd882c8aSJames Wright         break;
178bd882c8aSJames Wright       case CEED_EVAL_INTERP:
179*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
180*dd64fc84SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
181*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
182bd882c8aSJames Wright         break;
183bd882c8aSJames Wright       case CEED_EVAL_GRAD:
184*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
185*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
186bd882c8aSJames Wright         CeedCallBackend(CeedBasisGetDimension(basis, &dim));
187*dd64fc84SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
188*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
189bd882c8aSJames Wright         break;
190bd882c8aSJames Wright       case CEED_EVAL_WEIGHT:  // Only on input fields
191*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
192*dd64fc84SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
193*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
194*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, NULL, 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;
211*dd64fc84SJeremy L Thompson   bool                is_setup_done;
212*dd64fc84SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
213*dd64fc84SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
214bd882c8aSJames Wright   CeedQFunction       qf;
215*dd64fc84SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
216*dd64fc84SJeremy L Thompson   CeedOperator_Sycl  *impl;
217*dd64fc84SJeremy L Thompson 
218*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
219*dd64fc84SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
220*dd64fc84SJeremy L Thompson 
221*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
222*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
223bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
224bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
225*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
226*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
227*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
228bd882c8aSJames Wright 
229bd882c8aSJames Wright   // Allocate
230*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
231bd882c8aSJames Wright 
232*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
233*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
234bd882c8aSJames Wright 
235*dd64fc84SJeremy L Thompson   impl->num_e_in  = num_input_fields;
236*dd64fc84SJeremy L Thompson   impl->num_e_out = num_output_fields;
237bd882c8aSJames Wright 
238*dd64fc84SJeremy L Thompson   // Set up infield and outfield  e_vecs and  q_vecs
239bd882c8aSJames Wright   // Infields
240*dd64fc84SJeremy 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
242*dd64fc84SJeremy 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 //------------------------------------------------------------------------------
251*dd64fc84SJeremy L Thompson static inline int CeedOperatorSetupInputs_Sycl(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
252*dd64fc84SJeremy L Thompson                                                CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
253bd882c8aSJames Wright                                                CeedOperator_Sycl *impl, CeedRequest *request) {
254*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
255*dd64fc84SJeremy L Thompson     CeedEvalMode        e_mode;
256bd882c8aSJames Wright     CeedVector          vec;
257*dd64fc84SJeremy L Thompson     CeedElemRestriction rstr;
258bd882c8aSJames Wright 
259bd882c8aSJames Wright     // Get input vector
260*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
261bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
262*dd64fc84SJeremy L Thompson       if (skip_active) continue;
263*dd64fc84SJeremy L Thompson       else vec = in_vec;
264bd882c8aSJames Wright     }
265bd882c8aSJames Wright 
266*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &e_mode));
267*dd64fc84SJeremy L Thompson     if (e_mode == CEED_EVAL_WEIGHT) {  // Skip
268bd882c8aSJames Wright     } else {
269bd882c8aSJames Wright       // Get input vector
270*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
271bd882c8aSJames Wright       // Get input element restriction
272*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
273*dd64fc84SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
274bd882c8aSJames Wright       // Restrict, if necessary
275*dd64fc84SJeremy L Thompson       if (!impl->e_vecs[i]) {
276bd882c8aSJames Wright         // No restriction for this field; read data directly from vec.
277*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
278bd882c8aSJames Wright       } else {
279*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
280bd882c8aSJames Wright         // Get evec
281*dd64fc84SJeremy 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 //------------------------------------------------------------------------------
291*dd64fc84SJeremy L Thompson static inline int CeedOperatorInputBasis_Sycl(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
292*dd64fc84SJeremy L Thompson                                               CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
293bd882c8aSJames Wright                                               CeedOperator_Sycl *impl) {
294*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
295*dd64fc84SJeremy L Thompson     CeedInt             elem_size, size;
296*dd64fc84SJeremy L Thompson     CeedElemRestriction rstr;
297*dd64fc84SJeremy L Thompson     CeedEvalMode        e_mode;
298bd882c8aSJames Wright     CeedBasis           basis;
299bd882c8aSJames Wright 
300bd882c8aSJames Wright     // Skip active input
301*dd64fc84SJeremy L Thompson     if (skip_active) {
302bd882c8aSJames Wright       CeedVector vec;
303*dd64fc84SJeremy L Thompson 
304*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
305bd882c8aSJames Wright       if (vec == CEED_VECTOR_ACTIVE) continue;
306bd882c8aSJames Wright     }
307*dd64fc84SJeremy L Thompson     // Get elem_size,  e_mode, size
308*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
309*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
310*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &e_mode));
311*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
312bd882c8aSJames Wright     // Basis action
313*dd64fc84SJeremy L Thompson     switch (e_mode) {
314bd882c8aSJames Wright       case CEED_EVAL_NONE:
315*dd64fc84SJeremy 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:
318*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
319*dd64fc84SJeremy 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:
322*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
323*dd64fc84SJeremy 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 //------------------------------------------------------------------------------
339*dd64fc84SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Sycl(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
340*dd64fc84SJeremy L Thompson                                                  const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Sycl *impl) {
341*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
342*dd64fc84SJeremy L Thompson     CeedEvalMode e_mode;
343bd882c8aSJames Wright     CeedVector   vec;
344bd882c8aSJames Wright 
345bd882c8aSJames Wright     // Skip active input
346*dd64fc84SJeremy L Thompson     if (skip_active) {
347*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
348bd882c8aSJames Wright       if (vec == CEED_VECTOR_ACTIVE) continue;
349bd882c8aSJames Wright     }
350*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &e_mode));
351*dd64fc84SJeremy L Thompson     if (e_mode == CEED_EVAL_WEIGHT) {  // Skip
352bd882c8aSJames Wright     } else {
353*dd64fc84SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a  skip_restriction case
354*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
355*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
356bd882c8aSJames Wright       } else {
357*dd64fc84SJeremy 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 //------------------------------------------------------------------------------
367*dd64fc84SJeremy L Thompson static int CeedOperatorApplyAdd_Sycl(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
368*dd64fc84SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
369*dd64fc84SJeremy L Thompson   CeedEvalMode        e_mode;
370*dd64fc84SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {0};
371*dd64fc84SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
372bd882c8aSJames Wright   CeedQFunction       qf;
373*dd64fc84SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
374*dd64fc84SJeremy L Thompson   CeedOperator_Sycl  *impl;
375*dd64fc84SJeremy L Thompson 
376*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
377bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
378bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
379*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
380*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
381*dd64fc84SJeremy 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
387*dd64fc84SJeremy 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
390*dd64fc84SJeremy 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
393*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
394*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &e_mode));
395*dd64fc84SJeremy L Thompson     if (e_mode == CEED_EVAL_NONE) {
396bd882c8aSJames Wright       // Set the output Q-Vector to use the E-Vector data directly
397*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_e_in], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
398*dd64fc84SJeremy 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
403*dd64fc84SJeremy 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
406*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
407*dd64fc84SJeremy L Thompson     CeedElemRestriction rstr;
408*dd64fc84SJeremy L Thompson     CeedBasis           basis;
409*dd64fc84SJeremy L Thompson 
410*dd64fc84SJeremy L Thompson     // Get elem_size,  e_mode, size
411*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
412*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
413*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &e_mode));
414*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
415bd882c8aSJames Wright     // Basis action
416*dd64fc84SJeremy L Thompson     switch (e_mode) {
417bd882c8aSJames Wright       case CEED_EVAL_NONE:
418bd882c8aSJames Wright         break;
419bd882c8aSJames Wright       case CEED_EVAL_INTERP:
420*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
421*dd64fc84SJeremy 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:
424*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
425*dd64fc84SJeremy 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;
430bd882c8aSJames Wright         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
431bd882c8aSJames Wright         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
432bd882c8aSJames Wright         break;  // Should not occur
433bd882c8aSJames Wright       case CEED_EVAL_DIV:
434bd882c8aSJames Wright         break;  // TODO: Not implemented
435bd882c8aSJames Wright       case CEED_EVAL_CURL:
436bd882c8aSJames Wright         break;  // TODO: Not implemented
437bd882c8aSJames Wright                 // LCOV_EXCL_STOP
438bd882c8aSJames Wright     }
439bd882c8aSJames Wright   }
440bd882c8aSJames Wright 
441bd882c8aSJames Wright   // Output restriction
442*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
443*dd64fc84SJeremy L Thompson     CeedVector          vec;
444*dd64fc84SJeremy L Thompson     CeedElemRestriction rstr;
445*dd64fc84SJeremy L Thompson 
446bd882c8aSJames Wright     // Restore evec
447*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &e_mode));
448*dd64fc84SJeremy L Thompson     if (e_mode == CEED_EVAL_NONE) {
449*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_e_in], &e_data[i + num_input_fields]));
450bd882c8aSJames Wright     }
451bd882c8aSJames Wright     // Get output vector
452*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
453bd882c8aSJames Wright     // Restrict
454*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
455bd882c8aSJames Wright     // Active
456*dd64fc84SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
457bd882c8aSJames Wright 
458*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_e_in], vec, request));
459bd882c8aSJames Wright   }
460bd882c8aSJames Wright 
461bd882c8aSJames Wright   // Restore input arrays
462*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
463bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
464bd882c8aSJames Wright }
465bd882c8aSJames Wright 
466bd882c8aSJames Wright //------------------------------------------------------------------------------
467bd882c8aSJames Wright // Core code for assembling linear QFunction
468bd882c8aSJames Wright //------------------------------------------------------------------------------
469bd882c8aSJames Wright static inline int CeedOperatorLinearAssembleQFunctionCore_Sycl(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
470bd882c8aSJames Wright                                                                CeedRequest *request) {
471*dd64fc84SJeremy L Thompson   Ceed                ceed, ceed_parent;
472e984cf9aSJeremy L Thompson   CeedSize            q_size;
473*dd64fc84SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
474*dd64fc84SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
475*dd64fc84SJeremy L Thompson   CeedVector         *active_in;
476*dd64fc84SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
477*dd64fc84SJeremy L Thompson   CeedQFunction       qf;
478*dd64fc84SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
479*dd64fc84SJeremy L Thompson   CeedOperator_Sycl  *impl;
480*dd64fc84SJeremy L Thompson 
481bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
482*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
483e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
484e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
485e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
486*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
487*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
488*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
489*dd64fc84SJeremy L Thompson   active_in     = impl->qf_active_in;
490*dd64fc84SJeremy L Thompson   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
491bd882c8aSJames Wright 
492bd882c8aSJames Wright   // Setup
493bd882c8aSJames Wright   CeedCallBackend(CeedOperatorSetup_Sycl(op));
494bd882c8aSJames Wright 
495bd882c8aSJames Wright   // Check for identity
496*dd64fc84SJeremy L Thompson   bool is_identity_qf;
497*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf));
498*dd64fc84SJeremy L Thompson   if (is_identity_qf) {
499bd882c8aSJames Wright     // LCOV_EXCL_START
500bd882c8aSJames Wright     return CeedError(ceed, CEED_ERROR_BACKEND, "Assembling identity QFunctions not supported");
501bd882c8aSJames Wright     // LCOV_EXCL_STOP
502bd882c8aSJames Wright   }
503bd882c8aSJames Wright 
504bd882c8aSJames Wright   // Input Evecs and Restriction
505*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
506bd882c8aSJames Wright 
507bd882c8aSJames Wright   // Count number of active input fields
508*dd64fc84SJeremy L Thompson   if (!num_active_in) {
509*dd64fc84SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
510*dd64fc84SJeremy L Thompson       CeedScalar *q_vec_array;
511*dd64fc84SJeremy L Thompson       CeedVector  vec;
512*dd64fc84SJeremy L Thompson 
513bd882c8aSJames Wright       // Get input vector
514*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
515bd882c8aSJames Wright       // Check if active input
516bd882c8aSJames Wright       if (vec == CEED_VECTOR_ACTIVE) {
517*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
518*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
519*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
520*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_in));
521bd882c8aSJames Wright         for (CeedInt field = 0; field < size; field++) {
522*dd64fc84SJeremy L Thompson           q_size = (CeedSize)Q * num_elem;
523*dd64fc84SJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_in[num_active_in + field]));
524*dd64fc84SJeremy L Thompson           CeedCallBackend(
525*dd64fc84SJeremy L Thompson               CeedVectorSetArray(active_in[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
526bd882c8aSJames Wright         }
527*dd64fc84SJeremy L Thompson         num_active_in += size;
528*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
529bd882c8aSJames Wright       }
530bd882c8aSJames Wright     }
531*dd64fc84SJeremy L Thompson     impl->num_active_in = num_active_in;
532*dd64fc84SJeremy L Thompson     impl->qf_active_in  = active_in;
533bd882c8aSJames Wright   }
534bd882c8aSJames Wright 
535bd882c8aSJames Wright   // Count number of active output fields
536*dd64fc84SJeremy L Thompson   if (!num_active_out) {
537*dd64fc84SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
538*dd64fc84SJeremy L Thompson       CeedVector vec;
539*dd64fc84SJeremy L Thompson 
540bd882c8aSJames Wright       // Get output vector
541*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
542bd882c8aSJames Wright       // Check if active output
543bd882c8aSJames Wright       if (vec == CEED_VECTOR_ACTIVE) {
544*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
545*dd64fc84SJeremy L Thompson         num_active_out += size;
546bd882c8aSJames Wright       }
547bd882c8aSJames Wright     }
548*dd64fc84SJeremy L Thompson     impl->num_active_out = num_active_out;
549bd882c8aSJames Wright   }
550bd882c8aSJames Wright 
551bd882c8aSJames Wright   // Check sizes
552*dd64fc84SJeremy L Thompson   if (!num_active_in || !num_active_out) {
553bd882c8aSJames Wright     // LCOV_EXCL_START
554bd882c8aSJames Wright     return CeedError(ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
555bd882c8aSJames Wright     // LCOV_EXCL_STOP
556bd882c8aSJames Wright   }
557bd882c8aSJames Wright 
558bd882c8aSJames Wright   // Build objects if needed
559bd882c8aSJames Wright   if (build_objects) {
560*dd64fc84SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
561*dd64fc84SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
562*dd64fc84SJeremy L Thompson 
563bd882c8aSJames Wright     // Create output restriction
564*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
565*dd64fc84SJeremy L Thompson                                                      num_active_in * num_active_out * num_elem * Q, strides, rstr));
566bd882c8aSJames Wright     // Create assembled vector
567*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
568bd882c8aSJames Wright   }
569bd882c8aSJames Wright   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
570*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
571bd882c8aSJames Wright 
572bd882c8aSJames Wright   // Input basis apply
573*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Sycl(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
574bd882c8aSJames Wright 
575bd882c8aSJames Wright   // Assemble QFunction
576*dd64fc84SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
577bd882c8aSJames Wright     // Set Inputs
578*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(active_in[in], 1.0));
579*dd64fc84SJeremy L Thompson     if (num_active_in > 1) {
580*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_in[(in + num_active_in - 1) % num_active_in], 0.0));
581bd882c8aSJames Wright     }
582bd882c8aSJames Wright     // Set Outputs
583*dd64fc84SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
584*dd64fc84SJeremy L Thompson       CeedVector vec;
585*dd64fc84SJeremy L Thompson 
586bd882c8aSJames Wright       // Get output vector
587*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
588bd882c8aSJames Wright       // Check if active output
589bd882c8aSJames Wright       if (vec == CEED_VECTOR_ACTIVE) {
590*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
591*dd64fc84SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
592*dd64fc84SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
593bd882c8aSJames Wright       }
594bd882c8aSJames Wright     }
595bd882c8aSJames Wright     // Apply QFunction
596*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
597bd882c8aSJames Wright   }
598bd882c8aSJames Wright 
599bd882c8aSJames Wright   // Un-set output Qvecs to prevent accidental overwrite of Assembled
600*dd64fc84SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
601*dd64fc84SJeremy L Thompson     CeedVector vec;
602*dd64fc84SJeremy L Thompson 
603bd882c8aSJames Wright     // Get output vector
604*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
605bd882c8aSJames Wright     // Check if active output
606bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
607*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
608bd882c8aSJames Wright     }
609bd882c8aSJames Wright   }
610bd882c8aSJames Wright 
611bd882c8aSJames Wright   // Restore input arrays
612*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
613bd882c8aSJames Wright 
614bd882c8aSJames Wright   // Restore output
615*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
616bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
617bd882c8aSJames Wright }
618bd882c8aSJames Wright 
619bd882c8aSJames Wright //------------------------------------------------------------------------------
620bd882c8aSJames Wright // Assemble Linear QFunction
621bd882c8aSJames Wright //------------------------------------------------------------------------------
622bd882c8aSJames Wright static int CeedOperatorLinearAssembleQFunction_Sycl(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
623bd882c8aSJames Wright   return CeedOperatorLinearAssembleQFunctionCore_Sycl(op, true, assembled, rstr, request);
624bd882c8aSJames Wright }
625bd882c8aSJames Wright 
626bd882c8aSJames Wright //------------------------------------------------------------------------------
627bd882c8aSJames Wright // Update Assembled Linear QFunction
628bd882c8aSJames Wright //------------------------------------------------------------------------------
629bd882c8aSJames Wright static int CeedOperatorLinearAssembleQFunctionUpdate_Sycl(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
630bd882c8aSJames Wright   return CeedOperatorLinearAssembleQFunctionCore_Sycl(op, false, &assembled, &rstr, request);
631bd882c8aSJames Wright }
632bd882c8aSJames Wright 
633bd882c8aSJames Wright //------------------------------------------------------------------------------
634bd882c8aSJames Wright // Create point block restriction
635bd882c8aSJames Wright //------------------------------------------------------------------------------
636*dd64fc84SJeremy L Thompson static int CreatePBRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) {
637bd882c8aSJames Wright   Ceed           ceed;
638*dd64fc84SJeremy L Thompson   CeedSize       l_size;
639*dd64fc84SJeremy L Thompson   CeedInt        num_elem, num_comp, elem_size, comp_stride, *point_block_offsets;
640bd882c8aSJames Wright   const CeedInt *offsets;
641*dd64fc84SJeremy L Thompson 
642*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
643bd882c8aSJames Wright   CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
644bd882c8aSJames Wright 
645bd882c8aSJames Wright   // Expand offsets
646*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
647*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
648*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
649*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
650bd882c8aSJames Wright   CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
651*dd64fc84SJeremy L Thompson   CeedInt shift = num_comp;
652*dd64fc84SJeremy L Thompson 
653*dd64fc84SJeremy L Thompson   if (comp_stride != 1) shift *= num_comp;
654*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_elem * elem_size, &point_block_offsets));
655*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
656*dd64fc84SJeremy L Thompson     point_block_offsets[i] = offsets[i] * shift;
657bd882c8aSJames Wright   }
658bd882c8aSJames Wright 
659bd882c8aSJames Wright   // Create new restriction
660*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
661*dd64fc84SJeremy L Thompson                                             point_block_offsets, point_block_rstr));
662bd882c8aSJames Wright 
663bd882c8aSJames Wright   // Cleanup
664bd882c8aSJames Wright   CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
665bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
666bd882c8aSJames Wright }
667bd882c8aSJames Wright 
668bd882c8aSJames Wright //------------------------------------------------------------------------------
669bd882c8aSJames Wright // Assemble diagonal setup
670bd882c8aSJames Wright //------------------------------------------------------------------------------
671*dd64fc84SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalSetup_Sycl(CeedOperator op, const bool is_point_block) {
672bd882c8aSJames Wright   Ceed                ceed;
673*dd64fc84SJeremy L Thompson   Ceed_Sycl          *sycl_data;
674*dd64fc84SJeremy L Thompson   CeedInt             num_input_fields, num_output_fields, num_e_mode_in = 0, num_comp = 0, dim = 1, num_e_mode_out = 0;
675*dd64fc84SJeremy L Thompson   CeedEvalMode       *e_mode_in = NULL, *e_mode_out = NULL;
676*dd64fc84SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
677*dd64fc84SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
678*dd64fc84SJeremy L Thompson   CeedQFunctionField *qf_fields;
679bd882c8aSJames Wright   CeedQFunction       qf;
680*dd64fc84SJeremy L Thompson   CeedOperatorField  *op_fields;
681*dd64fc84SJeremy L Thompson   CeedOperator_Sycl  *impl;
682*dd64fc84SJeremy L Thompson 
683*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
684bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
685*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
686bd882c8aSJames Wright 
687bd882c8aSJames Wright   // Determine active input basis
688*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
689*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
690*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
691bd882c8aSJames Wright     CeedVector vec;
692*dd64fc84SJeremy L Thompson 
693*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
694bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
695*dd64fc84SJeremy L Thompson       CeedEvalMode        e_mode;
696bd882c8aSJames Wright       CeedElemRestriction rstr;
697*dd64fc84SJeremy L Thompson 
698*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
699*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
700*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis_in, &dim));
701*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
702*dd64fc84SJeremy L Thompson       if (rstr_in && rstr_in != rstr) {
703bd882c8aSJames Wright         // LCOV_EXCL_START
704bd882c8aSJames Wright         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator diagonal assembly");
705bd882c8aSJames Wright         // LCOV_EXCL_STOP
706bd882c8aSJames Wright       }
707*dd64fc84SJeremy L Thompson       rstr_in = rstr;
708*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode));
709*dd64fc84SJeremy L Thompson       switch (e_mode) {
710bd882c8aSJames Wright         case CEED_EVAL_NONE:
711bd882c8aSJames Wright         case CEED_EVAL_INTERP:
712*dd64fc84SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_in + 1, &e_mode_in));
713*dd64fc84SJeremy L Thompson           e_mode_in[num_e_mode_in] = e_mode;
714*dd64fc84SJeremy L Thompson           num_e_mode_in += 1;
715bd882c8aSJames Wright           break;
716bd882c8aSJames Wright         case CEED_EVAL_GRAD:
717*dd64fc84SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_in + dim, &e_mode_in));
718*dd64fc84SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) e_mode_in[num_e_mode_in + d] = e_mode;
719*dd64fc84SJeremy L Thompson           num_e_mode_in += dim;
720bd882c8aSJames Wright           break;
721bd882c8aSJames Wright         case CEED_EVAL_WEIGHT:
722bd882c8aSJames Wright         case CEED_EVAL_DIV:
723bd882c8aSJames Wright         case CEED_EVAL_CURL:
724bd882c8aSJames Wright           break;  // Caught by QF Assembly
725bd882c8aSJames Wright       }
726bd882c8aSJames Wright     }
727bd882c8aSJames Wright   }
728bd882c8aSJames Wright 
729bd882c8aSJames Wright   // Determine active output basis
730*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
731*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
732*dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
733bd882c8aSJames Wright     CeedVector vec;
734*dd64fc84SJeremy L Thompson 
735*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
736bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
737*dd64fc84SJeremy L Thompson       CeedEvalMode        e_mode;
738bd882c8aSJames Wright       CeedElemRestriction rstr;
739*dd64fc84SJeremy L Thompson 
740*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
741*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
742*dd64fc84SJeremy L Thompson       if (rstr_out && rstr_out != rstr) {
743bd882c8aSJames Wright         // LCOV_EXCL_START
744bd882c8aSJames Wright         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator diagonal assembly");
745bd882c8aSJames Wright         // LCOV_EXCL_STOP
746bd882c8aSJames Wright       }
747*dd64fc84SJeremy L Thompson       rstr_out = rstr;
748*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode));
749*dd64fc84SJeremy L Thompson       switch (e_mode) {
750bd882c8aSJames Wright         case CEED_EVAL_NONE:
751bd882c8aSJames Wright         case CEED_EVAL_INTERP:
752*dd64fc84SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_out + 1, &e_mode_out));
753*dd64fc84SJeremy L Thompson           e_mode_out[num_e_mode_out] = e_mode;
754*dd64fc84SJeremy L Thompson           num_e_mode_out += 1;
755bd882c8aSJames Wright           break;
756bd882c8aSJames Wright         case CEED_EVAL_GRAD:
757*dd64fc84SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_out + dim, &e_mode_out));
758*dd64fc84SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) e_mode_out[num_e_mode_out + d] = e_mode;
759*dd64fc84SJeremy L Thompson           num_e_mode_out += dim;
760bd882c8aSJames Wright           break;
761bd882c8aSJames Wright         case CEED_EVAL_WEIGHT:
762bd882c8aSJames Wright         case CEED_EVAL_DIV:
763bd882c8aSJames Wright         case CEED_EVAL_CURL:
764bd882c8aSJames Wright           break;  // Caught by QF Assembly
765bd882c8aSJames Wright       }
766bd882c8aSJames Wright     }
767bd882c8aSJames Wright   }
768bd882c8aSJames Wright 
769bd882c8aSJames Wright   // Operator data struct
770bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetData(op, &impl));
771bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
772bd882c8aSJames Wright   CeedCallBackend(CeedCalloc(1, &impl->diag));
773bd882c8aSJames Wright   CeedOperatorDiag_Sycl *diag = impl->diag;
774*dd64fc84SJeremy L Thompson 
775*dd64fc84SJeremy L Thompson   diag->basis_in       = basis_in;
776*dd64fc84SJeremy L Thompson   diag->basis_out      = basis_out;
777*dd64fc84SJeremy L Thompson   diag->h_e_mode_in    = e_mode_in;
778*dd64fc84SJeremy L Thompson   diag->h_e_mode_out   = e_mode_out;
779*dd64fc84SJeremy L Thompson   diag->num_e_mode_in  = num_e_mode_in;
780*dd64fc84SJeremy L Thompson   diag->num_e_mode_out = num_e_mode_out;
781bd882c8aSJames Wright 
782bd882c8aSJames Wright   // Kernel parameters
783*dd64fc84SJeremy L Thompson   CeedInt num_nodes, num_qpts;
784*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
785*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
786*dd64fc84SJeremy L Thompson   diag->num_nodes = num_nodes;
787*dd64fc84SJeremy L Thompson   diag->num_qpts  = num_qpts;
788*dd64fc84SJeremy L Thompson   diag->num_comp  = num_comp;
789bd882c8aSJames Wright 
790bd882c8aSJames Wright   // Basis matrices
791*dd64fc84SJeremy L Thompson   const CeedInt     i_len = num_qpts * num_nodes;
792*dd64fc84SJeremy L Thompson   const CeedInt     g_len = num_qpts * num_nodes * dim;
793*dd64fc84SJeremy L Thompson   const CeedScalar *interp_in, *interp_out, *grad_in, *grad_out;
794bd882c8aSJames Wright 
795bd882c8aSJames Wright   // CEED_EVAL_NONE
796bd882c8aSJames Wright   CeedScalar *identity      = NULL;
797*dd64fc84SJeremy L Thompson   bool        has_eval_none = false;
798*dd64fc84SJeremy 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);
799*dd64fc84SJeremy 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);
800bd882c8aSJames Wright 
801bd882c8aSJames Wright   // Order queue
802bd882c8aSJames Wright   sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
803bd882c8aSJames Wright 
804bd882c8aSJames Wright   std::vector<sycl::event> copy_events;
805*dd64fc84SJeremy L Thompson   if (has_eval_none) {
806*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedCalloc(num_qpts * num_nodes, &identity));
807*dd64fc84SJeremy L Thompson     for (CeedSize i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
808*dd64fc84SJeremy L Thompson     CeedCallSycl(ceed, diag->d_identity = sycl::malloc_device<CeedScalar>(i_len, sycl_data->sycl_device, sycl_data->sycl_context));
809*dd64fc84SJeremy L Thompson     sycl::event identity_copy = sycl_data->sycl_queue.copy<CeedScalar>(identity, diag->d_identity, i_len, {e});
810bd882c8aSJames Wright     copy_events.push_back(identity_copy);
811bd882c8aSJames Wright   }
812bd882c8aSJames Wright 
813bd882c8aSJames Wright   // CEED_EVAL_INTERP
814*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in));
815*dd64fc84SJeremy L Thompson   CeedCallSycl(ceed, diag->d_interp_in = sycl::malloc_device<CeedScalar>(i_len, sycl_data->sycl_device, sycl_data->sycl_context));
816*dd64fc84SJeremy L Thompson   sycl::event interp_in_copy = sycl_data->sycl_queue.copy<CeedScalar>(interp_in, diag->d_interp_in, i_len, {e});
817*dd64fc84SJeremy L Thompson   copy_events.push_back(interp_in_copy);
818bd882c8aSJames Wright 
819*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out));
820*dd64fc84SJeremy L Thompson   CeedCallSycl(ceed, diag->d_interp_out = sycl::malloc_device<CeedScalar>(i_len, sycl_data->sycl_device, sycl_data->sycl_context));
821*dd64fc84SJeremy L Thompson   sycl::event interp_out_copy = sycl_data->sycl_queue.copy<CeedScalar>(interp_out, diag->d_interp_out, i_len, {e});
822*dd64fc84SJeremy L Thompson   copy_events.push_back(interp_out_copy);
823bd882c8aSJames Wright 
824bd882c8aSJames Wright   // CEED_EVAL_GRAD
825*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in));
826*dd64fc84SJeremy L Thompson   CeedCallSycl(ceed, diag->d_grad_in = sycl::malloc_device<CeedScalar>(g_len, sycl_data->sycl_device, sycl_data->sycl_context));
827*dd64fc84SJeremy L Thompson   sycl::event grad_in_copy = sycl_data->sycl_queue.copy<CeedScalar>(grad_in, diag->d_grad_in, g_len, {e});
828*dd64fc84SJeremy L Thompson   copy_events.push_back(grad_in_copy);
829bd882c8aSJames Wright 
830*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out));
831*dd64fc84SJeremy L Thompson   CeedCallSycl(ceed, diag->d_grad_out = sycl::malloc_device<CeedScalar>(g_len, sycl_data->sycl_device, sycl_data->sycl_context));
832*dd64fc84SJeremy L Thompson   sycl::event grad_out_copy = sycl_data->sycl_queue.copy<CeedScalar>(grad_out, diag->d_grad_out, g_len, {e});
833*dd64fc84SJeremy L Thompson   copy_events.push_back(grad_out_copy);
834bd882c8aSJames Wright 
835*dd64fc84SJeremy L Thompson   // Arrays of  e_modes
836*dd64fc84SJeremy 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));
837*dd64fc84SJeremy L Thompson   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});
838*dd64fc84SJeremy L Thompson   copy_events.push_back(e_mode_in_copy);
839bd882c8aSJames Wright 
840*dd64fc84SJeremy 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));
841*dd64fc84SJeremy L Thompson   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});
842*dd64fc84SJeremy L Thompson   copy_events.push_back(e_mode_out_copy);
843bd882c8aSJames Wright 
844bd882c8aSJames Wright   // Restriction
845*dd64fc84SJeremy L Thompson   diag->diag_rstr = rstr_out;
846bd882c8aSJames Wright 
847bd882c8aSJames Wright   // Wait for all copies to complete and handle exceptions
848bd882c8aSJames Wright   CeedCallSycl(ceed, sycl::event::wait_and_throw(copy_events));
849bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
850bd882c8aSJames Wright }
851bd882c8aSJames Wright 
852bd882c8aSJames Wright //------------------------------------------------------------------------------
853bd882c8aSJames Wright //  Kernel for diagonal assembly
854bd882c8aSJames Wright //------------------------------------------------------------------------------
855*dd64fc84SJeremy L Thompson static int CeedOperatorLinearDiagonal_Sycl(sycl::queue &sycl_queue, const bool is_point_block, const CeedInt num_elem,
856*dd64fc84SJeremy L Thompson                                            const CeedOperatorDiag_Sycl *diag, const CeedScalar *assembled_qf_array, CeedScalar *elem_diag_array) {
857*dd64fc84SJeremy L Thompson   const CeedSize      num_nodes      = diag->num_nodes;
858*dd64fc84SJeremy L Thompson   const CeedSize      num_qpts       = diag->num_qpts;
859*dd64fc84SJeremy L Thompson   const CeedSize      num_comp       = diag->num_comp;
860*dd64fc84SJeremy L Thompson   const CeedSize      num_e_mode_in  = diag->num_e_mode_in;
861*dd64fc84SJeremy L Thompson   const CeedSize      num_e_mode_out = diag->num_e_mode_out;
862bd882c8aSJames Wright   const CeedScalar   *identity       = diag->d_identity;
863*dd64fc84SJeremy L Thompson   const CeedScalar   *interp_in      = diag->d_interp_in;
864*dd64fc84SJeremy L Thompson   const CeedScalar   *grad_in        = diag->d_grad_in;
865*dd64fc84SJeremy L Thompson   const CeedScalar   *interp_out     = diag->d_interp_out;
866*dd64fc84SJeremy L Thompson   const CeedScalar   *grad_out       = diag->d_grad_out;
867*dd64fc84SJeremy L Thompson   const CeedEvalMode *e_mode_in      = diag->d_e_mode_in;
868*dd64fc84SJeremy L Thompson   const CeedEvalMode *e_mode_out     = diag->d_e_mode_out;
869bd882c8aSJames Wright 
870*dd64fc84SJeremy L Thompson   sycl::range<1> kernel_range(num_elem * num_nodes);
871bd882c8aSJames Wright 
872bd882c8aSJames Wright   // Order queue
873bd882c8aSJames Wright   sycl::event e = sycl_queue.ext_oneapi_submit_barrier();
874bd882c8aSJames Wright   sycl_queue.parallel_for<CeedOperatorSyclLinearDiagonal>(kernel_range, {e}, [=](sycl::id<1> idx) {
875*dd64fc84SJeremy L Thompson     const CeedInt tid = idx % num_nodes;
876*dd64fc84SJeremy L Thompson     const CeedInt e   = idx / num_nodes;
877bd882c8aSJames Wright 
878bd882c8aSJames Wright     // Compute the diagonal of B^T D B
879bd882c8aSJames Wright     // Each element
880*dd64fc84SJeremy L Thompson     CeedInt d_out = -1;
881bd882c8aSJames Wright     // Each basis eval mode pair
882*dd64fc84SJeremy L Thompson     for (CeedSize e_out = 0; e_out < num_e_mode_out; e_out++) {
883bd882c8aSJames Wright       const CeedScalar *bt = NULL;
884*dd64fc84SJeremy L Thompson 
885*dd64fc84SJeremy L Thompson       if (e_mode_out[e_out] == CEED_EVAL_GRAD) ++d_out;
886*dd64fc84SJeremy L Thompson       CeedOperatorGetBasisPointer_Sycl(&bt, e_mode_out[e_out], identity, interp_out, &grad_out[d_out * num_qpts * num_nodes]);
887*dd64fc84SJeremy L Thompson       CeedInt d_in = -1;
888*dd64fc84SJeremy L Thompson 
889*dd64fc84SJeremy L Thompson       for (CeedSize e_in = 0; e_in < num_e_mode_in; e_in++) {
890bd882c8aSJames Wright         const CeedScalar *b = NULL;
891*dd64fc84SJeremy L Thompson 
892*dd64fc84SJeremy L Thompson         if (e_mode_in[e_in] == CEED_EVAL_GRAD) ++d_in;
893*dd64fc84SJeremy L Thompson         CeedOperatorGetBasisPointer_Sycl(&b, e_mode_in[e_in], identity, interp_in, &grad_in[d_in * num_qpts * num_nodes]);
894bd882c8aSJames Wright         // Each component
895*dd64fc84SJeremy L Thompson         for (CeedSize comp_out = 0; comp_out < num_comp; comp_out++) {
896bd882c8aSJames Wright           // Each qpoint/node pair
897*dd64fc84SJeremy L Thompson           if (is_point_block) {
898bd882c8aSJames Wright             // Point Block Diagonal
899*dd64fc84SJeremy L Thompson             for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
900*dd64fc84SJeremy L Thompson               CeedScalar e_value = 0.0;
901*dd64fc84SJeremy L Thompson 
902*dd64fc84SJeremy L Thompson               for (CeedSize q = 0; q < num_qpts; q++) {
903*dd64fc84SJeremy L Thompson                 const CeedScalar qf_value =
904*dd64fc84SJeremy 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 +
905*dd64fc84SJeremy L Thompson                                        q];
906*dd64fc84SJeremy L Thompson 
907*dd64fc84SJeremy L Thompson                 e_value += bt[q * num_nodes + tid] * qf_value * b[q * num_nodes + tid];
908bd882c8aSJames Wright               }
909*dd64fc84SJeremy L Thompson               elem_diag_array[((comp_out * num_comp + comp_in) * num_elem + e) * num_nodes + tid] += e_value;
910bd882c8aSJames Wright             }
911bd882c8aSJames Wright           } else {
912bd882c8aSJames Wright             // Diagonal Only
913*dd64fc84SJeremy L Thompson             CeedScalar e_value = 0.0;
914*dd64fc84SJeremy L Thompson 
915*dd64fc84SJeremy L Thompson             for (CeedSize q = 0; q < num_qpts; q++) {
916*dd64fc84SJeremy L Thompson               const CeedScalar qf_value =
917*dd64fc84SJeremy 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];
918*dd64fc84SJeremy L Thompson               e_value += bt[q * num_nodes + tid] * qf_value * b[q * num_nodes + tid];
919bd882c8aSJames Wright             }
920*dd64fc84SJeremy L Thompson             elem_diag_array[(comp_out * num_elem + e) * num_nodes + tid] += e_value;
921bd882c8aSJames Wright           }
922bd882c8aSJames Wright         }
923bd882c8aSJames Wright       }
924bd882c8aSJames Wright     }
925bd882c8aSJames Wright   });
926bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
927bd882c8aSJames Wright }
928bd882c8aSJames Wright 
929bd882c8aSJames Wright //------------------------------------------------------------------------------
930bd882c8aSJames Wright // Assemble diagonal common code
931bd882c8aSJames Wright //------------------------------------------------------------------------------
932*dd64fc84SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Sycl(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
933bd882c8aSJames Wright   Ceed                ceed;
934bd882c8aSJames Wright   Ceed_Sycl          *sycl_data;
935*dd64fc84SJeremy L Thompson   CeedInt             num_elem;
936*dd64fc84SJeremy L Thompson   CeedScalar         *elem_diag_array;
937*dd64fc84SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
938*dd64fc84SJeremy L Thompson   CeedVector          assembled_qf = NULL;
939*dd64fc84SJeremy L Thompson   CeedElemRestriction rstr         = NULL;
940*dd64fc84SJeremy L Thompson   CeedOperator_Sycl  *impl;
941*dd64fc84SJeremy L Thompson 
942*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
943*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
944bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
945bd882c8aSJames Wright 
946bd882c8aSJames Wright   // Assemble QFunction
947*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr, request));
948bd882c8aSJames Wright   CeedCallBackend(CeedElemRestrictionDestroy(&rstr));
949bd882c8aSJames Wright 
950bd882c8aSJames Wright   // Setup
951bd882c8aSJames Wright   if (!impl->diag) {
952*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Sycl(op, is_point_block));
953bd882c8aSJames Wright   }
954bd882c8aSJames Wright   CeedOperatorDiag_Sycl *diag = impl->diag;
955*dd64fc84SJeremy L Thompson 
956bd882c8aSJames Wright   assert(diag != NULL);
957bd882c8aSJames Wright 
958bd882c8aSJames Wright   // Restriction
959*dd64fc84SJeremy L Thompson   if (is_point_block && !diag->point_block_diag_rstr) {
960*dd64fc84SJeremy L Thompson     CeedElemRestriction point_block_diag_rstr;
961*dd64fc84SJeremy L Thompson     CeedCallBackend(CreatePBRestriction(diag->diag_rstr, &point_block_diag_rstr));
962*dd64fc84SJeremy L Thompson     diag->point_block_diag_rstr = point_block_diag_rstr;
963bd882c8aSJames Wright   }
964*dd64fc84SJeremy L Thompson   CeedElemRestriction diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
965bd882c8aSJames Wright 
966bd882c8aSJames Wright   // Create diagonal vector
967*dd64fc84SJeremy L Thompson   CeedVector elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
968*dd64fc84SJeremy L Thompson 
969*dd64fc84SJeremy L Thompson   if (!elem_diag) {
970*dd64fc84SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(diag_rstr, NULL, &elem_diag));
971*dd64fc84SJeremy L Thompson     if (is_point_block) diag->point_block_elem_diag = elem_diag;
972*dd64fc84SJeremy L Thompson     else diag->elem_diag = elem_diag;
973bd882c8aSJames Wright   }
974*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
975bd882c8aSJames Wright 
976bd882c8aSJames Wright   // Assemble element operator diagonals
977*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
978*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
979*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
980bd882c8aSJames Wright 
981bd882c8aSJames Wright   // Compute the diagonal of B^T D B
982bd882c8aSJames Wright   // Umesh: This needs to be reviewed later
983*dd64fc84SJeremy L Thompson   // if (is_point_block) {
984*dd64fc84SJeremy L Thompson   //  CeedCallBackend(CeedOperatorLinearPointBlockDiagonal_Sycl(sycl_data->sycl_queue, num_elem, diag, assembled_qf_array, elem_diag_array));
985bd882c8aSJames Wright   //} else {
986*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorLinearDiagonal_Sycl(sycl_data->sycl_queue, is_point_block, num_elem, diag, assembled_qf_array, elem_diag_array));
987bd882c8aSJames Wright   // }
988bd882c8aSJames Wright 
989bd882c8aSJames Wright   // Wait for queue to complete and handle exceptions
990bd882c8aSJames Wright   sycl_data->sycl_queue.wait_and_throw();
991bd882c8aSJames Wright 
992bd882c8aSJames Wright   // Restore arrays
993*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
994*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
995bd882c8aSJames Wright 
996bd882c8aSJames Wright   // Assemble local operator diagonal
997*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
998bd882c8aSJames Wright 
999bd882c8aSJames Wright   // Cleanup
1000*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1001bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1002bd882c8aSJames Wright }
1003bd882c8aSJames Wright 
1004bd882c8aSJames Wright //------------------------------------------------------------------------------
1005bd882c8aSJames Wright // Assemble Linear Diagonal
1006bd882c8aSJames Wright //------------------------------------------------------------------------------
1007bd882c8aSJames Wright static int CeedOperatorLinearAssembleAddDiagonal_Sycl(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1008bd882c8aSJames Wright   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Sycl(op, assembled, request, false));
1009bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1010bd882c8aSJames Wright }
1011bd882c8aSJames Wright 
1012bd882c8aSJames Wright //------------------------------------------------------------------------------
1013bd882c8aSJames Wright // Assemble Linear Point Block Diagonal
1014bd882c8aSJames Wright //------------------------------------------------------------------------------
1015bd882c8aSJames Wright static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Sycl(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1016bd882c8aSJames Wright   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Sycl(op, assembled, request, true));
1017bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1018bd882c8aSJames Wright }
1019bd882c8aSJames Wright 
1020bd882c8aSJames Wright //------------------------------------------------------------------------------
1021bd882c8aSJames Wright // Single operator assembly setup
1022bd882c8aSJames Wright //------------------------------------------------------------------------------
1023bd882c8aSJames Wright static int CeedSingleOperatorAssembleSetup_Sycl(CeedOperator op) {
1024bd882c8aSJames Wright   Ceed    ceed;
1025*dd64fc84SJeremy 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,
1026*dd64fc84SJeremy L Thompson                                                     num_B_out_mats_to_load = 0, size_B_out = 0, num_qpts = 0, elem_size = 0, num_elem, num_comp,
1027*dd64fc84SJeremy L Thompson                                                     mat_start = 0;
1028*dd64fc84SJeremy L Thompson   CeedEvalMode       *eval_mode_in = NULL, *eval_mode_out = NULL;
1029*dd64fc84SJeremy L Thompson   const CeedScalar   *interp_in, *grad_in;
1030*dd64fc84SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1031*dd64fc84SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1032*dd64fc84SJeremy L Thompson   CeedQFunctionField *qf_fields;
1033*dd64fc84SJeremy L Thompson   CeedQFunction       qf;
1034*dd64fc84SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1035bd882c8aSJames Wright   CeedOperator_Sycl  *impl;
1036*dd64fc84SJeremy L Thompson 
1037*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1038bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetData(op, &impl));
1039bd882c8aSJames Wright 
1040bd882c8aSJames Wright   // Get input and output fields
1041bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1042bd882c8aSJames Wright 
1043bd882c8aSJames Wright   // Determine active input basis eval mode
1044bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1045bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1046bd882c8aSJames Wright   // Note that the kernel will treat each dimension of a gradient action separately;
1047*dd64fc84SJeremy L Thompson   // i.e., when an active input has a CEED_EVAL_GRAD mode, num_ e_mode_in will increment by dim.
1048*dd64fc84SJeremy 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,
1049*dd64fc84SJeremy L Thompson   // so num_B_in_mats_to_load will be incremented by 1.
1050bd882c8aSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
1051*dd64fc84SJeremy L Thompson     CeedEvalMode eval_mode;
1052bd882c8aSJames Wright     CeedVector   vec;
1053*dd64fc84SJeremy L Thompson 
1054bd882c8aSJames Wright     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1055bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
1056bd882c8aSJames Wright       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis_in));
1057bd882c8aSJames Wright       CeedCallBackend(CeedBasisGetDimension(basis_in, &dim));
1058*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1059bd882c8aSJames Wright       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1060*dd64fc84SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size));
1061bd882c8aSJames Wright       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1062bd882c8aSJames Wright       if (eval_mode != CEED_EVAL_NONE) {
1063bd882c8aSJames Wright         CeedCallBackend(CeedRealloc(num_B_in_mats_to_load + 1, &eval_mode_in));
1064bd882c8aSJames Wright         eval_mode_in[num_B_in_mats_to_load] = eval_mode;
1065bd882c8aSJames Wright         num_B_in_mats_to_load += 1;
1066bd882c8aSJames Wright         if (eval_mode == CEED_EVAL_GRAD) {
1067*dd64fc84SJeremy L Thompson           num_e_mode_in += dim;
1068*dd64fc84SJeremy L Thompson           size_B_in += dim * elem_size * num_qpts;
1069bd882c8aSJames Wright         } else {
1070*dd64fc84SJeremy L Thompson           num_e_mode_in += 1;
1071*dd64fc84SJeremy L Thompson           size_B_in += elem_size * num_qpts;
1072bd882c8aSJames Wright         }
1073bd882c8aSJames Wright       }
1074bd882c8aSJames Wright     }
1075bd882c8aSJames Wright   }
1076bd882c8aSJames Wright 
1077bd882c8aSJames Wright   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
1078bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1079bd882c8aSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
1080*dd64fc84SJeremy L Thompson     CeedEvalMode eval_mode;
1081bd882c8aSJames Wright     CeedVector   vec;
1082*dd64fc84SJeremy L Thompson 
1083bd882c8aSJames Wright     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1084bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
1085bd882c8aSJames Wright       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis_out));
1086bd882c8aSJames Wright       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1087bd882c8aSJames Wright       if (rstr_out && rstr_out != rstr_in) {
1088bd882c8aSJames Wright         // LCOV_EXCL_START
1089bd882c8aSJames Wright         return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator assembly");
1090bd882c8aSJames Wright         // LCOV_EXCL_STOP
1091bd882c8aSJames Wright       }
1092bd882c8aSJames Wright       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1093bd882c8aSJames Wright       if (eval_mode != CEED_EVAL_NONE) {
1094bd882c8aSJames Wright         CeedCallBackend(CeedRealloc(num_B_out_mats_to_load + 1, &eval_mode_out));
1095bd882c8aSJames Wright         eval_mode_out[num_B_out_mats_to_load] = eval_mode;
1096bd882c8aSJames Wright         num_B_out_mats_to_load += 1;
1097bd882c8aSJames Wright         if (eval_mode == CEED_EVAL_GRAD) {
1098*dd64fc84SJeremy L Thompson           num_e_mode_out += dim;
1099*dd64fc84SJeremy L Thompson           size_B_out += dim * elem_size * num_qpts;
1100bd882c8aSJames Wright         } else {
1101*dd64fc84SJeremy L Thompson           num_e_mode_out += 1;
1102*dd64fc84SJeremy L Thompson           size_B_out += elem_size * num_qpts;
1103bd882c8aSJames Wright         }
1104bd882c8aSJames Wright       }
1105bd882c8aSJames Wright     }
1106bd882c8aSJames Wright   }
1107bd882c8aSJames Wright 
1108*dd64fc84SJeremy L Thompson   if (num_e_mode_in == 0 || num_e_mode_out == 0) {
1109bd882c8aSJames Wright     // LCOV_EXCL_START
1110bd882c8aSJames Wright     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1111bd882c8aSJames Wright     // LCOV_EXCL_STOP
1112bd882c8aSJames Wright   }
1113bd882c8aSJames Wright 
1114*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem));
1115*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp));
1116bd882c8aSJames Wright 
1117bd882c8aSJames Wright   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1118bd882c8aSJames Wright   CeedOperatorAssemble_Sycl *asmb = impl->asmb;
1119*dd64fc84SJeremy L Thompson   asmb->num_elem                  = num_elem;
1120bd882c8aSJames Wright 
1121bd882c8aSJames Wright   Ceed_Sycl *sycl_data;
1122bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
1123bd882c8aSJames Wright 
1124bd882c8aSJames Wright   // Kernel setup
1125*dd64fc84SJeremy L Thompson   int elem_per_block   = 1;
1126*dd64fc84SJeremy L Thompson   asmb->elem_per_block = elem_per_block;
1127*dd64fc84SJeremy L Thompson   CeedInt block_size   = elem_size * elem_size * elem_per_block;
1128*dd64fc84SJeremy L Thompson 
1129bd882c8aSJames Wright   /* CeedInt maxThreadsPerBlock = sycl_data->sycl_device.get_info<sycl::info::device::max_work_group_size>();
1130bd882c8aSJames Wright   bool    fallback           = block_size > maxThreadsPerBlock;
1131bd882c8aSJames Wright   asmb->fallback             = fallback;
1132bd882c8aSJames Wright   if (fallback) {
1133bd882c8aSJames Wright     // Use fallback kernel with 1D threadblock
1134*dd64fc84SJeremy L Thompson     block_size         = elem_size * elem_per_block;
1135*dd64fc84SJeremy L Thompson     asmb->block_size_x = elem_size;
1136bd882c8aSJames Wright     asmb->block_size_y = 1;
1137bd882c8aSJames Wright   } else {  // Use kernel with 2D threadblock
1138*dd64fc84SJeremy L Thompson     asmb->block_size_x = elem_size;
1139*dd64fc84SJeremy L Thompson     asmb->block_size_y = elem_size;
1140bd882c8aSJames Wright   }*/
1141*dd64fc84SJeremy L Thompson   asmb->block_size_x   = elem_size;
1142*dd64fc84SJeremy L Thompson   asmb->block_size_y   = elem_size;
1143*dd64fc84SJeremy L Thompson   asmb->num_e_mode_in  = num_e_mode_in;
1144*dd64fc84SJeremy L Thompson   asmb->num_e_mode_out = num_e_mode_out;
1145*dd64fc84SJeremy L Thompson   asmb->num_qpts       = num_qpts;
1146*dd64fc84SJeremy L Thompson   asmb->num_nodes      = elem_size;
1147bd882c8aSJames Wright   asmb->block_size     = block_size;
1148*dd64fc84SJeremy L Thompson   asmb->num_comp       = num_comp;
1149bd882c8aSJames Wright 
1150bd882c8aSJames Wright   // Build 'full' B matrices (not 1D arrays used for tensor-product matrices
1151bd882c8aSJames Wright   CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in));
1152bd882c8aSJames Wright   CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in));
1153bd882c8aSJames Wright 
1154bd882c8aSJames Wright   // Load into B_in, in order that they will be used in eval_mode
1155bd882c8aSJames Wright   CeedCallSycl(ceed, asmb->d_B_in = sycl::malloc_device<CeedScalar>(size_B_in, sycl_data->sycl_device, sycl_data->sycl_context));
1156bd882c8aSJames Wright   for (int i = 0; i < num_B_in_mats_to_load; i++) {
1157bd882c8aSJames Wright     CeedEvalMode eval_mode = eval_mode_in[i];
1158*dd64fc84SJeremy L Thompson 
1159bd882c8aSJames Wright     if (eval_mode == CEED_EVAL_INTERP) {
1160bd882c8aSJames Wright       // Order queue
1161bd882c8aSJames Wright       sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
1162*dd64fc84SJeremy L Thompson       sycl_data->sycl_queue.copy<CeedScalar>(interp_in, &asmb->d_B_in[mat_start], elem_size * num_qpts, {e});
1163*dd64fc84SJeremy L Thompson       mat_start += elem_size * num_qpts;
1164bd882c8aSJames Wright     } else if (eval_mode == CEED_EVAL_GRAD) {
1165bd882c8aSJames Wright       // Order queue
1166bd882c8aSJames Wright       sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
1167*dd64fc84SJeremy L Thompson       sycl_data->sycl_queue.copy<CeedScalar>(grad_in, &asmb->d_B_in[mat_start], dim * elem_size * num_qpts, {e});
1168*dd64fc84SJeremy L Thompson       mat_start += dim * elem_size * num_qpts;
1169bd882c8aSJames Wright     }
1170bd882c8aSJames Wright   }
1171bd882c8aSJames Wright 
1172bd882c8aSJames Wright   const CeedScalar *interp_out, *grad_out;
1173bd882c8aSJames Wright   // Note that this function currently assumes 1 basis, so this should always be true
1174bd882c8aSJames Wright   // for now
1175bd882c8aSJames Wright   if (basis_out == basis_in) {
1176bd882c8aSJames Wright     interp_out = interp_in;
1177bd882c8aSJames Wright     grad_out   = grad_in;
1178bd882c8aSJames Wright   } else {
1179bd882c8aSJames Wright     CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out));
1180bd882c8aSJames Wright     CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out));
1181bd882c8aSJames Wright   }
1182bd882c8aSJames Wright 
1183bd882c8aSJames Wright   // Load into B_out, in order that they will be used in eval_mode
1184bd882c8aSJames Wright   mat_start = 0;
1185bd882c8aSJames Wright   CeedCallSycl(ceed, asmb->d_B_out = sycl::malloc_device<CeedScalar>(size_B_out, sycl_data->sycl_device, sycl_data->sycl_context));
1186bd882c8aSJames Wright   for (int i = 0; i < num_B_out_mats_to_load; i++) {
1187bd882c8aSJames Wright     CeedEvalMode eval_mode = eval_mode_out[i];
1188*dd64fc84SJeremy L Thompson 
1189bd882c8aSJames Wright     if (eval_mode == CEED_EVAL_INTERP) {
1190bd882c8aSJames Wright       // Order queue
1191bd882c8aSJames Wright       sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
1192*dd64fc84SJeremy L Thompson       sycl_data->sycl_queue.copy<CeedScalar>(interp_out, &asmb->d_B_out[mat_start], elem_size * num_qpts, {e});
1193*dd64fc84SJeremy L Thompson       mat_start += elem_size * num_qpts;
1194bd882c8aSJames Wright     } else if (eval_mode == CEED_EVAL_GRAD) {
1195bd882c8aSJames Wright       // Order queue
1196bd882c8aSJames Wright       sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
1197*dd64fc84SJeremy L Thompson       sycl_data->sycl_queue.copy<CeedScalar>(grad_out, &asmb->d_B_out[mat_start], dim * elem_size * num_qpts, {e});
1198*dd64fc84SJeremy L Thompson       mat_start += dim * elem_size * num_qpts;
1199bd882c8aSJames Wright     }
1200bd882c8aSJames Wright   }
1201bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1202bd882c8aSJames Wright }
1203bd882c8aSJames Wright 
1204bd882c8aSJames Wright //------------------------------------------------------------------------------
1205bd882c8aSJames Wright // Matrix assembly kernel for low-order elements (3D thread block)
1206bd882c8aSJames Wright //------------------------------------------------------------------------------
1207bd882c8aSJames Wright static int CeedOperatorLinearAssemble_Sycl(sycl::queue &sycl_queue, const CeedOperator_Sycl *impl, const CeedScalar *qf_array,
1208bd882c8aSJames Wright                                            CeedScalar *values_array) {
1209bd882c8aSJames Wright   // This kernels assumes B_in and B_out have the same number of quadrature points and basis points.
1210bd882c8aSJames Wright   // TODO: expand to more general cases
1211bd882c8aSJames Wright   CeedOperatorAssemble_Sycl *asmb           = impl->asmb;
1212*dd64fc84SJeremy L Thompson   const CeedInt              num_elem       = asmb->num_elem;
1213*dd64fc84SJeremy L Thompson   const CeedSize             num_nodes      = asmb->num_nodes;
1214*dd64fc84SJeremy L Thompson   const CeedSize             num_comp       = asmb->num_comp;
1215*dd64fc84SJeremy L Thompson   const CeedSize             num_qpts       = asmb->num_qpts;
1216*dd64fc84SJeremy L Thompson   const CeedSize             num_e_mode_in  = asmb->num_e_mode_in;
1217*dd64fc84SJeremy L Thompson   const CeedSize             num_e_mode_out = asmb->num_e_mode_out;
1218bd882c8aSJames Wright 
1219bd882c8aSJames Wright   // Strides for final output ordering, determined by the reference (inference) implementation of the symbolic assembly, slowest --> fastest: element,
1220bd882c8aSJames Wright   // comp_in, comp_out, node_row, node_col
1221*dd64fc84SJeremy L Thompson   const CeedSize comp_out_stride = num_nodes * num_nodes;
1222*dd64fc84SJeremy L Thompson   const CeedSize comp_in_stride  = comp_out_stride * num_comp;
1223*dd64fc84SJeremy L Thompson   const CeedSize e_stride        = comp_in_stride * num_comp;
1224*dd64fc84SJeremy L Thompson   // Strides for QF array, slowest --> fastest:  e_mode_in, comp_in,  e_mode_out, comp_out, elem, qpt
1225*dd64fc84SJeremy L Thompson   const CeedSize q_e_stride          = num_qpts;
1226*dd64fc84SJeremy L Thompson   const CeedSize q_comp_out_stride   = num_elem * q_e_stride;
1227*dd64fc84SJeremy L Thompson   const CeedSize q_e_mode_out_stride = q_comp_out_stride * num_comp;
1228*dd64fc84SJeremy L Thompson   const CeedSize q_comp_in_stride    = q_e_mode_out_stride * num_e_mode_out;
1229*dd64fc84SJeremy L Thompson   const CeedSize q_e_mode_in_stride  = q_comp_in_stride * num_comp;
1230bd882c8aSJames Wright 
1231bd882c8aSJames Wright   CeedScalar *B_in, *B_out;
1232bd882c8aSJames Wright   B_in                       = asmb->d_B_in;
1233bd882c8aSJames Wright   B_out                      = asmb->d_B_out;
1234bd882c8aSJames Wright   const CeedInt block_size_x = asmb->block_size_x;
1235bd882c8aSJames Wright   const CeedInt block_size_y = asmb->block_size_y;
1236bd882c8aSJames Wright 
1237*dd64fc84SJeremy L Thompson   sycl::range<3> kernel_range(num_elem, block_size_y, block_size_x);
1238bd882c8aSJames Wright 
1239bd882c8aSJames Wright   // Order queue
1240bd882c8aSJames Wright   sycl::event e = sycl_queue.ext_oneapi_submit_barrier();
1241bd882c8aSJames Wright   sycl_queue.parallel_for<CeedOperatorSyclLinearAssemble>(kernel_range, {e}, [=](sycl::id<3> idx) {
1242bd882c8aSJames Wright     const int e = idx.get(0);  // Element index
1243bd882c8aSJames Wright     const int l = idx.get(1);  // The output column index of each B^TDB operation
1244bd882c8aSJames Wright     const int i = idx.get(2);  // The output row index of each B^TDB operation
1245bd882c8aSJames Wright                                // such that we have (Bout^T)_ij D_jk Bin_kl = C_il
1246*dd64fc84SJeremy L Thompson     for (CeedSize comp_in = 0; comp_in < num_comp; comp_in++) {
1247*dd64fc84SJeremy L Thompson       for (CeedSize comp_out = 0; comp_out < num_comp; comp_out++) {
1248bd882c8aSJames Wright         CeedScalar result        = 0.0;
1249*dd64fc84SJeremy L Thompson         CeedSize   qf_index_comp = q_comp_in_stride * comp_in + q_comp_out_stride * comp_out + q_e_stride * e;
1250*dd64fc84SJeremy L Thompson 
1251*dd64fc84SJeremy L Thompson         for (CeedSize e_mode_in = 0; e_mode_in < num_e_mode_in; e_mode_in++) {
1252*dd64fc84SJeremy L Thompson           CeedSize b_in_index = e_mode_in * num_qpts * num_nodes;
1253*dd64fc84SJeremy L Thompson 
1254*dd64fc84SJeremy L Thompson           for (CeedSize e_mode_out = 0; e_mode_out < num_e_mode_out; e_mode_out++) {
1255*dd64fc84SJeremy L Thompson             CeedSize b_out_index = e_mode_out * num_qpts * num_nodes;
1256*dd64fc84SJeremy L Thompson             CeedSize qf_index    = qf_index_comp + q_e_mode_out_stride * e_mode_out + q_e_mode_in_stride * e_mode_in;
1257*dd64fc84SJeremy L Thompson 
1258bd882c8aSJames Wright             // Perform the B^T D B operation for this 'chunk' of D (the qf_array)
1259*dd64fc84SJeremy L Thompson             for (CeedSize j = 0; j < num_qpts; j++) {
1260*dd64fc84SJeremy 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];
1261bd882c8aSJames Wright             }
1262*dd64fc84SJeremy L Thompson           }  // end of  e_mode_out
1263*dd64fc84SJeremy L Thompson         }    // end of  e_mode_in
1264*dd64fc84SJeremy L Thompson         CeedSize val_index = comp_in_stride * comp_in + comp_out_stride * comp_out + e_stride * e + num_nodes * i + l;
1265*dd64fc84SJeremy L Thompson 
1266bd882c8aSJames Wright         values_array[val_index] = result;
1267bd882c8aSJames Wright       }  // end of out component
1268bd882c8aSJames Wright     }    // end of in component
1269bd882c8aSJames Wright   });
1270bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1271bd882c8aSJames Wright }
1272bd882c8aSJames Wright 
1273bd882c8aSJames Wright //------------------------------------------------------------------------------
1274bd882c8aSJames Wright // Fallback kernel for larger orders (1D thread block)
1275bd882c8aSJames Wright //------------------------------------------------------------------------------
1276bd882c8aSJames Wright /*
1277bd882c8aSJames Wright static int CeedOperatorLinearAssembleFallback_Sycl(sycl::queue &sycl_queue, const CeedOperator_Sycl *impl, const CeedScalar *qf_array,
1278bd882c8aSJames Wright                                                    CeedScalar *values_array) {
1279bd882c8aSJames Wright   // This kernel assumes B_in and B_out have the same number of quadrature points and basis points.
1280bd882c8aSJames Wright   // TODO: expand to more general cases
1281bd882c8aSJames Wright   CeedOperatorAssemble_Sycl *asmb        = impl->asmb;
1282*dd64fc84SJeremy L Thompson   const CeedInt              num_elem       = asmb->num_elem;
1283*dd64fc84SJeremy L Thompson   const CeedInt              num_nodes      = asmb->num_nodes;
1284*dd64fc84SJeremy L Thompson   const CeedInt              num_comp       = asmb->num_comp;
1285*dd64fc84SJeremy L Thompson   const CeedInt              num_qpts       = asmb->num_qpts;
1286*dd64fc84SJeremy L Thompson   const CeedInt              num_e_mode_in  = asmb->num_e_mode_in;
1287*dd64fc84SJeremy L Thompson   const CeedInt              num_e_mode_out = asmb->num_e_mode_out;
1288bd882c8aSJames Wright 
1289bd882c8aSJames Wright   // Strides for final output ordering, determined by the reference (interface) implementation of the symbolic assembly, slowest --> fastest: elememt,
1290bd882c8aSJames Wright   // comp_in, comp_out, node_row, node_col
1291*dd64fc84SJeremy L Thompson   const CeedInt comp_out_stride = num_nodes * num_nodes;
1292*dd64fc84SJeremy L Thompson   const CeedInt comp_in_stride  = comp_out_stride * num_comp;
1293*dd64fc84SJeremy L Thompson   const CeedInt e_stride        = comp_in_stride * num_comp;
1294*dd64fc84SJeremy L Thompson   // Strides for QF array, slowest --> fastest:  e_mode_in, comp_in,  e_mode_out, comp_out, elem, qpt
1295*dd64fc84SJeremy L Thompson   const CeedInt q_e_stride         = num_qpts;
1296*dd64fc84SJeremy L Thompson   const CeedInt q_comp_out_stride  = num_elem * q_e_stride;
1297*dd64fc84SJeremy L Thompson   const CeedInt q_e_mode_out_stride = q_comp_out_stride * num_comp;
1298*dd64fc84SJeremy L Thompson   const CeedInt q_comp_in_stride   = q_e_mode_out_stride * num_e_mode_out;
1299*dd64fc84SJeremy L Thompson   const CeedInt q_e_mode_in_stride  = q_comp_in_stride * num_comp;
1300bd882c8aSJames Wright 
1301bd882c8aSJames Wright   CeedScalar *B_in, *B_out;
1302bd882c8aSJames Wright   B_in                        = asmb->d_B_in;
1303bd882c8aSJames Wright   B_out                       = asmb->d_B_out;
1304*dd64fc84SJeremy L Thompson   const CeedInt elem_per_block = asmb->elem_per_block;
1305bd882c8aSJames Wright   const CeedInt block_size_x  = asmb->block_size_x;
1306bd882c8aSJames Wright   const CeedInt block_size_y  = asmb->block_size_y;  // This will be 1 for the fallback kernel
1307bd882c8aSJames Wright 
1308*dd64fc84SJeremy L Thompson   const CeedInt     grid = num_elem / elem_per_block + ((num_elem / elem_per_block * elem_per_block < num_elem) ? 1 : 0);
1309*dd64fc84SJeremy L Thompson   sycl::range<3>    local_range(block_size_x, block_size_y, elem_per_block);
1310*dd64fc84SJeremy L Thompson   sycl::range<3>    global_range(grid * block_size_x, block_size_y, elem_per_block);
1311bd882c8aSJames Wright   sycl::nd_range<3> kernel_range(global_range, local_range);
1312bd882c8aSJames Wright 
1313bd882c8aSJames Wright   sycl_queue.parallel_for<CeedOperatorSyclLinearAssembleFallback>(kernel_range, [=](sycl::nd_item<3> work_item) {
1314bd882c8aSJames Wright     const CeedInt blockIdx  = work_item.get_group(0);
1315bd882c8aSJames Wright     const CeedInt gridDimx  = work_item.get_group_range(0);
1316bd882c8aSJames Wright     const CeedInt threadIdx = work_item.get_local_id(0);
1317bd882c8aSJames Wright     const CeedInt threadIdz = work_item.get_local_id(2);
1318bd882c8aSJames Wright     const CeedInt blockDimz = work_item.get_local_range(2);
1319bd882c8aSJames Wright 
1320bd882c8aSJames Wright     const int l = threadIdx;  // The output column index of each B^TDB operation
1321bd882c8aSJames Wright                               // such that we have (Bout^T)_ij D_jk Bin_kl = C_il
1322*dd64fc84SJeremy L Thompson     for (CeedInt e = blockIdx * blockDimz + threadIdz; e < num_elem; e += gridDimx * blockDimz) {
1323*dd64fc84SJeremy L Thompson       for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
1324*dd64fc84SJeremy L Thompson         for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
1325*dd64fc84SJeremy L Thompson           for (CeedInt i = 0; i < num_nodes; i++) {
1326bd882c8aSJames Wright             CeedScalar result        = 0.0;
1327*dd64fc84SJeremy L Thompson             CeedInt    qf_index_comp = q_comp_in_stride * comp_in + q_comp_out_stride * comp_out + q_e_stride * e;
1328*dd64fc84SJeremy L Thompson             for (CeedInt  e_mode_in = 0;  e_mode_in < num_e_mode_in;  e_mode_in++) {
1329*dd64fc84SJeremy L Thompson               CeedInt b_in_index =  e_mode_in * num_qpts * num_nodes;
1330*dd64fc84SJeremy L Thompson               for (CeedInt  e_mode_out = 0;  e_mode_out < num_e_mode_out;  e_mode_out++) {
1331*dd64fc84SJeremy L Thompson                 CeedInt b_out_index =  e_mode_out * num_qpts * num_nodes;
1332*dd64fc84SJeremy L Thompson                 CeedInt qf_index    = qf_index_comp + q_e_mode_out_stride *  e_mode_out + q_e_mode_in_stride *  e_mode_in;
1333bd882c8aSJames Wright                 // Perform the B^T D B operation for this 'chunk' of D (the qf_array)
1334*dd64fc84SJeremy L Thompson                 for (CeedInt j = 0; j < num_qpts; j++) {
1335*dd64fc84SJeremy 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];
1336bd882c8aSJames Wright                 }
1337*dd64fc84SJeremy L Thompson               }  // end of  e_mode_out
1338*dd64fc84SJeremy L Thompson             }    // end of  e_mode_in
1339*dd64fc84SJeremy L Thompson             CeedInt val_index       = comp_in_stride * comp_in + comp_out_stride * comp_out + e_stride * e + num_nodes * i + l;
1340bd882c8aSJames Wright             values_array[val_index] = result;
1341bd882c8aSJames Wright           }  // end of loop over element node index, i
1342bd882c8aSJames Wright         }    // end of out component
1343bd882c8aSJames Wright       }      // end of in component
1344bd882c8aSJames Wright     }        // end of element loop
1345bd882c8aSJames Wright   });
1346bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1347bd882c8aSJames Wright }*/
1348bd882c8aSJames Wright 
1349bd882c8aSJames Wright //------------------------------------------------------------------------------
1350bd882c8aSJames Wright // Assemble matrix data for COO matrix of assembled operator.
1351bd882c8aSJames Wright // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1352bd882c8aSJames Wright //
1353bd882c8aSJames Wright // Note that this (and other assembly routines) currently assume only one active
1354bd882c8aSJames Wright // input restriction/basis per operator (could have multiple basis eval modes).
1355bd882c8aSJames Wright // TODO: allow multiple active input restrictions/basis objects
1356bd882c8aSJames Wright //------------------------------------------------------------------------------
1357bd882c8aSJames Wright static int CeedSingleOperatorAssemble_Sycl(CeedOperator op, CeedInt offset, CeedVector values) {
1358bd882c8aSJames Wright   Ceed                ceed;
1359bd882c8aSJames Wright   Ceed_Sycl          *sycl_data;
1360*dd64fc84SJeremy L Thompson   CeedScalar         *values_array;
1361*dd64fc84SJeremy L Thompson   const CeedScalar   *qf_array;
1362*dd64fc84SJeremy L Thompson   CeedVector          assembled_qf = NULL;
1363*dd64fc84SJeremy L Thompson   CeedElemRestriction rstr_q       = NULL;
1364*dd64fc84SJeremy L Thompson   CeedOperator_Sycl  *impl;
1365*dd64fc84SJeremy L Thompson 
1366*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1367*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1368bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
1369bd882c8aSJames Wright 
1370bd882c8aSJames Wright   // Setup
1371bd882c8aSJames Wright   if (!impl->asmb) {
1372bd882c8aSJames Wright     CeedCallBackend(CeedSingleOperatorAssembleSetup_Sycl(op));
1373bd882c8aSJames Wright     assert(impl->asmb != NULL);
1374bd882c8aSJames Wright   }
1375bd882c8aSJames Wright 
1376bd882c8aSJames Wright   // Assemble QFunction
1377bd882c8aSJames Wright   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
1378bd882c8aSJames Wright   CeedCallBackend(CeedElemRestrictionDestroy(&rstr_q));
1379bd882c8aSJames Wright   CeedCallBackend(CeedVectorGetArrayWrite(values, CEED_MEM_DEVICE, &values_array));
1380bd882c8aSJames Wright   values_array += offset;
1381bd882c8aSJames Wright   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &qf_array));
1382bd882c8aSJames Wright 
1383bd882c8aSJames Wright   // Compute B^T D B
1384bd882c8aSJames Wright   CeedCallBackend(CeedOperatorLinearAssemble_Sycl(sycl_data->sycl_queue, impl, qf_array, values_array));
1385bd882c8aSJames Wright 
1386bd882c8aSJames Wright   // Wait for kernels to be completed
1387bd882c8aSJames Wright   // Kris: Review if this is necessary -- enqueing an async barrier may be sufficient
1388bd882c8aSJames Wright   sycl_data->sycl_queue.wait_and_throw();
1389bd882c8aSJames Wright 
1390bd882c8aSJames Wright   // Restore arrays
1391bd882c8aSJames Wright   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1392bd882c8aSJames Wright   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &qf_array));
1393bd882c8aSJames Wright 
1394bd882c8aSJames Wright   // Cleanup
1395bd882c8aSJames Wright   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1396bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1397bd882c8aSJames Wright }
1398bd882c8aSJames Wright 
1399bd882c8aSJames Wright //------------------------------------------------------------------------------
1400bd882c8aSJames Wright // Create operator
1401bd882c8aSJames Wright //------------------------------------------------------------------------------
1402bd882c8aSJames Wright int CeedOperatorCreate_Sycl(CeedOperator op) {
1403bd882c8aSJames Wright   Ceed               ceed;
1404bd882c8aSJames Wright   CeedOperator_Sycl *impl;
1405bd882c8aSJames Wright 
1406*dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1407*dd64fc84SJeremy L Thompson 
1408bd882c8aSJames Wright   CeedCallBackend(CeedCalloc(1, &impl));
1409bd882c8aSJames Wright   CeedCallBackend(CeedOperatorSetData(op, impl));
1410bd882c8aSJames Wright 
1411bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Sycl));
1412bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Sycl));
1413bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Sycl));
1414bd882c8aSJames Wright   CeedCallBackend(
1415bd882c8aSJames Wright       CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Sycl));
1416bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Sycl));
1417bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Sycl));
1418bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Sycl));
1419bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1420bd882c8aSJames Wright }
1421bd882c8aSJames Wright 
1422bd882c8aSJames Wright //------------------------------------------------------------------------------
1423