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