1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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>
100d0321e0SJeremy L Thompson #include <cuda_runtime.h>
1149aac155SJeremy L Thompson #include <stdbool.h>
120d0321e0SJeremy L Thompson #include <string.h>
132b730f8bSJeremy L Thompson
1449aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h"
150d0321e0SJeremy L Thompson #include "ceed-cuda-ref.h"
160d0321e0SJeremy L Thompson
170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
180d0321e0SJeremy L Thompson // Sync host to device
190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextSyncH2D_Cuda(const CeedQFunctionContext ctx)202b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSyncH2D_Cuda(const CeedQFunctionContext ctx) {
210d0321e0SJeremy L Thompson Ceed ceed;
22ca735530SJeremy L Thompson size_t ctx_size;
230d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
24ca735530SJeremy L Thompson
25ca735530SJeremy 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
30ca735530SJeremy 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 {
36ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_data_owned, ctx_size));
370d0321e0SJeremy L Thompson impl->d_data = impl->d_data_owned;
380d0321e0SJeremy L Thompson }
39ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(impl->d_data, impl->h_data, ctx_size, cudaMemcpyHostToDevice));
409bc66399SJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed));
410d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
420d0321e0SJeremy L Thompson }
430d0321e0SJeremy L Thompson
440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
450d0321e0SJeremy L Thompson // Sync device to host
460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextSyncD2H_Cuda(const CeedQFunctionContext ctx)472b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSyncD2H_Cuda(const CeedQFunctionContext ctx) {
480d0321e0SJeremy L Thompson Ceed ceed;
49ca735530SJeremy L Thompson size_t ctx_size;
500d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
51ca735530SJeremy L Thompson
52ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
532b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
540d0321e0SJeremy L Thompson
556574a04fSJeremy L Thompson CeedCheck(impl->d_data, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host");
560d0321e0SJeremy L Thompson
57ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
58539ec17dSJeremy L Thompson
590d0321e0SJeremy L Thompson if (impl->h_data_borrowed) {
600d0321e0SJeremy L Thompson impl->h_data = impl->h_data_borrowed;
610d0321e0SJeremy L Thompson } else if (impl->h_data_owned) {
620d0321e0SJeremy L Thompson impl->h_data = impl->h_data_owned;
630d0321e0SJeremy L Thompson } else {
64ca735530SJeremy L Thompson CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->h_data_owned));
650d0321e0SJeremy L Thompson impl->h_data = impl->h_data_owned;
660d0321e0SJeremy L Thompson }
67ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(impl->h_data, impl->d_data, ctx_size, cudaMemcpyDeviceToHost));
689bc66399SJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed));
690d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
700d0321e0SJeremy L Thompson }
710d0321e0SJeremy L Thompson
720d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
730d0321e0SJeremy L Thompson // Sync data of type
740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextSync_Cuda(const CeedQFunctionContext ctx,CeedMemType mem_type)752b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSync_Cuda(const CeedQFunctionContext ctx, CeedMemType mem_type) {
7643c928f4SJeremy L Thompson switch (mem_type) {
772b730f8bSJeremy L Thompson case CEED_MEM_HOST:
782b730f8bSJeremy L Thompson return CeedQFunctionContextSyncD2H_Cuda(ctx);
792b730f8bSJeremy L Thompson case CEED_MEM_DEVICE:
802b730f8bSJeremy L Thompson return CeedQFunctionContextSyncH2D_Cuda(ctx);
810d0321e0SJeremy L Thompson }
820d0321e0SJeremy L Thompson return CEED_ERROR_UNSUPPORTED;
830d0321e0SJeremy L Thompson }
840d0321e0SJeremy L Thompson
850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
860d0321e0SJeremy L Thompson // Set all pointers as invalid
870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextSetAllInvalid_Cuda(const CeedQFunctionContext ctx)882b730f8bSJeremy L Thompson static inline int CeedQFunctionContextSetAllInvalid_Cuda(const CeedQFunctionContext ctx) {
890d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
900d0321e0SJeremy L Thompson
91ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
920d0321e0SJeremy L Thompson impl->h_data = NULL;
930d0321e0SJeremy L Thompson impl->d_data = NULL;
940d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
950d0321e0SJeremy L Thompson }
960d0321e0SJeremy L Thompson
970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
980d0321e0SJeremy L Thompson // Check if ctx has valid data
990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextHasValidData_Cuda(const CeedQFunctionContext ctx,bool * has_valid_data)1002b730f8bSJeremy L Thompson static inline int CeedQFunctionContextHasValidData_Cuda(const CeedQFunctionContext ctx, bool *has_valid_data) {
1010d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
102ca735530SJeremy L Thompson
1032b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1041c66c397SJeremy L Thompson *has_valid_data = impl && (impl->h_data || impl->d_data);
1050d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
1060d0321e0SJeremy L Thompson }
1070d0321e0SJeremy L Thompson
1080d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1090d0321e0SJeremy L Thompson // Check if ctx has borrowed data
1100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextHasBorrowedDataOfType_Cuda(const CeedQFunctionContext ctx,CeedMemType mem_type,bool * has_borrowed_data_of_type)1112b730f8bSJeremy L Thompson static inline int CeedQFunctionContextHasBorrowedDataOfType_Cuda(const CeedQFunctionContext ctx, CeedMemType mem_type,
1120d0321e0SJeremy L Thompson bool *has_borrowed_data_of_type) {
1130d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
1140d0321e0SJeremy L Thompson
115ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
11643c928f4SJeremy L Thompson switch (mem_type) {
1170d0321e0SJeremy L Thompson case CEED_MEM_HOST:
1181c66c397SJeremy L Thompson *has_borrowed_data_of_type = impl->h_data_borrowed;
1190d0321e0SJeremy L Thompson break;
1200d0321e0SJeremy L Thompson case CEED_MEM_DEVICE:
1211c66c397SJeremy L Thompson *has_borrowed_data_of_type = impl->d_data_borrowed;
1220d0321e0SJeremy L Thompson break;
1230d0321e0SJeremy L Thompson }
1240d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
1250d0321e0SJeremy L Thompson }
1260d0321e0SJeremy L Thompson
1270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1280d0321e0SJeremy L Thompson // Check if data of given type needs sync
1290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextNeedSync_Cuda(const CeedQFunctionContext ctx,CeedMemType mem_type,bool * need_sync)1302b730f8bSJeremy L Thompson static inline int CeedQFunctionContextNeedSync_Cuda(const CeedQFunctionContext ctx, CeedMemType mem_type, bool *need_sync) {
1310d0321e0SJeremy L Thompson bool has_valid_data = true;
132ca735530SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
133ca735530SJeremy L Thompson
134ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1352b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
13643c928f4SJeremy L Thompson switch (mem_type) {
1370d0321e0SJeremy L Thompson case CEED_MEM_HOST:
1380d0321e0SJeremy L Thompson *need_sync = has_valid_data && !impl->h_data;
1390d0321e0SJeremy L Thompson break;
1400d0321e0SJeremy L Thompson case CEED_MEM_DEVICE:
1410d0321e0SJeremy L Thompson *need_sync = has_valid_data && !impl->d_data;
1420d0321e0SJeremy L Thompson break;
1430d0321e0SJeremy L Thompson }
1440d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
1450d0321e0SJeremy L Thompson }
1460d0321e0SJeremy L Thompson
1470d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1480d0321e0SJeremy L Thompson // Set data from host
1490d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextSetDataHost_Cuda(const CeedQFunctionContext ctx,const CeedCopyMode copy_mode,void * data)1502b730f8bSJeremy L Thompson static int CeedQFunctionContextSetDataHost_Cuda(const CeedQFunctionContext ctx, const CeedCopyMode copy_mode, void *data) {
1510d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
152ca735530SJeremy L Thompson
1532b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1540d0321e0SJeremy L Thompson
1552b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->h_data_owned));
15643c928f4SJeremy L Thompson switch (copy_mode) {
1570d0321e0SJeremy L Thompson case CEED_COPY_VALUES: {
158ca735530SJeremy L Thompson size_t ctx_size;
159ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
160ca735530SJeremy L Thompson CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->h_data_owned));
1610d0321e0SJeremy L Thompson impl->h_data_borrowed = NULL;
1620d0321e0SJeremy L Thompson impl->h_data = impl->h_data_owned;
163ca735530SJeremy L Thompson memcpy(impl->h_data, data, ctx_size);
1640d0321e0SJeremy L Thompson } break;
1650d0321e0SJeremy L Thompson case CEED_OWN_POINTER:
1660d0321e0SJeremy L Thompson impl->h_data_owned = data;
1670d0321e0SJeremy L Thompson impl->h_data_borrowed = NULL;
1680d0321e0SJeremy L Thompson impl->h_data = data;
1690d0321e0SJeremy L Thompson break;
1700d0321e0SJeremy L Thompson case CEED_USE_POINTER:
1710d0321e0SJeremy L Thompson impl->h_data_borrowed = data;
1720d0321e0SJeremy L Thompson impl->h_data = data;
1730d0321e0SJeremy L Thompson break;
1740d0321e0SJeremy L Thompson }
1750d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
1760d0321e0SJeremy L Thompson }
1770d0321e0SJeremy L Thompson
1780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1790d0321e0SJeremy L Thompson // Set data from device
1800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextSetDataDevice_Cuda(const CeedQFunctionContext ctx,const CeedCopyMode copy_mode,void * data)1812b730f8bSJeremy L Thompson static int CeedQFunctionContextSetDataDevice_Cuda(const CeedQFunctionContext ctx, const CeedCopyMode copy_mode, void *data) {
1820d0321e0SJeremy L Thompson Ceed ceed;
1830d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
184ca735530SJeremy L Thompson
185ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
1862b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1870d0321e0SJeremy L Thompson
1882b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(impl->d_data_owned));
1890d0321e0SJeremy L Thompson impl->d_data_owned = NULL;
19043c928f4SJeremy L Thompson switch (copy_mode) {
191539ec17dSJeremy L Thompson case CEED_COPY_VALUES: {
192ca735530SJeremy L Thompson size_t ctx_size;
193ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
194ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_data_owned, ctx_size));
1950d0321e0SJeremy L Thompson impl->d_data_borrowed = NULL;
1960d0321e0SJeremy L Thompson impl->d_data = impl->d_data_owned;
197ca735530SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(impl->d_data, data, ctx_size, cudaMemcpyDeviceToDevice));
198539ec17dSJeremy L Thompson } break;
1990d0321e0SJeremy L Thompson case CEED_OWN_POINTER:
2000d0321e0SJeremy L Thompson impl->d_data_owned = data;
2010d0321e0SJeremy L Thompson impl->d_data_borrowed = NULL;
2020d0321e0SJeremy L Thompson impl->d_data = data;
2030d0321e0SJeremy L Thompson break;
2040d0321e0SJeremy L Thompson case CEED_USE_POINTER:
2050d0321e0SJeremy L Thompson impl->d_data_owned = NULL;
2060d0321e0SJeremy L Thompson impl->d_data_borrowed = data;
2070d0321e0SJeremy L Thompson impl->d_data = data;
2080d0321e0SJeremy L Thompson break;
2090d0321e0SJeremy L Thompson }
2109bc66399SJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed));
2110d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
2120d0321e0SJeremy L Thompson }
2130d0321e0SJeremy L Thompson
2140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2150d0321e0SJeremy L Thompson // Set the data used by a user context,
2160d0321e0SJeremy L Thompson // freeing any previously allocated data if applicable
2170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextSetData_Cuda(const CeedQFunctionContext ctx,const CeedMemType mem_type,const CeedCopyMode copy_mode,void * data)2182b730f8bSJeremy L Thompson static int CeedQFunctionContextSetData_Cuda(const CeedQFunctionContext ctx, const CeedMemType mem_type, const CeedCopyMode copy_mode, void *data) {
2192b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextSetAllInvalid_Cuda(ctx));
22043c928f4SJeremy L Thompson switch (mem_type) {
2210d0321e0SJeremy L Thompson case CEED_MEM_HOST:
22243c928f4SJeremy L Thompson return CeedQFunctionContextSetDataHost_Cuda(ctx, copy_mode, data);
2230d0321e0SJeremy L Thompson case CEED_MEM_DEVICE:
22443c928f4SJeremy L Thompson return CeedQFunctionContextSetDataDevice_Cuda(ctx, copy_mode, data);
2250d0321e0SJeremy L Thompson }
2260d0321e0SJeremy L Thompson return CEED_ERROR_UNSUPPORTED;
2270d0321e0SJeremy L Thompson }
2280d0321e0SJeremy L Thompson
2290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2300d0321e0SJeremy L Thompson // Take data
2310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextTakeData_Cuda(const CeedQFunctionContext ctx,const CeedMemType mem_type,void * data)2322b730f8bSJeremy L Thompson static int CeedQFunctionContextTakeData_Cuda(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
2330d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
234ca735530SJeremy L Thompson
2352b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
2360d0321e0SJeremy L Thompson
23743c928f4SJeremy L Thompson // Sync data to requested mem_type
2380d0321e0SJeremy L Thompson bool need_sync = false;
2392b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextNeedSync_Cuda(ctx, mem_type, &need_sync));
2402b730f8bSJeremy L Thompson if (need_sync) CeedCallBackend(CeedQFunctionContextSync_Cuda(ctx, mem_type));
2410d0321e0SJeremy L Thompson
2420d0321e0SJeremy L Thompson // Update pointer
24343c928f4SJeremy L Thompson switch (mem_type) {
2440d0321e0SJeremy L Thompson case CEED_MEM_HOST:
2450d0321e0SJeremy L Thompson *(void **)data = impl->h_data_borrowed;
2460d0321e0SJeremy L Thompson impl->h_data_borrowed = NULL;
2470d0321e0SJeremy L Thompson impl->h_data = NULL;
2480d0321e0SJeremy L Thompson break;
2490d0321e0SJeremy L Thompson case CEED_MEM_DEVICE:
2500d0321e0SJeremy L Thompson *(void **)data = impl->d_data_borrowed;
2510d0321e0SJeremy L Thompson impl->d_data_borrowed = NULL;
2520d0321e0SJeremy L Thompson impl->d_data = NULL;
2530d0321e0SJeremy L Thompson break;
2540d0321e0SJeremy L Thompson }
2550d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
2560d0321e0SJeremy L Thompson }
2570d0321e0SJeremy L Thompson
2580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
25928bfd0b7SJeremy L Thompson // Core logic for GetData.
26028bfd0b7SJeremy L Thompson // If a different memory type is most up to date, this will perform a copy
2610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextGetDataCore_Cuda(const CeedQFunctionContext ctx,const CeedMemType mem_type,void * data)2622b730f8bSJeremy L Thompson static int CeedQFunctionContextGetDataCore_Cuda(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
263ca735530SJeremy L Thompson bool need_sync = false;
2640d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
265ca735530SJeremy L Thompson
2662b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
2670d0321e0SJeremy L Thompson
26843c928f4SJeremy L Thompson // Sync data to requested mem_type
2692b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextNeedSync_Cuda(ctx, mem_type, &need_sync));
2702b730f8bSJeremy L Thompson if (need_sync) CeedCallBackend(CeedQFunctionContextSync_Cuda(ctx, mem_type));
2710d0321e0SJeremy L Thompson
2720d0321e0SJeremy L Thompson // Update pointer
27343c928f4SJeremy L Thompson switch (mem_type) {
2740d0321e0SJeremy L Thompson case CEED_MEM_HOST:
2750d0321e0SJeremy L Thompson *(void **)data = impl->h_data;
2760d0321e0SJeremy L Thompson break;
2770d0321e0SJeremy L Thompson case CEED_MEM_DEVICE:
2780d0321e0SJeremy L Thompson *(void **)data = impl->d_data;
2790d0321e0SJeremy L Thompson break;
2800d0321e0SJeremy L Thompson }
28128bfd0b7SJeremy L Thompson return CEED_ERROR_SUCCESS;
28228bfd0b7SJeremy L Thompson }
28328bfd0b7SJeremy L Thompson
28428bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
28528bfd0b7SJeremy L Thompson // Get read-only access to the data
28628bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextGetDataRead_Cuda(const CeedQFunctionContext ctx,const CeedMemType mem_type,void * data)2872b730f8bSJeremy L Thompson static int CeedQFunctionContextGetDataRead_Cuda(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
28828bfd0b7SJeremy L Thompson return CeedQFunctionContextGetDataCore_Cuda(ctx, mem_type, data);
28928bfd0b7SJeremy L Thompson }
29028bfd0b7SJeremy L Thompson
29128bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
29228bfd0b7SJeremy L Thompson // Get read/write access to the data
29328bfd0b7SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextGetData_Cuda(const CeedQFunctionContext ctx,const CeedMemType mem_type,void * data)2942b730f8bSJeremy L Thompson static int CeedQFunctionContextGetData_Cuda(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) {
29528bfd0b7SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
29628bfd0b7SJeremy L Thompson
297ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
2982b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetDataCore_Cuda(ctx, mem_type, data));
29928bfd0b7SJeremy L Thompson
3000d0321e0SJeremy L Thompson // Mark only pointer for requested memory as valid
3012b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextSetAllInvalid_Cuda(ctx));
30243c928f4SJeremy L Thompson switch (mem_type) {
3030d0321e0SJeremy L Thompson case CEED_MEM_HOST:
3040d0321e0SJeremy L Thompson impl->h_data = *(void **)data;
3050d0321e0SJeremy L Thompson break;
3060d0321e0SJeremy L Thompson case CEED_MEM_DEVICE:
3070d0321e0SJeremy L Thompson impl->d_data = *(void **)data;
3080d0321e0SJeremy L Thompson break;
3090d0321e0SJeremy L Thompson }
3100d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
3110d0321e0SJeremy L Thompson }
3120d0321e0SJeremy L Thompson
3130d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3140d0321e0SJeremy L Thompson // Destroy the user context
3150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextDestroy_Cuda(const CeedQFunctionContext ctx)3160d0321e0SJeremy L Thompson static int CeedQFunctionContextDestroy_Cuda(const CeedQFunctionContext ctx) {
3170d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
3180d0321e0SJeremy L Thompson
319ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
3206e536b99SJeremy L Thompson CeedCallCuda(CeedQFunctionContextReturnCeed(ctx), cudaFree(impl->d_data_owned));
3212b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->h_data_owned));
3222b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl));
3230d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
3240d0321e0SJeremy L Thompson }
3250d0321e0SJeremy L Thompson
3260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3270d0321e0SJeremy L Thompson // QFunctionContext Create
3280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextCreate_Cuda(CeedQFunctionContext ctx)3290d0321e0SJeremy L Thompson int CeedQFunctionContextCreate_Cuda(CeedQFunctionContext ctx) {
3300d0321e0SJeremy L Thompson CeedQFunctionContext_Cuda *impl;
3310d0321e0SJeremy L Thompson Ceed ceed;
3320d0321e0SJeremy L Thompson
333ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
3342b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Cuda));
3352b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Cuda));
3362b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Cuda));
3372b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Cuda));
3382b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Cuda));
3392b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetDataRead_Cuda));
3402b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Cuda));
3419bc66399SJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed));
3422b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl));
3432b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl));
3440d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS;
3450d0321e0SJeremy L Thompson }
3462a86cc9dSSebastian Grimberg
3470d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
348