xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-qfunctioncontext.c (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
1*3d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2*3d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3777ff853SJeremy L Thompson //
4*3d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5777ff853SJeremy L Thompson //
6*3d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7777ff853SJeremy L Thompson 
8ec3da8bcSJed Brown #include <ceed/ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d576824SJeremy L Thompson #include <string.h>
11777ff853SJeremy L Thompson #include "ceed-ref.h"
12777ff853SJeremy L Thompson 
13777ff853SJeremy L Thompson //------------------------------------------------------------------------------
149c774eddSJeremy L Thompson // QFunctionContext has valid data
159c774eddSJeremy L Thompson //------------------------------------------------------------------------------
169c774eddSJeremy L Thompson static int CeedQFunctionContextHasValidData_Ref(CeedQFunctionContext ctx,
179c774eddSJeremy L Thompson     bool *has_valid_data) {
189c774eddSJeremy L Thompson   int ierr;
199c774eddSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
209c774eddSJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
219c774eddSJeremy L Thompson   CeedChkBackend(ierr);
229c774eddSJeremy L Thompson 
239c774eddSJeremy L Thompson   *has_valid_data = !!impl->data;
249c774eddSJeremy L Thompson 
259c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
269c774eddSJeremy L Thompson }
279c774eddSJeremy L Thompson 
289c774eddSJeremy L Thompson //------------------------------------------------------------------------------
299c774eddSJeremy L Thompson // QFunctionContext has borrowed data
309c774eddSJeremy L Thompson //------------------------------------------------------------------------------
319c774eddSJeremy L Thompson static int CeedQFunctionContextHasBorrowedDataOfType_Ref(
329c774eddSJeremy L Thompson   CeedQFunctionContext ctx, CeedMemType mem_type,
339c774eddSJeremy L Thompson   bool *has_borrowed_data_of_type) {
349c774eddSJeremy L Thompson   int ierr;
359c774eddSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
369c774eddSJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
379c774eddSJeremy L Thompson   CeedChkBackend(ierr);
389c774eddSJeremy L Thompson   Ceed ceed;
399c774eddSJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
409c774eddSJeremy L Thompson 
419c774eddSJeremy L Thompson   switch (mem_type) {
429c774eddSJeremy L Thompson   case CEED_MEM_HOST:
439c774eddSJeremy L Thompson     *has_borrowed_data_of_type = !!impl->data_borrowed;
449c774eddSJeremy L Thompson     break;
459c774eddSJeremy L Thompson   default:
469c774eddSJeremy L Thompson     // LCOV_EXCL_START
479c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
489c774eddSJeremy L Thompson                      "Can only set HOST memory for this backend");
499c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
509c774eddSJeremy L Thompson     break;
519c774eddSJeremy L Thompson   }
529c774eddSJeremy L Thompson 
539c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
549c774eddSJeremy L Thompson }
559c774eddSJeremy L Thompson 
569c774eddSJeremy L Thompson //------------------------------------------------------------------------------
57777ff853SJeremy L Thompson // QFunctionContext Set Data
58777ff853SJeremy L Thompson //------------------------------------------------------------------------------
59777ff853SJeremy L Thompson static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx,
609c774eddSJeremy L Thompson     CeedMemType mem_type, CeedCopyMode copy_mode, void *data) {
61777ff853SJeremy L Thompson   int ierr;
62777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
63e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
64e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
65d1d35e2fSjeremylt   size_t ctx_size;
66d1d35e2fSjeremylt   ierr = CeedQFunctionContextGetContextSize(ctx, &ctx_size); CeedChkBackend(ierr);
67777ff853SJeremy L Thompson   Ceed ceed;
68e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
69777ff853SJeremy L Thompson 
70d1d35e2fSjeremylt   if (mem_type != CEED_MEM_HOST)
71777ff853SJeremy L Thompson     // LCOV_EXCL_START
729c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
739c774eddSJeremy L Thompson                      "Can only set HOST memory for this backend");
74777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
759c774eddSJeremy L Thompson 
769c774eddSJeremy L Thompson   ierr = CeedFree(&impl->data_owned); CeedChkBackend(ierr);
77d1d35e2fSjeremylt   switch (copy_mode) {
78777ff853SJeremy L Thompson   case CEED_COPY_VALUES:
799c774eddSJeremy L Thompson     ierr = CeedMallocArray(1, ctx_size, &impl->data_owned); CeedChkBackend(ierr);
809c774eddSJeremy L Thompson     impl->data_borrowed = NULL;
819c774eddSJeremy L Thompson     impl->data = impl->data_owned;
82d1d35e2fSjeremylt     memcpy(impl->data, data, ctx_size);
83777ff853SJeremy L Thompson     break;
84777ff853SJeremy L Thompson   case CEED_OWN_POINTER:
859c774eddSJeremy L Thompson     impl->data_owned = data;
869c774eddSJeremy L Thompson     impl->data_borrowed = NULL;
87777ff853SJeremy L Thompson     impl->data = data;
88777ff853SJeremy L Thompson     break;
89777ff853SJeremy L Thompson   case CEED_USE_POINTER:
909c774eddSJeremy L Thompson     impl->data_borrowed = data;
91777ff853SJeremy L Thompson     impl->data = data;
92777ff853SJeremy L Thompson   }
93e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
94777ff853SJeremy L Thompson }
95777ff853SJeremy L Thompson 
96777ff853SJeremy L Thompson //------------------------------------------------------------------------------
97891038deSjeremylt // QFunctionContext Take Data
98891038deSjeremylt //------------------------------------------------------------------------------
99891038deSjeremylt static int CeedQFunctionContextTakeData_Ref(CeedQFunctionContext ctx,
1009c774eddSJeremy L Thompson     CeedMemType mem_type, void *data) {
101891038deSjeremylt   int ierr;
102891038deSjeremylt   CeedQFunctionContext_Ref *impl;
103891038deSjeremylt   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
104891038deSjeremylt   CeedChkBackend(ierr);
105891038deSjeremylt   Ceed ceed;
106891038deSjeremylt   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
107891038deSjeremylt 
108891038deSjeremylt   if (mem_type != CEED_MEM_HOST)
109891038deSjeremylt     // LCOV_EXCL_START
1109c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
1119c774eddSJeremy L Thompson                      "Can only provide HOST memory for this backend");
112891038deSjeremylt   // LCOV_EXCL_STOP
1139c774eddSJeremy L Thompson 
114891038deSjeremylt   *(void **)data = impl->data;
1159c774eddSJeremy L Thompson   impl->data_borrowed = NULL;
116891038deSjeremylt   impl->data = NULL;
1179c774eddSJeremy L Thompson 
118891038deSjeremylt   return CEED_ERROR_SUCCESS;
119891038deSjeremylt }
120891038deSjeremylt 
121891038deSjeremylt //------------------------------------------------------------------------------
122777ff853SJeremy L Thompson // QFunctionContext Get Data
123777ff853SJeremy L Thompson //------------------------------------------------------------------------------
124777ff853SJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx,
1259c774eddSJeremy L Thompson     CeedMemType mem_type, void *data) {
126777ff853SJeremy L Thompson   int ierr;
127777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
128e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
129e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
130777ff853SJeremy L Thompson   Ceed ceed;
131e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
132777ff853SJeremy L Thompson 
133d1d35e2fSjeremylt   if (mem_type != CEED_MEM_HOST)
134777ff853SJeremy L Thompson     // LCOV_EXCL_START
1359c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
1369c774eddSJeremy L Thompson                      "Can only provide HOST memory for this backend");
137777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
1389c774eddSJeremy L Thompson 
139777ff853SJeremy L Thompson   *(void **)data = impl->data;
1409c774eddSJeremy L Thompson 
141e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
142777ff853SJeremy L Thompson }
143777ff853SJeremy L Thompson 
144777ff853SJeremy L Thompson //------------------------------------------------------------------------------
145777ff853SJeremy L Thompson // QFunctionContext Restore Data
146777ff853SJeremy L Thompson //------------------------------------------------------------------------------
147777ff853SJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) {
148e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
149777ff853SJeremy L Thompson }
150777ff853SJeremy L Thompson 
151777ff853SJeremy L Thompson //------------------------------------------------------------------------------
152777ff853SJeremy L Thompson // QFunctionContext Destroy
153777ff853SJeremy L Thompson //------------------------------------------------------------------------------
154777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
155777ff853SJeremy L Thompson   int ierr;
156777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
157e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, &impl); CeedChkBackend(ierr);
158777ff853SJeremy L Thompson 
1599c774eddSJeremy L Thompson   ierr = CeedFree(&impl->data_owned); CeedChkBackend(ierr);
160e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl); CeedChkBackend(ierr);
161e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
162777ff853SJeremy L Thompson }
163777ff853SJeremy L Thompson 
164777ff853SJeremy L Thompson //------------------------------------------------------------------------------
165777ff853SJeremy L Thompson // QFunctionContext Create
166777ff853SJeremy L Thompson //------------------------------------------------------------------------------
167777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
168777ff853SJeremy L Thompson   int ierr;
169777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
170777ff853SJeremy L Thompson   Ceed ceed;
171e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
172777ff853SJeremy L Thompson 
1739c774eddSJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData",
1749c774eddSJeremy L Thompson                                 CeedQFunctionContextHasValidData_Ref);
1759c774eddSJeremy L Thompson   CeedChkBackend(ierr);
1769c774eddSJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx,
1779c774eddSJeremy L Thompson                                 "HasBorrowedDataOfType",
1789c774eddSJeremy L Thompson                                 CeedQFunctionContextHasBorrowedDataOfType_Ref);
1799c774eddSJeremy L Thompson   CeedChkBackend(ierr);
180777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData",
181e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextSetData_Ref); CeedChkBackend(ierr);
182891038deSjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData",
183891038deSjeremylt                                 CeedQFunctionContextTakeData_Ref); CeedChkBackend(ierr);
184777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData",
185e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextGetData_Ref); CeedChkBackend(ierr);
18628bfd0b7SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead",
18728bfd0b7SJeremy L Thompson                                 CeedQFunctionContextGetData_Ref); CeedChkBackend(ierr);
188777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData",
189e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextRestoreData_Ref); CeedChkBackend(ierr);
19028bfd0b7SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead",
19128bfd0b7SJeremy L Thompson                                 CeedQFunctionContextRestoreData_Ref); CeedChkBackend(ierr);
192777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy",
193e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextDestroy_Ref); CeedChkBackend(ierr);
1949c774eddSJeremy L Thompson 
195e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
196e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextSetBackendData(ctx, impl); CeedChkBackend(ierr);
1979c774eddSJeremy L Thompson 
198e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
199777ff853SJeremy L Thompson }
200777ff853SJeremy L Thompson //------------------------------------------------------------------------------
201