xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-qfunctioncontext.c (revision 9560d06a92c065fb7d600a8c20ade8d9a4cda324)
1777ff853SJeremy L Thompson // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2777ff853SJeremy L Thompson // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3777ff853SJeremy L Thompson // 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 <ceed-impl.h>
203d576824SJeremy L Thompson #include <stdint.h>
213d576824SJeremy L Thompson #include <stdio.h>
22777ff853SJeremy L Thompson 
23777ff853SJeremy L Thompson /// @file
24777ff853SJeremy L Thompson /// Implementation of public CeedQFunctionContext interfaces
25777ff853SJeremy L Thompson 
26777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
27777ff853SJeremy L Thompson /// CeedQFunctionContext Backend API
28777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
29777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionBackend
30777ff853SJeremy L Thompson /// @{
31777ff853SJeremy L Thompson 
32777ff853SJeremy L Thompson /**
33777ff853SJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunctionContext
34777ff853SJeremy L Thompson 
35777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext
36777ff853SJeremy L Thompson   @param[out] ceed  Variable to store Ceed
37777ff853SJeremy L Thompson 
38777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
39777ff853SJeremy L Thompson 
40777ff853SJeremy L Thompson   @ref Backend
41777ff853SJeremy L Thompson **/
42777ff853SJeremy L Thompson int CeedQFunctionContextGetCeed(CeedQFunctionContext ctx, Ceed *ceed) {
43777ff853SJeremy L Thompson   *ceed = ctx->ceed;
44e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
45777ff853SJeremy L Thompson }
46777ff853SJeremy L Thompson 
47777ff853SJeremy L Thompson /**
48777ff853SJeremy L Thompson   @brief Get the state of a CeedQFunctionContext
49777ff853SJeremy L Thompson 
50777ff853SJeremy L Thompson   @param ctx         CeedQFunctionContext to retrieve state
51777ff853SJeremy L Thompson   @param[out] state  Variable to store state
52777ff853SJeremy L Thompson 
53777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
54777ff853SJeremy L Thompson 
55777ff853SJeremy L Thompson   @ref Backend
56777ff853SJeremy L Thompson **/
57777ff853SJeremy L Thompson int CeedQFunctionContextGetState(CeedQFunctionContext ctx, uint64_t *state) {
58777ff853SJeremy L Thompson   *state = ctx->state;
59e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
60777ff853SJeremy L Thompson }
61777ff853SJeremy L Thompson 
62777ff853SJeremy L Thompson /**
63777ff853SJeremy L Thompson   @brief Get data size for a Context
64777ff853SJeremy L Thompson 
65777ff853SJeremy L Thompson   @param ctx            CeedQFunctionContext
66d1d35e2fSjeremylt   @param[out] ctx_size  Variable to store size of context data values
67777ff853SJeremy L Thompson 
68777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
69777ff853SJeremy L Thompson 
70777ff853SJeremy L Thompson   @ref Backend
71777ff853SJeremy L Thompson **/
72777ff853SJeremy L Thompson int CeedQFunctionContextGetContextSize(CeedQFunctionContext ctx,
73d1d35e2fSjeremylt                                        size_t *ctx_size) {
74d1d35e2fSjeremylt   *ctx_size = ctx->ctx_size;
75e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
76777ff853SJeremy L Thompson }
77777ff853SJeremy L Thompson 
78777ff853SJeremy L Thompson /**
79777ff853SJeremy L Thompson   @brief Get backend data of a CeedQFunctionContext
80777ff853SJeremy L Thompson 
81777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext
82777ff853SJeremy L Thompson   @param[out] data  Variable to store data
83777ff853SJeremy L Thompson 
84777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85777ff853SJeremy L Thompson 
86777ff853SJeremy L Thompson   @ref Backend
87777ff853SJeremy L Thompson **/
88777ff853SJeremy L Thompson int CeedQFunctionContextGetBackendData(CeedQFunctionContext ctx, void *data) {
89777ff853SJeremy L Thompson   *(void **)data = ctx->data;
90e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
91777ff853SJeremy L Thompson }
92777ff853SJeremy L Thompson 
93777ff853SJeremy L Thompson /**
94777ff853SJeremy L Thompson   @brief Set backend data of a CeedQFunctionContext
95777ff853SJeremy L Thompson 
96777ff853SJeremy L Thompson   @param[out] ctx  CeedQFunctionContext
97777ff853SJeremy L Thompson   @param data      Data to set
98777ff853SJeremy L Thompson 
99777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
100777ff853SJeremy L Thompson 
101777ff853SJeremy L Thompson   @ref Backend
102777ff853SJeremy L Thompson **/
103777ff853SJeremy L Thompson int CeedQFunctionContextSetBackendData(CeedQFunctionContext ctx, void *data) {
104777ff853SJeremy L Thompson   ctx->data = data;
105e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
106777ff853SJeremy L Thompson }
107777ff853SJeremy L Thompson 
10834359f16Sjeremylt /**
10934359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunctionContext
11034359f16Sjeremylt 
11134359f16Sjeremylt   @param ctx  CeedQFunctionContext to increment the reference counter
11234359f16Sjeremylt 
11334359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
11434359f16Sjeremylt 
11534359f16Sjeremylt   @ref Backend
11634359f16Sjeremylt **/
117*9560d06aSjeremylt int CeedQFunctionContextReference(CeedQFunctionContext ctx) {
11834359f16Sjeremylt   ctx->ref_count++;
11934359f16Sjeremylt   return CEED_ERROR_SUCCESS;
12034359f16Sjeremylt }
12134359f16Sjeremylt 
122777ff853SJeremy L Thompson /// @}
123777ff853SJeremy L Thompson 
124777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
125777ff853SJeremy L Thompson /// CeedQFunctionContext Public API
126777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
127777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionUser
128777ff853SJeremy L Thompson /// @{
129777ff853SJeremy L Thompson 
130777ff853SJeremy L Thompson /**
131777ff853SJeremy L Thompson   @brief Create a CeedQFunctionContext for storing CeedQFunction user context data
132777ff853SJeremy L Thompson 
133777ff853SJeremy L Thompson   @param ceed      A Ceed object where the CeedQFunctionContext will be created
134777ff853SJeremy L Thompson   @param[out] ctx  Address of the variable where the newly created
135777ff853SJeremy L Thompson                      CeedQFunctionContext will be stored
136777ff853SJeremy L Thompson 
137777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
138777ff853SJeremy L Thompson 
139777ff853SJeremy L Thompson   @ref User
140777ff853SJeremy L Thompson **/
141777ff853SJeremy L Thompson int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) {
142777ff853SJeremy L Thompson   int ierr;
143777ff853SJeremy L Thompson 
144777ff853SJeremy L Thompson   if (!ceed->QFunctionContextCreate) {
145777ff853SJeremy L Thompson     Ceed delegate;
146777ff853SJeremy L Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "Context"); CeedChk(ierr);
147777ff853SJeremy L Thompson 
148777ff853SJeremy L Thompson     if (!delegate)
149777ff853SJeremy L Thompson       // LCOV_EXCL_START
150e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
151e15f9bd0SJeremy L Thompson                        "Backend does not support ContextCreate");
152777ff853SJeremy L Thompson     // LCOV_EXCL_STOP
153777ff853SJeremy L Thompson 
154777ff853SJeremy L Thompson     ierr = CeedQFunctionContextCreate(delegate, ctx); CeedChk(ierr);
155e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
156777ff853SJeremy L Thompson   }
157777ff853SJeremy L Thompson 
158777ff853SJeremy L Thompson   ierr = CeedCalloc(1, ctx); CeedChk(ierr);
159777ff853SJeremy L Thompson   (*ctx)->ceed = ceed;
160*9560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
161d1d35e2fSjeremylt   (*ctx)->ref_count = 1;
162777ff853SJeremy L Thompson   ierr = ceed->QFunctionContextCreate(*ctx); CeedChk(ierr);
163e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
164777ff853SJeremy L Thompson }
165777ff853SJeremy L Thompson 
166777ff853SJeremy L Thompson /**
167*9560d06aSjeremylt   @brief Copy the pointer to a CeedQFunctionContext. Both pointers should
168*9560d06aSjeremylt            be destroyed with `CeedQFunctionContextDestroy()`;
169*9560d06aSjeremylt            Note: If `*ctx_copy` is non-NULL, then it is assumed that
170*9560d06aSjeremylt            `*ctx_copy` is a pointer to a CeedQFunctionContext. This
171*9560d06aSjeremylt            CeedQFunctionContext will be destroyed if `*ctx_copy` is the
172*9560d06aSjeremylt            only reference to this CeedQFunctionContext.
173*9560d06aSjeremylt 
174*9560d06aSjeremylt   @param ctx            CeedQFunctionContext to copy reference to
175*9560d06aSjeremylt   @param[out] ctx_copy  Variable to store copied reference
176*9560d06aSjeremylt 
177*9560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
178*9560d06aSjeremylt 
179*9560d06aSjeremylt   @ref User
180*9560d06aSjeremylt **/
181*9560d06aSjeremylt int CeedQFunctionContextReferenceCopy(CeedQFunctionContext ctx,
182*9560d06aSjeremylt                                       CeedQFunctionContext *ctx_copy) {
183*9560d06aSjeremylt   int ierr;
184*9560d06aSjeremylt 
185*9560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
186*9560d06aSjeremylt   ierr = CeedQFunctionContextDestroy(ctx_copy); CeedChk(ierr);
187*9560d06aSjeremylt   *ctx_copy = ctx;
188*9560d06aSjeremylt   return CEED_ERROR_SUCCESS;
189*9560d06aSjeremylt }
190*9560d06aSjeremylt 
191*9560d06aSjeremylt /**
192777ff853SJeremy L Thompson   @brief Set the data used by a CeedQFunctionContext, freeing any previously allocated
193777ff853SJeremy L Thompson            data if applicable. The backend may copy values to a different
194777ff853SJeremy L Thompson            memtype, such as during @ref CeedQFunctionApply().
195777ff853SJeremy L Thompson            See also @ref CeedQFunctionContextTakeData().
196777ff853SJeremy L Thompson 
197777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext
198d1d35e2fSjeremylt   @param mem_type   Memory type of the data being passed
199d1d35e2fSjeremylt   @param copy_mode  Copy mode for the data
200777ff853SJeremy L Thompson   @param data       Data to be used
201777ff853SJeremy L Thompson 
202777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
203777ff853SJeremy L Thompson 
204777ff853SJeremy L Thompson   @ref User
205777ff853SJeremy L Thompson **/
206d1d35e2fSjeremylt int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type,
207d1d35e2fSjeremylt                                 CeedCopyMode copy_mode,
208777ff853SJeremy L Thompson                                 size_t size, void *data) {
209777ff853SJeremy L Thompson   int ierr;
210777ff853SJeremy L Thompson 
211777ff853SJeremy L Thompson   if (!ctx->SetData)
212777ff853SJeremy L Thompson     // LCOV_EXCL_START
213e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
214e15f9bd0SJeremy L Thompson                      "Backend does not support ContextSetData");
215777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
216777ff853SJeremy L Thompson 
217777ff853SJeremy L Thompson   if (ctx->state % 2 == 1)
218777ff853SJeremy L Thompson     // LCOV_EXCL_START
219777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
220777ff853SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
221777ff853SJeremy L Thompson                      "access lock is already in use");
222777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
223777ff853SJeremy L Thompson 
224d1d35e2fSjeremylt   ctx->ctx_size = size;
225d1d35e2fSjeremylt   ierr = ctx->SetData(ctx, mem_type, copy_mode, data); CeedChk(ierr);
226777ff853SJeremy L Thompson   ctx->state += 2;
227e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
228777ff853SJeremy L Thompson }
229777ff853SJeremy L Thompson 
230777ff853SJeremy L Thompson /**
231777ff853SJeremy L Thompson   @brief Get read/write access to a CeedQFunctionContext via the specified memory type.
232777ff853SJeremy L Thompson            Restore access with @ref CeedQFunctionContextRestoreData().
233777ff853SJeremy L Thompson 
234777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext to access
235d1d35e2fSjeremylt   @param mem_type   Memory type on which to access the data. If the backend
236777ff853SJeremy L Thompson                       uses a different memory type, this will perform a copy.
237d1d35e2fSjeremylt   @param[out] data  Data on memory type mem_type
238777ff853SJeremy L Thompson 
239777ff853SJeremy L Thompson   @note The CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions
240777ff853SJeremy L Thompson     provide access to array pointers in the desired memory space. Pairing
241777ff853SJeremy L Thompson     get/restore allows the Context to track access.
242777ff853SJeremy L Thompson 
243777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
244777ff853SJeremy L Thompson 
245777ff853SJeremy L Thompson   @ref User
246777ff853SJeremy L Thompson **/
247d1d35e2fSjeremylt int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type,
248777ff853SJeremy L Thompson                                 void *data) {
249777ff853SJeremy L Thompson   int ierr;
250777ff853SJeremy L Thompson 
251777ff853SJeremy L Thompson   if (!ctx->GetData)
252777ff853SJeremy L Thompson     // LCOV_EXCL_START
253e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
254e15f9bd0SJeremy L Thompson                      "Backend does not support GetData");
255777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
256777ff853SJeremy L Thompson 
257777ff853SJeremy L Thompson   if (ctx->state % 2 == 1)
258777ff853SJeremy L Thompson     // LCOV_EXCL_START
259777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
260777ff853SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
261777ff853SJeremy L Thompson                      "access lock is already in use");
262777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
263777ff853SJeremy L Thompson 
264d1d35e2fSjeremylt   ierr = ctx->GetData(ctx, mem_type, data); CeedChk(ierr);
265777ff853SJeremy L Thompson   ctx->state += 1;
266e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
267777ff853SJeremy L Thompson }
268777ff853SJeremy L Thompson 
269777ff853SJeremy L Thompson /**
270777ff853SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetData()
271777ff853SJeremy L Thompson 
272777ff853SJeremy L Thompson   @param ctx   CeedQFunctionContext to restore
273777ff853SJeremy L Thompson   @param data  Data to restore
274777ff853SJeremy L Thompson 
275777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
276777ff853SJeremy L Thompson 
277777ff853SJeremy L Thompson   @ref User
278777ff853SJeremy L Thompson **/
279777ff853SJeremy L Thompson int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) {
280777ff853SJeremy L Thompson   int ierr;
281777ff853SJeremy L Thompson 
282777ff853SJeremy L Thompson   if (!ctx->RestoreData)
283777ff853SJeremy L Thompson     // LCOV_EXCL_START
284e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
285e15f9bd0SJeremy L Thompson                      "Backend does not support RestoreData");
286777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
287777ff853SJeremy L Thompson 
288777ff853SJeremy L Thompson   if (ctx->state % 2 != 1)
289777ff853SJeremy L Thompson     // LCOV_EXCL_START
290777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
291777ff853SJeremy L Thompson                      "Cannot restore CeedQFunctionContext array access, "
292777ff853SJeremy L Thompson                      "access was not granted");
293777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
294777ff853SJeremy L Thompson 
295777ff853SJeremy L Thompson   ierr = ctx->RestoreData(ctx); CeedChk(ierr);
296777ff853SJeremy L Thompson   *(void **)data = NULL;
297777ff853SJeremy L Thompson   ctx->state += 1;
298e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
299777ff853SJeremy L Thompson }
300777ff853SJeremy L Thompson 
301777ff853SJeremy L Thompson /**
302777ff853SJeremy L Thompson   @brief View a CeedQFunctionContext
303777ff853SJeremy L Thompson 
304777ff853SJeremy L Thompson   @param[in] ctx     CeedQFunctionContext to view
305777ff853SJeremy L Thompson   @param[in] stream  Filestream to write to
306777ff853SJeremy L Thompson 
307777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
308777ff853SJeremy L Thompson 
309777ff853SJeremy L Thompson   @ref User
310777ff853SJeremy L Thompson **/
311777ff853SJeremy L Thompson int CeedQFunctionContextView(CeedQFunctionContext ctx, FILE *stream) {
312777ff853SJeremy L Thompson   fprintf(stream, "CeedQFunctionContext\n");
313d1d35e2fSjeremylt   fprintf(stream, "  Context Data Size: %ld\n", ctx->ctx_size);
314e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
315777ff853SJeremy L Thompson }
316777ff853SJeremy L Thompson 
317777ff853SJeremy L Thompson /**
318777ff853SJeremy L Thompson   @brief Destroy a CeedQFunctionContext
319777ff853SJeremy L Thompson 
320777ff853SJeremy L Thompson   @param ctx  CeedQFunctionContext to destroy
321777ff853SJeremy L Thompson 
322777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
323777ff853SJeremy L Thompson 
324777ff853SJeremy L Thompson   @ref User
325777ff853SJeremy L Thompson **/
326777ff853SJeremy L Thompson int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx) {
327777ff853SJeremy L Thompson   int ierr;
328777ff853SJeremy L Thompson 
329d1d35e2fSjeremylt   if (!*ctx || --(*ctx)->ref_count > 0)
330e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
331777ff853SJeremy L Thompson 
332777ff853SJeremy L Thompson   if ((*ctx) && ((*ctx)->state % 2) == 1)
333777ff853SJeremy L Thompson     // LCOV_EXCL_START
334777ff853SJeremy L Thompson     return CeedError((*ctx)->ceed, 1,
335777ff853SJeremy L Thompson                      "Cannot destroy CeedQFunctionContext, the access "
336777ff853SJeremy L Thompson                      "lock is in use");
337777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
338777ff853SJeremy L Thompson 
339777ff853SJeremy L Thompson   if ((*ctx)->Destroy) {
340777ff853SJeremy L Thompson     ierr = (*ctx)->Destroy(*ctx); CeedChk(ierr);
341777ff853SJeremy L Thompson   }
342777ff853SJeremy L Thompson   ierr = CeedDestroy(&(*ctx)->ceed); CeedChk(ierr);
343777ff853SJeremy L Thompson   ierr = CeedFree(ctx); CeedChk(ierr);
344e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
345777ff853SJeremy L Thompson }
346777ff853SJeremy L Thompson 
347777ff853SJeremy L Thompson /// @}
348