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