xref: /libCEED/interface/ceed-qfunctioncontext.c (revision 34359f16efbc10f0e69405f262a23387c38de222)
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 
108*34359f16Sjeremylt /**
109*34359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunctionContext
110*34359f16Sjeremylt 
111*34359f16Sjeremylt   @param ctx  CeedQFunctionContext to increment the reference counter
112*34359f16Sjeremylt 
113*34359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
114*34359f16Sjeremylt 
115*34359f16Sjeremylt   @ref Backend
116*34359f16Sjeremylt **/
117*34359f16Sjeremylt int CeedQFunctionContextIncrementRefCounter(CeedQFunctionContext ctx) {
118*34359f16Sjeremylt   ctx->ref_count++;
119*34359f16Sjeremylt   return CEED_ERROR_SUCCESS;
120*34359f16Sjeremylt }
121*34359f16Sjeremylt 
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*34359f16Sjeremylt   ierr = CeedIncrementRefCounter(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 /**
167777ff853SJeremy L Thompson   @brief Set the data used by a CeedQFunctionContext, freeing any previously allocated
168777ff853SJeremy L Thompson            data if applicable. The backend may copy values to a different
169777ff853SJeremy L Thompson            memtype, such as during @ref CeedQFunctionApply().
170777ff853SJeremy L Thompson            See also @ref CeedQFunctionContextTakeData().
171777ff853SJeremy L Thompson 
172777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext
173d1d35e2fSjeremylt   @param mem_type   Memory type of the data being passed
174d1d35e2fSjeremylt   @param copy_mode  Copy mode for the data
175777ff853SJeremy L Thompson   @param data       Data to be used
176777ff853SJeremy L Thompson 
177777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
178777ff853SJeremy L Thompson 
179777ff853SJeremy L Thompson   @ref User
180777ff853SJeremy L Thompson **/
181d1d35e2fSjeremylt int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type,
182d1d35e2fSjeremylt                                 CeedCopyMode copy_mode,
183777ff853SJeremy L Thompson                                 size_t size, void *data) {
184777ff853SJeremy L Thompson   int ierr;
185777ff853SJeremy L Thompson 
186777ff853SJeremy L Thompson   if (!ctx->SetData)
187777ff853SJeremy L Thompson     // LCOV_EXCL_START
188e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
189e15f9bd0SJeremy L Thompson                      "Backend does not support ContextSetData");
190777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
191777ff853SJeremy L Thompson 
192777ff853SJeremy L Thompson   if (ctx->state % 2 == 1)
193777ff853SJeremy L Thompson     // LCOV_EXCL_START
194777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
195777ff853SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
196777ff853SJeremy L Thompson                      "access lock is already in use");
197777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
198777ff853SJeremy L Thompson 
199d1d35e2fSjeremylt   ctx->ctx_size = size;
200d1d35e2fSjeremylt   ierr = ctx->SetData(ctx, mem_type, copy_mode, data); CeedChk(ierr);
201777ff853SJeremy L Thompson   ctx->state += 2;
202e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
203777ff853SJeremy L Thompson }
204777ff853SJeremy L Thompson 
205777ff853SJeremy L Thompson /**
206777ff853SJeremy L Thompson   @brief Get read/write access to a CeedQFunctionContext via the specified memory type.
207777ff853SJeremy L Thompson            Restore access with @ref CeedQFunctionContextRestoreData().
208777ff853SJeremy L Thompson 
209777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext to access
210d1d35e2fSjeremylt   @param mem_type   Memory type on which to access the data. If the backend
211777ff853SJeremy L Thompson                       uses a different memory type, this will perform a copy.
212d1d35e2fSjeremylt   @param[out] data  Data on memory type mem_type
213777ff853SJeremy L Thompson 
214777ff853SJeremy L Thompson   @note The CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions
215777ff853SJeremy L Thompson     provide access to array pointers in the desired memory space. Pairing
216777ff853SJeremy L Thompson     get/restore allows the Context to track access.
217777ff853SJeremy L Thompson 
218777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
219777ff853SJeremy L Thompson 
220777ff853SJeremy L Thompson   @ref User
221777ff853SJeremy L Thompson **/
222d1d35e2fSjeremylt int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type,
223777ff853SJeremy L Thompson                                 void *data) {
224777ff853SJeremy L Thompson   int ierr;
225777ff853SJeremy L Thompson 
226777ff853SJeremy L Thompson   if (!ctx->GetData)
227777ff853SJeremy L Thompson     // LCOV_EXCL_START
228e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
229e15f9bd0SJeremy L Thompson                      "Backend does not support GetData");
230777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
231777ff853SJeremy L Thompson 
232777ff853SJeremy L Thompson   if (ctx->state % 2 == 1)
233777ff853SJeremy L Thompson     // LCOV_EXCL_START
234777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
235777ff853SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
236777ff853SJeremy L Thompson                      "access lock is already in use");
237777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
238777ff853SJeremy L Thompson 
239d1d35e2fSjeremylt   ierr = ctx->GetData(ctx, mem_type, data); CeedChk(ierr);
240777ff853SJeremy L Thompson   ctx->state += 1;
241e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
242777ff853SJeremy L Thompson }
243777ff853SJeremy L Thompson 
244777ff853SJeremy L Thompson /**
245777ff853SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetData()
246777ff853SJeremy L Thompson 
247777ff853SJeremy L Thompson   @param ctx   CeedQFunctionContext to restore
248777ff853SJeremy L Thompson   @param data  Data to restore
249777ff853SJeremy L Thompson 
250777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
251777ff853SJeremy L Thompson 
252777ff853SJeremy L Thompson   @ref User
253777ff853SJeremy L Thompson **/
254777ff853SJeremy L Thompson int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) {
255777ff853SJeremy L Thompson   int ierr;
256777ff853SJeremy L Thompson 
257777ff853SJeremy L Thompson   if (!ctx->RestoreData)
258777ff853SJeremy L Thompson     // LCOV_EXCL_START
259e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
260e15f9bd0SJeremy L Thompson                      "Backend does not support RestoreData");
261777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
262777ff853SJeremy L Thompson 
263777ff853SJeremy L Thompson   if (ctx->state % 2 != 1)
264777ff853SJeremy L Thompson     // LCOV_EXCL_START
265777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
266777ff853SJeremy L Thompson                      "Cannot restore CeedQFunctionContext array access, "
267777ff853SJeremy L Thompson                      "access was not granted");
268777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
269777ff853SJeremy L Thompson 
270777ff853SJeremy L Thompson   ierr = ctx->RestoreData(ctx); CeedChk(ierr);
271777ff853SJeremy L Thompson   *(void **)data = NULL;
272777ff853SJeremy L Thompson   ctx->state += 1;
273e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
274777ff853SJeremy L Thompson }
275777ff853SJeremy L Thompson 
276777ff853SJeremy L Thompson /**
277777ff853SJeremy L Thompson   @brief View a CeedQFunctionContext
278777ff853SJeremy L Thompson 
279777ff853SJeremy L Thompson   @param[in] ctx     CeedQFunctionContext to view
280777ff853SJeremy L Thompson   @param[in] stream  Filestream to write to
281777ff853SJeremy L Thompson 
282777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
283777ff853SJeremy L Thompson 
284777ff853SJeremy L Thompson   @ref User
285777ff853SJeremy L Thompson **/
286777ff853SJeremy L Thompson int CeedQFunctionContextView(CeedQFunctionContext ctx, FILE *stream) {
287777ff853SJeremy L Thompson   fprintf(stream, "CeedQFunctionContext\n");
288d1d35e2fSjeremylt   fprintf(stream, "  Context Data Size: %ld\n", ctx->ctx_size);
289e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
290777ff853SJeremy L Thompson }
291777ff853SJeremy L Thompson 
292777ff853SJeremy L Thompson /**
293777ff853SJeremy L Thompson   @brief Destroy a CeedQFunctionContext
294777ff853SJeremy L Thompson 
295777ff853SJeremy L Thompson   @param ctx  CeedQFunctionContext to destroy
296777ff853SJeremy L Thompson 
297777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
298777ff853SJeremy L Thompson 
299777ff853SJeremy L Thompson   @ref User
300777ff853SJeremy L Thompson **/
301777ff853SJeremy L Thompson int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx) {
302777ff853SJeremy L Thompson   int ierr;
303777ff853SJeremy L Thompson 
304d1d35e2fSjeremylt   if (!*ctx || --(*ctx)->ref_count > 0)
305e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
306777ff853SJeremy L Thompson 
307777ff853SJeremy L Thompson   if ((*ctx) && ((*ctx)->state % 2) == 1)
308777ff853SJeremy L Thompson     // LCOV_EXCL_START
309777ff853SJeremy L Thompson     return CeedError((*ctx)->ceed, 1,
310777ff853SJeremy L Thompson                      "Cannot destroy CeedQFunctionContext, the access "
311777ff853SJeremy L Thompson                      "lock is in use");
312777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
313777ff853SJeremy L Thompson 
314777ff853SJeremy L Thompson   if ((*ctx)->Destroy) {
315777ff853SJeremy L Thompson     ierr = (*ctx)->Destroy(*ctx); CeedChk(ierr);
316777ff853SJeremy L Thompson   }
317777ff853SJeremy L Thompson   ierr = CeedDestroy(&(*ctx)->ceed); CeedChk(ierr);
318777ff853SJeremy L Thompson   ierr = CeedFree(ctx); CeedChk(ierr);
319e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
320777ff853SJeremy L Thompson }
321777ff853SJeremy L Thompson 
322777ff853SJeremy L Thompson /// @}
323