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