xref: /libCEED/rust/libceed-sys/c-src/backends/sycl-ref/ceed-sycl-ref-operator.sycl.cpp (revision 4e3038a58c29aaa28aa8eaaef94bbc7eab6ca469)
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;
430*4e3038a5SJeremy L Thompson 
431bd882c8aSJames Wright         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
432bd882c8aSJames Wright         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
433bd882c8aSJames Wright         break;  // Should not occur
434bd882c8aSJames Wright       case CEED_EVAL_DIV:
435*4e3038a5SJeremy L Thompson       case CEED_EVAL_CURL: {
436*4e3038a5SJeremy L Thompson         Ceed ceed;
437*4e3038a5SJeremy L Thompson 
438*4e3038a5SJeremy L Thompson         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
439*4e3038a5SJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
440*4e3038a5SJeremy L Thompson         break;  // Should not occur
441*4e3038a5SJeremy L Thompson       }
442bd882c8aSJames Wright         // LCOV_EXCL_STOP
443bd882c8aSJames Wright     }
444bd882c8aSJames Wright   }
445bd882c8aSJames Wright 
446bd882c8aSJames Wright   // Output restriction
447dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
448dd64fc84SJeremy L Thompson     CeedVector          vec;
449dd64fc84SJeremy L Thompson     CeedElemRestriction rstr;
450dd64fc84SJeremy L Thompson 
451bd882c8aSJames Wright     // Restore evec
452dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &e_mode));
453dd64fc84SJeremy L Thompson     if (e_mode == CEED_EVAL_NONE) {
454dd64fc84SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_e_in], &e_data[i + num_input_fields]));
455bd882c8aSJames Wright     }
456bd882c8aSJames Wright     // Get output vector
457dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
458bd882c8aSJames Wright     // Restrict
459dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
460bd882c8aSJames Wright     // Active
461dd64fc84SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
462bd882c8aSJames Wright 
463dd64fc84SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_e_in], vec, request));
464bd882c8aSJames Wright   }
465bd882c8aSJames Wright 
466bd882c8aSJames Wright   // Restore input arrays
467dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
468bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
469bd882c8aSJames Wright }
470bd882c8aSJames Wright 
471bd882c8aSJames Wright //------------------------------------------------------------------------------
472bd882c8aSJames Wright // Core code for assembling linear QFunction
473bd882c8aSJames Wright //------------------------------------------------------------------------------
474bd882c8aSJames Wright static inline int CeedOperatorLinearAssembleQFunctionCore_Sycl(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
475bd882c8aSJames Wright                                                                CeedRequest *request) {
476dd64fc84SJeremy L Thompson   Ceed                ceed, ceed_parent;
477e984cf9aSJeremy L Thompson   CeedSize            q_size;
478dd64fc84SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
479dd64fc84SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
480dd64fc84SJeremy L Thompson   CeedVector         *active_in;
481dd64fc84SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
482dd64fc84SJeremy L Thompson   CeedQFunction       qf;
483dd64fc84SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
484dd64fc84SJeremy L Thompson   CeedOperator_Sycl  *impl;
485dd64fc84SJeremy L Thompson 
486bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
487dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
488e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
489e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
490e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
491dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
492dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
493dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
494dd64fc84SJeremy L Thompson   active_in     = impl->qf_active_in;
495dd64fc84SJeremy L Thompson   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
496bd882c8aSJames Wright 
497bd882c8aSJames Wright   // Setup
498bd882c8aSJames Wright   CeedCallBackend(CeedOperatorSetup_Sycl(op));
499bd882c8aSJames Wright 
500bd882c8aSJames Wright   // Input Evecs and Restriction
501dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
502bd882c8aSJames Wright 
503bd882c8aSJames Wright   // Count number of active input fields
504dd64fc84SJeremy L Thompson   if (!num_active_in) {
505dd64fc84SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
506dd64fc84SJeremy L Thompson       CeedScalar *q_vec_array;
507dd64fc84SJeremy L Thompson       CeedVector  vec;
508dd64fc84SJeremy L Thompson 
509bd882c8aSJames Wright       // Get input vector
510dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
511bd882c8aSJames Wright       // Check if active input
512bd882c8aSJames Wright       if (vec == CEED_VECTOR_ACTIVE) {
513dd64fc84SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
514dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
515dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
516dd64fc84SJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_in));
517bd882c8aSJames Wright         for (CeedInt field = 0; field < size; field++) {
518dd64fc84SJeremy L Thompson           q_size = (CeedSize)Q * num_elem;
519dd64fc84SJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_in[num_active_in + field]));
520dd64fc84SJeremy L Thompson           CeedCallBackend(
521dd64fc84SJeremy L Thompson               CeedVectorSetArray(active_in[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
522bd882c8aSJames Wright         }
523dd64fc84SJeremy L Thompson         num_active_in += size;
524dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
525bd882c8aSJames Wright       }
526bd882c8aSJames Wright     }
527dd64fc84SJeremy L Thompson     impl->num_active_in = num_active_in;
528dd64fc84SJeremy L Thompson     impl->qf_active_in  = active_in;
529bd882c8aSJames Wright   }
530bd882c8aSJames Wright 
531bd882c8aSJames Wright   // Count number of active output fields
532dd64fc84SJeremy L Thompson   if (!num_active_out) {
533dd64fc84SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
534dd64fc84SJeremy L Thompson       CeedVector vec;
535dd64fc84SJeremy L Thompson 
536bd882c8aSJames Wright       // Get output vector
537dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
538bd882c8aSJames Wright       // Check if active output
539bd882c8aSJames Wright       if (vec == CEED_VECTOR_ACTIVE) {
540dd64fc84SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
541dd64fc84SJeremy L Thompson         num_active_out += size;
542bd882c8aSJames Wright       }
543bd882c8aSJames Wright     }
544dd64fc84SJeremy L Thompson     impl->num_active_out = num_active_out;
545bd882c8aSJames Wright   }
546bd882c8aSJames Wright 
547bd882c8aSJames Wright   // Check sizes
548*4e3038a5SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
549bd882c8aSJames Wright 
550bd882c8aSJames Wright   // Build objects if needed
551bd882c8aSJames Wright   if (build_objects) {
552dd64fc84SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
553dd64fc84SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
554dd64fc84SJeremy L Thompson 
555bd882c8aSJames Wright     // Create output restriction
556dd64fc84SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
557dd64fc84SJeremy L Thompson                                                      num_active_in * num_active_out * num_elem * Q, strides, rstr));
558bd882c8aSJames Wright     // Create assembled vector
559dd64fc84SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
560bd882c8aSJames Wright   }
561bd882c8aSJames Wright   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
562dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
563bd882c8aSJames Wright 
564bd882c8aSJames Wright   // Input basis apply
565dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Sycl(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
566bd882c8aSJames Wright 
567bd882c8aSJames Wright   // Assemble QFunction
568dd64fc84SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
569bd882c8aSJames Wright     // Set Inputs
570dd64fc84SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(active_in[in], 1.0));
571dd64fc84SJeremy L Thompson     if (num_active_in > 1) {
572dd64fc84SJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_in[(in + num_active_in - 1) % num_active_in], 0.0));
573bd882c8aSJames Wright     }
574bd882c8aSJames Wright     // Set Outputs
575dd64fc84SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
576dd64fc84SJeremy L Thompson       CeedVector vec;
577dd64fc84SJeremy L Thompson 
578bd882c8aSJames Wright       // Get output vector
579dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
580bd882c8aSJames Wright       // Check if active output
581bd882c8aSJames Wright       if (vec == CEED_VECTOR_ACTIVE) {
582dd64fc84SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
583dd64fc84SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
584dd64fc84SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
585bd882c8aSJames Wright       }
586bd882c8aSJames Wright     }
587bd882c8aSJames Wright     // Apply QFunction
588dd64fc84SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
589bd882c8aSJames Wright   }
590bd882c8aSJames Wright 
591bd882c8aSJames Wright   // Un-set output Qvecs to prevent accidental overwrite of Assembled
592dd64fc84SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
593dd64fc84SJeremy L Thompson     CeedVector vec;
594dd64fc84SJeremy L Thompson 
595bd882c8aSJames Wright     // Get output vector
596dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
597bd882c8aSJames Wright     // Check if active output
598bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
599dd64fc84SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
600bd882c8aSJames Wright     }
601bd882c8aSJames Wright   }
602bd882c8aSJames Wright 
603bd882c8aSJames Wright   // Restore input arrays
604dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
605bd882c8aSJames Wright 
606bd882c8aSJames Wright   // Restore output
607dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
608bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
609bd882c8aSJames Wright }
610bd882c8aSJames Wright 
611bd882c8aSJames Wright //------------------------------------------------------------------------------
612bd882c8aSJames Wright // Assemble Linear QFunction
613bd882c8aSJames Wright //------------------------------------------------------------------------------
614bd882c8aSJames Wright static int CeedOperatorLinearAssembleQFunction_Sycl(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
615bd882c8aSJames Wright   return CeedOperatorLinearAssembleQFunctionCore_Sycl(op, true, assembled, rstr, request);
616bd882c8aSJames Wright }
617bd882c8aSJames Wright 
618bd882c8aSJames Wright //------------------------------------------------------------------------------
619bd882c8aSJames Wright // Update Assembled Linear QFunction
620bd882c8aSJames Wright //------------------------------------------------------------------------------
621bd882c8aSJames Wright static int CeedOperatorLinearAssembleQFunctionUpdate_Sycl(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
622bd882c8aSJames Wright   return CeedOperatorLinearAssembleQFunctionCore_Sycl(op, false, &assembled, &rstr, request);
623bd882c8aSJames Wright }
624bd882c8aSJames Wright 
625bd882c8aSJames Wright //------------------------------------------------------------------------------
626bd882c8aSJames Wright // Assemble diagonal setup
627bd882c8aSJames Wright //------------------------------------------------------------------------------
628506b1a0cSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Sycl(CeedOperator op) {
629bd882c8aSJames Wright   Ceed                ceed;
630dd64fc84SJeremy L Thompson   Ceed_Sycl          *sycl_data;
631dd64fc84SJeremy L Thompson   CeedInt             num_input_fields, num_output_fields, num_e_mode_in = 0, num_comp = 0, dim = 1, num_e_mode_out = 0;
632dd64fc84SJeremy L Thompson   CeedEvalMode       *e_mode_in = NULL, *e_mode_out = NULL;
633dd64fc84SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
634dd64fc84SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
635dd64fc84SJeremy L Thompson   CeedQFunctionField *qf_fields;
636bd882c8aSJames Wright   CeedQFunction       qf;
637dd64fc84SJeremy L Thompson   CeedOperatorField  *op_fields;
638dd64fc84SJeremy L Thompson   CeedOperator_Sycl  *impl;
639dd64fc84SJeremy L Thompson 
640dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
641bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
642dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
643bd882c8aSJames Wright 
644bd882c8aSJames Wright   // Determine active input basis
645dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
646dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
647dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
648bd882c8aSJames Wright     CeedVector vec;
649dd64fc84SJeremy L Thompson 
650dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
651bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
652dd64fc84SJeremy L Thompson       CeedEvalMode        e_mode;
653bd882c8aSJames Wright       CeedElemRestriction rstr;
654dd64fc84SJeremy L Thompson 
655dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
656dd64fc84SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
657dd64fc84SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis_in, &dim));
658dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
659*4e3038a5SJeremy L Thompson       CeedCheck(!rstr_in || rstr_in == rstr, ceed, CEED_ERROR_BACKEND,
660*4e3038a5SJeremy L Thompson                 "Backend does not implement multi-field non-composite operator diagonal assembly");
661dd64fc84SJeremy L Thompson       rstr_in = rstr;
662dd64fc84SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode));
663dd64fc84SJeremy L Thompson       switch (e_mode) {
664bd882c8aSJames Wright         case CEED_EVAL_NONE:
665bd882c8aSJames Wright         case CEED_EVAL_INTERP:
666dd64fc84SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_in + 1, &e_mode_in));
667dd64fc84SJeremy L Thompson           e_mode_in[num_e_mode_in] = e_mode;
668dd64fc84SJeremy L Thompson           num_e_mode_in += 1;
669bd882c8aSJames Wright           break;
670bd882c8aSJames Wright         case CEED_EVAL_GRAD:
671dd64fc84SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_in + dim, &e_mode_in));
672dd64fc84SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) e_mode_in[num_e_mode_in + d] = e_mode;
673dd64fc84SJeremy L Thompson           num_e_mode_in += dim;
674bd882c8aSJames Wright           break;
675bd882c8aSJames Wright         case CEED_EVAL_WEIGHT:
676bd882c8aSJames Wright         case CEED_EVAL_DIV:
677bd882c8aSJames Wright         case CEED_EVAL_CURL:
678bd882c8aSJames Wright           break;  // Caught by QF Assembly
679bd882c8aSJames Wright       }
680bd882c8aSJames Wright     }
681bd882c8aSJames Wright   }
682bd882c8aSJames Wright 
683bd882c8aSJames Wright   // Determine active output basis
684dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
685dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
686dd64fc84SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
687bd882c8aSJames Wright     CeedVector vec;
688dd64fc84SJeremy L Thompson 
689dd64fc84SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
690bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
691dd64fc84SJeremy L Thompson       CeedEvalMode        e_mode;
692bd882c8aSJames Wright       CeedElemRestriction rstr;
693dd64fc84SJeremy L Thompson 
694dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
695dd64fc84SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
696*4e3038a5SJeremy L Thompson       CeedCheck(!rstr_out || rstr_out == rstr, ceed, CEED_ERROR_BACKEND,
697*4e3038a5SJeremy L Thompson                 "Backend does not implement multi-field non-composite operator diagonal assembly");
698dd64fc84SJeremy L Thompson       rstr_out = rstr;
699dd64fc84SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &e_mode));
700dd64fc84SJeremy L Thompson       switch (e_mode) {
701bd882c8aSJames Wright         case CEED_EVAL_NONE:
702bd882c8aSJames Wright         case CEED_EVAL_INTERP:
703dd64fc84SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_out + 1, &e_mode_out));
704dd64fc84SJeremy L Thompson           e_mode_out[num_e_mode_out] = e_mode;
705dd64fc84SJeremy L Thompson           num_e_mode_out += 1;
706bd882c8aSJames Wright           break;
707bd882c8aSJames Wright         case CEED_EVAL_GRAD:
708dd64fc84SJeremy L Thompson           CeedCallBackend(CeedRealloc(num_e_mode_out + dim, &e_mode_out));
709dd64fc84SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) e_mode_out[num_e_mode_out + d] = e_mode;
710dd64fc84SJeremy L Thompson           num_e_mode_out += dim;
711bd882c8aSJames Wright           break;
712bd882c8aSJames Wright         case CEED_EVAL_WEIGHT:
713bd882c8aSJames Wright         case CEED_EVAL_DIV:
714bd882c8aSJames Wright         case CEED_EVAL_CURL:
715bd882c8aSJames Wright           break;  // Caught by QF Assembly
716bd882c8aSJames Wright       }
717bd882c8aSJames Wright     }
718bd882c8aSJames Wright   }
719bd882c8aSJames Wright 
720bd882c8aSJames Wright   // Operator data struct
721bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetData(op, &impl));
722bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
723bd882c8aSJames Wright   CeedCallBackend(CeedCalloc(1, &impl->diag));
724bd882c8aSJames Wright   CeedOperatorDiag_Sycl *diag = impl->diag;
725dd64fc84SJeremy L Thompson 
726dd64fc84SJeremy L Thompson   diag->basis_in       = basis_in;
727dd64fc84SJeremy L Thompson   diag->basis_out      = basis_out;
728dd64fc84SJeremy L Thompson   diag->h_e_mode_in    = e_mode_in;
729dd64fc84SJeremy L Thompson   diag->h_e_mode_out   = e_mode_out;
730dd64fc84SJeremy L Thompson   diag->num_e_mode_in  = num_e_mode_in;
731dd64fc84SJeremy L Thompson   diag->num_e_mode_out = num_e_mode_out;
732bd882c8aSJames Wright 
733bd882c8aSJames Wright   // Kernel parameters
734dd64fc84SJeremy L Thompson   CeedInt num_nodes, num_qpts;
735dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
736dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
737dd64fc84SJeremy L Thompson   diag->num_nodes = num_nodes;
738dd64fc84SJeremy L Thompson   diag->num_qpts  = num_qpts;
739dd64fc84SJeremy L Thompson   diag->num_comp  = num_comp;
740bd882c8aSJames Wright 
741bd882c8aSJames Wright   // Basis matrices
742dd64fc84SJeremy L Thompson   const CeedInt     i_len = num_qpts * num_nodes;
743dd64fc84SJeremy L Thompson   const CeedInt     g_len = num_qpts * num_nodes * dim;
744dd64fc84SJeremy L Thompson   const CeedScalar *interp_in, *interp_out, *grad_in, *grad_out;
745bd882c8aSJames Wright 
746bd882c8aSJames Wright   // CEED_EVAL_NONE
747bd882c8aSJames Wright   CeedScalar *identity      = NULL;
748dd64fc84SJeremy L Thompson   bool        has_eval_none = false;
749dd64fc84SJeremy 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);
750dd64fc84SJeremy 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);
751bd882c8aSJames Wright 
752bd882c8aSJames Wright   // Order queue
753bd882c8aSJames Wright   sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
754bd882c8aSJames Wright 
755bd882c8aSJames Wright   std::vector<sycl::event> copy_events;
756dd64fc84SJeremy L Thompson   if (has_eval_none) {
757dd64fc84SJeremy L Thompson     CeedCallBackend(CeedCalloc(num_qpts * num_nodes, &identity));
758dd64fc84SJeremy L Thompson     for (CeedSize i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
759dd64fc84SJeremy L Thompson     CeedCallSycl(ceed, diag->d_identity = sycl::malloc_device<CeedScalar>(i_len, sycl_data->sycl_device, sycl_data->sycl_context));
760dd64fc84SJeremy L Thompson     sycl::event identity_copy = sycl_data->sycl_queue.copy<CeedScalar>(identity, diag->d_identity, i_len, {e});
761bd882c8aSJames Wright     copy_events.push_back(identity_copy);
762bd882c8aSJames Wright   }
763bd882c8aSJames Wright 
764bd882c8aSJames Wright   // CEED_EVAL_INTERP
765dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in));
766dd64fc84SJeremy L Thompson   CeedCallSycl(ceed, diag->d_interp_in = sycl::malloc_device<CeedScalar>(i_len, sycl_data->sycl_device, sycl_data->sycl_context));
767dd64fc84SJeremy L Thompson   sycl::event interp_in_copy = sycl_data->sycl_queue.copy<CeedScalar>(interp_in, diag->d_interp_in, i_len, {e});
768dd64fc84SJeremy L Thompson   copy_events.push_back(interp_in_copy);
769bd882c8aSJames Wright 
770dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out));
771dd64fc84SJeremy L Thompson   CeedCallSycl(ceed, diag->d_interp_out = sycl::malloc_device<CeedScalar>(i_len, sycl_data->sycl_device, sycl_data->sycl_context));
772dd64fc84SJeremy L Thompson   sycl::event interp_out_copy = sycl_data->sycl_queue.copy<CeedScalar>(interp_out, diag->d_interp_out, i_len, {e});
773dd64fc84SJeremy L Thompson   copy_events.push_back(interp_out_copy);
774bd882c8aSJames Wright 
775bd882c8aSJames Wright   // CEED_EVAL_GRAD
776dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in));
777dd64fc84SJeremy L Thompson   CeedCallSycl(ceed, diag->d_grad_in = sycl::malloc_device<CeedScalar>(g_len, sycl_data->sycl_device, sycl_data->sycl_context));
778dd64fc84SJeremy L Thompson   sycl::event grad_in_copy = sycl_data->sycl_queue.copy<CeedScalar>(grad_in, diag->d_grad_in, g_len, {e});
779dd64fc84SJeremy L Thompson   copy_events.push_back(grad_in_copy);
780bd882c8aSJames Wright 
781dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out));
782dd64fc84SJeremy L Thompson   CeedCallSycl(ceed, diag->d_grad_out = sycl::malloc_device<CeedScalar>(g_len, sycl_data->sycl_device, sycl_data->sycl_context));
783dd64fc84SJeremy L Thompson   sycl::event grad_out_copy = sycl_data->sycl_queue.copy<CeedScalar>(grad_out, diag->d_grad_out, g_len, {e});
784dd64fc84SJeremy L Thompson   copy_events.push_back(grad_out_copy);
785bd882c8aSJames Wright 
786dd64fc84SJeremy L Thompson   // Arrays of  e_modes
787dd64fc84SJeremy 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));
788dd64fc84SJeremy 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});
789dd64fc84SJeremy L Thompson   copy_events.push_back(e_mode_in_copy);
790bd882c8aSJames Wright 
791dd64fc84SJeremy 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));
792dd64fc84SJeremy 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});
793dd64fc84SJeremy L Thompson   copy_events.push_back(e_mode_out_copy);
794bd882c8aSJames Wright 
795bd882c8aSJames Wright   // Restriction
796dd64fc84SJeremy L Thompson   diag->diag_rstr = rstr_out;
797bd882c8aSJames Wright 
798bd882c8aSJames Wright   // Wait for all copies to complete and handle exceptions
799bd882c8aSJames Wright   CeedCallSycl(ceed, sycl::event::wait_and_throw(copy_events));
800bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
801bd882c8aSJames Wright }
802bd882c8aSJames Wright 
803bd882c8aSJames Wright //------------------------------------------------------------------------------
804bd882c8aSJames Wright //  Kernel for diagonal assembly
805bd882c8aSJames Wright //------------------------------------------------------------------------------
806dd64fc84SJeremy L Thompson static int CeedOperatorLinearDiagonal_Sycl(sycl::queue &sycl_queue, const bool is_point_block, const CeedInt num_elem,
807dd64fc84SJeremy L Thompson                                            const CeedOperatorDiag_Sycl *diag, const CeedScalar *assembled_qf_array, CeedScalar *elem_diag_array) {
808dd64fc84SJeremy L Thompson   const CeedSize      num_nodes      = diag->num_nodes;
809dd64fc84SJeremy L Thompson   const CeedSize      num_qpts       = diag->num_qpts;
810dd64fc84SJeremy L Thompson   const CeedSize      num_comp       = diag->num_comp;
811dd64fc84SJeremy L Thompson   const CeedSize      num_e_mode_in  = diag->num_e_mode_in;
812dd64fc84SJeremy L Thompson   const CeedSize      num_e_mode_out = diag->num_e_mode_out;
813bd882c8aSJames Wright   const CeedScalar   *identity       = diag->d_identity;
814dd64fc84SJeremy L Thompson   const CeedScalar   *interp_in      = diag->d_interp_in;
815dd64fc84SJeremy L Thompson   const CeedScalar   *grad_in        = diag->d_grad_in;
816dd64fc84SJeremy L Thompson   const CeedScalar   *interp_out     = diag->d_interp_out;
817dd64fc84SJeremy L Thompson   const CeedScalar   *grad_out       = diag->d_grad_out;
818dd64fc84SJeremy L Thompson   const CeedEvalMode *e_mode_in      = diag->d_e_mode_in;
819dd64fc84SJeremy L Thompson   const CeedEvalMode *e_mode_out     = diag->d_e_mode_out;
820bd882c8aSJames Wright 
821dd64fc84SJeremy L Thompson   sycl::range<1> kernel_range(num_elem * num_nodes);
822bd882c8aSJames Wright 
823bd882c8aSJames Wright   // Order queue
824bd882c8aSJames Wright   sycl::event e = sycl_queue.ext_oneapi_submit_barrier();
825bd882c8aSJames Wright   sycl_queue.parallel_for<CeedOperatorSyclLinearDiagonal>(kernel_range, {e}, [=](sycl::id<1> idx) {
826dd64fc84SJeremy L Thompson     const CeedInt tid = idx % num_nodes;
827dd64fc84SJeremy L Thompson     const CeedInt e   = idx / num_nodes;
828bd882c8aSJames Wright 
829bd882c8aSJames Wright     // Compute the diagonal of B^T D B
830bd882c8aSJames Wright     // Each element
831dd64fc84SJeremy L Thompson     CeedInt d_out = -1;
832bd882c8aSJames Wright     // Each basis eval mode pair
833dd64fc84SJeremy L Thompson     for (CeedSize e_out = 0; e_out < num_e_mode_out; e_out++) {
834bd882c8aSJames Wright       const CeedScalar *bt = NULL;
835dd64fc84SJeremy L Thompson 
836dd64fc84SJeremy L Thompson       if (e_mode_out[e_out] == CEED_EVAL_GRAD) ++d_out;
837dd64fc84SJeremy L Thompson       CeedOperatorGetBasisPointer_Sycl(&bt, e_mode_out[e_out], identity, interp_out, &grad_out[d_out * num_qpts * num_nodes]);
838dd64fc84SJeremy L Thompson       CeedInt d_in = -1;
839dd64fc84SJeremy L Thompson 
840dd64fc84SJeremy L Thompson       for (CeedSize e_in = 0; e_in < num_e_mode_in; e_in++) {
841bd882c8aSJames Wright         const CeedScalar *b = NULL;
842dd64fc84SJeremy L Thompson 
843dd64fc84SJeremy L Thompson         if (e_mode_in[e_in] == CEED_EVAL_GRAD) ++d_in;
844dd64fc84SJeremy L Thompson         CeedOperatorGetBasisPointer_Sycl(&b, e_mode_in[e_in], identity, interp_in, &grad_in[d_in * num_qpts * num_nodes]);
845bd882c8aSJames Wright         // Each component
846dd64fc84SJeremy L Thompson         for (CeedSize comp_out = 0; comp_out < num_comp; comp_out++) {
847bd882c8aSJames Wright           // Each qpoint/node pair
848dd64fc84SJeremy L Thompson           if (is_point_block) {
849bd882c8aSJames Wright             // Point Block Diagonal
850dd64fc84SJeremy L Thompson             for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
851dd64fc84SJeremy L Thompson               CeedScalar e_value = 0.0;
852dd64fc84SJeremy L Thompson 
853dd64fc84SJeremy L Thompson               for (CeedSize q = 0; q < num_qpts; q++) {
854dd64fc84SJeremy L Thompson                 const CeedScalar qf_value =
855dd64fc84SJeremy 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 +
856dd64fc84SJeremy L Thompson                                        q];
857dd64fc84SJeremy L Thompson 
858dd64fc84SJeremy L Thompson                 e_value += bt[q * num_nodes + tid] * qf_value * b[q * num_nodes + tid];
859bd882c8aSJames Wright               }
860dd64fc84SJeremy L Thompson               elem_diag_array[((comp_out * num_comp + comp_in) * num_elem + e) * num_nodes + tid] += e_value;
861bd882c8aSJames Wright             }
862bd882c8aSJames Wright           } else {
863bd882c8aSJames Wright             // Diagonal Only
864dd64fc84SJeremy L Thompson             CeedScalar e_value = 0.0;
865dd64fc84SJeremy L Thompson 
866dd64fc84SJeremy L Thompson             for (CeedSize q = 0; q < num_qpts; q++) {
867dd64fc84SJeremy L Thompson               const CeedScalar qf_value =
868dd64fc84SJeremy 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];
869dd64fc84SJeremy L Thompson               e_value += bt[q * num_nodes + tid] * qf_value * b[q * num_nodes + tid];
870bd882c8aSJames Wright             }
871dd64fc84SJeremy L Thompson             elem_diag_array[(comp_out * num_elem + e) * num_nodes + tid] += e_value;
872bd882c8aSJames Wright           }
873bd882c8aSJames Wright         }
874bd882c8aSJames Wright       }
875bd882c8aSJames Wright     }
876bd882c8aSJames Wright   });
877bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
878bd882c8aSJames Wright }
879bd882c8aSJames Wright 
880bd882c8aSJames Wright //------------------------------------------------------------------------------
881bd882c8aSJames Wright // Assemble diagonal common code
882bd882c8aSJames Wright //------------------------------------------------------------------------------
883dd64fc84SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Sycl(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
884bd882c8aSJames Wright   Ceed                ceed;
885bd882c8aSJames Wright   Ceed_Sycl          *sycl_data;
886dd64fc84SJeremy L Thompson   CeedInt             num_elem;
887dd64fc84SJeremy L Thompson   CeedScalar         *elem_diag_array;
888dd64fc84SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
889dd64fc84SJeremy L Thompson   CeedVector          assembled_qf = NULL;
890dd64fc84SJeremy L Thompson   CeedElemRestriction rstr         = NULL;
891dd64fc84SJeremy L Thompson   CeedOperator_Sycl  *impl;
892dd64fc84SJeremy L Thompson 
893dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
894dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
895bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
896bd882c8aSJames Wright 
897bd882c8aSJames Wright   // Assemble QFunction
898dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr, request));
899bd882c8aSJames Wright   CeedCallBackend(CeedElemRestrictionDestroy(&rstr));
900bd882c8aSJames Wright 
901bd882c8aSJames Wright   // Setup
902bd882c8aSJames Wright   if (!impl->diag) {
903506b1a0cSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Sycl(op));
904bd882c8aSJames Wright   }
905bd882c8aSJames Wright   CeedOperatorDiag_Sycl *diag = impl->diag;
906dd64fc84SJeremy L Thompson 
907bd882c8aSJames Wright   assert(diag != NULL);
908bd882c8aSJames Wright 
909bd882c8aSJames Wright   // Restriction
910dd64fc84SJeremy L Thompson   if (is_point_block && !diag->point_block_diag_rstr) {
911506b1a0cSSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(diag->diag_rstr, &diag->point_block_diag_rstr));
912bd882c8aSJames Wright   }
913dd64fc84SJeremy L Thompson   CeedElemRestriction diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
914bd882c8aSJames Wright 
915bd882c8aSJames Wright   // Create diagonal vector
916dd64fc84SJeremy L Thompson   CeedVector elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
917dd64fc84SJeremy L Thompson 
918dd64fc84SJeremy L Thompson   if (!elem_diag) {
919dd64fc84SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(diag_rstr, NULL, &elem_diag));
920dd64fc84SJeremy L Thompson     if (is_point_block) diag->point_block_elem_diag = elem_diag;
921dd64fc84SJeremy L Thompson     else diag->elem_diag = elem_diag;
922bd882c8aSJames Wright   }
923dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
924bd882c8aSJames Wright 
925bd882c8aSJames Wright   // Assemble element operator diagonals
926dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
927dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
928dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
929bd882c8aSJames Wright 
930bd882c8aSJames Wright   // Compute the diagonal of B^T D B
931dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorLinearDiagonal_Sycl(sycl_data->sycl_queue, is_point_block, num_elem, diag, assembled_qf_array, elem_diag_array));
932bd882c8aSJames Wright 
933bd882c8aSJames Wright   // Wait for queue to complete and handle exceptions
934bd882c8aSJames Wright   sycl_data->sycl_queue.wait_and_throw();
935bd882c8aSJames Wright 
936bd882c8aSJames Wright   // Restore arrays
937dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
938dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
939bd882c8aSJames Wright 
940bd882c8aSJames Wright   // Assemble local operator diagonal
941dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
942bd882c8aSJames Wright 
943bd882c8aSJames Wright   // Cleanup
944dd64fc84SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
945bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
946bd882c8aSJames Wright }
947bd882c8aSJames Wright 
948bd882c8aSJames Wright //------------------------------------------------------------------------------
949bd882c8aSJames Wright // Assemble Linear Diagonal
950bd882c8aSJames Wright //------------------------------------------------------------------------------
951bd882c8aSJames Wright static int CeedOperatorLinearAssembleAddDiagonal_Sycl(CeedOperator op, CeedVector assembled, CeedRequest *request) {
952bd882c8aSJames Wright   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Sycl(op, assembled, request, false));
953bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
954bd882c8aSJames Wright }
955bd882c8aSJames Wright 
956bd882c8aSJames Wright //------------------------------------------------------------------------------
957bd882c8aSJames Wright // Assemble Linear Point Block Diagonal
958bd882c8aSJames Wright //------------------------------------------------------------------------------
959bd882c8aSJames Wright static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Sycl(CeedOperator op, CeedVector assembled, CeedRequest *request) {
960bd882c8aSJames Wright   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Sycl(op, assembled, request, true));
961bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
962bd882c8aSJames Wright }
963bd882c8aSJames Wright 
964bd882c8aSJames Wright //------------------------------------------------------------------------------
965bd882c8aSJames Wright // Single operator assembly setup
966bd882c8aSJames Wright //------------------------------------------------------------------------------
967bd882c8aSJames Wright static int CeedSingleOperatorAssembleSetup_Sycl(CeedOperator op) {
968bd882c8aSJames Wright   Ceed    ceed;
969dd64fc84SJeremy 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,
970dd64fc84SJeremy L Thompson                                                num_B_out_mats_to_load = 0, size_B_out = 0, num_qpts = 0, elem_size = 0, num_elem, num_comp,
971dd64fc84SJeremy L Thompson                                                mat_start = 0;
972dd64fc84SJeremy L Thompson   CeedEvalMode       *eval_mode_in = NULL, *eval_mode_out = NULL;
973dd64fc84SJeremy L Thompson   const CeedScalar   *interp_in, *grad_in;
974dd64fc84SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
975dd64fc84SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
976dd64fc84SJeremy L Thompson   CeedQFunctionField *qf_fields;
977dd64fc84SJeremy L Thompson   CeedQFunction       qf;
978dd64fc84SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
979bd882c8aSJames Wright   CeedOperator_Sycl  *impl;
980dd64fc84SJeremy L Thompson 
981dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
982bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetData(op, &impl));
983bd882c8aSJames Wright 
984bd882c8aSJames Wright   // Get input and output fields
985bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
986bd882c8aSJames Wright 
987bd882c8aSJames Wright   // Determine active input basis eval mode
988bd882c8aSJames Wright   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
989bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
990bd882c8aSJames Wright   // Note that the kernel will treat each dimension of a gradient action separately;
991dd64fc84SJeremy L Thompson   // i.e., when an active input has a CEED_EVAL_GRAD mode, num_ e_mode_in will increment by dim.
992dd64fc84SJeremy 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,
993dd64fc84SJeremy L Thompson   // so num_B_in_mats_to_load will be incremented by 1.
994bd882c8aSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
995dd64fc84SJeremy L Thompson     CeedEvalMode eval_mode;
996bd882c8aSJames Wright     CeedVector   vec;
997dd64fc84SJeremy L Thompson 
998bd882c8aSJames Wright     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
999bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
1000bd882c8aSJames Wright       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis_in));
1001bd882c8aSJames Wright       CeedCallBackend(CeedBasisGetDimension(basis_in, &dim));
1002dd64fc84SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1003bd882c8aSJames Wright       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1004dd64fc84SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size));
1005bd882c8aSJames Wright       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1006bd882c8aSJames Wright       if (eval_mode != CEED_EVAL_NONE) {
1007bd882c8aSJames Wright         CeedCallBackend(CeedRealloc(num_B_in_mats_to_load + 1, &eval_mode_in));
1008bd882c8aSJames Wright         eval_mode_in[num_B_in_mats_to_load] = eval_mode;
1009bd882c8aSJames Wright         num_B_in_mats_to_load += 1;
1010bd882c8aSJames Wright         if (eval_mode == CEED_EVAL_GRAD) {
1011dd64fc84SJeremy L Thompson           num_e_mode_in += dim;
1012dd64fc84SJeremy L Thompson           size_B_in += dim * elem_size * num_qpts;
1013bd882c8aSJames Wright         } else {
1014dd64fc84SJeremy L Thompson           num_e_mode_in += 1;
1015dd64fc84SJeremy L Thompson           size_B_in += elem_size * num_qpts;
1016bd882c8aSJames Wright         }
1017bd882c8aSJames Wright       }
1018bd882c8aSJames Wright     }
1019bd882c8aSJames Wright   }
1020bd882c8aSJames Wright 
1021bd882c8aSJames Wright   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
1022bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1023bd882c8aSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
1024dd64fc84SJeremy L Thompson     CeedEvalMode eval_mode;
1025bd882c8aSJames Wright     CeedVector   vec;
1026dd64fc84SJeremy L Thompson 
1027bd882c8aSJames Wright     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1028bd882c8aSJames Wright     if (vec == CEED_VECTOR_ACTIVE) {
1029bd882c8aSJames Wright       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis_out));
1030bd882c8aSJames Wright       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1031*4e3038a5SJeremy L Thompson       CeedCheck(!rstr_out || rstr_out == rstr_in, ceed, CEED_ERROR_BACKEND, "Backend does not implement multi-field non-composite operator assembly");
1032bd882c8aSJames Wright       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1033bd882c8aSJames Wright       if (eval_mode != CEED_EVAL_NONE) {
1034bd882c8aSJames Wright         CeedCallBackend(CeedRealloc(num_B_out_mats_to_load + 1, &eval_mode_out));
1035bd882c8aSJames Wright         eval_mode_out[num_B_out_mats_to_load] = eval_mode;
1036bd882c8aSJames Wright         num_B_out_mats_to_load += 1;
1037bd882c8aSJames Wright         if (eval_mode == CEED_EVAL_GRAD) {
1038dd64fc84SJeremy L Thompson           num_e_mode_out += dim;
1039dd64fc84SJeremy L Thompson           size_B_out += dim * elem_size * num_qpts;
1040bd882c8aSJames Wright         } else {
1041dd64fc84SJeremy L Thompson           num_e_mode_out += 1;
1042dd64fc84SJeremy L Thompson           size_B_out += elem_size * num_qpts;
1043bd882c8aSJames Wright         }
1044bd882c8aSJames Wright       }
1045bd882c8aSJames Wright     }
1046bd882c8aSJames Wright   }
1047*4e3038a5SJeremy L Thompson   CeedCheck(num_e_mode_in > 0 && num_e_mode_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1048bd882c8aSJames Wright 
1049dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem));
1050dd64fc84SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp));
1051bd882c8aSJames Wright 
1052bd882c8aSJames Wright   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1053bd882c8aSJames Wright   CeedOperatorAssemble_Sycl *asmb = impl->asmb;
1054dd64fc84SJeremy L Thompson   asmb->num_elem                  = num_elem;
1055bd882c8aSJames Wright 
1056bd882c8aSJames Wright   Ceed_Sycl *sycl_data;
1057bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
1058bd882c8aSJames Wright 
1059bd882c8aSJames Wright   // Kernel setup
1060004e4986SSebastian Grimberg   int elems_per_block   = 1;
1061004e4986SSebastian Grimberg   asmb->elems_per_block = elems_per_block;
1062dd64fc84SJeremy L Thompson   asmb->block_size_x    = elem_size;
1063dd64fc84SJeremy L Thompson   asmb->block_size_y    = elem_size;
1064dd64fc84SJeremy L Thompson   asmb->num_e_mode_in   = num_e_mode_in;
1065dd64fc84SJeremy L Thompson   asmb->num_e_mode_out  = num_e_mode_out;
1066dd64fc84SJeremy L Thompson   asmb->num_qpts        = num_qpts;
1067dd64fc84SJeremy L Thompson   asmb->num_nodes       = elem_size;
10685700671aSSebastian Grimberg   asmb->block_size      = elem_size * elem_size * elems_per_block;
1069dd64fc84SJeremy L Thompson   asmb->num_comp        = num_comp;
1070bd882c8aSJames Wright 
1071bd882c8aSJames Wright   // Build 'full' B matrices (not 1D arrays used for tensor-product matrices
1072bd882c8aSJames Wright   CeedCallBackend(CeedBasisGetInterp(basis_in, &interp_in));
1073bd882c8aSJames Wright   CeedCallBackend(CeedBasisGetGrad(basis_in, &grad_in));
1074bd882c8aSJames Wright 
1075bd882c8aSJames Wright   // Load into B_in, in order that they will be used in eval_mode
1076bd882c8aSJames Wright   CeedCallSycl(ceed, asmb->d_B_in = sycl::malloc_device<CeedScalar>(size_B_in, sycl_data->sycl_device, sycl_data->sycl_context));
1077bd882c8aSJames Wright   for (int i = 0; i < num_B_in_mats_to_load; i++) {
1078bd882c8aSJames Wright     CeedEvalMode eval_mode = eval_mode_in[i];
1079dd64fc84SJeremy L Thompson 
1080bd882c8aSJames Wright     if (eval_mode == CEED_EVAL_INTERP) {
1081bd882c8aSJames Wright       // Order queue
1082bd882c8aSJames Wright       sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
1083dd64fc84SJeremy L Thompson       sycl_data->sycl_queue.copy<CeedScalar>(interp_in, &asmb->d_B_in[mat_start], elem_size * num_qpts, {e});
1084dd64fc84SJeremy L Thompson       mat_start += elem_size * num_qpts;
1085bd882c8aSJames Wright     } else if (eval_mode == CEED_EVAL_GRAD) {
1086bd882c8aSJames Wright       // Order queue
1087bd882c8aSJames Wright       sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
1088dd64fc84SJeremy L Thompson       sycl_data->sycl_queue.copy<CeedScalar>(grad_in, &asmb->d_B_in[mat_start], dim * elem_size * num_qpts, {e});
1089dd64fc84SJeremy L Thompson       mat_start += dim * elem_size * num_qpts;
1090bd882c8aSJames Wright     }
1091bd882c8aSJames Wright   }
1092bd882c8aSJames Wright 
1093bd882c8aSJames Wright   const CeedScalar *interp_out, *grad_out;
1094bd882c8aSJames Wright   // Note that this function currently assumes 1 basis, so this should always be true
1095bd882c8aSJames Wright   // for now
1096bd882c8aSJames Wright   if (basis_out == basis_in) {
1097bd882c8aSJames Wright     interp_out = interp_in;
1098bd882c8aSJames Wright     grad_out   = grad_in;
1099bd882c8aSJames Wright   } else {
1100bd882c8aSJames Wright     CeedCallBackend(CeedBasisGetInterp(basis_out, &interp_out));
1101bd882c8aSJames Wright     CeedCallBackend(CeedBasisGetGrad(basis_out, &grad_out));
1102bd882c8aSJames Wright   }
1103bd882c8aSJames Wright 
1104bd882c8aSJames Wright   // Load into B_out, in order that they will be used in eval_mode
1105bd882c8aSJames Wright   mat_start = 0;
1106bd882c8aSJames Wright   CeedCallSycl(ceed, asmb->d_B_out = sycl::malloc_device<CeedScalar>(size_B_out, sycl_data->sycl_device, sycl_data->sycl_context));
1107bd882c8aSJames Wright   for (int i = 0; i < num_B_out_mats_to_load; i++) {
1108bd882c8aSJames Wright     CeedEvalMode eval_mode = eval_mode_out[i];
1109dd64fc84SJeremy L Thompson 
1110bd882c8aSJames Wright     if (eval_mode == CEED_EVAL_INTERP) {
1111bd882c8aSJames Wright       // Order queue
1112bd882c8aSJames Wright       sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
1113dd64fc84SJeremy L Thompson       sycl_data->sycl_queue.copy<CeedScalar>(interp_out, &asmb->d_B_out[mat_start], elem_size * num_qpts, {e});
1114dd64fc84SJeremy L Thompson       mat_start += elem_size * num_qpts;
1115bd882c8aSJames Wright     } else if (eval_mode == CEED_EVAL_GRAD) {
1116bd882c8aSJames Wright       // Order queue
1117bd882c8aSJames Wright       sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
1118dd64fc84SJeremy L Thompson       sycl_data->sycl_queue.copy<CeedScalar>(grad_out, &asmb->d_B_out[mat_start], dim * elem_size * num_qpts, {e});
1119dd64fc84SJeremy L Thompson       mat_start += dim * elem_size * num_qpts;
1120bd882c8aSJames Wright     }
1121bd882c8aSJames Wright   }
1122bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1123bd882c8aSJames Wright }
1124bd882c8aSJames Wright 
1125bd882c8aSJames Wright //------------------------------------------------------------------------------
1126bd882c8aSJames Wright // Matrix assembly kernel for low-order elements (3D thread block)
1127bd882c8aSJames Wright //------------------------------------------------------------------------------
1128bd882c8aSJames Wright static int CeedOperatorLinearAssemble_Sycl(sycl::queue &sycl_queue, const CeedOperator_Sycl *impl, const CeedScalar *qf_array,
1129bd882c8aSJames Wright                                            CeedScalar *values_array) {
1130bd882c8aSJames Wright   // This kernels assumes B_in and B_out have the same number of quadrature points and basis points.
1131bd882c8aSJames Wright   // TODO: expand to more general cases
1132bd882c8aSJames Wright   CeedOperatorAssemble_Sycl *asmb           = impl->asmb;
1133dd64fc84SJeremy L Thompson   const CeedInt              num_elem       = asmb->num_elem;
1134dd64fc84SJeremy L Thompson   const CeedSize             num_nodes      = asmb->num_nodes;
1135dd64fc84SJeremy L Thompson   const CeedSize             num_comp       = asmb->num_comp;
1136dd64fc84SJeremy L Thompson   const CeedSize             num_qpts       = asmb->num_qpts;
1137dd64fc84SJeremy L Thompson   const CeedSize             num_e_mode_in  = asmb->num_e_mode_in;
1138dd64fc84SJeremy L Thompson   const CeedSize             num_e_mode_out = asmb->num_e_mode_out;
1139bd882c8aSJames Wright 
1140bd882c8aSJames Wright   // Strides for final output ordering, determined by the reference (inference) implementation of the symbolic assembly, slowest --> fastest: element,
1141bd882c8aSJames Wright   // comp_in, comp_out, node_row, node_col
1142dd64fc84SJeremy L Thompson   const CeedSize comp_out_stride = num_nodes * num_nodes;
1143dd64fc84SJeremy L Thompson   const CeedSize comp_in_stride  = comp_out_stride * num_comp;
1144dd64fc84SJeremy L Thompson   const CeedSize e_stride        = comp_in_stride * num_comp;
1145dd64fc84SJeremy L Thompson   // Strides for QF array, slowest --> fastest:  e_mode_in, comp_in,  e_mode_out, comp_out, elem, qpt
1146dd64fc84SJeremy L Thompson   const CeedSize q_e_stride          = num_qpts;
1147dd64fc84SJeremy L Thompson   const CeedSize q_comp_out_stride   = num_elem * q_e_stride;
1148dd64fc84SJeremy L Thompson   const CeedSize q_e_mode_out_stride = q_comp_out_stride * num_comp;
1149dd64fc84SJeremy L Thompson   const CeedSize q_comp_in_stride    = q_e_mode_out_stride * num_e_mode_out;
1150dd64fc84SJeremy L Thompson   const CeedSize q_e_mode_in_stride  = q_comp_in_stride * num_comp;
1151bd882c8aSJames Wright 
1152bd882c8aSJames Wright   CeedScalar *B_in, *B_out;
1153bd882c8aSJames Wright   B_in                       = asmb->d_B_in;
1154bd882c8aSJames Wright   B_out                      = asmb->d_B_out;
1155bd882c8aSJames Wright   const CeedInt block_size_x = asmb->block_size_x;
1156bd882c8aSJames Wright   const CeedInt block_size_y = asmb->block_size_y;
1157bd882c8aSJames Wright 
1158dd64fc84SJeremy L Thompson   sycl::range<3> kernel_range(num_elem, block_size_y, block_size_x);
1159bd882c8aSJames Wright 
1160bd882c8aSJames Wright   // Order queue
1161bd882c8aSJames Wright   sycl::event e = sycl_queue.ext_oneapi_submit_barrier();
1162bd882c8aSJames Wright   sycl_queue.parallel_for<CeedOperatorSyclLinearAssemble>(kernel_range, {e}, [=](sycl::id<3> idx) {
1163bd882c8aSJames Wright     const int e = idx.get(0);  // Element index
1164bd882c8aSJames Wright     const int l = idx.get(1);  // The output column index of each B^TDB operation
1165bd882c8aSJames Wright     const int i = idx.get(2);  // The output row index of each B^TDB operation
1166bd882c8aSJames Wright                                // such that we have (Bout^T)_ij D_jk Bin_kl = C_il
1167dd64fc84SJeremy L Thompson     for (CeedSize comp_in = 0; comp_in < num_comp; comp_in++) {
1168dd64fc84SJeremy L Thompson       for (CeedSize comp_out = 0; comp_out < num_comp; comp_out++) {
1169bd882c8aSJames Wright         CeedScalar result        = 0.0;
1170dd64fc84SJeremy L Thompson         CeedSize   qf_index_comp = q_comp_in_stride * comp_in + q_comp_out_stride * comp_out + q_e_stride * e;
1171dd64fc84SJeremy L Thompson 
1172dd64fc84SJeremy L Thompson         for (CeedSize e_mode_in = 0; e_mode_in < num_e_mode_in; e_mode_in++) {
1173dd64fc84SJeremy L Thompson           CeedSize b_in_index = e_mode_in * num_qpts * num_nodes;
1174dd64fc84SJeremy L Thompson 
1175dd64fc84SJeremy L Thompson           for (CeedSize e_mode_out = 0; e_mode_out < num_e_mode_out; e_mode_out++) {
1176dd64fc84SJeremy L Thompson             CeedSize b_out_index = e_mode_out * num_qpts * num_nodes;
1177dd64fc84SJeremy L Thompson             CeedSize qf_index    = qf_index_comp + q_e_mode_out_stride * e_mode_out + q_e_mode_in_stride * e_mode_in;
1178dd64fc84SJeremy L Thompson 
1179bd882c8aSJames Wright             // Perform the B^T D B operation for this 'chunk' of D (the qf_array)
1180dd64fc84SJeremy L Thompson             for (CeedSize j = 0; j < num_qpts; j++) {
1181dd64fc84SJeremy 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];
1182bd882c8aSJames Wright             }
1183dd64fc84SJeremy L Thompson           }  // end of  e_mode_out
1184dd64fc84SJeremy L Thompson         }    // end of  e_mode_in
1185dd64fc84SJeremy L Thompson         CeedSize val_index = comp_in_stride * comp_in + comp_out_stride * comp_out + e_stride * e + num_nodes * i + l;
1186dd64fc84SJeremy L Thompson 
1187bd882c8aSJames Wright         values_array[val_index] = result;
1188bd882c8aSJames Wright       }  // end of out component
1189bd882c8aSJames Wright     }    // end of in component
1190bd882c8aSJames Wright   });
1191bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1192bd882c8aSJames Wright }
1193bd882c8aSJames Wright 
1194bd882c8aSJames Wright //------------------------------------------------------------------------------
1195bd882c8aSJames Wright // Fallback kernel for larger orders (1D thread block)
1196bd882c8aSJames Wright //------------------------------------------------------------------------------
1197bd882c8aSJames Wright /*
1198bd882c8aSJames Wright static int CeedOperatorLinearAssembleFallback_Sycl(sycl::queue &sycl_queue, const CeedOperator_Sycl *impl, const CeedScalar *qf_array,
1199bd882c8aSJames Wright                                                    CeedScalar *values_array) {
1200bd882c8aSJames Wright   // This kernel assumes B_in and B_out have the same number of quadrature points and basis points.
1201bd882c8aSJames Wright   // TODO: expand to more general cases
1202bd882c8aSJames Wright   CeedOperatorAssemble_Sycl *asmb        = impl->asmb;
1203dd64fc84SJeremy L Thompson   const CeedInt              num_elem       = asmb->num_elem;
1204dd64fc84SJeremy L Thompson   const CeedInt              num_nodes      = asmb->num_nodes;
1205dd64fc84SJeremy L Thompson   const CeedInt              num_comp       = asmb->num_comp;
1206dd64fc84SJeremy L Thompson   const CeedInt              num_qpts       = asmb->num_qpts;
1207dd64fc84SJeremy L Thompson   const CeedInt              num_e_mode_in  = asmb->num_e_mode_in;
1208dd64fc84SJeremy L Thompson   const CeedInt              num_e_mode_out = asmb->num_e_mode_out;
1209bd882c8aSJames Wright 
1210bd882c8aSJames Wright   // Strides for final output ordering, determined by the reference (interface) implementation of the symbolic assembly, slowest --> fastest: elememt,
1211bd882c8aSJames Wright   // comp_in, comp_out, node_row, node_col
1212dd64fc84SJeremy L Thompson   const CeedInt comp_out_stride = num_nodes * num_nodes;
1213dd64fc84SJeremy L Thompson   const CeedInt comp_in_stride  = comp_out_stride * num_comp;
1214dd64fc84SJeremy L Thompson   const CeedInt e_stride        = comp_in_stride * num_comp;
1215dd64fc84SJeremy L Thompson   // Strides for QF array, slowest --> fastest:  e_mode_in, comp_in,  e_mode_out, comp_out, elem, qpt
1216dd64fc84SJeremy L Thompson   const CeedInt q_e_stride         = num_qpts;
1217dd64fc84SJeremy L Thompson   const CeedInt q_comp_out_stride  = num_elem * q_e_stride;
1218dd64fc84SJeremy L Thompson   const CeedInt q_e_mode_out_stride = q_comp_out_stride * num_comp;
1219dd64fc84SJeremy L Thompson   const CeedInt q_comp_in_stride   = q_e_mode_out_stride * num_e_mode_out;
1220dd64fc84SJeremy L Thompson   const CeedInt q_e_mode_in_stride  = q_comp_in_stride * num_comp;
1221bd882c8aSJames Wright 
1222bd882c8aSJames Wright   CeedScalar *B_in, *B_out;
1223bd882c8aSJames Wright   B_in                        = asmb->d_B_in;
1224bd882c8aSJames Wright   B_out                       = asmb->d_B_out;
1225004e4986SSebastian Grimberg   const CeedInt elems_per_block = asmb->elems_per_block;
1226bd882c8aSJames Wright   const CeedInt block_size_x  = asmb->block_size_x;
1227bd882c8aSJames Wright   const CeedInt block_size_y  = asmb->block_size_y;  // This will be 1 for the fallback kernel
1228bd882c8aSJames Wright 
1229004e4986SSebastian Grimberg   const CeedInt     grid = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
1230004e4986SSebastian Grimberg   sycl::range<3>    local_range(block_size_x, block_size_y, elems_per_block);
1231004e4986SSebastian Grimberg   sycl::range<3>    global_range(grid * block_size_x, block_size_y, elems_per_block);
1232bd882c8aSJames Wright   sycl::nd_range<3> kernel_range(global_range, local_range);
1233bd882c8aSJames Wright 
1234bd882c8aSJames Wright   sycl_queue.parallel_for<CeedOperatorSyclLinearAssembleFallback>(kernel_range, [=](sycl::nd_item<3> work_item) {
1235bd882c8aSJames Wright     const CeedInt blockIdx  = work_item.get_group(0);
1236bd882c8aSJames Wright     const CeedInt gridDimx  = work_item.get_group_range(0);
1237bd882c8aSJames Wright     const CeedInt threadIdx = work_item.get_local_id(0);
1238bd882c8aSJames Wright     const CeedInt threadIdz = work_item.get_local_id(2);
1239bd882c8aSJames Wright     const CeedInt blockDimz = work_item.get_local_range(2);
1240bd882c8aSJames Wright 
1241bd882c8aSJames Wright     const int l = threadIdx;  // The output column index of each B^TDB operation
1242bd882c8aSJames Wright                               // such that we have (Bout^T)_ij D_jk Bin_kl = C_il
1243dd64fc84SJeremy L Thompson     for (CeedInt e = blockIdx * blockDimz + threadIdz; e < num_elem; e += gridDimx * blockDimz) {
1244dd64fc84SJeremy L Thompson       for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
1245dd64fc84SJeremy L Thompson         for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
1246dd64fc84SJeremy L Thompson           for (CeedInt i = 0; i < num_nodes; i++) {
1247bd882c8aSJames Wright             CeedScalar result        = 0.0;
1248dd64fc84SJeremy L Thompson             CeedInt    qf_index_comp = q_comp_in_stride * comp_in + q_comp_out_stride * comp_out + q_e_stride * e;
1249dd64fc84SJeremy L Thompson             for (CeedInt  e_mode_in = 0;  e_mode_in < num_e_mode_in;  e_mode_in++) {
1250dd64fc84SJeremy L Thompson               CeedInt b_in_index =  e_mode_in * num_qpts * num_nodes;
1251dd64fc84SJeremy L Thompson               for (CeedInt  e_mode_out = 0;  e_mode_out < num_e_mode_out;  e_mode_out++) {
1252dd64fc84SJeremy L Thompson                 CeedInt b_out_index =  e_mode_out * num_qpts * num_nodes;
1253dd64fc84SJeremy L Thompson                 CeedInt qf_index    = qf_index_comp + q_e_mode_out_stride *  e_mode_out + q_e_mode_in_stride *  e_mode_in;
1254bd882c8aSJames Wright                 // Perform the B^T D B operation for this 'chunk' of D (the qf_array)
1255dd64fc84SJeremy L Thompson                 for (CeedInt j = 0; j < num_qpts; j++) {
1256dd64fc84SJeremy 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];
1257bd882c8aSJames Wright                 }
1258dd64fc84SJeremy L Thompson               }  // end of  e_mode_out
1259dd64fc84SJeremy L Thompson             }    // end of  e_mode_in
1260dd64fc84SJeremy L Thompson             CeedInt val_index       = comp_in_stride * comp_in + comp_out_stride * comp_out + e_stride * e + num_nodes * i + l;
1261bd882c8aSJames Wright             values_array[val_index] = result;
1262bd882c8aSJames Wright           }  // end of loop over element node index, i
1263bd882c8aSJames Wright         }    // end of out component
1264bd882c8aSJames Wright       }      // end of in component
1265bd882c8aSJames Wright     }        // end of element loop
1266bd882c8aSJames Wright   });
1267bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1268bd882c8aSJames Wright }*/
1269bd882c8aSJames Wright 
1270bd882c8aSJames Wright //------------------------------------------------------------------------------
1271bd882c8aSJames Wright // Assemble matrix data for COO matrix of assembled operator.
1272bd882c8aSJames Wright // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1273bd882c8aSJames Wright //
1274bd882c8aSJames Wright // Note that this (and other assembly routines) currently assume only one active
1275bd882c8aSJames Wright // input restriction/basis per operator (could have multiple basis eval modes).
1276bd882c8aSJames Wright // TODO: allow multiple active input restrictions/basis objects
1277bd882c8aSJames Wright //------------------------------------------------------------------------------
1278bd882c8aSJames Wright static int CeedSingleOperatorAssemble_Sycl(CeedOperator op, CeedInt offset, CeedVector values) {
1279bd882c8aSJames Wright   Ceed                ceed;
1280bd882c8aSJames Wright   Ceed_Sycl          *sycl_data;
1281dd64fc84SJeremy L Thompson   CeedScalar         *values_array;
1282dd64fc84SJeremy L Thompson   const CeedScalar   *qf_array;
1283dd64fc84SJeremy L Thompson   CeedVector          assembled_qf = NULL;
1284dd64fc84SJeremy L Thompson   CeedElemRestriction rstr_q       = NULL;
1285dd64fc84SJeremy L Thompson   CeedOperator_Sycl  *impl;
1286dd64fc84SJeremy L Thompson 
1287dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1288dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1289bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
1290bd882c8aSJames Wright 
1291bd882c8aSJames Wright   // Setup
1292bd882c8aSJames Wright   if (!impl->asmb) {
1293bd882c8aSJames Wright     CeedCallBackend(CeedSingleOperatorAssembleSetup_Sycl(op));
1294bd882c8aSJames Wright     assert(impl->asmb != NULL);
1295bd882c8aSJames Wright   }
1296bd882c8aSJames Wright 
1297bd882c8aSJames Wright   // Assemble QFunction
1298bd882c8aSJames Wright   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
1299bd882c8aSJames Wright   CeedCallBackend(CeedElemRestrictionDestroy(&rstr_q));
1300bd882c8aSJames Wright   CeedCallBackend(CeedVectorGetArrayWrite(values, CEED_MEM_DEVICE, &values_array));
1301bd882c8aSJames Wright   values_array += offset;
1302bd882c8aSJames Wright   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &qf_array));
1303bd882c8aSJames Wright 
1304bd882c8aSJames Wright   // Compute B^T D B
1305bd882c8aSJames Wright   CeedCallBackend(CeedOperatorLinearAssemble_Sycl(sycl_data->sycl_queue, impl, qf_array, values_array));
1306bd882c8aSJames Wright 
1307bd882c8aSJames Wright   // Wait for kernels to be completed
1308bd882c8aSJames Wright   // Kris: Review if this is necessary -- enqueing an async barrier may be sufficient
1309bd882c8aSJames Wright   sycl_data->sycl_queue.wait_and_throw();
1310bd882c8aSJames Wright 
1311bd882c8aSJames Wright   // Restore arrays
1312bd882c8aSJames Wright   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1313bd882c8aSJames Wright   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &qf_array));
1314bd882c8aSJames Wright 
1315bd882c8aSJames Wright   // Cleanup
1316bd882c8aSJames Wright   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1317bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1318bd882c8aSJames Wright }
1319bd882c8aSJames Wright 
1320bd882c8aSJames Wright //------------------------------------------------------------------------------
1321bd882c8aSJames Wright // Create operator
1322bd882c8aSJames Wright //------------------------------------------------------------------------------
1323bd882c8aSJames Wright int CeedOperatorCreate_Sycl(CeedOperator op) {
1324bd882c8aSJames Wright   Ceed               ceed;
1325bd882c8aSJames Wright   CeedOperator_Sycl *impl;
1326bd882c8aSJames Wright 
1327dd64fc84SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1328dd64fc84SJeremy L Thompson 
1329bd882c8aSJames Wright   CeedCallBackend(CeedCalloc(1, &impl));
1330bd882c8aSJames Wright   CeedCallBackend(CeedOperatorSetData(op, impl));
1331bd882c8aSJames Wright 
1332bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Sycl));
1333bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Sycl));
1334bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Sycl));
1335bd882c8aSJames Wright   CeedCallBackend(
1336bd882c8aSJames Wright       CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Sycl));
1337bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Sycl));
1338bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Sycl));
1339bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Sycl));
1340bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
1341bd882c8aSJames Wright }
1342bd882c8aSJames Wright 
1343bd882c8aSJames Wright //------------------------------------------------------------------------------
1344