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. 3777ff853SJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5777ff853SJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7777ff853SJeremy L Thompson 849aac155SJeremy L Thompson #include <ceed.h> 9ec3da8bcSJed Brown #include <ceed/backend.h> 1049aac155SJeremy L Thompson #include <stdbool.h> 113d576824SJeremy L Thompson #include <string.h> 122b730f8bSJeremy L Thompson 13777ff853SJeremy L Thompson #include "ceed-ref.h" 14777ff853SJeremy L Thompson 15777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 169c774eddSJeremy L Thompson // QFunctionContext has valid data 179c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 182b730f8bSJeremy L Thompson static int CeedQFunctionContextHasValidData_Ref(CeedQFunctionContext ctx, bool *has_valid_data) { 199c774eddSJeremy L Thompson CeedQFunctionContext_Ref *impl; 202b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 219c774eddSJeremy L Thompson 229c774eddSJeremy L Thompson *has_valid_data = !!impl->data; 239c774eddSJeremy L Thompson 249c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 259c774eddSJeremy L Thompson } 269c774eddSJeremy L Thompson 279c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 289c774eddSJeremy L Thompson // QFunctionContext has borrowed data 299c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 302b730f8bSJeremy L Thompson static int CeedQFunctionContextHasBorrowedDataOfType_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, bool *has_borrowed_data_of_type) { 319c774eddSJeremy L Thompson CeedQFunctionContext_Ref *impl; 322b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 339c774eddSJeremy L Thompson Ceed ceed; 342b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 359c774eddSJeremy L Thompson 369c774eddSJeremy L Thompson switch (mem_type) { 379c774eddSJeremy L Thompson case CEED_MEM_HOST: 389c774eddSJeremy L Thompson *has_borrowed_data_of_type = !!impl->data_borrowed; 399c774eddSJeremy L Thompson break; 409c774eddSJeremy L Thompson default: 419c774eddSJeremy L Thompson // LCOV_EXCL_START 422b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 439c774eddSJeremy L Thompson // LCOV_EXCL_STOP 449c774eddSJeremy L Thompson break; 459c774eddSJeremy L Thompson } 469c774eddSJeremy L Thompson 479c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 489c774eddSJeremy L Thompson } 499c774eddSJeremy L Thompson 509c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 51777ff853SJeremy L Thompson // QFunctionContext Set Data 52777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 532b730f8bSJeremy L Thompson static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, void *data) { 54777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 552b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 56d1d35e2fSjeremylt size_t ctx_size; 572b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size)); 58777ff853SJeremy L Thompson Ceed ceed; 592b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 60777ff853SJeremy L Thompson 61*6574a04fSJeremy L Thompson CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 629c774eddSJeremy L Thompson 632b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->data_owned)); 64d1d35e2fSjeremylt switch (copy_mode) { 65777ff853SJeremy L Thompson case CEED_COPY_VALUES: 662b730f8bSJeremy L Thompson CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->data_owned)); 679c774eddSJeremy L Thompson impl->data_borrowed = NULL; 689c774eddSJeremy L Thompson impl->data = impl->data_owned; 69d1d35e2fSjeremylt memcpy(impl->data, data, ctx_size); 70777ff853SJeremy L Thompson break; 71777ff853SJeremy L Thompson case CEED_OWN_POINTER: 729c774eddSJeremy L Thompson impl->data_owned = data; 739c774eddSJeremy L Thompson impl->data_borrowed = NULL; 74777ff853SJeremy L Thompson impl->data = data; 75777ff853SJeremy L Thompson break; 76777ff853SJeremy L Thompson case CEED_USE_POINTER: 779c774eddSJeremy L Thompson impl->data_borrowed = data; 78777ff853SJeremy L Thompson impl->data = data; 79777ff853SJeremy L Thompson } 80e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 81777ff853SJeremy L Thompson } 82777ff853SJeremy L Thompson 83777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 84891038deSjeremylt // QFunctionContext Take Data 85891038deSjeremylt //------------------------------------------------------------------------------ 862b730f8bSJeremy L Thompson static int CeedQFunctionContextTakeData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 87891038deSjeremylt CeedQFunctionContext_Ref *impl; 882b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 89891038deSjeremylt Ceed ceed; 902b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 91891038deSjeremylt 92*6574a04fSJeremy L Thompson CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 939c774eddSJeremy L Thompson 94891038deSjeremylt *(void **)data = impl->data; 959c774eddSJeremy L Thompson impl->data_borrowed = NULL; 96891038deSjeremylt impl->data = NULL; 979c774eddSJeremy L Thompson 98891038deSjeremylt return CEED_ERROR_SUCCESS; 99891038deSjeremylt } 100891038deSjeremylt 101891038deSjeremylt //------------------------------------------------------------------------------ 102777ff853SJeremy L Thompson // QFunctionContext Get Data 103777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 1042b730f8bSJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 105777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 1062b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 107777ff853SJeremy L Thompson Ceed ceed; 1082b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 109777ff853SJeremy L Thompson 110*6574a04fSJeremy L Thompson CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 1119c774eddSJeremy L Thompson 112777ff853SJeremy L Thompson *(void **)data = impl->data; 1139c774eddSJeremy L Thompson 114e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 115777ff853SJeremy L Thompson } 116777ff853SJeremy L Thompson 117777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 118777ff853SJeremy L Thompson // QFunctionContext Restore Data 119777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 1202b730f8bSJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) { return CEED_ERROR_SUCCESS; } 121777ff853SJeremy L Thompson 122777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 123777ff853SJeremy L Thompson // QFunctionContext Destroy 124777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 125777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) { 126777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 1272b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl)); 128777ff853SJeremy L Thompson 1292b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->data_owned)); 1302b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 131e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 132777ff853SJeremy L Thompson } 133777ff853SJeremy L Thompson 134777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 135777ff853SJeremy L Thompson // QFunctionContext Create 136777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 137777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) { 138777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 139777ff853SJeremy L Thompson Ceed ceed; 1402b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 141777ff853SJeremy L Thompson 1422b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Ref)); 1432b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Ref)); 1442b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Ref)); 1452b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Ref)); 1462b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Ref)); 1472b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetData_Ref)); 1482b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData", CeedQFunctionContextRestoreData_Ref)); 1492b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead", CeedQFunctionContextRestoreData_Ref)); 1502b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Ref)); 1519c774eddSJeremy L Thompson 1522b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 1532b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl)); 1549c774eddSJeremy L Thompson 155e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 156777ff853SJeremy L Thompson } 157777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 158