xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-qfunctioncontext.c (revision c85e864000754611328ed43fe87e29f99787d593)
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>
12*c85e8640SSebastian 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;
222b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
230d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
242b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
250d0321e0SJeremy L Thompson 
262b730f8bSJeremy L Thompson   if (!impl->h_data) {
270d0321e0SJeremy L Thompson     // LCOV_EXCL_START
282b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "No valid host data to sync to device");
290d0321e0SJeremy L Thompson     // LCOV_EXCL_STOP
302b730f8bSJeremy L Thompson   }
310d0321e0SJeremy L Thompson 
32539ec17dSJeremy L Thompson   size_t ctxsize;
332b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctxsize));
34539ec17dSJeremy L Thompson 
350d0321e0SJeremy L Thompson   if (impl->d_data_borrowed) {
360d0321e0SJeremy L Thompson     impl->d_data = impl->d_data_borrowed;
370d0321e0SJeremy L Thompson   } else if (impl->d_data_owned) {
380d0321e0SJeremy L Thompson     impl->d_data = impl->d_data_owned;
390d0321e0SJeremy L Thompson   } else {
402b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&impl->d_data_owned, ctxsize));
410d0321e0SJeremy L Thompson     impl->d_data = impl->d_data_owned;
420d0321e0SJeremy L Thompson   }
430d0321e0SJeremy L Thompson 
442b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->d_data, impl->h_data, ctxsize, hipMemcpyHostToDevice));
450d0321e0SJeremy L Thompson 
460d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
470d0321e0SJeremy L Thompson }
480d0321e0SJeremy L Thompson 
490d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
500d0321e0SJeremy L Thompson // Sync device to host
510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
522b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSyncD2H_Hip(const CeedQFunctionContext ctx) {
530d0321e0SJeremy L Thompson   Ceed ceed;
542b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
550d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
562b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
570d0321e0SJeremy L Thompson 
582b730f8bSJeremy L Thompson   if (!impl->d_data) {
590d0321e0SJeremy L Thompson     // LCOV_EXCL_START
602b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host");
610d0321e0SJeremy L Thompson     // LCOV_EXCL_STOP
622b730f8bSJeremy L Thompson   }
630d0321e0SJeremy L Thompson 
64539ec17dSJeremy L Thompson   size_t ctxsize;
652b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctxsize));
66539ec17dSJeremy L Thompson 
670d0321e0SJeremy L Thompson   if (impl->h_data_borrowed) {
680d0321e0SJeremy L Thompson     impl->h_data = impl->h_data_borrowed;
690d0321e0SJeremy L Thompson   } else if (impl->h_data_owned) {
700d0321e0SJeremy L Thompson     impl->h_data = impl->h_data_owned;
710d0321e0SJeremy L Thompson   } else {
722b730f8bSJeremy L Thompson     CeedCallBackend(CeedMallocArray(1, ctxsize, &impl->h_data_owned));
730d0321e0SJeremy L Thompson     impl->h_data = impl->h_data_owned;
740d0321e0SJeremy L Thompson   }
750d0321e0SJeremy L Thompson 
762b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(impl->h_data, impl->d_data, ctxsize, hipMemcpyDeviceToHost));
770d0321e0SJeremy L Thompson 
780d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
790d0321e0SJeremy L Thompson }
800d0321e0SJeremy L Thompson 
810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
820d0321e0SJeremy L Thompson // Sync data of type
830d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
842b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSync_Hip(const CeedQFunctionContext ctx, CeedMemType mem_type) {
8543c928f4SJeremy L Thompson   switch (mem_type) {
862b730f8bSJeremy L Thompson     case CEED_MEM_HOST:
872b730f8bSJeremy L Thompson       return CeedQFunctionContextSyncD2H_Hip(ctx);
882b730f8bSJeremy L Thompson     case CEED_MEM_DEVICE:
892b730f8bSJeremy L Thompson       return CeedQFunctionContextSyncH2D_Hip(ctx);
900d0321e0SJeremy L Thompson   }
910d0321e0SJeremy L Thompson   return CEED_ERROR_UNSUPPORTED;
920d0321e0SJeremy L Thompson }
930d0321e0SJeremy L Thompson 
940d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
950d0321e0SJeremy L Thompson // Set all pointers as invalid
960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
972b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSetAllInvalid_Hip(const CeedQFunctionContext ctx) {
980d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
992b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1000d0321e0SJeremy L Thompson 
1010d0321e0SJeremy L Thompson   impl->h_data = NULL;
1020d0321e0SJeremy L Thompson   impl->d_data = NULL;
1030d0321e0SJeremy L Thompson 
1040d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1050d0321e0SJeremy L Thompson }
1060d0321e0SJeremy L Thompson 
1070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1080d0321e0SJeremy L Thompson // Check for valid data
1090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1102b730f8bSJeremy L Thompson static inline int CeedQFunctionContextHasValidData_Hip(const CeedQFunctionContext ctx, bool *has_valid_data) {
1110d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
1122b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1130d0321e0SJeremy L Thompson 
1140d0321e0SJeremy L Thompson   *has_valid_data = !!impl->h_data || !!impl->d_data;
1150d0321e0SJeremy L Thompson 
1160d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1170d0321e0SJeremy L Thompson }
1180d0321e0SJeremy L Thompson 
1190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1200d0321e0SJeremy L Thompson // Check if ctx has borrowed data
1210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1222b730f8bSJeremy L Thompson static inline int CeedQFunctionContextHasBorrowedDataOfType_Hip(const CeedQFunctionContext ctx, CeedMemType mem_type,
1230d0321e0SJeremy L Thompson                                                                 bool *has_borrowed_data_of_type) {
1240d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
1252b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1260d0321e0SJeremy L Thompson 
12743c928f4SJeremy L Thompson   switch (mem_type) {
1280d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
1290d0321e0SJeremy L Thompson       *has_borrowed_data_of_type = !!impl->h_data_borrowed;
1300d0321e0SJeremy L Thompson       break;
1310d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
1320d0321e0SJeremy L Thompson       *has_borrowed_data_of_type = !!impl->d_data_borrowed;
1330d0321e0SJeremy L Thompson       break;
1340d0321e0SJeremy L Thompson   }
1350d0321e0SJeremy L Thompson 
1360d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1370d0321e0SJeremy L Thompson }
1380d0321e0SJeremy L Thompson 
1390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1400d0321e0SJeremy L Thompson // Check if data of given type needs sync
1410d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1422b730f8bSJeremy L Thompson static inline int CeedQFunctionContextNeedSync_Hip(const CeedQFunctionContext ctx, CeedMemType mem_type, bool *need_sync) {
1430d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
1442b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1450d0321e0SJeremy L Thompson 
1460d0321e0SJeremy L Thompson   bool has_valid_data = true;
1472b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextHasValidData_Hip(ctx, &has_valid_data));
14843c928f4SJeremy L Thompson   switch (mem_type) {
1490d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
1500d0321e0SJeremy L Thompson       *need_sync = has_valid_data && !impl->h_data;
1510d0321e0SJeremy L Thompson       break;
1520d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
1530d0321e0SJeremy L Thompson       *need_sync = has_valid_data && !impl->d_data;
1540d0321e0SJeremy L Thompson       break;
1550d0321e0SJeremy L Thompson   }
1560d0321e0SJeremy L Thompson 
1570d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1580d0321e0SJeremy L Thompson }
1590d0321e0SJeremy L Thompson 
1600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1610d0321e0SJeremy L Thompson // Set data from host
1620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1632b730f8bSJeremy L Thompson static int CeedQFunctionContextSetDataHost_Hip(const CeedQFunctionContext ctx, const CeedCopyMode copy_mode, void *data) {
1640d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
1652b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1660d0321e0SJeremy L Thompson 
1672b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_data_owned));
16843c928f4SJeremy L Thompson   switch (copy_mode) {
1690d0321e0SJeremy L Thompson     case CEED_COPY_VALUES: {
170539ec17dSJeremy L Thompson       size_t ctxsize;
1712b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctxsize));
1722b730f8bSJeremy L Thompson       CeedCallBackend(CeedMallocArray(1, ctxsize, &impl->h_data_owned));
1730d0321e0SJeremy L Thompson       impl->h_data_borrowed = NULL;
1740d0321e0SJeremy L Thompson       impl->h_data          = impl->h_data_owned;
175539ec17dSJeremy L Thompson       memcpy(impl->h_data, data, ctxsize);
1760d0321e0SJeremy L Thompson     } break;
1770d0321e0SJeremy L Thompson     case CEED_OWN_POINTER:
1780d0321e0SJeremy L Thompson       impl->h_data_owned    = data;
1790d0321e0SJeremy L Thompson       impl->h_data_borrowed = NULL;
1800d0321e0SJeremy L Thompson       impl->h_data          = data;
1810d0321e0SJeremy L Thompson       break;
1820d0321e0SJeremy L Thompson     case CEED_USE_POINTER:
1830d0321e0SJeremy L Thompson       impl->h_data_borrowed = data;
1840d0321e0SJeremy L Thompson       impl->h_data          = data;
1850d0321e0SJeremy L Thompson       break;
1860d0321e0SJeremy L Thompson   }
1870d0321e0SJeremy L Thompson 
1880d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1890d0321e0SJeremy L Thompson }
1900d0321e0SJeremy L Thompson 
1910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1920d0321e0SJeremy L Thompson // Set data from device
1930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1942b730f8bSJeremy L Thompson static int CeedQFunctionContextSetDataDevice_Hip(const CeedQFunctionContext ctx, const CeedCopyMode copy_mode, void *data) {
1950d0321e0SJeremy L Thompson   Ceed ceed;
1962b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
1970d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
1982b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1990d0321e0SJeremy L Thompson 
2002b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_data_owned));
2010d0321e0SJeremy L Thompson   impl->d_data_owned = NULL;
20243c928f4SJeremy L Thompson   switch (copy_mode) {
203539ec17dSJeremy L Thompson     case CEED_COPY_VALUES: {
204539ec17dSJeremy L Thompson       size_t ctxsize;
2052b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctxsize));
2062b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipMalloc((void **)&impl->d_data_owned, ctxsize));
2070d0321e0SJeremy L Thompson       impl->d_data_borrowed = NULL;
2080d0321e0SJeremy L Thompson       impl->d_data          = impl->d_data_owned;
2092b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(impl->d_data, data, ctxsize, hipMemcpyDeviceToDevice));
210539ec17dSJeremy L Thompson     } break;
2110d0321e0SJeremy L Thompson     case CEED_OWN_POINTER:
2120d0321e0SJeremy L Thompson       impl->d_data_owned    = data;
2130d0321e0SJeremy L Thompson       impl->d_data_borrowed = NULL;
2140d0321e0SJeremy L Thompson       impl->d_data          = data;
2150d0321e0SJeremy L Thompson       break;
2160d0321e0SJeremy L Thompson     case CEED_USE_POINTER:
2170d0321e0SJeremy L Thompson       impl->d_data_owned    = NULL;
2180d0321e0SJeremy L Thompson       impl->d_data_borrowed = data;
2190d0321e0SJeremy L Thompson       impl->d_data          = data;
2200d0321e0SJeremy L Thompson       break;
2210d0321e0SJeremy L Thompson   }
2220d0321e0SJeremy L Thompson 
2230d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2240d0321e0SJeremy L Thompson }
2250d0321e0SJeremy L Thompson 
2260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
227ea61e9acSJeremy L Thompson // Set the data used by a user context, freeing any previously allocated data if applicable
2280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2292b730f8bSJeremy L Thompson static int CeedQFunctionContextSetData_Hip(const CeedQFunctionContext ctx, const CeedMemType mem_type, const CeedCopyMode copy_mode, void *data) {
2300d0321e0SJeremy L Thompson   Ceed ceed;
2312b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
2320d0321e0SJeremy L Thompson 
2332b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSetAllInvalid_Hip(ctx));
23443c928f4SJeremy L Thompson   switch (mem_type) {
2350d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
23643c928f4SJeremy L Thompson       return CeedQFunctionContextSetDataHost_Hip(ctx, copy_mode, data);
2370d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
23843c928f4SJeremy L Thompson       return CeedQFunctionContextSetDataDevice_Hip(ctx, copy_mode, data);
2390d0321e0SJeremy L Thompson   }
2400d0321e0SJeremy L Thompson 
2410d0321e0SJeremy L Thompson   return CEED_ERROR_UNSUPPORTED;
2420d0321e0SJeremy L Thompson }
2430d0321e0SJeremy L Thompson 
2440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2450d0321e0SJeremy L Thompson // Take data
2460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2472b730f8bSJeremy L Thompson static int CeedQFunctionContextTakeData_Hip(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
2480d0321e0SJeremy L Thompson   Ceed ceed;
2492b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
2500d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
2512b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
2520d0321e0SJeremy L Thompson 
25343c928f4SJeremy L Thompson   // Sync data to requested mem_type
2540d0321e0SJeremy L Thompson   bool need_sync = false;
2552b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextNeedSync_Hip(ctx, mem_type, &need_sync));
2560d0321e0SJeremy L Thompson   if (need_sync) {
2572b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionContextSync_Hip(ctx, mem_type));
2580d0321e0SJeremy L Thompson   }
2590d0321e0SJeremy L Thompson 
2600d0321e0SJeremy L Thompson   // Update pointer
26143c928f4SJeremy L Thompson   switch (mem_type) {
2620d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
2630d0321e0SJeremy L Thompson       *(void **)data        = impl->h_data_borrowed;
2640d0321e0SJeremy L Thompson       impl->h_data_borrowed = NULL;
2650d0321e0SJeremy L Thompson       impl->h_data          = NULL;
2660d0321e0SJeremy L Thompson       break;
2670d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
2680d0321e0SJeremy L Thompson       *(void **)data        = impl->d_data_borrowed;
2690d0321e0SJeremy L Thompson       impl->d_data_borrowed = NULL;
2700d0321e0SJeremy L Thompson       impl->d_data          = NULL;
2710d0321e0SJeremy L Thompson       break;
2720d0321e0SJeremy L Thompson   }
2730d0321e0SJeremy L Thompson 
2740d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2750d0321e0SJeremy L Thompson }
2760d0321e0SJeremy L Thompson 
2770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
27828bfd0b7SJeremy L Thompson // Core logic for GetData.
27928bfd0b7SJeremy L Thompson //   If a different memory type is most up to date, this will perform a copy
2800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2812b730f8bSJeremy L Thompson static int CeedQFunctionContextGetDataCore_Hip(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
2820d0321e0SJeremy L Thompson   Ceed ceed;
2832b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
2840d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
2852b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
2860d0321e0SJeremy L Thompson 
28743c928f4SJeremy L Thompson   // Sync data to requested mem_type
2880d0321e0SJeremy L Thompson   bool need_sync = false;
2892b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextNeedSync_Hip(ctx, mem_type, &need_sync));
2902b730f8bSJeremy L Thompson   if (need_sync) CeedCallBackend(CeedQFunctionContextSync_Hip(ctx, mem_type));
2910d0321e0SJeremy L Thompson 
29243c928f4SJeremy L Thompson   // Sync data to requested mem_type and update pointer
29343c928f4SJeremy L Thompson   switch (mem_type) {
2940d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
2950d0321e0SJeremy L Thompson       *(void **)data = impl->h_data;
2960d0321e0SJeremy L Thompson       break;
2970d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
2980d0321e0SJeremy L Thompson       *(void **)data = impl->d_data;
2990d0321e0SJeremy L Thompson       break;
3000d0321e0SJeremy L Thompson   }
3010d0321e0SJeremy L Thompson 
30228bfd0b7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
30328bfd0b7SJeremy L Thompson }
30428bfd0b7SJeremy L Thompson 
30528bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
30628bfd0b7SJeremy L Thompson // Get read-only access to the data
30728bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
3082b730f8bSJeremy L Thompson static int CeedQFunctionContextGetDataRead_Hip(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
30928bfd0b7SJeremy L Thompson   return CeedQFunctionContextGetDataCore_Hip(ctx, mem_type, data);
31028bfd0b7SJeremy L Thompson }
31128bfd0b7SJeremy L Thompson 
31228bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
31328bfd0b7SJeremy L Thompson // Get read/write access to the data
31428bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
3152b730f8bSJeremy L Thompson static int CeedQFunctionContextGetData_Hip(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
31628bfd0b7SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
3172b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
31828bfd0b7SJeremy L Thompson 
3192b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetDataCore_Hip(ctx, mem_type, data));
32028bfd0b7SJeremy L Thompson 
3210d0321e0SJeremy L Thompson   // Mark only pointer for requested memory as valid
3222b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSetAllInvalid_Hip(ctx));
32343c928f4SJeremy L Thompson   switch (mem_type) {
3240d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
3250d0321e0SJeremy L Thompson       impl->h_data = *(void **)data;
3260d0321e0SJeremy L Thompson       break;
3270d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
3280d0321e0SJeremy L Thompson       impl->d_data = *(void **)data;
3290d0321e0SJeremy L Thompson       break;
3300d0321e0SJeremy L Thompson   }
3310d0321e0SJeremy L Thompson 
3320d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3330d0321e0SJeremy L Thompson }
3340d0321e0SJeremy L Thompson 
3350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3360d0321e0SJeremy L Thompson // Destroy the user context
3370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3380d0321e0SJeremy L Thompson static int CeedQFunctionContextDestroy_Hip(const CeedQFunctionContext ctx) {
3390d0321e0SJeremy L Thompson   Ceed ceed;
3402b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
3410d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
3422b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
3430d0321e0SJeremy L Thompson 
3442b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(impl->d_data_owned));
3452b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_data_owned));
3462b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
3470d0321e0SJeremy L Thompson 
3480d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3490d0321e0SJeremy L Thompson }
3500d0321e0SJeremy L Thompson 
3510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3520d0321e0SJeremy L Thompson // QFunctionContext Create
3530d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3540d0321e0SJeremy L Thompson int CeedQFunctionContextCreate_Hip(CeedQFunctionContext ctx) {
3550d0321e0SJeremy L Thompson   CeedQFunctionContext_Hip *impl;
3560d0321e0SJeremy L Thompson   Ceed                      ceed;
3572b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
3580d0321e0SJeremy L Thompson 
3592b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Hip));
3602b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Hip));
3612b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Hip));
3622b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Hip));
3632b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Hip));
3642b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetDataRead_Hip));
3652b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Hip));
3660d0321e0SJeremy L Thompson 
3672b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
3682b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl));
3690d0321e0SJeremy L Thompson 
3700d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3710d0321e0SJeremy L Thompson }
3722a86cc9dSSebastian Grimberg 
3730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
374