xref: /libCEED/interface/ceed-qfunctioncontext.c (revision d1d35e2f02dc969aee8debf3fd943dd784aa847a)
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
66*d1d35e2fSjeremylt   @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,
73*d1d35e2fSjeremylt                                        size_t *ctx_size) {
74*d1d35e2fSjeremylt   *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 
108777ff853SJeremy L Thompson /// @}
109777ff853SJeremy L Thompson 
110777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
111777ff853SJeremy L Thompson /// CeedQFunctionContext Public API
112777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
113777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionUser
114777ff853SJeremy L Thompson /// @{
115777ff853SJeremy L Thompson 
116777ff853SJeremy L Thompson /**
117777ff853SJeremy L Thompson   @brief Create a CeedQFunctionContext for storing CeedQFunction user context data
118777ff853SJeremy L Thompson 
119777ff853SJeremy L Thompson   @param ceed      A Ceed object where the CeedQFunctionContext will be created
120777ff853SJeremy L Thompson   @param[out] ctx  Address of the variable where the newly created
121777ff853SJeremy L Thompson                      CeedQFunctionContext will be stored
122777ff853SJeremy L Thompson 
123777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
124777ff853SJeremy L Thompson 
125777ff853SJeremy L Thompson   @ref User
126777ff853SJeremy L Thompson **/
127777ff853SJeremy L Thompson int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) {
128777ff853SJeremy L Thompson   int ierr;
129777ff853SJeremy L Thompson 
130777ff853SJeremy L Thompson   if (!ceed->QFunctionContextCreate) {
131777ff853SJeremy L Thompson     Ceed delegate;
132777ff853SJeremy L Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "Context"); CeedChk(ierr);
133777ff853SJeremy L Thompson 
134777ff853SJeremy L Thompson     if (!delegate)
135777ff853SJeremy L Thompson       // LCOV_EXCL_START
136e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
137e15f9bd0SJeremy L Thompson                        "Backend does not support ContextCreate");
138777ff853SJeremy L Thompson     // LCOV_EXCL_STOP
139777ff853SJeremy L Thompson 
140777ff853SJeremy L Thompson     ierr = CeedQFunctionContextCreate(delegate, ctx); CeedChk(ierr);
141e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
142777ff853SJeremy L Thompson   }
143777ff853SJeremy L Thompson 
144777ff853SJeremy L Thompson   ierr = CeedCalloc(1, ctx); CeedChk(ierr);
145777ff853SJeremy L Thompson   (*ctx)->ceed = ceed;
146*d1d35e2fSjeremylt   ceed->ref_count++;
147*d1d35e2fSjeremylt   (*ctx)->ref_count = 1;
148777ff853SJeremy L Thompson   ierr = ceed->QFunctionContextCreate(*ctx); CeedChk(ierr);
149e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
150777ff853SJeremy L Thompson }
151777ff853SJeremy L Thompson 
152777ff853SJeremy L Thompson /**
153777ff853SJeremy L Thompson   @brief Set the data used by a CeedQFunctionContext, freeing any previously allocated
154777ff853SJeremy L Thompson            data if applicable. The backend may copy values to a different
155777ff853SJeremy L Thompson            memtype, such as during @ref CeedQFunctionApply().
156777ff853SJeremy L Thompson            See also @ref CeedQFunctionContextTakeData().
157777ff853SJeremy L Thompson 
158777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext
159*d1d35e2fSjeremylt   @param mem_type   Memory type of the data being passed
160*d1d35e2fSjeremylt   @param copy_mode  Copy mode for the data
161777ff853SJeremy L Thompson   @param data       Data to be used
162777ff853SJeremy L Thompson 
163777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
164777ff853SJeremy L Thompson 
165777ff853SJeremy L Thompson   @ref User
166777ff853SJeremy L Thompson **/
167*d1d35e2fSjeremylt int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type,
168*d1d35e2fSjeremylt                                 CeedCopyMode copy_mode,
169777ff853SJeremy L Thompson                                 size_t size, void *data) {
170777ff853SJeremy L Thompson   int ierr;
171777ff853SJeremy L Thompson 
172777ff853SJeremy L Thompson   if (!ctx->SetData)
173777ff853SJeremy L Thompson     // LCOV_EXCL_START
174e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
175e15f9bd0SJeremy L Thompson                      "Backend does not support ContextSetData");
176777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
177777ff853SJeremy L Thompson 
178777ff853SJeremy L Thompson   if (ctx->state % 2 == 1)
179777ff853SJeremy L Thompson     // LCOV_EXCL_START
180777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
181777ff853SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
182777ff853SJeremy L Thompson                      "access lock is already in use");
183777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
184777ff853SJeremy L Thompson 
185*d1d35e2fSjeremylt   ctx->ctx_size = size;
186*d1d35e2fSjeremylt   ierr = ctx->SetData(ctx, mem_type, copy_mode, data); CeedChk(ierr);
187777ff853SJeremy L Thompson   ctx->state += 2;
188e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
189777ff853SJeremy L Thompson }
190777ff853SJeremy L Thompson 
191777ff853SJeremy L Thompson /**
192777ff853SJeremy L Thompson   @brief Get read/write access to a CeedQFunctionContext via the specified memory type.
193777ff853SJeremy L Thompson            Restore access with @ref CeedQFunctionContextRestoreData().
194777ff853SJeremy L Thompson 
195777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext to access
196*d1d35e2fSjeremylt   @param mem_type   Memory type on which to access the data. If the backend
197777ff853SJeremy L Thompson                       uses a different memory type, this will perform a copy.
198*d1d35e2fSjeremylt   @param[out] data  Data on memory type mem_type
199777ff853SJeremy L Thompson 
200777ff853SJeremy L Thompson   @note The CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions
201777ff853SJeremy L Thompson     provide access to array pointers in the desired memory space. Pairing
202777ff853SJeremy L Thompson     get/restore allows the Context to track access.
203777ff853SJeremy L Thompson 
204777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
205777ff853SJeremy L Thompson 
206777ff853SJeremy L Thompson   @ref User
207777ff853SJeremy L Thompson **/
208*d1d35e2fSjeremylt int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type,
209777ff853SJeremy L Thompson                                 void *data) {
210777ff853SJeremy L Thompson   int ierr;
211777ff853SJeremy L Thompson 
212777ff853SJeremy L Thompson   if (!ctx->GetData)
213777ff853SJeremy L Thompson     // LCOV_EXCL_START
214e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
215e15f9bd0SJeremy L Thompson                      "Backend does not support GetData");
216777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
217777ff853SJeremy L Thompson 
218777ff853SJeremy L Thompson   if (ctx->state % 2 == 1)
219777ff853SJeremy L Thompson     // LCOV_EXCL_START
220777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
221777ff853SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
222777ff853SJeremy L Thompson                      "access lock is already in use");
223777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
224777ff853SJeremy L Thompson 
225*d1d35e2fSjeremylt   ierr = ctx->GetData(ctx, mem_type, data); CeedChk(ierr);
226777ff853SJeremy L Thompson   ctx->state += 1;
227e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
228777ff853SJeremy L Thompson }
229777ff853SJeremy L Thompson 
230777ff853SJeremy L Thompson /**
231777ff853SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetData()
232777ff853SJeremy L Thompson 
233777ff853SJeremy L Thompson   @param ctx   CeedQFunctionContext to restore
234777ff853SJeremy L Thompson   @param data  Data to restore
235777ff853SJeremy L Thompson 
236777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
237777ff853SJeremy L Thompson 
238777ff853SJeremy L Thompson   @ref User
239777ff853SJeremy L Thompson **/
240777ff853SJeremy L Thompson int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) {
241777ff853SJeremy L Thompson   int ierr;
242777ff853SJeremy L Thompson 
243777ff853SJeremy L Thompson   if (!ctx->RestoreData)
244777ff853SJeremy L Thompson     // LCOV_EXCL_START
245e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
246e15f9bd0SJeremy L Thompson                      "Backend does not support RestoreData");
247777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
248777ff853SJeremy L Thompson 
249777ff853SJeremy L Thompson   if (ctx->state % 2 != 1)
250777ff853SJeremy L Thompson     // LCOV_EXCL_START
251777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
252777ff853SJeremy L Thompson                      "Cannot restore CeedQFunctionContext array access, "
253777ff853SJeremy L Thompson                      "access was not granted");
254777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
255777ff853SJeremy L Thompson 
256777ff853SJeremy L Thompson   ierr = ctx->RestoreData(ctx); CeedChk(ierr);
257777ff853SJeremy L Thompson   *(void **)data = NULL;
258777ff853SJeremy L Thompson   ctx->state += 1;
259e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
260777ff853SJeremy L Thompson }
261777ff853SJeremy L Thompson 
262777ff853SJeremy L Thompson /**
263777ff853SJeremy L Thompson   @brief View a CeedQFunctionContext
264777ff853SJeremy L Thompson 
265777ff853SJeremy L Thompson   @param[in] ctx     CeedQFunctionContext to view
266777ff853SJeremy L Thompson   @param[in] stream  Filestream to write to
267777ff853SJeremy L Thompson 
268777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
269777ff853SJeremy L Thompson 
270777ff853SJeremy L Thompson   @ref User
271777ff853SJeremy L Thompson **/
272777ff853SJeremy L Thompson int CeedQFunctionContextView(CeedQFunctionContext ctx, FILE *stream) {
273777ff853SJeremy L Thompson   fprintf(stream, "CeedQFunctionContext\n");
274*d1d35e2fSjeremylt   fprintf(stream, "  Context Data Size: %ld\n", ctx->ctx_size);
275e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
276777ff853SJeremy L Thompson }
277777ff853SJeremy L Thompson 
278777ff853SJeremy L Thompson /**
279777ff853SJeremy L Thompson   @brief Destroy a CeedQFunctionContext
280777ff853SJeremy L Thompson 
281777ff853SJeremy L Thompson   @param ctx  CeedQFunctionContext to destroy
282777ff853SJeremy L Thompson 
283777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
284777ff853SJeremy L Thompson 
285777ff853SJeremy L Thompson   @ref User
286777ff853SJeremy L Thompson **/
287777ff853SJeremy L Thompson int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx) {
288777ff853SJeremy L Thompson   int ierr;
289777ff853SJeremy L Thompson 
290*d1d35e2fSjeremylt   if (!*ctx || --(*ctx)->ref_count > 0)
291e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
292777ff853SJeremy L Thompson 
293777ff853SJeremy L Thompson   if ((*ctx) && ((*ctx)->state % 2) == 1)
294777ff853SJeremy L Thompson     // LCOV_EXCL_START
295777ff853SJeremy L Thompson     return CeedError((*ctx)->ceed, 1,
296777ff853SJeremy L Thompson                      "Cannot destroy CeedQFunctionContext, the access "
297777ff853SJeremy L Thompson                      "lock is in use");
298777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
299777ff853SJeremy L Thompson 
300777ff853SJeremy L Thompson   if ((*ctx)->Destroy) {
301777ff853SJeremy L Thompson     ierr = (*ctx)->Destroy(*ctx); CeedChk(ierr);
302777ff853SJeremy L Thompson   }
303777ff853SJeremy L Thompson   ierr = CeedDestroy(&(*ctx)->ceed); CeedChk(ierr);
304777ff853SJeremy L Thompson   ierr = CeedFree(ctx); CeedChk(ierr);
305e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
306777ff853SJeremy L Thompson }
307777ff853SJeremy L Thompson 
308777ff853SJeremy L Thompson /// @}
309