xref: /libCEED/rust/libceed-sys/c-src/backends/sycl-ref/ceed-sycl-ref-qfunctioncontext.sycl.cpp (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1*5aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2bd882c8aSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3bd882c8aSJames Wright //
4bd882c8aSJames Wright // SPDX-License-Identifier: BSD-2-Clause
5bd882c8aSJames Wright //
6bd882c8aSJames Wright // This file is part of CEED:  http://github.com/ceed
7bd882c8aSJames Wright 
8bd882c8aSJames Wright #include <ceed/backend.h>
9bd882c8aSJames Wright #include <ceed/ceed.h>
10bd882c8aSJames Wright 
11bd882c8aSJames Wright #include <string>
12bd882c8aSJames Wright #include <sycl/sycl.hpp>
13bd882c8aSJames Wright 
14bd882c8aSJames Wright #include "ceed-sycl-ref.hpp"
15bd882c8aSJames Wright 
16bd882c8aSJames Wright //------------------------------------------------------------------------------
17bd882c8aSJames Wright // Sync host to device
18bd882c8aSJames Wright //------------------------------------------------------------------------------
19bd882c8aSJames Wright static inline int CeedQFunctionContextSyncH2D_Sycl(const CeedQFunctionContext ctx) {
20bd882c8aSJames Wright   Ceed                       ceed;
21bd882c8aSJames Wright   Ceed_Sycl                 *sycl_data;
22dd64fc84SJeremy L Thompson   size_t                     ctx_size;
23dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
24dd64fc84SJeremy L Thompson 
25dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
26dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
27bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
284e3038a5SJeremy L Thompson   CeedCheck(impl->h_data, ceed, CEED_ERROR_BACKEND, "No valid host data to sync to device");
29bd882c8aSJames Wright 
30dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
31bd882c8aSJames Wright 
32bd882c8aSJames Wright   if (impl->d_data_borrowed) {
33bd882c8aSJames Wright     impl->d_data = impl->d_data_borrowed;
34bd882c8aSJames Wright   } else if (impl->d_data_owned) {
35bd882c8aSJames Wright     impl->d_data = impl->d_data_owned;
36bd882c8aSJames Wright   } else {
37dd64fc84SJeremy L Thompson     CeedCallSycl(ceed, impl->d_data_owned = sycl::malloc_device(ctx_size, sycl_data->sycl_device, sycl_data->sycl_context));
38bd882c8aSJames Wright     impl->d_data = impl->d_data_owned;
39bd882c8aSJames Wright   }
40bd882c8aSJames Wright   // Order queue
41bd882c8aSJames Wright   sycl::event e          = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
42dd64fc84SJeremy L Thompson   sycl::event copy_event = sycl_data->sycl_queue.memcpy(impl->d_data, impl->h_data, ctx_size, {e});
43bd882c8aSJames Wright   CeedCallSycl(ceed, copy_event.wait_and_throw());
44bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
45bd882c8aSJames Wright }
46bd882c8aSJames Wright 
47bd882c8aSJames Wright //------------------------------------------------------------------------------
48bd882c8aSJames Wright // Sync device to host
49bd882c8aSJames Wright //------------------------------------------------------------------------------
50bd882c8aSJames Wright static inline int CeedQFunctionContextSyncD2H_Sycl(const CeedQFunctionContext ctx) {
51bd882c8aSJames Wright   Ceed                       ceed;
52bd882c8aSJames Wright   Ceed_Sycl                 *sycl_data;
53dd64fc84SJeremy L Thompson   size_t                     ctx_size;
54dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
55dd64fc84SJeremy L Thompson 
56dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
57dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
58bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
594e3038a5SJeremy L Thompson   CeedCheck(impl->d_data, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host");
60bd882c8aSJames Wright 
61dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
62bd882c8aSJames Wright 
63bd882c8aSJames Wright   if (impl->h_data_borrowed) {
64bd882c8aSJames Wright     impl->h_data = impl->h_data_borrowed;
65bd882c8aSJames Wright   } else if (impl->h_data_owned) {
66bd882c8aSJames Wright     impl->h_data = impl->h_data_owned;
67bd882c8aSJames Wright   } else {
68dd64fc84SJeremy L Thompson     CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->h_data_owned));
69bd882c8aSJames Wright     impl->h_data = impl->h_data_owned;
70bd882c8aSJames Wright   }
71bd882c8aSJames Wright 
72bd882c8aSJames Wright   // Order queue
73bd882c8aSJames Wright   sycl::event e          = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
74dd64fc84SJeremy L Thompson   sycl::event copy_event = sycl_data->sycl_queue.memcpy(impl->h_data, impl->d_data, ctx_size, {e});
75bd882c8aSJames Wright   CeedCallSycl(ceed, copy_event.wait_and_throw());
76bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
77bd882c8aSJames Wright }
78bd882c8aSJames Wright 
79bd882c8aSJames Wright //------------------------------------------------------------------------------
80bd882c8aSJames Wright // Sync data of type
81bd882c8aSJames Wright //------------------------------------------------------------------------------
82bd882c8aSJames Wright static inline int CeedQFunctionContextSync_Sycl(const CeedQFunctionContext ctx, CeedMemType mem_type) {
83bd882c8aSJames Wright   switch (mem_type) {
84bd882c8aSJames Wright     case CEED_MEM_HOST:
85bd882c8aSJames Wright       return CeedQFunctionContextSyncD2H_Sycl(ctx);
86bd882c8aSJames Wright     case CEED_MEM_DEVICE:
87bd882c8aSJames Wright       return CeedQFunctionContextSyncH2D_Sycl(ctx);
88bd882c8aSJames Wright   }
89bd882c8aSJames Wright   return CEED_ERROR_UNSUPPORTED;
90bd882c8aSJames Wright }
91bd882c8aSJames Wright 
92bd882c8aSJames Wright //------------------------------------------------------------------------------
93bd882c8aSJames Wright // Set all pointers as invalid
94bd882c8aSJames Wright //------------------------------------------------------------------------------
95bd882c8aSJames Wright static inline int CeedQFunctionContextSetAllInvalid_Sycl(const CeedQFunctionContext ctx) {
96bd882c8aSJames Wright   CeedQFunctionContext_Sycl *impl;
97bd882c8aSJames Wright 
98dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
99bd882c8aSJames Wright   impl->h_data = NULL;
100bd882c8aSJames Wright   impl->d_data = NULL;
101bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
102bd882c8aSJames Wright }
103bd882c8aSJames Wright 
104bd882c8aSJames Wright //------------------------------------------------------------------------------
105bd882c8aSJames Wright // Check if ctx has valid data
106bd882c8aSJames Wright //------------------------------------------------------------------------------
107bd882c8aSJames Wright static inline int CeedQFunctionContextHasValidData_Sycl(const CeedQFunctionContext ctx, bool *has_valid_data) {
108bd882c8aSJames Wright   CeedQFunctionContext_Sycl *impl;
109dd64fc84SJeremy L Thompson 
110bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1111c66c397SJeremy L Thompson   *has_valid_data = impl && (impl->h_data || impl->d_data);
112bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
113bd882c8aSJames Wright }
114bd882c8aSJames Wright 
115bd882c8aSJames Wright //------------------------------------------------------------------------------
116bd882c8aSJames Wright // Check if ctx has borrowed data
117bd882c8aSJames Wright //------------------------------------------------------------------------------
118bd882c8aSJames Wright static inline int CeedQFunctionContextHasBorrowedDataOfType_Sycl(const CeedQFunctionContext ctx, CeedMemType mem_type,
119bd882c8aSJames Wright                                                                  bool *has_borrowed_data_of_type) {
120bd882c8aSJames Wright   CeedQFunctionContext_Sycl *impl;
121bd882c8aSJames Wright 
122dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
123bd882c8aSJames Wright   switch (mem_type) {
124bd882c8aSJames Wright     case CEED_MEM_HOST:
1251c66c397SJeremy L Thompson       *has_borrowed_data_of_type = impl->h_data_borrowed;
126bd882c8aSJames Wright       break;
127bd882c8aSJames Wright     case CEED_MEM_DEVICE:
1281c66c397SJeremy L Thompson       *has_borrowed_data_of_type = impl->d_data_borrowed;
129bd882c8aSJames Wright       break;
130bd882c8aSJames Wright   }
131bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
132bd882c8aSJames Wright }
133bd882c8aSJames Wright 
134bd882c8aSJames Wright //------------------------------------------------------------------------------
135bd882c8aSJames Wright // Check if data of given type needs sync
136bd882c8aSJames Wright //------------------------------------------------------------------------------
137bd882c8aSJames Wright static inline int CeedQFunctionContextNeedSync_Sycl(const CeedQFunctionContext ctx, CeedMemType mem_type, bool *need_sync) {
138bd882c8aSJames Wright   bool                       has_valid_data = true;
139dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
140dd64fc84SJeremy L Thompson 
141dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
142bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
143bd882c8aSJames Wright   switch (mem_type) {
144bd882c8aSJames Wright     case CEED_MEM_HOST:
145bd882c8aSJames Wright       *need_sync = has_valid_data && !impl->h_data;
146bd882c8aSJames Wright       break;
147bd882c8aSJames Wright     case CEED_MEM_DEVICE:
148bd882c8aSJames Wright       *need_sync = has_valid_data && !impl->d_data;
149bd882c8aSJames Wright       break;
150bd882c8aSJames Wright   }
151bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
152bd882c8aSJames Wright }
153bd882c8aSJames Wright 
154bd882c8aSJames Wright //------------------------------------------------------------------------------
155bd882c8aSJames Wright // Set data from host
156bd882c8aSJames Wright //------------------------------------------------------------------------------
157bd882c8aSJames Wright static int CeedQFunctionContextSetDataHost_Sycl(const CeedQFunctionContext ctx, const CeedCopyMode copy_mode, void *data) {
158bd882c8aSJames Wright   CeedQFunctionContext_Sycl *impl;
159bd882c8aSJames Wright 
160dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
161bd882c8aSJames Wright   CeedCallBackend(CeedFree(&impl->h_data_owned));
162bd882c8aSJames Wright   switch (copy_mode) {
163bd882c8aSJames Wright     case CEED_COPY_VALUES:
164dd64fc84SJeremy L Thompson       size_t ctx_size;
165dd64fc84SJeremy L Thompson 
166dd64fc84SJeremy L Thompson       CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
167dd64fc84SJeremy L Thompson       CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->h_data_owned));
168bd882c8aSJames Wright       impl->h_data_borrowed = NULL;
169bd882c8aSJames Wright       impl->h_data          = impl->h_data_owned;
170dd64fc84SJeremy L Thompson       memcpy(impl->h_data, data, ctx_size);
171bd882c8aSJames Wright       break;
172bd882c8aSJames Wright     case CEED_OWN_POINTER:
173bd882c8aSJames Wright       impl->h_data_owned    = data;
174bd882c8aSJames Wright       impl->h_data_borrowed = NULL;
175bd882c8aSJames Wright       impl->h_data          = data;
176bd882c8aSJames Wright       break;
177bd882c8aSJames Wright     case CEED_USE_POINTER:
178bd882c8aSJames Wright       impl->h_data_borrowed = data;
179bd882c8aSJames Wright       impl->h_data          = data;
180bd882c8aSJames Wright       break;
181bd882c8aSJames Wright   }
182bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
183bd882c8aSJames Wright }
184bd882c8aSJames Wright 
185bd882c8aSJames Wright //------------------------------------------------------------------------------
186bd882c8aSJames Wright // Set data from device
187bd882c8aSJames Wright //------------------------------------------------------------------------------
188bd882c8aSJames Wright static int CeedQFunctionContextSetDataDevice_Sycl(const CeedQFunctionContext ctx, const CeedCopyMode copy_mode, void *data) {
189bd882c8aSJames Wright   Ceed                       ceed;
190bd882c8aSJames Wright   Ceed_Sycl                 *sycl_data;
191dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
192dd64fc84SJeremy L Thompson 
193dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
194dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
195bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
196bd882c8aSJames Wright 
197bd882c8aSJames Wright   // Order queue
198bd882c8aSJames Wright   sycl::event e = sycl_data->sycl_queue.ext_oneapi_submit_barrier();
199bd882c8aSJames Wright 
200bd882c8aSJames Wright   // Wait for all work to finish before freeing memory
201bd882c8aSJames Wright   if (impl->d_data_owned) {
202bd882c8aSJames Wright     CeedCallSycl(ceed, sycl_data->sycl_queue.wait_and_throw());
203bd882c8aSJames Wright     CeedCallSycl(ceed, sycl::free(impl->d_data_owned, sycl_data->sycl_context));
204bd882c8aSJames Wright     impl->d_data_owned = NULL;
205bd882c8aSJames Wright   }
206bd882c8aSJames Wright 
207bd882c8aSJames Wright   switch (copy_mode) {
208bd882c8aSJames Wright     case CEED_COPY_VALUES: {
209dd64fc84SJeremy L Thompson       size_t ctx_size;
210dd64fc84SJeremy L Thompson 
211dd64fc84SJeremy L Thompson       CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
212dd64fc84SJeremy L Thompson       CeedCallSycl(ceed, impl->d_data_owned = sycl::malloc_device(ctx_size, sycl_data->sycl_device, sycl_data->sycl_context));
213bd882c8aSJames Wright       impl->d_data_borrowed  = NULL;
214bd882c8aSJames Wright       impl->d_data           = impl->d_data_owned;
215dd64fc84SJeremy L Thompson       sycl::event copy_event = sycl_data->sycl_queue.memcpy(impl->d_data, data, ctx_size, {e});
216bd882c8aSJames Wright       CeedCallSycl(ceed, copy_event.wait_and_throw());
217bd882c8aSJames Wright     } break;
218bd882c8aSJames Wright     case CEED_OWN_POINTER: {
219bd882c8aSJames Wright       impl->d_data_owned    = data;
220bd882c8aSJames Wright       impl->d_data_borrowed = NULL;
221bd882c8aSJames Wright       impl->d_data          = data;
222bd882c8aSJames Wright     } break;
223bd882c8aSJames Wright     case CEED_USE_POINTER: {
224bd882c8aSJames Wright       impl->d_data_owned    = NULL;
225bd882c8aSJames Wright       impl->d_data_borrowed = data;
226bd882c8aSJames Wright       impl->d_data          = data;
227bd882c8aSJames Wright     } break;
228bd882c8aSJames Wright   }
229bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
230bd882c8aSJames Wright }
231bd882c8aSJames Wright 
232bd882c8aSJames Wright //------------------------------------------------------------------------------
233bd882c8aSJames Wright // Set the data used by a user context,
234bd882c8aSJames Wright //   freeing any previously allocated data if applicable
235bd882c8aSJames Wright //------------------------------------------------------------------------------
236bd882c8aSJames Wright static int CeedQFunctionContextSetData_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, const CeedCopyMode copy_mode, void *data) {
237bd882c8aSJames Wright   Ceed ceed;
238bd882c8aSJames Wright 
239dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
240bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextSetAllInvalid_Sycl(ctx));
241bd882c8aSJames Wright   switch (mem_type) {
242bd882c8aSJames Wright     case CEED_MEM_HOST:
243bd882c8aSJames Wright       return CeedQFunctionContextSetDataHost_Sycl(ctx, copy_mode, data);
244bd882c8aSJames Wright     case CEED_MEM_DEVICE:
245bd882c8aSJames Wright       return CeedQFunctionContextSetDataDevice_Sycl(ctx, copy_mode, data);
246bd882c8aSJames Wright   }
247bd882c8aSJames Wright   return CEED_ERROR_UNSUPPORTED;
248bd882c8aSJames Wright }
249bd882c8aSJames Wright 
250bd882c8aSJames Wright //------------------------------------------------------------------------------
251bd882c8aSJames Wright // Take data
252bd882c8aSJames Wright //------------------------------------------------------------------------------
253bd882c8aSJames Wright static int CeedQFunctionContextTakeData_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
254bd882c8aSJames Wright   Ceed                       ceed;
255bd882c8aSJames Wright   Ceed_Sycl                 *ceedSycl;
256dd64fc84SJeremy L Thompson   bool                       need_sync = false;
257dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
258dd64fc84SJeremy L Thompson 
259dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
260dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
261bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &ceedSycl));
262bd882c8aSJames Wright 
263bd882c8aSJames Wright   // Order queue
264bd882c8aSJames Wright   ceedSycl->sycl_queue.ext_oneapi_submit_barrier();
265bd882c8aSJames Wright 
266bd882c8aSJames Wright   // Sync data to requested mem_type
267bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextNeedSync_Sycl(ctx, mem_type, &need_sync));
268bd882c8aSJames Wright   if (need_sync) CeedCallBackend(CeedQFunctionContextSync_Sycl(ctx, mem_type));
269bd882c8aSJames Wright 
270bd882c8aSJames Wright   // Update pointer
271bd882c8aSJames Wright   switch (mem_type) {
272bd882c8aSJames Wright     case CEED_MEM_HOST:
273bd882c8aSJames Wright       *(void **)data        = impl->h_data_borrowed;
274bd882c8aSJames Wright       impl->h_data_borrowed = NULL;
275bd882c8aSJames Wright       impl->h_data          = NULL;
276bd882c8aSJames Wright       break;
277bd882c8aSJames Wright     case CEED_MEM_DEVICE:
278bd882c8aSJames Wright       *(void **)data        = impl->d_data_borrowed;
279bd882c8aSJames Wright       impl->d_data_borrowed = NULL;
280bd882c8aSJames Wright       impl->d_data          = NULL;
281bd882c8aSJames Wright       break;
282bd882c8aSJames Wright   }
283bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
284bd882c8aSJames Wright }
285bd882c8aSJames Wright 
286bd882c8aSJames Wright //------------------------------------------------------------------------------
287bd882c8aSJames Wright // Core logic for GetData.
288bd882c8aSJames Wright //   If a different memory type is most up to date, this will perform a copy
289bd882c8aSJames Wright //------------------------------------------------------------------------------
290bd882c8aSJames Wright static int CeedQFunctionContextGetDataCore_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
291bd882c8aSJames Wright   Ceed                       ceed;
292dd64fc84SJeremy L Thompson   bool                       need_sync = false;
293bd882c8aSJames Wright   CeedQFunctionContext_Sycl *impl;
294dd64fc84SJeremy L Thompson 
295dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
296bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
297bd882c8aSJames Wright 
298bd882c8aSJames Wright   // Sync data to requested mem_type
299bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextNeedSync_Sycl(ctx, mem_type, &need_sync));
300bd882c8aSJames Wright   if (need_sync) CeedCallBackend(CeedQFunctionContextSync_Sycl(ctx, mem_type));
301bd882c8aSJames Wright 
302bd882c8aSJames Wright   // Update pointer
303bd882c8aSJames Wright   switch (mem_type) {
304bd882c8aSJames Wright     case CEED_MEM_HOST:
305bd882c8aSJames Wright       *(void **)data = impl->h_data;
306bd882c8aSJames Wright       break;
307bd882c8aSJames Wright     case CEED_MEM_DEVICE:
308bd882c8aSJames Wright       *(void **)data = impl->d_data;
309bd882c8aSJames Wright       break;
310bd882c8aSJames Wright   }
311bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
312bd882c8aSJames Wright }
313bd882c8aSJames Wright 
314bd882c8aSJames Wright //------------------------------------------------------------------------------
315bd882c8aSJames Wright // Get read-only access to the data
316bd882c8aSJames Wright //------------------------------------------------------------------------------
317bd882c8aSJames Wright static int CeedQFunctionContextGetDataRead_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
318bd882c8aSJames Wright   return CeedQFunctionContextGetDataCore_Sycl(ctx, mem_type, data);
319bd882c8aSJames Wright }
320bd882c8aSJames Wright 
321bd882c8aSJames Wright //------------------------------------------------------------------------------
322bd882c8aSJames Wright // Get read/write access to the data
323bd882c8aSJames Wright //------------------------------------------------------------------------------
324bd882c8aSJames Wright static int CeedQFunctionContextGetData_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
325bd882c8aSJames Wright   Ceed                       ceed;
326dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
327bd882c8aSJames Wright 
328dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
329dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
330bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextGetDataCore_Sycl(ctx, mem_type, data));
331bd882c8aSJames Wright 
332bd882c8aSJames Wright   // Mark only pointer for requested memory as valid
333bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextSetAllInvalid_Sycl(ctx));
334bd882c8aSJames Wright   switch (mem_type) {
335bd882c8aSJames Wright     case CEED_MEM_HOST:
336bd882c8aSJames Wright       impl->h_data = *(void **)data;
337bd882c8aSJames Wright       break;
338bd882c8aSJames Wright     case CEED_MEM_DEVICE:
339bd882c8aSJames Wright       impl->d_data = *(void **)data;
340bd882c8aSJames Wright       break;
341bd882c8aSJames Wright   }
342bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
343bd882c8aSJames Wright }
344bd882c8aSJames Wright 
345bd882c8aSJames Wright //------------------------------------------------------------------------------
346bd882c8aSJames Wright // Destroy the user context
347bd882c8aSJames Wright //------------------------------------------------------------------------------
348bd882c8aSJames Wright static int CeedQFunctionContextDestroy_Sycl(const CeedQFunctionContext ctx) {
349bd882c8aSJames Wright   Ceed                       ceed;
350bd882c8aSJames Wright   Ceed_Sycl                 *sycl_data;
351dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
352dd64fc84SJeremy L Thompson 
353dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
354dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
355bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
356bd882c8aSJames Wright 
357bd882c8aSJames Wright   // Wait for all work to finish before freeing memory
358bd882c8aSJames Wright   CeedCallSycl(ceed, sycl_data->sycl_queue.wait_and_throw());
359bd882c8aSJames Wright   CeedCallSycl(ceed, sycl::free(impl->d_data_owned, sycl_data->sycl_context));
360bd882c8aSJames Wright   CeedCallBackend(CeedFree(&impl->h_data_owned));
361bd882c8aSJames Wright   CeedCallBackend(CeedFree(&impl));
362bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
363bd882c8aSJames Wright }
364bd882c8aSJames Wright 
365bd882c8aSJames Wright //------------------------------------------------------------------------------
366bd882c8aSJames Wright // QFunctionContext Create
367bd882c8aSJames Wright //------------------------------------------------------------------------------
368bd882c8aSJames Wright int CeedQFunctionContextCreate_Sycl(CeedQFunctionContext ctx) {
369bd882c8aSJames Wright   Ceed                       ceed;
370dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
371bd882c8aSJames Wright 
372dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
373bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Sycl));
374bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Sycl));
375bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Sycl));
376bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Sycl));
377bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Sycl));
378bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetDataRead_Sycl));
379bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Sycl));
380bd882c8aSJames Wright   CeedCallBackend(CeedCalloc(1, &impl));
381bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl));
382bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
383bd882c8aSJames Wright }
384ff1e7120SSebastian Grimberg 
385bd882c8aSJames Wright //------------------------------------------------------------------------------
386