xref: /libCEED/backends/sycl-ref/ceed-sycl-ref-qfunctioncontext.sycl.cpp (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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 //------------------------------------------------------------------------------
CeedQFunctionContextSyncH2D_Sycl(const CeedQFunctionContext ctx)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   }
401f4b1b45SUmesh Unnikrishnan   std::vector<sycl::event> e;
411f4b1b45SUmesh Unnikrishnan 
421f4b1b45SUmesh Unnikrishnan   if (!sycl_data->sycl_queue.is_in_order()) e = {sycl_data->sycl_queue.ext_oneapi_submit_barrier()};
431f4b1b45SUmesh 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());
459bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
46bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
47bd882c8aSJames Wright }
48bd882c8aSJames Wright 
49bd882c8aSJames Wright //------------------------------------------------------------------------------
50bd882c8aSJames Wright // Sync device to host
51bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextSyncD2H_Sycl(const CeedQFunctionContext ctx)52bd882c8aSJames Wright static inline int CeedQFunctionContextSyncD2H_Sycl(const CeedQFunctionContext ctx) {
53bd882c8aSJames Wright   Ceed                       ceed;
54bd882c8aSJames Wright   Ceed_Sycl                 *sycl_data;
55dd64fc84SJeremy L Thompson   size_t                     ctx_size;
56dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
57dd64fc84SJeremy L Thompson 
58dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
59dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
60bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
614e3038a5SJeremy L Thompson   CeedCheck(impl->d_data, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host");
62bd882c8aSJames Wright 
63dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
64bd882c8aSJames Wright 
65bd882c8aSJames Wright   if (impl->h_data_borrowed) {
66bd882c8aSJames Wright     impl->h_data = impl->h_data_borrowed;
67bd882c8aSJames Wright   } else if (impl->h_data_owned) {
68bd882c8aSJames Wright     impl->h_data = impl->h_data_owned;
69bd882c8aSJames Wright   } else {
70dd64fc84SJeremy L Thompson     CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->h_data_owned));
71bd882c8aSJames Wright     impl->h_data = impl->h_data_owned;
72bd882c8aSJames Wright   }
73bd882c8aSJames Wright 
741f4b1b45SUmesh Unnikrishnan   std::vector<sycl::event> e;
751f4b1b45SUmesh Unnikrishnan 
761f4b1b45SUmesh Unnikrishnan   if (!sycl_data->sycl_queue.is_in_order()) e = {sycl_data->sycl_queue.ext_oneapi_submit_barrier()};
771f4b1b45SUmesh Unnikrishnan   sycl::event copy_event = sycl_data->sycl_queue.memcpy(impl->h_data, impl->d_data, ctx_size, e);
78bd882c8aSJames Wright   CeedCallSycl(ceed, copy_event.wait_and_throw());
799bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
80bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
81bd882c8aSJames Wright }
82bd882c8aSJames Wright 
83bd882c8aSJames Wright //------------------------------------------------------------------------------
84bd882c8aSJames Wright // Sync data of type
85bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextSync_Sycl(const CeedQFunctionContext ctx,CeedMemType mem_type)86bd882c8aSJames Wright static inline int CeedQFunctionContextSync_Sycl(const CeedQFunctionContext ctx, CeedMemType mem_type) {
87bd882c8aSJames Wright   switch (mem_type) {
88bd882c8aSJames Wright     case CEED_MEM_HOST:
89bd882c8aSJames Wright       return CeedQFunctionContextSyncD2H_Sycl(ctx);
90bd882c8aSJames Wright     case CEED_MEM_DEVICE:
91bd882c8aSJames Wright       return CeedQFunctionContextSyncH2D_Sycl(ctx);
92bd882c8aSJames Wright   }
93bd882c8aSJames Wright   return CEED_ERROR_UNSUPPORTED;
94bd882c8aSJames Wright }
95bd882c8aSJames Wright 
96bd882c8aSJames Wright //------------------------------------------------------------------------------
97bd882c8aSJames Wright // Set all pointers as invalid
98bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextSetAllInvalid_Sycl(const CeedQFunctionContext ctx)99bd882c8aSJames Wright static inline int CeedQFunctionContextSetAllInvalid_Sycl(const CeedQFunctionContext ctx) {
100bd882c8aSJames Wright   CeedQFunctionContext_Sycl *impl;
101bd882c8aSJames Wright 
102dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
103bd882c8aSJames Wright   impl->h_data = NULL;
104bd882c8aSJames Wright   impl->d_data = NULL;
105bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
106bd882c8aSJames Wright }
107bd882c8aSJames Wright 
108bd882c8aSJames Wright //------------------------------------------------------------------------------
109bd882c8aSJames Wright // Check if ctx has valid data
110bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextHasValidData_Sycl(const CeedQFunctionContext ctx,bool * has_valid_data)111bd882c8aSJames Wright static inline int CeedQFunctionContextHasValidData_Sycl(const CeedQFunctionContext ctx, bool *has_valid_data) {
112bd882c8aSJames Wright   CeedQFunctionContext_Sycl *impl;
113dd64fc84SJeremy L Thompson 
114bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1151c66c397SJeremy L Thompson   *has_valid_data = impl && (impl->h_data || impl->d_data);
116bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
117bd882c8aSJames Wright }
118bd882c8aSJames Wright 
119bd882c8aSJames Wright //------------------------------------------------------------------------------
120bd882c8aSJames Wright // Check if ctx has borrowed data
121bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextHasBorrowedDataOfType_Sycl(const CeedQFunctionContext ctx,CeedMemType mem_type,bool * has_borrowed_data_of_type)122bd882c8aSJames Wright static inline int CeedQFunctionContextHasBorrowedDataOfType_Sycl(const CeedQFunctionContext ctx, CeedMemType mem_type,
123bd882c8aSJames Wright                                                                  bool *has_borrowed_data_of_type) {
124bd882c8aSJames Wright   CeedQFunctionContext_Sycl *impl;
125bd882c8aSJames Wright 
126dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
127bd882c8aSJames Wright   switch (mem_type) {
128bd882c8aSJames Wright     case CEED_MEM_HOST:
1291c66c397SJeremy L Thompson       *has_borrowed_data_of_type = impl->h_data_borrowed;
130bd882c8aSJames Wright       break;
131bd882c8aSJames Wright     case CEED_MEM_DEVICE:
1321c66c397SJeremy L Thompson       *has_borrowed_data_of_type = impl->d_data_borrowed;
133bd882c8aSJames Wright       break;
134bd882c8aSJames Wright   }
135bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
136bd882c8aSJames Wright }
137bd882c8aSJames Wright 
138bd882c8aSJames Wright //------------------------------------------------------------------------------
139bd882c8aSJames Wright // Check if data of given type needs sync
140bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextNeedSync_Sycl(const CeedQFunctionContext ctx,CeedMemType mem_type,bool * need_sync)141bd882c8aSJames Wright static inline int CeedQFunctionContextNeedSync_Sycl(const CeedQFunctionContext ctx, CeedMemType mem_type, bool *need_sync) {
142bd882c8aSJames Wright   bool                       has_valid_data = true;
143dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
144dd64fc84SJeremy L Thompson 
145dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
146bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
147bd882c8aSJames Wright   switch (mem_type) {
148bd882c8aSJames Wright     case CEED_MEM_HOST:
149bd882c8aSJames Wright       *need_sync = has_valid_data && !impl->h_data;
150bd882c8aSJames Wright       break;
151bd882c8aSJames Wright     case CEED_MEM_DEVICE:
152bd882c8aSJames Wright       *need_sync = has_valid_data && !impl->d_data;
153bd882c8aSJames Wright       break;
154bd882c8aSJames Wright   }
155bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
156bd882c8aSJames Wright }
157bd882c8aSJames Wright 
158bd882c8aSJames Wright //------------------------------------------------------------------------------
159bd882c8aSJames Wright // Set data from host
160bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextSetDataHost_Sycl(const CeedQFunctionContext ctx,const CeedCopyMode copy_mode,void * data)161bd882c8aSJames Wright static int CeedQFunctionContextSetDataHost_Sycl(const CeedQFunctionContext ctx, const CeedCopyMode copy_mode, void *data) {
162bd882c8aSJames Wright   CeedQFunctionContext_Sycl *impl;
163bd882c8aSJames Wright 
164dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
165bd882c8aSJames Wright   CeedCallBackend(CeedFree(&impl->h_data_owned));
166bd882c8aSJames Wright   switch (copy_mode) {
167bd882c8aSJames Wright     case CEED_COPY_VALUES:
168dd64fc84SJeremy L Thompson       size_t ctx_size;
169dd64fc84SJeremy L Thompson 
170dd64fc84SJeremy L Thompson       CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
171dd64fc84SJeremy L Thompson       CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->h_data_owned));
172bd882c8aSJames Wright       impl->h_data_borrowed = NULL;
173bd882c8aSJames Wright       impl->h_data          = impl->h_data_owned;
174dd64fc84SJeremy L Thompson       memcpy(impl->h_data, data, ctx_size);
175bd882c8aSJames Wright       break;
176bd882c8aSJames Wright     case CEED_OWN_POINTER:
177bd882c8aSJames Wright       impl->h_data_owned    = data;
178bd882c8aSJames Wright       impl->h_data_borrowed = NULL;
179bd882c8aSJames Wright       impl->h_data          = data;
180bd882c8aSJames Wright       break;
181bd882c8aSJames Wright     case CEED_USE_POINTER:
182bd882c8aSJames Wright       impl->h_data_borrowed = data;
183bd882c8aSJames Wright       impl->h_data          = data;
184bd882c8aSJames Wright       break;
185bd882c8aSJames Wright   }
186bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
187bd882c8aSJames Wright }
188bd882c8aSJames Wright 
189bd882c8aSJames Wright //------------------------------------------------------------------------------
190bd882c8aSJames Wright // Set data from device
191bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextSetDataDevice_Sycl(const CeedQFunctionContext ctx,const CeedCopyMode copy_mode,void * data)192bd882c8aSJames Wright static int CeedQFunctionContextSetDataDevice_Sycl(const CeedQFunctionContext ctx, const CeedCopyMode copy_mode, void *data) {
193bd882c8aSJames Wright   Ceed                       ceed;
194bd882c8aSJames Wright   Ceed_Sycl                 *sycl_data;
195dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
196dd64fc84SJeremy L Thompson 
197dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
198dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
199bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &sycl_data));
200bd882c8aSJames Wright 
2011f4b1b45SUmesh Unnikrishnan   std::vector<sycl::event> e;
2021f4b1b45SUmesh Unnikrishnan 
2031f4b1b45SUmesh Unnikrishnan   if (!sycl_data->sycl_queue.is_in_order()) e = {sycl_data->sycl_queue.ext_oneapi_submit_barrier()};
204bd882c8aSJames Wright 
205bd882c8aSJames Wright   // Wait for all work to finish before freeing memory
206bd882c8aSJames Wright   if (impl->d_data_owned) {
207bd882c8aSJames Wright     CeedCallSycl(ceed, sycl_data->sycl_queue.wait_and_throw());
208bd882c8aSJames Wright     CeedCallSycl(ceed, sycl::free(impl->d_data_owned, sycl_data->sycl_context));
209bd882c8aSJames Wright     impl->d_data_owned = NULL;
210bd882c8aSJames Wright   }
211bd882c8aSJames Wright 
212bd882c8aSJames Wright   switch (copy_mode) {
213bd882c8aSJames Wright     case CEED_COPY_VALUES: {
214dd64fc84SJeremy L Thompson       size_t ctx_size;
215dd64fc84SJeremy L Thompson 
216dd64fc84SJeremy L Thompson       CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
217dd64fc84SJeremy L Thompson       CeedCallSycl(ceed, impl->d_data_owned = sycl::malloc_device(ctx_size, sycl_data->sycl_device, sycl_data->sycl_context));
218bd882c8aSJames Wright       impl->d_data_borrowed  = NULL;
219bd882c8aSJames Wright       impl->d_data           = impl->d_data_owned;
2201f4b1b45SUmesh Unnikrishnan       sycl::event copy_event = sycl_data->sycl_queue.memcpy(impl->d_data, data, ctx_size, e);
221bd882c8aSJames Wright       CeedCallSycl(ceed, copy_event.wait_and_throw());
222bd882c8aSJames Wright     } break;
223bd882c8aSJames Wright     case CEED_OWN_POINTER: {
224bd882c8aSJames Wright       impl->d_data_owned    = data;
225bd882c8aSJames Wright       impl->d_data_borrowed = NULL;
226bd882c8aSJames Wright       impl->d_data          = data;
227bd882c8aSJames Wright     } break;
228bd882c8aSJames Wright     case CEED_USE_POINTER: {
229bd882c8aSJames Wright       impl->d_data_owned    = NULL;
230bd882c8aSJames Wright       impl->d_data_borrowed = data;
231bd882c8aSJames Wright       impl->d_data          = data;
232bd882c8aSJames Wright     } break;
233bd882c8aSJames Wright   }
2349bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
235bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
236bd882c8aSJames Wright }
237bd882c8aSJames Wright 
238bd882c8aSJames Wright //------------------------------------------------------------------------------
239bd882c8aSJames Wright // Set the data used by a user context,
240bd882c8aSJames Wright //   freeing any previously allocated data if applicable
241bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextSetData_Sycl(const CeedQFunctionContext ctx,const CeedMemType mem_type,const CeedCopyMode copy_mode,void * data)242bd882c8aSJames Wright static int CeedQFunctionContextSetData_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, const CeedCopyMode copy_mode, void *data) {
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 //------------------------------------------------------------------------------
CeedQFunctionContextTakeData_Sycl(const CeedQFunctionContext ctx,const CeedMemType mem_type,void * data)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));
263bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &ceedSycl));
2649bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
2659bc66399SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
266bd882c8aSJames Wright 
2671f4b1b45SUmesh Unnikrishnan   // Order queue if needed
2681f4b1b45SUmesh Unnikrishnan   if (!ceedSycl->sycl_queue.is_in_order()) ceedSycl->sycl_queue.ext_oneapi_submit_barrier();
269bd882c8aSJames Wright 
270bd882c8aSJames Wright   // Sync data to requested mem_type
271bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextNeedSync_Sycl(ctx, mem_type, &need_sync));
272bd882c8aSJames Wright   if (need_sync) CeedCallBackend(CeedQFunctionContextSync_Sycl(ctx, mem_type));
273bd882c8aSJames Wright 
274bd882c8aSJames Wright   // Update pointer
275bd882c8aSJames Wright   switch (mem_type) {
276bd882c8aSJames Wright     case CEED_MEM_HOST:
277bd882c8aSJames Wright       *(void **)data        = impl->h_data_borrowed;
278bd882c8aSJames Wright       impl->h_data_borrowed = NULL;
279bd882c8aSJames Wright       impl->h_data          = NULL;
280bd882c8aSJames Wright       break;
281bd882c8aSJames Wright     case CEED_MEM_DEVICE:
282bd882c8aSJames Wright       *(void **)data        = impl->d_data_borrowed;
283bd882c8aSJames Wright       impl->d_data_borrowed = NULL;
284bd882c8aSJames Wright       impl->d_data          = NULL;
285bd882c8aSJames Wright       break;
286bd882c8aSJames Wright   }
287bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
288bd882c8aSJames Wright }
289bd882c8aSJames Wright 
290bd882c8aSJames Wright //------------------------------------------------------------------------------
291bd882c8aSJames Wright // Core logic for GetData.
292bd882c8aSJames Wright //   If a different memory type is most up to date, this will perform a copy
293bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextGetDataCore_Sycl(const CeedQFunctionContext ctx,const CeedMemType mem_type,void * data)294bd882c8aSJames Wright static int CeedQFunctionContextGetDataCore_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
295dd64fc84SJeremy L Thompson   bool                       need_sync = false;
296bd882c8aSJames Wright   CeedQFunctionContext_Sycl *impl;
297dd64fc84SJeremy L Thompson 
298bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
299bd882c8aSJames Wright 
300bd882c8aSJames Wright   // Sync data to requested mem_type
301bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextNeedSync_Sycl(ctx, mem_type, &need_sync));
302bd882c8aSJames Wright   if (need_sync) CeedCallBackend(CeedQFunctionContextSync_Sycl(ctx, mem_type));
303bd882c8aSJames Wright 
304bd882c8aSJames Wright   // Update pointer
305bd882c8aSJames Wright   switch (mem_type) {
306bd882c8aSJames Wright     case CEED_MEM_HOST:
307bd882c8aSJames Wright       *(void **)data = impl->h_data;
308bd882c8aSJames Wright       break;
309bd882c8aSJames Wright     case CEED_MEM_DEVICE:
310bd882c8aSJames Wright       *(void **)data = impl->d_data;
311bd882c8aSJames Wright       break;
312bd882c8aSJames Wright   }
313bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
314bd882c8aSJames Wright }
315bd882c8aSJames Wright 
316bd882c8aSJames Wright //------------------------------------------------------------------------------
317bd882c8aSJames Wright // Get read-only access to the data
318bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextGetDataRead_Sycl(const CeedQFunctionContext ctx,const CeedMemType mem_type,void * data)319bd882c8aSJames Wright static int CeedQFunctionContextGetDataRead_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
320bd882c8aSJames Wright   return CeedQFunctionContextGetDataCore_Sycl(ctx, mem_type, data);
321bd882c8aSJames Wright }
322bd882c8aSJames Wright 
323bd882c8aSJames Wright //------------------------------------------------------------------------------
324bd882c8aSJames Wright // Get read/write access to the data
325bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextGetData_Sycl(const CeedQFunctionContext ctx,const CeedMemType mem_type,void * data)326bd882c8aSJames Wright static int CeedQFunctionContextGetData_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
327dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
328bd882c8aSJames Wright 
329dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
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 //------------------------------------------------------------------------------
CeedQFunctionContextDestroy_Sycl(const CeedQFunctionContext ctx)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));
3609bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
361bd882c8aSJames Wright   CeedCallBackend(CeedFree(&impl->h_data_owned));
362bd882c8aSJames Wright   CeedCallBackend(CeedFree(&impl));
363bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
364bd882c8aSJames Wright }
365bd882c8aSJames Wright 
366bd882c8aSJames Wright //------------------------------------------------------------------------------
367bd882c8aSJames Wright // QFunctionContext Create
368bd882c8aSJames Wright //------------------------------------------------------------------------------
CeedQFunctionContextCreate_Sycl(CeedQFunctionContext ctx)369bd882c8aSJames Wright int CeedQFunctionContextCreate_Sycl(CeedQFunctionContext ctx) {
370bd882c8aSJames Wright   Ceed                       ceed;
371dd64fc84SJeremy L Thompson   CeedQFunctionContext_Sycl *impl;
372bd882c8aSJames Wright 
373dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
374bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Sycl));
375bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Sycl));
376bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Sycl));
377bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Sycl));
378bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Sycl));
379bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetDataRead_Sycl));
380bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Sycl));
3819bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
382bd882c8aSJames Wright   CeedCallBackend(CeedCalloc(1, &impl));
383bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl));
384bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
385bd882c8aSJames Wright }
386ff1e7120SSebastian Grimberg 
387bd882c8aSJames Wright //------------------------------------------------------------------------------
388