xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-qfunctioncontext.c (revision b7453713e95c1c6eb59ce174cbcb87227e92884e)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
849aac155SJeremy L Thompson #include <ceed.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
1049aac155SJeremy L Thompson #include <stdbool.h>
110d0321e0SJeremy L Thompson #include <string.h>
12c85e8640SSebastian Grimberg #include <hip/hip_runtime.h>
132b730f8bSJeremy L Thompson 
1449aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
150d0321e0SJeremy L Thompson #include "ceed-hip-ref.h"
160d0321e0SJeremy L Thompson 
170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
180d0321e0SJeremy L Thompson // Sync host to device
190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
202b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSyncH2D_Hip(const CeedQFunctionContext ctx) {
210d0321e0SJeremy L Thompson   Ceed                      ceed;
22*b7453713SJeremy L Thompson   size_t                    ctx_size;
230d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
24*b7453713SJeremy L Thompson 
25*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
270d0321e0SJeremy L Thompson 
286574a04fSJeremy L Thompson   CeedCheck(impl->h_data, ceed, CEED_ERROR_BACKEND, "No valid host data to sync to device");
290d0321e0SJeremy L Thompson 
30*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
310d0321e0SJeremy L Thompson   if (impl->d_data_borrowed) {
320d0321e0SJeremy L Thompson     impl->d_data = impl->d_data_borrowed;
330d0321e0SJeremy L Thompson   } else if (impl->d_data_owned) {
340d0321e0SJeremy L Thompson     impl->d_data = impl->d_data_owned;
350d0321e0SJeremy L Thompson   } else {
36*b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&impl->d_data_owned, ctx_size));
370d0321e0SJeremy L Thompson     impl->d_data = impl->d_data_owned;
380d0321e0SJeremy L Thompson   }
39*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_data, impl->h_data, ctx_size, hipMemcpyHostToDevice));
400d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
410d0321e0SJeremy L Thompson }
420d0321e0SJeremy L Thompson 
430d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
440d0321e0SJeremy L Thompson // Sync device to host
450d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
462b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSyncD2H_Hip(const CeedQFunctionContext ctx) {
470d0321e0SJeremy L Thompson   Ceed                      ceed;
48*b7453713SJeremy L Thompson   size_t                    ctx_size;
490d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
50*b7453713SJeremy L Thompson 
51*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
522b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
530d0321e0SJeremy L Thompson 
546574a04fSJeremy L Thompson   CeedCheck(impl->d_data, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host");
550d0321e0SJeremy L Thompson 
56*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
570d0321e0SJeremy L Thompson   if (impl->h_data_borrowed) {
580d0321e0SJeremy L Thompson     impl->h_data = impl->h_data_borrowed;
590d0321e0SJeremy L Thompson   } else if (impl->h_data_owned) {
600d0321e0SJeremy L Thompson     impl->h_data = impl->h_data_owned;
610d0321e0SJeremy L Thompson   } else {
62*b7453713SJeremy L Thompson     CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->h_data_owned));
630d0321e0SJeremy L Thompson     impl->h_data = impl->h_data_owned;
640d0321e0SJeremy L Thompson   }
65*b7453713SJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->h_data, impl->d_data, ctx_size, hipMemcpyDeviceToHost));
660d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
670d0321e0SJeremy L Thompson }
680d0321e0SJeremy L Thompson 
690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
700d0321e0SJeremy L Thompson // Sync data of type
710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
722b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSync_Hip(const CeedQFunctionContext ctx, CeedMemType mem_type) {
7343c928f4SJeremy L Thompson   switch (mem_type) {
742b730f8bSJeremy L Thompson     case CEED_MEM_HOST:
752b730f8bSJeremy L Thompson       return CeedQFunctionContextSyncD2H_Hip(ctx);
762b730f8bSJeremy L Thompson     case CEED_MEM_DEVICE:
772b730f8bSJeremy L Thompson       return CeedQFunctionContextSyncH2D_Hip(ctx);
780d0321e0SJeremy L Thompson   }
790d0321e0SJeremy L Thompson   return CEED_ERROR_UNSUPPORTED;
800d0321e0SJeremy L Thompson }
810d0321e0SJeremy L Thompson 
820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
830d0321e0SJeremy L Thompson // Set all pointers as invalid
840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
852b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSetAllInvalid_Hip(const CeedQFunctionContext ctx) {
860d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
870d0321e0SJeremy L Thompson 
88*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
890d0321e0SJeremy L Thompson   impl->h_data = NULL;
900d0321e0SJeremy L Thompson   impl->d_data = NULL;
910d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
920d0321e0SJeremy L Thompson }
930d0321e0SJeremy L Thompson 
940d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
950d0321e0SJeremy L Thompson // Check for valid data
960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
972b730f8bSJeremy L Thompson static inline int CeedQFunctionContextHasValidData_Hip(const CeedQFunctionContext ctx, bool *has_valid_data) {
980d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
99*b7453713SJeremy L Thompson 
1002b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1011c66c397SJeremy L Thompson   *has_valid_data = impl && (impl->h_data || impl->d_data);
1020d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1030d0321e0SJeremy L Thompson }
1040d0321e0SJeremy L Thompson 
1050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1060d0321e0SJeremy L Thompson // Check if ctx has borrowed data
1070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1082b730f8bSJeremy L Thompson static inline int CeedQFunctionContextHasBorrowedDataOfType_Hip(const CeedQFunctionContext ctx, CeedMemType mem_type,
1090d0321e0SJeremy L Thompson                                                                 bool *has_borrowed_data_of_type) {
1100d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
1110d0321e0SJeremy L Thompson 
112*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
11343c928f4SJeremy L Thompson   switch (mem_type) {
1140d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
1151c66c397SJeremy L Thompson       *has_borrowed_data_of_type = impl->h_data_borrowed;
1160d0321e0SJeremy L Thompson       break;
1170d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
1181c66c397SJeremy L Thompson       *has_borrowed_data_of_type = impl->d_data_borrowed;
1190d0321e0SJeremy L Thompson       break;
1200d0321e0SJeremy L Thompson   }
1210d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1220d0321e0SJeremy L Thompson }
1230d0321e0SJeremy L Thompson 
1240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1250d0321e0SJeremy L Thompson // Check if data of given type needs sync
1260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1272b730f8bSJeremy L Thompson static inline int CeedQFunctionContextNeedSync_Hip(const CeedQFunctionContext ctx, CeedMemType mem_type, bool *need_sync) {
1280d0321e0SJeremy L Thompson   bool                      has_valid_data = true;
129*b7453713SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
130*b7453713SJeremy L Thompson 
131*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1322b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextHasValidData_Hip(ctx, &has_valid_data));
13343c928f4SJeremy L Thompson   switch (mem_type) {
1340d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
1350d0321e0SJeremy L Thompson       *need_sync = has_valid_data && !impl->h_data;
1360d0321e0SJeremy L Thompson       break;
1370d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
1380d0321e0SJeremy L Thompson       *need_sync = has_valid_data && !impl->d_data;
1390d0321e0SJeremy L Thompson       break;
1400d0321e0SJeremy L Thompson   }
1410d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1420d0321e0SJeremy L Thompson }
1430d0321e0SJeremy L Thompson 
1440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1450d0321e0SJeremy L Thompson // Set data from host
1460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1472b730f8bSJeremy L Thompson static int CeedQFunctionContextSetDataHost_Hip(const CeedQFunctionContext ctx, const CeedCopyMode copy_mode, void *data) {
1480d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
1490d0321e0SJeremy L Thompson 
150*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1512b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_data_owned));
15243c928f4SJeremy L Thompson   switch (copy_mode) {
1530d0321e0SJeremy L Thompson     case CEED_COPY_VALUES: {
154*b7453713SJeremy L Thompson       size_t ctx_size;
155*b7453713SJeremy L Thompson 
156*b7453713SJeremy L Thompson       CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
157*b7453713SJeremy L Thompson       CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->h_data_owned));
1580d0321e0SJeremy L Thompson       impl->h_data_borrowed = NULL;
1590d0321e0SJeremy L Thompson       impl->h_data          = impl->h_data_owned;
160*b7453713SJeremy L Thompson       memcpy(impl->h_data, data, ctx_size);
1610d0321e0SJeremy L Thompson     } break;
1620d0321e0SJeremy L Thompson     case CEED_OWN_POINTER:
1630d0321e0SJeremy L Thompson       impl->h_data_owned    = data;
1640d0321e0SJeremy L Thompson       impl->h_data_borrowed = NULL;
1650d0321e0SJeremy L Thompson       impl->h_data          = data;
1660d0321e0SJeremy L Thompson       break;
1670d0321e0SJeremy L Thompson     case CEED_USE_POINTER:
1680d0321e0SJeremy L Thompson       impl->h_data_borrowed = data;
1690d0321e0SJeremy L Thompson       impl->h_data          = data;
1700d0321e0SJeremy L Thompson       break;
1710d0321e0SJeremy L Thompson   }
1720d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1730d0321e0SJeremy L Thompson }
1740d0321e0SJeremy L Thompson 
1750d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1760d0321e0SJeremy L Thompson // Set data from device
1770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1782b730f8bSJeremy L Thompson static int CeedQFunctionContextSetDataDevice_Hip(const CeedQFunctionContext ctx, const CeedCopyMode copy_mode, void *data) {
1790d0321e0SJeremy L Thompson   Ceed                      ceed;
1800d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
181*b7453713SJeremy L Thompson 
182*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
1832b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1840d0321e0SJeremy L Thompson 
1852b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_data_owned));
1860d0321e0SJeremy L Thompson   impl->d_data_owned = NULL;
18743c928f4SJeremy L Thompson   switch (copy_mode) {
188539ec17dSJeremy L Thompson     case CEED_COPY_VALUES: {
189*b7453713SJeremy L Thompson       size_t ctx_size;
190*b7453713SJeremy L Thompson       CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
191*b7453713SJeremy L Thompson       CeedCallHip(ceed, hipMalloc((void **)&impl->d_data_owned, ctx_size));
1920d0321e0SJeremy L Thompson       impl->d_data_borrowed = NULL;
1930d0321e0SJeremy L Thompson       impl->d_data          = impl->d_data_owned;
194*b7453713SJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(impl->d_data, data, ctx_size, hipMemcpyDeviceToDevice));
195539ec17dSJeremy L Thompson     } break;
1960d0321e0SJeremy L Thompson     case CEED_OWN_POINTER:
1970d0321e0SJeremy L Thompson       impl->d_data_owned    = data;
1980d0321e0SJeremy L Thompson       impl->d_data_borrowed = NULL;
1990d0321e0SJeremy L Thompson       impl->d_data          = data;
2000d0321e0SJeremy L Thompson       break;
2010d0321e0SJeremy L Thompson     case CEED_USE_POINTER:
2020d0321e0SJeremy L Thompson       impl->d_data_owned    = NULL;
2030d0321e0SJeremy L Thompson       impl->d_data_borrowed = data;
2040d0321e0SJeremy L Thompson       impl->d_data          = data;
2050d0321e0SJeremy L Thompson       break;
2060d0321e0SJeremy L Thompson   }
2070d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2080d0321e0SJeremy L Thompson }
2090d0321e0SJeremy L Thompson 
2100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
211b2165e7aSSebastian Grimberg // Set the data used by a user context,
212b2165e7aSSebastian Grimberg //    freeing any previously allocated data if applicable
2130d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2142b730f8bSJeremy L Thompson static int CeedQFunctionContextSetData_Hip(const CeedQFunctionContext ctx, const CeedMemType mem_type, const CeedCopyMode copy_mode, void *data) {
2150d0321e0SJeremy L Thompson   Ceed ceed;
2160d0321e0SJeremy L Thompson 
217*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
2182b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSetAllInvalid_Hip(ctx));
21943c928f4SJeremy L Thompson   switch (mem_type) {
2200d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
22143c928f4SJeremy L Thompson       return CeedQFunctionContextSetDataHost_Hip(ctx, copy_mode, data);
2220d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
22343c928f4SJeremy L Thompson       return CeedQFunctionContextSetDataDevice_Hip(ctx, copy_mode, data);
2240d0321e0SJeremy L Thompson   }
2250d0321e0SJeremy L Thompson   return CEED_ERROR_UNSUPPORTED;
2260d0321e0SJeremy L Thompson }
2270d0321e0SJeremy L Thompson 
2280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2290d0321e0SJeremy L Thompson // Take data
2300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2312b730f8bSJeremy L Thompson static int CeedQFunctionContextTakeData_Hip(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
232*b7453713SJeremy L Thompson   bool                      need_sync = false;
2330d0321e0SJeremy L Thompson   Ceed                      ceed;
2340d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
235*b7453713SJeremy L Thompson 
236*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
2372b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
2380d0321e0SJeremy L Thompson 
23943c928f4SJeremy L Thompson   // Sync data to requested mem_type
2402b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextNeedSync_Hip(ctx, mem_type, &need_sync));
241b2165e7aSSebastian Grimberg   if (need_sync) CeedCallBackend(CeedQFunctionContextSync_Hip(ctx, mem_type));
2420d0321e0SJeremy L Thompson 
2430d0321e0SJeremy L Thompson   // Update pointer
24443c928f4SJeremy L Thompson   switch (mem_type) {
2450d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
2460d0321e0SJeremy L Thompson       *(void **)data        = impl->h_data_borrowed;
2470d0321e0SJeremy L Thompson       impl->h_data_borrowed = NULL;
2480d0321e0SJeremy L Thompson       impl->h_data          = NULL;
2490d0321e0SJeremy L Thompson       break;
2500d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
2510d0321e0SJeremy L Thompson       *(void **)data        = impl->d_data_borrowed;
2520d0321e0SJeremy L Thompson       impl->d_data_borrowed = NULL;
2530d0321e0SJeremy L Thompson       impl->d_data          = NULL;
2540d0321e0SJeremy L Thompson       break;
2550d0321e0SJeremy L Thompson   }
2560d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2570d0321e0SJeremy L Thompson }
2580d0321e0SJeremy L Thompson 
2590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
26028bfd0b7SJeremy L Thompson // Core logic for GetData.
26128bfd0b7SJeremy L Thompson //   If a different memory type is most up to date, this will perform a copy
2620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2632b730f8bSJeremy L Thompson static int CeedQFunctionContextGetDataCore_Hip(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
264*b7453713SJeremy L Thompson   bool                      need_sync = false;
2650d0321e0SJeremy L Thompson   Ceed                      ceed;
2660d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
267*b7453713SJeremy L Thompson 
268*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
2692b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
2700d0321e0SJeremy L Thompson 
27143c928f4SJeremy L Thompson   // Sync data to requested mem_type
2722b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextNeedSync_Hip(ctx, mem_type, &need_sync));
2732b730f8bSJeremy L Thompson   if (need_sync) CeedCallBackend(CeedQFunctionContextSync_Hip(ctx, mem_type));
2740d0321e0SJeremy L Thompson 
275b2165e7aSSebastian Grimberg   // Update pointer
27643c928f4SJeremy L Thompson   switch (mem_type) {
2770d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
2780d0321e0SJeremy L Thompson       *(void **)data = impl->h_data;
2790d0321e0SJeremy L Thompson       break;
2800d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
2810d0321e0SJeremy L Thompson       *(void **)data = impl->d_data;
2820d0321e0SJeremy L Thompson       break;
2830d0321e0SJeremy L Thompson   }
28428bfd0b7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
28528bfd0b7SJeremy L Thompson }
28628bfd0b7SJeremy L Thompson 
28728bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
28828bfd0b7SJeremy L Thompson // Get read-only access to the data
28928bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
2902b730f8bSJeremy L Thompson static int CeedQFunctionContextGetDataRead_Hip(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
29128bfd0b7SJeremy L Thompson   return CeedQFunctionContextGetDataCore_Hip(ctx, mem_type, data);
29228bfd0b7SJeremy L Thompson }
29328bfd0b7SJeremy L Thompson 
29428bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
29528bfd0b7SJeremy L Thompson // Get read/write access to the data
29628bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
2972b730f8bSJeremy L Thompson static int CeedQFunctionContextGetData_Hip(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
29828bfd0b7SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
29928bfd0b7SJeremy L Thompson 
300*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
3012b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetDataCore_Hip(ctx, mem_type, data));
30228bfd0b7SJeremy L Thompson 
3030d0321e0SJeremy L Thompson   // Mark only pointer for requested memory as valid
3042b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSetAllInvalid_Hip(ctx));
30543c928f4SJeremy L Thompson   switch (mem_type) {
3060d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
3070d0321e0SJeremy L Thompson       impl->h_data = *(void **)data;
3080d0321e0SJeremy L Thompson       break;
3090d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
3100d0321e0SJeremy L Thompson       impl->d_data = *(void **)data;
3110d0321e0SJeremy L Thompson       break;
3120d0321e0SJeremy L Thompson   }
3130d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3140d0321e0SJeremy L Thompson }
3150d0321e0SJeremy L Thompson 
3160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3170d0321e0SJeremy L Thompson // Destroy the user context
3180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3190d0321e0SJeremy L Thompson static int CeedQFunctionContextDestroy_Hip(const CeedQFunctionContext ctx) {
3200d0321e0SJeremy L Thompson   Ceed                      ceed;
3210d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
3220d0321e0SJeremy L Thompson 
323*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
324*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
3252b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_data_owned));
3262b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_data_owned));
3272b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
3280d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3290d0321e0SJeremy L Thompson }
3300d0321e0SJeremy L Thompson 
3310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3320d0321e0SJeremy L Thompson // QFunctionContext Create
3330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3340d0321e0SJeremy L Thompson int CeedQFunctionContextCreate_Hip(CeedQFunctionContext ctx) {
3350d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
3360d0321e0SJeremy L Thompson   Ceed                      ceed;
3370d0321e0SJeremy L Thompson 
338*b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
3392b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Hip));
3402b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Hip));
3412b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Hip));
3422b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Hip));
3432b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Hip));
3442b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetDataRead_Hip));
3452b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Hip));
3462b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
3472b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl));
3480d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3490d0321e0SJeremy L Thompson }
3502a86cc9dSSebastian Grimberg 
3510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
352