xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-qfunctioncontext.c (revision ad70ee2c7083010521460a681a248527200af770)
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;
20*ad70ee2cSJeremy L Thompson 
212b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&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;
31*ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
329c774eddSJeremy L Thompson 
33*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl));
34*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
359c774eddSJeremy L Thompson   switch (mem_type) {
369c774eddSJeremy L Thompson     case CEED_MEM_HOST:
371c66c397SJeremy L Thompson       *has_borrowed_data_of_type = impl->data_borrowed;
389c774eddSJeremy L Thompson       break;
399c774eddSJeremy L Thompson     default:
409c774eddSJeremy L Thompson       // LCOV_EXCL_START
412b730f8bSJeremy 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   return CEED_ERROR_SUCCESS;
469c774eddSJeremy L Thompson }
479c774eddSJeremy L Thompson 
489c774eddSJeremy L Thompson //------------------------------------------------------------------------------
49777ff853SJeremy L Thompson // QFunctionContext Set Data
50777ff853SJeremy L Thompson //------------------------------------------------------------------------------
512b730f8bSJeremy L Thompson static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, void *data) {
52777ff853SJeremy L Thompson   Ceed                      ceed;
53*ad70ee2cSJeremy L Thompson   size_t                    ctx_size;
54*ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
55*ad70ee2cSJeremy L Thompson 
56*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl));
57*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
582b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
59777ff853SJeremy L Thompson 
606574a04fSJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
619c774eddSJeremy L Thompson 
622b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_owned));
63d1d35e2fSjeremylt   switch (copy_mode) {
64777ff853SJeremy L Thompson     case CEED_COPY_VALUES:
652b730f8bSJeremy L Thompson       CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->data_owned));
669c774eddSJeremy L Thompson       impl->data_borrowed = NULL;
679c774eddSJeremy L Thompson       impl->data          = impl->data_owned;
68d1d35e2fSjeremylt       memcpy(impl->data, data, ctx_size);
69777ff853SJeremy L Thompson       break;
70777ff853SJeremy L Thompson     case CEED_OWN_POINTER:
719c774eddSJeremy L Thompson       impl->data_owned    = data;
729c774eddSJeremy L Thompson       impl->data_borrowed = NULL;
73777ff853SJeremy L Thompson       impl->data          = data;
74777ff853SJeremy L Thompson       break;
75777ff853SJeremy L Thompson     case CEED_USE_POINTER:
769c774eddSJeremy L Thompson       impl->data_borrowed = data;
77777ff853SJeremy L Thompson       impl->data          = data;
78777ff853SJeremy L Thompson   }
79e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
80777ff853SJeremy L Thompson }
81777ff853SJeremy L Thompson 
82777ff853SJeremy L Thompson //------------------------------------------------------------------------------
83891038deSjeremylt // QFunctionContext Take Data
84891038deSjeremylt //------------------------------------------------------------------------------
852b730f8bSJeremy L Thompson static int CeedQFunctionContextTakeData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
86891038deSjeremylt   Ceed                      ceed;
87*ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
88*ad70ee2cSJeremy L Thompson 
89*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl));
902b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
91891038deSjeremylt 
926574a04fSJeremy 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;
97891038deSjeremylt   return CEED_ERROR_SUCCESS;
98891038deSjeremylt }
99891038deSjeremylt 
100891038deSjeremylt //------------------------------------------------------------------------------
101777ff853SJeremy L Thompson // QFunctionContext Get Data
102777ff853SJeremy L Thompson //------------------------------------------------------------------------------
1032b730f8bSJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
104777ff853SJeremy L Thompson   Ceed                      ceed;
105*ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
106*ad70ee2cSJeremy L Thompson 
107*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, (void *)&impl));
1082b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
109777ff853SJeremy L Thompson 
1106574a04fSJeremy 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;
113e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
114777ff853SJeremy L Thompson }
115777ff853SJeremy L Thompson 
116777ff853SJeremy L Thompson //------------------------------------------------------------------------------
117777ff853SJeremy L Thompson // QFunctionContext Restore Data
118777ff853SJeremy L Thompson //------------------------------------------------------------------------------
1192b730f8bSJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) { return CEED_ERROR_SUCCESS; }
120777ff853SJeremy L Thompson 
121777ff853SJeremy L Thompson //------------------------------------------------------------------------------
122777ff853SJeremy L Thompson // QFunctionContext Destroy
123777ff853SJeremy L Thompson //------------------------------------------------------------------------------
124777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
125777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
126777ff853SJeremy L Thompson 
127*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1282b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_owned));
1292b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
130e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
131777ff853SJeremy L Thompson }
132777ff853SJeremy L Thompson 
133777ff853SJeremy L Thompson //------------------------------------------------------------------------------
134777ff853SJeremy L Thompson // QFunctionContext Create
135777ff853SJeremy L Thompson //------------------------------------------------------------------------------
136777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
137777ff853SJeremy L Thompson   Ceed                      ceed;
138*ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
139777ff853SJeremy L Thompson 
140*ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
1412b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Ref));
1422b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Ref));
1432b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Ref));
1442b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Ref));
1452b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Ref));
1462b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetData_Ref));
1472b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData", CeedQFunctionContextRestoreData_Ref));
1482b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead", CeedQFunctionContextRestoreData_Ref));
1492b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Ref));
1502b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
1512b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl));
152e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
153777ff853SJeremy L Thompson }
1542a86cc9dSSebastian Grimberg 
155777ff853SJeremy L Thompson //------------------------------------------------------------------------------
156