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 8*49aac155SJeremy L Thompson #include <ceed.h> 9ec3da8bcSJed Brown #include <ceed/backend.h> 10*49aac155SJeremy 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 612b730f8bSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 62777ff853SJeremy L Thompson // LCOV_EXCL_START 632b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 64777ff853SJeremy L Thompson // LCOV_EXCL_STOP 652b730f8bSJeremy L Thompson } 669c774eddSJeremy L Thompson 672b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->data_owned)); 68d1d35e2fSjeremylt switch (copy_mode) { 69777ff853SJeremy L Thompson case CEED_COPY_VALUES: 702b730f8bSJeremy L Thompson CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->data_owned)); 719c774eddSJeremy L Thompson impl->data_borrowed = NULL; 729c774eddSJeremy L Thompson impl->data = impl->data_owned; 73d1d35e2fSjeremylt memcpy(impl->data, data, ctx_size); 74777ff853SJeremy L Thompson break; 75777ff853SJeremy L Thompson case CEED_OWN_POINTER: 769c774eddSJeremy L Thompson impl->data_owned = data; 779c774eddSJeremy L Thompson impl->data_borrowed = NULL; 78777ff853SJeremy L Thompson impl->data = data; 79777ff853SJeremy L Thompson break; 80777ff853SJeremy L Thompson case CEED_USE_POINTER: 819c774eddSJeremy L Thompson impl->data_borrowed = data; 82777ff853SJeremy L Thompson impl->data = data; 83777ff853SJeremy L Thompson } 84e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 85777ff853SJeremy L Thompson } 86777ff853SJeremy L Thompson 87777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 88891038deSjeremylt // QFunctionContext Take Data 89891038deSjeremylt //------------------------------------------------------------------------------ 902b730f8bSJeremy L Thompson static int CeedQFunctionContextTakeData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 91891038deSjeremylt CeedQFunctionContext_Ref *impl; 922b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 93891038deSjeremylt Ceed ceed; 942b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 95891038deSjeremylt 962b730f8bSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 97891038deSjeremylt // LCOV_EXCL_START 982b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 99891038deSjeremylt // LCOV_EXCL_STOP 1002b730f8bSJeremy L Thompson } 1019c774eddSJeremy L Thompson 102891038deSjeremylt *(void **)data = impl->data; 1039c774eddSJeremy L Thompson impl->data_borrowed = NULL; 104891038deSjeremylt impl->data = NULL; 1059c774eddSJeremy L Thompson 106891038deSjeremylt return CEED_ERROR_SUCCESS; 107891038deSjeremylt } 108891038deSjeremylt 109891038deSjeremylt //------------------------------------------------------------------------------ 110777ff853SJeremy L Thompson // QFunctionContext Get Data 111777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 1122b730f8bSJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 113777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 1142b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 115777ff853SJeremy L Thompson Ceed ceed; 1162b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 117777ff853SJeremy L Thompson 1182b730f8bSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 119777ff853SJeremy L Thompson // LCOV_EXCL_START 1202b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 121777ff853SJeremy L Thompson // LCOV_EXCL_STOP 1222b730f8bSJeremy L Thompson } 1239c774eddSJeremy L Thompson 124777ff853SJeremy L Thompson *(void **)data = impl->data; 1259c774eddSJeremy L Thompson 126e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 127777ff853SJeremy L Thompson } 128777ff853SJeremy L Thompson 129777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 130777ff853SJeremy L Thompson // QFunctionContext Restore Data 131777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 1322b730f8bSJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) { return CEED_ERROR_SUCCESS; } 133777ff853SJeremy L Thompson 134777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 135777ff853SJeremy L Thompson // QFunctionContext Destroy 136777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 137777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) { 138777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 1392b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl)); 140777ff853SJeremy L Thompson 1412b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->data_owned)); 1422b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 143e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 144777ff853SJeremy L Thompson } 145777ff853SJeremy L Thompson 146777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 147777ff853SJeremy L Thompson // QFunctionContext Create 148777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 149777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) { 150777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 151777ff853SJeremy L Thompson Ceed ceed; 1522b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 153777ff853SJeremy L Thompson 1542b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Ref)); 1552b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Ref)); 1562b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Ref)); 1572b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Ref)); 1582b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Ref)); 1592b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetData_Ref)); 1602b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData", CeedQFunctionContextRestoreData_Ref)); 1612b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead", CeedQFunctionContextRestoreData_Ref)); 1622b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Ref)); 1639c774eddSJeremy L Thompson 1642b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 1652b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl)); 1669c774eddSJeremy L Thompson 167e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 168777ff853SJeremy L Thompson } 169777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 170