xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-qfunctioncontext.c (revision 9a25c3513918281076c2babe50808fbe5e3a546e)
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;
20ad70ee2cSJeremy L Thompson 
21*9a25c351SJeremy 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) {
309c774eddSJeremy L Thompson   Ceed                      ceed;
31ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
329c774eddSJeremy L Thompson 
33*9a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
34ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
35bcbe1c99SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
361c66c397SJeremy L Thompson   *has_borrowed_data_of_type = impl->data_borrowed;
379c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
389c774eddSJeremy L Thompson }
399c774eddSJeremy L Thompson 
409c774eddSJeremy L Thompson //------------------------------------------------------------------------------
41777ff853SJeremy L Thompson // QFunctionContext Set Data
42777ff853SJeremy L Thompson //------------------------------------------------------------------------------
432b730f8bSJeremy L Thompson static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, void *data) {
44777ff853SJeremy L Thompson   Ceed                      ceed;
45ad70ee2cSJeremy L Thompson   size_t                    ctx_size;
46ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
47ad70ee2cSJeremy L Thompson 
48*9a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
49ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
502b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
51777ff853SJeremy L Thompson 
526574a04fSJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
539c774eddSJeremy L Thompson 
542b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_owned));
55d1d35e2fSjeremylt   switch (copy_mode) {
56777ff853SJeremy L Thompson     case CEED_COPY_VALUES:
572b730f8bSJeremy L Thompson       CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->data_owned));
589c774eddSJeremy L Thompson       impl->data_borrowed = NULL;
599c774eddSJeremy L Thompson       impl->data          = impl->data_owned;
60d1d35e2fSjeremylt       memcpy(impl->data, data, ctx_size);
61777ff853SJeremy L Thompson       break;
62777ff853SJeremy L Thompson     case CEED_OWN_POINTER:
639c774eddSJeremy L Thompson       impl->data_owned    = data;
649c774eddSJeremy L Thompson       impl->data_borrowed = NULL;
65777ff853SJeremy L Thompson       impl->data          = data;
66777ff853SJeremy L Thompson       break;
67777ff853SJeremy L Thompson     case CEED_USE_POINTER:
689c774eddSJeremy L Thompson       impl->data_borrowed = data;
69777ff853SJeremy L Thompson       impl->data          = data;
70777ff853SJeremy L Thompson   }
71e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
72777ff853SJeremy L Thompson }
73777ff853SJeremy L Thompson 
74777ff853SJeremy L Thompson //------------------------------------------------------------------------------
75891038deSjeremylt // QFunctionContext Take Data
76891038deSjeremylt //------------------------------------------------------------------------------
772b730f8bSJeremy L Thompson static int CeedQFunctionContextTakeData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
78891038deSjeremylt   Ceed                      ceed;
79ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
80ad70ee2cSJeremy L Thompson 
81*9a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
822b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
83891038deSjeremylt 
846574a04fSJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend");
859c774eddSJeremy L Thompson 
86891038deSjeremylt   *(void **)data      = impl->data;
879c774eddSJeremy L Thompson   impl->data_borrowed = NULL;
88891038deSjeremylt   impl->data          = NULL;
89891038deSjeremylt   return CEED_ERROR_SUCCESS;
90891038deSjeremylt }
91891038deSjeremylt 
92891038deSjeremylt //------------------------------------------------------------------------------
93777ff853SJeremy L Thompson // QFunctionContext Get Data
94777ff853SJeremy L Thompson //------------------------------------------------------------------------------
952b730f8bSJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
96777ff853SJeremy L Thompson   Ceed                      ceed;
97ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
98ad70ee2cSJeremy L Thompson 
99*9a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1002b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
101777ff853SJeremy L Thompson 
1026574a04fSJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend");
1039c774eddSJeremy L Thompson 
104777ff853SJeremy L Thompson   *(void **)data = impl->data;
105e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
106777ff853SJeremy L Thompson }
107777ff853SJeremy L Thompson 
108777ff853SJeremy L Thompson //------------------------------------------------------------------------------
109777ff853SJeremy L Thompson // QFunctionContext Restore Data
110777ff853SJeremy L Thompson //------------------------------------------------------------------------------
1112b730f8bSJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) { return CEED_ERROR_SUCCESS; }
112777ff853SJeremy L Thompson 
113777ff853SJeremy L Thompson //------------------------------------------------------------------------------
114777ff853SJeremy L Thompson // QFunctionContext Destroy
115777ff853SJeremy L Thompson //------------------------------------------------------------------------------
116777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
117777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
118777ff853SJeremy L Thompson 
119ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1202b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_owned));
1212b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
122e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
123777ff853SJeremy L Thompson }
124777ff853SJeremy L Thompson 
125777ff853SJeremy L Thompson //------------------------------------------------------------------------------
126777ff853SJeremy L Thompson // QFunctionContext Create
127777ff853SJeremy L Thompson //------------------------------------------------------------------------------
128777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
129777ff853SJeremy L Thompson   Ceed                      ceed;
130ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
131777ff853SJeremy L Thompson 
132ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
1332b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Ref));
1342b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Ref));
1352b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Ref));
1362b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Ref));
1372b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Ref));
1382b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetData_Ref));
1392b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData", CeedQFunctionContextRestoreData_Ref));
1402b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead", CeedQFunctionContextRestoreData_Ref));
1412b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Ref));
1422b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
1432b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl));
144e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
145777ff853SJeremy L Thompson }
1462a86cc9dSSebastian Grimberg 
147777ff853SJeremy L Thompson //------------------------------------------------------------------------------
148