xref: /libCEED/backends/ref/ceed-ref-qfunctioncontext.c (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1*5aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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;
20ad70ee2cSJeremy L Thompson 
219a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
221c66c397SJeremy L Thompson   *has_valid_data = impl->data;
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 //------------------------------------------------------------------------------
292b730f8bSJeremy L Thompson static int CeedQFunctionContextHasBorrowedDataOfType_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, bool *has_borrowed_data_of_type) {
30ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
319c774eddSJeremy L Thompson 
329a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
336e536b99SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
341c66c397SJeremy L Thompson   *has_borrowed_data_of_type = impl->data_borrowed;
359c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
369c774eddSJeremy L Thompson }
379c774eddSJeremy L Thompson 
389c774eddSJeremy L Thompson //------------------------------------------------------------------------------
39777ff853SJeremy L Thompson // QFunctionContext Set Data
40777ff853SJeremy L Thompson //------------------------------------------------------------------------------
412b730f8bSJeremy L Thompson static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, void *data) {
42ad70ee2cSJeremy L Thompson   size_t                    ctx_size;
43ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
44ad70ee2cSJeremy L Thompson 
459a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
46ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
47777ff853SJeremy L Thompson 
486e536b99SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
499c774eddSJeremy L Thompson 
502b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_owned));
51d1d35e2fSjeremylt   switch (copy_mode) {
52777ff853SJeremy L Thompson     case CEED_COPY_VALUES:
532b730f8bSJeremy L Thompson       CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->data_owned));
549c774eddSJeremy L Thompson       impl->data_borrowed = NULL;
559c774eddSJeremy L Thompson       impl->data          = impl->data_owned;
56d1d35e2fSjeremylt       memcpy(impl->data, data, ctx_size);
57777ff853SJeremy L Thompson       break;
58777ff853SJeremy L Thompson     case CEED_OWN_POINTER:
599c774eddSJeremy L Thompson       impl->data_owned    = data;
609c774eddSJeremy L Thompson       impl->data_borrowed = NULL;
61777ff853SJeremy L Thompson       impl->data          = data;
62777ff853SJeremy L Thompson       break;
63777ff853SJeremy L Thompson     case CEED_USE_POINTER:
649c774eddSJeremy L Thompson       impl->data_borrowed = data;
65777ff853SJeremy L Thompson       impl->data          = data;
66777ff853SJeremy L Thompson   }
67e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
68777ff853SJeremy L Thompson }
69777ff853SJeremy L Thompson 
70777ff853SJeremy L Thompson //------------------------------------------------------------------------------
71891038deSjeremylt // QFunctionContext Take Data
72891038deSjeremylt //------------------------------------------------------------------------------
732b730f8bSJeremy L Thompson static int CeedQFunctionContextTakeData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
74ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
75ad70ee2cSJeremy L Thompson 
769a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
77891038deSjeremylt 
786e536b99SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend");
799c774eddSJeremy L Thompson 
80891038deSjeremylt   *(void **)data      = impl->data;
819c774eddSJeremy L Thompson   impl->data_borrowed = NULL;
82891038deSjeremylt   impl->data          = NULL;
83891038deSjeremylt   return CEED_ERROR_SUCCESS;
84891038deSjeremylt }
85891038deSjeremylt 
86891038deSjeremylt //------------------------------------------------------------------------------
87777ff853SJeremy L Thompson // QFunctionContext Get Data
88777ff853SJeremy L Thompson //------------------------------------------------------------------------------
892b730f8bSJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
90ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
91ad70ee2cSJeremy L Thompson 
929a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
93777ff853SJeremy L Thompson 
946e536b99SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend");
959c774eddSJeremy L Thompson 
96777ff853SJeremy L Thompson   *(void **)data = impl->data;
97e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
98777ff853SJeremy L Thompson }
99777ff853SJeremy L Thompson 
100777ff853SJeremy L Thompson //------------------------------------------------------------------------------
101777ff853SJeremy L Thompson // QFunctionContext Restore Data
102777ff853SJeremy L Thompson //------------------------------------------------------------------------------
1032b730f8bSJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) { return CEED_ERROR_SUCCESS; }
104777ff853SJeremy L Thompson 
105777ff853SJeremy L Thompson //------------------------------------------------------------------------------
106777ff853SJeremy L Thompson // QFunctionContext Destroy
107777ff853SJeremy L Thompson //------------------------------------------------------------------------------
108777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
109777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
110777ff853SJeremy L Thompson 
111ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1122b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_owned));
1132b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
114e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
115777ff853SJeremy L Thompson }
116777ff853SJeremy L Thompson 
117777ff853SJeremy L Thompson //------------------------------------------------------------------------------
118777ff853SJeremy L Thompson // QFunctionContext Create
119777ff853SJeremy L Thompson //------------------------------------------------------------------------------
120777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
121777ff853SJeremy L Thompson   Ceed                      ceed;
122ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
123777ff853SJeremy L Thompson 
124ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
1252b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Ref));
1262b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Ref));
1272b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Ref));
1282b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Ref));
1292b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Ref));
1302b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetData_Ref));
1312b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData", CeedQFunctionContextRestoreData_Ref));
1322b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead", CeedQFunctionContextRestoreData_Ref));
1332b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Ref));
1342b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
1352b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl));
136e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
137777ff853SJeremy L Thompson }
1382a86cc9dSSebastian Grimberg 
139777ff853SJeremy L Thompson //------------------------------------------------------------------------------
140