xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-qfunctioncontext.c (revision 9c774eddf8c0b4f5416196d32c5355c9591a7190)
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 //------------------------------------------------------------------------------
23*9c774eddSJeremy L Thompson // QFunctionContext has valid data
24*9c774eddSJeremy L Thompson //------------------------------------------------------------------------------
25*9c774eddSJeremy L Thompson static int CeedQFunctionContextHasValidData_Ref(CeedQFunctionContext ctx,
26*9c774eddSJeremy L Thompson     bool *has_valid_data) {
27*9c774eddSJeremy L Thompson   int ierr;
28*9c774eddSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
29*9c774eddSJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
30*9c774eddSJeremy L Thompson   CeedChkBackend(ierr);
31*9c774eddSJeremy L Thompson 
32*9c774eddSJeremy L Thompson   *has_valid_data = !!impl->data;
33*9c774eddSJeremy L Thompson 
34*9c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
35*9c774eddSJeremy L Thompson }
36*9c774eddSJeremy L Thompson 
37*9c774eddSJeremy L Thompson //------------------------------------------------------------------------------
38*9c774eddSJeremy L Thompson // QFunctionContext has borrowed data
39*9c774eddSJeremy L Thompson //------------------------------------------------------------------------------
40*9c774eddSJeremy L Thompson static int CeedQFunctionContextHasBorrowedDataOfType_Ref(
41*9c774eddSJeremy L Thompson   CeedQFunctionContext ctx, CeedMemType mem_type,
42*9c774eddSJeremy L Thompson   bool *has_borrowed_data_of_type) {
43*9c774eddSJeremy L Thompson   int ierr;
44*9c774eddSJeremy L Thompson   CeedQFunctionContext_Ref *impl;
45*9c774eddSJeremy L Thompson   ierr = CeedQFunctionContextGetBackendData(ctx, (void *)&impl);
46*9c774eddSJeremy L Thompson   CeedChkBackend(ierr);
47*9c774eddSJeremy L Thompson   Ceed ceed;
48*9c774eddSJeremy L Thompson   ierr = CeedQFunctionContextGetCeed(ctx, &ceed); CeedChkBackend(ierr);
49*9c774eddSJeremy L Thompson 
50*9c774eddSJeremy L Thompson   switch (mem_type) {
51*9c774eddSJeremy L Thompson   case CEED_MEM_HOST:
52*9c774eddSJeremy L Thompson     *has_borrowed_data_of_type = !!impl->data_borrowed;
53*9c774eddSJeremy L Thompson     break;
54*9c774eddSJeremy L Thompson   default:
55*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
56*9c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
57*9c774eddSJeremy L Thompson                      "Can only set HOST memory for this backend");
58*9c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
59*9c774eddSJeremy L Thompson     break;
60*9c774eddSJeremy L Thompson   }
61*9c774eddSJeremy L Thompson 
62*9c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
63*9c774eddSJeremy L Thompson }
64*9c774eddSJeremy L Thompson 
65*9c774eddSJeremy L Thompson //------------------------------------------------------------------------------
66777ff853SJeremy L Thompson // QFunctionContext Set Data
67777ff853SJeremy L Thompson //------------------------------------------------------------------------------
68777ff853SJeremy L Thompson static int CeedQFunctionContextSetData_Ref(CeedQFunctionContext ctx,
69*9c774eddSJeremy 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
81*9c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
82*9c774eddSJeremy L Thompson                      "Can only set HOST memory for this backend");
83777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
84*9c774eddSJeremy L Thompson 
85*9c774eddSJeremy L Thompson   ierr = CeedFree(&impl->data_owned); CeedChkBackend(ierr);
86d1d35e2fSjeremylt   switch (copy_mode) {
87777ff853SJeremy L Thompson   case CEED_COPY_VALUES:
88*9c774eddSJeremy L Thompson     ierr = CeedMallocArray(1, ctx_size, &impl->data_owned); CeedChkBackend(ierr);
89*9c774eddSJeremy L Thompson     impl->data_borrowed = NULL;
90*9c774eddSJeremy 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:
94*9c774eddSJeremy L Thompson     impl->data_owned = data;
95*9c774eddSJeremy L Thompson     impl->data_borrowed = NULL;
96777ff853SJeremy L Thompson     impl->data = data;
97777ff853SJeremy L Thompson     break;
98777ff853SJeremy L Thompson   case CEED_USE_POINTER:
99*9c774eddSJeremy 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,
109*9c774eddSJeremy 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
119*9c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
120*9c774eddSJeremy L Thompson                      "Can only provide HOST memory for this backend");
121891038deSjeremylt   // LCOV_EXCL_STOP
122*9c774eddSJeremy L Thompson 
123891038deSjeremylt   *(void **)data = impl->data;
124*9c774eddSJeremy L Thompson   impl->data_borrowed = NULL;
125891038deSjeremylt   impl->data = NULL;
126*9c774eddSJeremy 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,
134*9c774eddSJeremy 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
144*9c774eddSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND,
145*9c774eddSJeremy L Thompson                      "Can only provide HOST memory for this backend");
146777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
147*9c774eddSJeremy L Thompson 
148777ff853SJeremy L Thompson   *(void **)data = impl->data;
149*9c774eddSJeremy 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 
168*9c774eddSJeremy 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 
182*9c774eddSJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData",
183*9c774eddSJeremy L Thompson                                 CeedQFunctionContextHasValidData_Ref);
184*9c774eddSJeremy L Thompson   CeedChkBackend(ierr);
185*9c774eddSJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx,
186*9c774eddSJeremy L Thompson                                 "HasBorrowedDataOfType",
187*9c774eddSJeremy L Thompson                                 CeedQFunctionContextHasBorrowedDataOfType_Ref);
188*9c774eddSJeremy 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);
195777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData",
196e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextRestoreData_Ref); CeedChkBackend(ierr);
197777ff853SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy",
198e15f9bd0SJeremy L Thompson                                 CeedQFunctionContextDestroy_Ref); CeedChkBackend(ierr);
199*9c774eddSJeremy L Thompson 
200e15f9bd0SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr);
201e15f9bd0SJeremy L Thompson   ierr = CeedQFunctionContextSetBackendData(ctx, impl); CeedChkBackend(ierr);
202*9c774eddSJeremy L Thompson 
203e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
204777ff853SJeremy L Thompson }
205777ff853SJeremy L Thompson //------------------------------------------------------------------------------
206