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 8ec3da8bcSJed Brown #include <ceed/backend.h> 9*2b730f8bSJeremy L Thompson #include <ceed/ceed.h> 103d576824SJeremy L Thompson #include <string.h> 11*2b730f8bSJeremy L Thompson 12777ff853SJeremy L Thompson #include "ceed-ref.h" 13777ff853SJeremy L Thompson 14777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 159c774eddSJeremy L Thompson // QFunctionContext has valid data 169c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 17*2b730f8bSJeremy L Thompson static int CeedQFunctionContextHasValidData_Ref(CeedQFunctionContext ctx, bool *has_valid_data) { 189c774eddSJeremy L Thompson CeedQFunctionContext_Ref *impl; 19*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 209c774eddSJeremy L Thompson 219c774eddSJeremy L Thompson *has_valid_data = !!impl->data; 229c774eddSJeremy L Thompson 239c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 249c774eddSJeremy L Thompson } 259c774eddSJeremy L Thompson 269c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 279c774eddSJeremy L Thompson // QFunctionContext has borrowed data 289c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 29*2b730f8bSJeremy L Thompson static int CeedQFunctionContextHasBorrowedDataOfType_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, bool *has_borrowed_data_of_type) { 309c774eddSJeremy L Thompson CeedQFunctionContext_Ref *impl; 31*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 329c774eddSJeremy L Thompson Ceed ceed; 33*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 349c774eddSJeremy L Thompson 359c774eddSJeremy L Thompson switch (mem_type) { 369c774eddSJeremy L Thompson case CEED_MEM_HOST: 379c774eddSJeremy L Thompson *has_borrowed_data_of_type = !!impl->data_borrowed; 389c774eddSJeremy L Thompson break; 399c774eddSJeremy L Thompson default: 409c774eddSJeremy L Thompson // LCOV_EXCL_START 41*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 429c774eddSJeremy L Thompson // LCOV_EXCL_STOP 439c774eddSJeremy L Thompson break; 449c774eddSJeremy L Thompson } 459c774eddSJeremy L Thompson 469c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 479c774eddSJeremy L Thompson } 489c774eddSJeremy L Thompson 499c774eddSJeremy L Thompson //------------------------------------------------------------------------------ 50777ff853SJeremy L Thompson // QFunctionContext Set Data 51777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 52*2b730f8bSJeremy L Thompson static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, void *data) { 53777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 54*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 55d1d35e2fSjeremylt size_t ctx_size; 56*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size)); 57777ff853SJeremy L Thompson Ceed ceed; 58*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 59777ff853SJeremy L Thompson 60*2b730f8bSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 61777ff853SJeremy L Thompson // LCOV_EXCL_START 62*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend"); 63777ff853SJeremy L Thompson // LCOV_EXCL_STOP 64*2b730f8bSJeremy L Thompson } 659c774eddSJeremy L Thompson 66*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->data_owned)); 67d1d35e2fSjeremylt switch (copy_mode) { 68777ff853SJeremy L Thompson case CEED_COPY_VALUES: 69*2b730f8bSJeremy L Thompson CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->data_owned)); 709c774eddSJeremy L Thompson impl->data_borrowed = NULL; 719c774eddSJeremy L Thompson impl->data = impl->data_owned; 72d1d35e2fSjeremylt memcpy(impl->data, data, ctx_size); 73777ff853SJeremy L Thompson break; 74777ff853SJeremy L Thompson case CEED_OWN_POINTER: 759c774eddSJeremy L Thompson impl->data_owned = data; 769c774eddSJeremy L Thompson impl->data_borrowed = NULL; 77777ff853SJeremy L Thompson impl->data = data; 78777ff853SJeremy L Thompson break; 79777ff853SJeremy L Thompson case CEED_USE_POINTER: 809c774eddSJeremy L Thompson impl->data_borrowed = data; 81777ff853SJeremy L Thompson impl->data = data; 82777ff853SJeremy L Thompson } 83e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 84777ff853SJeremy L Thompson } 85777ff853SJeremy L Thompson 86777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 87891038deSjeremylt // QFunctionContext Take Data 88891038deSjeremylt //------------------------------------------------------------------------------ 89*2b730f8bSJeremy L Thompson static int CeedQFunctionContextTakeData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 90891038deSjeremylt CeedQFunctionContext_Ref *impl; 91*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 92891038deSjeremylt Ceed ceed; 93*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 94891038deSjeremylt 95*2b730f8bSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 96891038deSjeremylt // LCOV_EXCL_START 97*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 98891038deSjeremylt // LCOV_EXCL_STOP 99*2b730f8bSJeremy L Thompson } 1009c774eddSJeremy L Thompson 101891038deSjeremylt *(void **)data = impl->data; 1029c774eddSJeremy L Thompson impl->data_borrowed = NULL; 103891038deSjeremylt impl->data = NULL; 1049c774eddSJeremy L Thompson 105891038deSjeremylt return CEED_ERROR_SUCCESS; 106891038deSjeremylt } 107891038deSjeremylt 108891038deSjeremylt //------------------------------------------------------------------------------ 109777ff853SJeremy L Thompson // QFunctionContext Get Data 110777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 111*2b730f8bSJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 112777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 113*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl)); 114777ff853SJeremy L Thompson Ceed ceed; 115*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 116777ff853SJeremy L Thompson 117*2b730f8bSJeremy L Thompson if (mem_type != CEED_MEM_HOST) { 118777ff853SJeremy L Thompson // LCOV_EXCL_START 119*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend"); 120777ff853SJeremy L Thompson // LCOV_EXCL_STOP 121*2b730f8bSJeremy L Thompson } 1229c774eddSJeremy L Thompson 123777ff853SJeremy L Thompson *(void **)data = impl->data; 1249c774eddSJeremy L Thompson 125e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 126777ff853SJeremy L Thompson } 127777ff853SJeremy L Thompson 128777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 129777ff853SJeremy L Thompson // QFunctionContext Restore Data 130777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 131*2b730f8bSJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) { return CEED_ERROR_SUCCESS; } 132777ff853SJeremy L Thompson 133777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 134777ff853SJeremy L Thompson // QFunctionContext Destroy 135777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 136777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) { 137777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 138*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl)); 139777ff853SJeremy L Thompson 140*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->data_owned)); 141*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 142e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 143777ff853SJeremy L Thompson } 144777ff853SJeremy L Thompson 145777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 146777ff853SJeremy L Thompson // QFunctionContext Create 147777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 148777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) { 149777ff853SJeremy L Thompson CeedQFunctionContext_Ref *impl; 150777ff853SJeremy L Thompson Ceed ceed; 151*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); 152777ff853SJeremy L Thompson 153*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Ref)); 154*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Ref)); 155*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Ref)); 156*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Ref)); 157*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Ref)); 158*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetData_Ref)); 159*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData", CeedQFunctionContextRestoreData_Ref)); 160*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead", CeedQFunctionContextRestoreData_Ref)); 161*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Ref)); 1629c774eddSJeremy L Thompson 163*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 164*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl)); 1659c774eddSJeremy L Thompson 166e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 167777ff853SJeremy L Thompson } 168777ff853SJeremy L Thompson //------------------------------------------------------------------------------ 169