xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-qfunctioncontext.c (revision 28bfd0b742d5bb98b1c0887e682aa0983923c5c6)
1777ff853SJeremy L Thompson // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2777ff853SJeremy L Thompson // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3777ff853SJeremy L Thompson // All Rights reserved. See files LICENSE and NOTICE for details.
4777ff853SJeremy L Thompson //
5777ff853SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software
6777ff853SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral
7777ff853SJeremy L Thompson // element discretizations for exascale applications. For more information and
8777ff853SJeremy L Thompson // source code availability see http://github.com/ceed.
9777ff853SJeremy L Thompson //
10777ff853SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11777ff853SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office
12777ff853SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for
13777ff853SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including
14777ff853SJeremy L Thompson // software, applications, hardware, advanced system engineering and early
15777ff853SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative.
16777ff853SJeremy L Thompson 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <string.h>
20777ff853SJeremy L Thompson #include "ceed-ref.h"
21777ff853SJeremy L Thompson 
22777ff853SJeremy L Thompson //------------------------------------------------------------------------------
239c774eddSJeremy L Thompson // QFunctionContext has valid data
249c774eddSJeremy L Thompson //------------------------------------------------------------------------------
259c774eddSJeremy L Thompson static int CeedQFunctionContextHasValidData_Ref(CeedQFunctionContext ctx,
269c774eddSJeremy L Thompson     bool *has_valid_data) {
279c774eddSJeremy L Thompson   int ierr;
289c774eddSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
299c774eddSJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
309c774eddSJeremy L Thompson   CeedChkBackend(ierr);
319c774eddSJeremy L Thompson 
329c774eddSJeremy L Thompson   *has_valid_data = !!impl->data;
339c774eddSJeremy L Thompson 
349c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
359c774eddSJeremy L Thompson }
369c774eddSJeremy L Thompson 
379c774eddSJeremy L Thompson //------------------------------------------------------------------------------
389c774eddSJeremy L Thompson // QFunctionContext has borrowed data
399c774eddSJeremy L Thompson //------------------------------------------------------------------------------
409c774eddSJeremy L Thompson static int CeedQFunctionContextHasBorrowedDataOfType_Ref(
419c774eddSJeremy L Thompson   CeedQFunctionContext ctx, CeedMemType mem_type,
429c774eddSJeremy L Thompson   bool *has_borrowed_data_of_type) {
439c774eddSJeremy L Thompson   int ierr;
449c774eddSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
459c774eddSJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
469c774eddSJeremy L Thompson   CeedChkBackend(ierr);
479c774eddSJeremy L Thompson   Ceed ceed;
489c774eddSJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
499c774eddSJeremy L Thompson 
509c774eddSJeremy L Thompson   switch (mem_type) {
519c774eddSJeremy L Thompson   case CEED_MEM_HOST:
529c774eddSJeremy L Thompson     *has_borrowed_data_of_type = !!impl->data_borrowed;
539c774eddSJeremy L Thompson     break;
549c774eddSJeremy L Thompson   default:
559c774eddSJeremy L Thompson     // LCOV_EXCL_START
569c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
579c774eddSJeremy L Thompson                      "Can only set HOST memory for this backend");
589c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
599c774eddSJeremy L Thompson     break;
609c774eddSJeremy L Thompson   }
619c774eddSJeremy L Thompson 
629c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
639c774eddSJeremy L Thompson }
649c774eddSJeremy L Thompson 
659c774eddSJeremy L Thompson //------------------------------------------------------------------------------
66777ff853SJeremy L Thompson // QFunctionContext Set Data
67777ff853SJeremy L Thompson //------------------------------------------------------------------------------
68777ff853SJeremy L Thompson static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx,
699c774eddSJeremy L Thompson     CeedMemType mem_type, CeedCopyMode copy_mode, void *data) {
70777ff853SJeremy L Thompson   int ierr;
71777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
72e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
73e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
74d1d35e2fSjeremylt   size_t ctx_size;
75d1d35e2fSjeremylt   ierr = CeedQFunctionContextGetContextSize(ctx, &ctx_size); CeedChkBackend(ierr);
76777ff853SJeremy L Thompson   Ceed ceed;
77e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
78777ff853SJeremy L Thompson 
79d1d35e2fSjeremylt   if (mem_type != CEED_MEM_HOST)
80777ff853SJeremy L Thompson     // LCOV_EXCL_START
819c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
829c774eddSJeremy L Thompson                      "Can only set HOST memory for this backend");
83777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
849c774eddSJeremy L Thompson 
859c774eddSJeremy L Thompson   ierr = CeedFree(&impl->data_owned); CeedChkBackend(ierr);
86d1d35e2fSjeremylt   switch (copy_mode) {
87777ff853SJeremy L Thompson   case CEED_COPY_VALUES:
889c774eddSJeremy L Thompson     ierr = CeedMallocArray(1, ctx_size, &impl->data_owned); CeedChkBackend(ierr);
899c774eddSJeremy L Thompson     impl->data_borrowed = NULL;
909c774eddSJeremy L Thompson     impl->data = impl->data_owned;
91d1d35e2fSjeremylt     memcpy(impl->data, data, ctx_size);
92777ff853SJeremy L Thompson     break;
93777ff853SJeremy L Thompson   case CEED_OWN_POINTER:
949c774eddSJeremy L Thompson     impl->data_owned = data;
959c774eddSJeremy L Thompson     impl->data_borrowed = NULL;
96777ff853SJeremy L Thompson     impl->data = data;
97777ff853SJeremy L Thompson     break;
98777ff853SJeremy L Thompson   case CEED_USE_POINTER:
999c774eddSJeremy L Thompson     impl->data_borrowed = data;
100777ff853SJeremy L Thompson     impl->data = data;
101777ff853SJeremy L Thompson   }
102e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
103777ff853SJeremy L Thompson }
104777ff853SJeremy L Thompson 
105777ff853SJeremy L Thompson //------------------------------------------------------------------------------
106891038deSjeremylt // QFunctionContext Take Data
107891038deSjeremylt //------------------------------------------------------------------------------
108891038deSjeremylt static int CeedQFunctionContextTakeData_Ref(CeedQFunctionContext ctx,
1099c774eddSJeremy L Thompson     CeedMemType mem_type, void *data) {
110891038deSjeremylt   int ierr;
111891038deSjeremylt   CeedQFunctionContext_Ref *impl;
112891038deSjeremylt   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
113891038deSjeremylt   CeedChkBackend(ierr);
114891038deSjeremylt   Ceed ceed;
115891038deSjeremylt   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
116891038deSjeremylt 
117891038deSjeremylt   if (mem_type != CEED_MEM_HOST)
118891038deSjeremylt     // LCOV_EXCL_START
1199c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
1209c774eddSJeremy L Thompson                      "Can only provide HOST memory for this backend");
121891038deSjeremylt   // LCOV_EXCL_STOP
1229c774eddSJeremy L Thompson 
123891038deSjeremylt   *(void **)data = impl->data;
1249c774eddSJeremy L Thompson   impl->data_borrowed = NULL;
125891038deSjeremylt   impl->data = NULL;
1269c774eddSJeremy L Thompson 
127891038deSjeremylt   return CEED_ERROR_SUCCESS;
128891038deSjeremylt }
129891038deSjeremylt 
130891038deSjeremylt //------------------------------------------------------------------------------
131777ff853SJeremy L Thompson // QFunctionContext Get Data
132777ff853SJeremy L Thompson //------------------------------------------------------------------------------
133777ff853SJeremy L Thompson static int CeedQFunctionContextGetData_Ref(CeedQFunctionContext ctx,
1349c774eddSJeremy L Thompson     CeedMemType mem_type, void *data) {
135777ff853SJeremy L Thompson   int ierr;
136777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
137e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
138e15f9bd0SJeremy L Thompson   CeedChkBackend(ierr);
139777ff853SJeremy L Thompson   Ceed ceed;
140e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
141777ff853SJeremy L Thompson 
142d1d35e2fSjeremylt   if (mem_type != CEED_MEM_HOST)
143777ff853SJeremy L Thompson     // LCOV_EXCL_START
1449c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
1459c774eddSJeremy L Thompson                      "Can only provide HOST memory for this backend");
146777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
1479c774eddSJeremy L Thompson 
148777ff853SJeremy L Thompson   *(void **)data = impl->data;
1499c774eddSJeremy L Thompson 
150e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
151777ff853SJeremy L Thompson }
152777ff853SJeremy L Thompson 
153777ff853SJeremy L Thompson //------------------------------------------------------------------------------
154777ff853SJeremy L Thompson // QFunctionContext Restore Data
155777ff853SJeremy L Thompson //------------------------------------------------------------------------------
156777ff853SJeremy L Thompson static int CeedQFunctionContextRestoreData_Ref(CeedQFunctionContext ctx) {
157e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
158777ff853SJeremy L Thompson }
159777ff853SJeremy L Thompson 
160777ff853SJeremy L Thompson //------------------------------------------------------------------------------
161777ff853SJeremy L Thompson // QFunctionContext Destroy
162777ff853SJeremy L Thompson //------------------------------------------------------------------------------
163777ff853SJeremy L Thompson static int CeedQFunctionContextDestroy_Ref(CeedQFunctionContext ctx) {
164777ff853SJeremy L Thompson   int ierr;
165777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
166e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, &impl); CeedChkBackend(ierr);
167777ff853SJeremy L Thompson 
1689c774eddSJeremy L Thompson   ierr = CeedFree(&impl->data_owned); CeedChkBackend(ierr);
169e15f9bd0SJeremy L Thompson   ierr = CeedFree(&impl); CeedChkBackend(ierr);
170e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
171777ff853SJeremy L Thompson }
172777ff853SJeremy L Thompson 
173777ff853SJeremy L Thompson //------------------------------------------------------------------------------
174777ff853SJeremy L Thompson // QFunctionContext Create
175777ff853SJeremy L Thompson //------------------------------------------------------------------------------
176777ff853SJeremy L Thompson int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) {
177777ff853SJeremy L Thompson   int ierr;
178777ff853SJeremy L Thompson   CeedQFunctionContext_Ref *impl;
179777ff853SJeremy L Thompson   Ceed ceed;
180e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
181777ff853SJeremy L Thompson 
1829c774eddSJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData",
1839c774eddSJeremy L Thompson                                 CeedQFunctionContextHasValidData_Ref);
1849c774eddSJeremy L Thompson   CeedChkBackend(ierr);
1859c774eddSJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx,
1869c774eddSJeremy L Thompson                                 "HasBorrowedDataOfType",
1879c774eddSJeremy L Thompson                                 CeedQFunctionContextHasBorrowedDataOfType_Ref);
1889c774eddSJeremy L Thompson   CeedChkBackend(ierr);
189777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData",
190e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextSetData_Ref); CeedChkBackend(ierr);
191891038deSjeremylt   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData",
192891038deSjeremylt                                 CeedQFunctionContextTakeData_Ref); CeedChkBackend(ierr);
193777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData",
194e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextGetData_Ref); CeedChkBackend(ierr);
195*28bfd0b7SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead",
196*28bfd0b7SJeremy L Thompson                                 CeedQFunctionContextGetData_Ref); CeedChkBackend(ierr);
197777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData",
198e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextRestoreData_Ref); CeedChkBackend(ierr);
199*28bfd0b7SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead",
200*28bfd0b7SJeremy L Thompson                                 CeedQFunctionContextRestoreData_Ref); CeedChkBackend(ierr);
201777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy",
202e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextDestroy_Ref); CeedChkBackend(ierr);
2039c774eddSJeremy L Thompson 
204e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
205e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextSetBackendData(ctx, impl); CeedChkBackend(ierr);
2069c774eddSJeremy L Thompson 
207e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
208777ff853SJeremy L Thompson }
209777ff853SJeremy L Thompson //------------------------------------------------------------------------------
210