xref: /libCEED/interface/ceed-qfunctioncontext.c (revision 34359f16efbc10f0e69405f262a23387c38de222)
1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3 // reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 #include <ceed/ceed.h>
18 #include <ceed/backend.h>
19 #include <ceed-impl.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 
23 /// @file
24 /// Implementation of public CeedQFunctionContext interfaces
25 
26 /// ----------------------------------------------------------------------------
27 /// CeedQFunctionContext Backend API
28 /// ----------------------------------------------------------------------------
29 /// @addtogroup CeedQFunctionBackend
30 /// @{
31 
32 /**
33   @brief Get the Ceed associated with a CeedQFunctionContext
34 
35   @param ctx        CeedQFunctionContext
36   @param[out] ceed  Variable to store Ceed
37 
38   @return An error code: 0 - success, otherwise - failure
39 
40   @ref Backend
41 **/
42 int CeedQFunctionContextGetCeed(CeedQFunctionContext ctx, Ceed *ceed) {
43   *ceed = ctx->ceed;
44   return CEED_ERROR_SUCCESS;
45 }
46 
47 /**
48   @brief Get the state of a CeedQFunctionContext
49 
50   @param ctx         CeedQFunctionContext to retrieve state
51   @param[out] state  Variable to store state
52 
53   @return An error code: 0 - success, otherwise - failure
54 
55   @ref Backend
56 **/
57 int CeedQFunctionContextGetState(CeedQFunctionContext ctx, uint64_t *state) {
58   *state = ctx->state;
59   return CEED_ERROR_SUCCESS;
60 }
61 
62 /**
63   @brief Get data size for a Context
64 
65   @param ctx            CeedQFunctionContext
66   @param[out] ctx_size  Variable to store size of context data values
67 
68   @return An error code: 0 - success, otherwise - failure
69 
70   @ref Backend
71 **/
72 int CeedQFunctionContextGetContextSize(CeedQFunctionContext ctx,
73                                        size_t *ctx_size) {
74   *ctx_size = ctx->ctx_size;
75   return CEED_ERROR_SUCCESS;
76 }
77 
78 /**
79   @brief Get backend data of a CeedQFunctionContext
80 
81   @param ctx        CeedQFunctionContext
82   @param[out] data  Variable to store data
83 
84   @return An error code: 0 - success, otherwise - failure
85 
86   @ref Backend
87 **/
88 int CeedQFunctionContextGetBackendData(CeedQFunctionContext ctx, void *data) {
89   *(void **)data = ctx->data;
90   return CEED_ERROR_SUCCESS;
91 }
92 
93 /**
94   @brief Set backend data of a CeedQFunctionContext
95 
96   @param[out] ctx  CeedQFunctionContext
97   @param data      Data to set
98 
99   @return An error code: 0 - success, otherwise - failure
100 
101   @ref Backend
102 **/
103 int CeedQFunctionContextSetBackendData(CeedQFunctionContext ctx, void *data) {
104   ctx->data = data;
105   return CEED_ERROR_SUCCESS;
106 }
107 
108 /**
109   @brief Increment the reference counter for a CeedQFunctionContext
110 
111   @param ctx  CeedQFunctionContext to increment the reference counter
112 
113   @return An error code: 0 - success, otherwise - failure
114 
115   @ref Backend
116 **/
117 int CeedQFunctionContextIncrementRefCounter(CeedQFunctionContext ctx) {
118   ctx->ref_count++;
119   return CEED_ERROR_SUCCESS;
120 }
121 
122 /// @}
123 
124 /// ----------------------------------------------------------------------------
125 /// CeedQFunctionContext Public API
126 /// ----------------------------------------------------------------------------
127 /// @addtogroup CeedQFunctionUser
128 /// @{
129 
130 /**
131   @brief Create a CeedQFunctionContext for storing CeedQFunction user context data
132 
133   @param ceed      A Ceed object where the CeedQFunctionContext will be created
134   @param[out] ctx  Address of the variable where the newly created
135                      CeedQFunctionContext will be stored
136 
137   @return An error code: 0 - success, otherwise - failure
138 
139   @ref User
140 **/
141 int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) {
142   int ierr;
143 
144   if (!ceed->QFunctionContextCreate) {
145     Ceed delegate;
146     ierr = CeedGetObjectDelegate(ceed, &delegate, "Context"); CeedChk(ierr);
147 
148     if (!delegate)
149       // LCOV_EXCL_START
150       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
151                        "Backend does not support ContextCreate");
152     // LCOV_EXCL_STOP
153 
154     ierr = CeedQFunctionContextCreate(delegate, ctx); CeedChk(ierr);
155     return CEED_ERROR_SUCCESS;
156   }
157 
158   ierr = CeedCalloc(1, ctx); CeedChk(ierr);
159   (*ctx)->ceed = ceed;
160   ierr = CeedIncrementRefCounter(ceed); CeedChk(ierr);
161   (*ctx)->ref_count = 1;
162   ierr = ceed->QFunctionContextCreate(*ctx); CeedChk(ierr);
163   return CEED_ERROR_SUCCESS;
164 }
165 
166 /**
167   @brief Set the data used by a CeedQFunctionContext, freeing any previously allocated
168            data if applicable. The backend may copy values to a different
169            memtype, such as during @ref CeedQFunctionApply().
170            See also @ref CeedQFunctionContextTakeData().
171 
172   @param ctx        CeedQFunctionContext
173   @param mem_type   Memory type of the data being passed
174   @param copy_mode  Copy mode for the data
175   @param data       Data to be used
176 
177   @return An error code: 0 - success, otherwise - failure
178 
179   @ref User
180 **/
181 int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type,
182                                 CeedCopyMode copy_mode,
183                                 size_t size, void *data) {
184   int ierr;
185 
186   if (!ctx->SetData)
187     // LCOV_EXCL_START
188     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
189                      "Backend does not support ContextSetData");
190   // LCOV_EXCL_STOP
191 
192   if (ctx->state % 2 == 1)
193     // LCOV_EXCL_START
194     return CeedError(ctx->ceed, 1,
195                      "Cannot grant CeedQFunctionContext data access, the "
196                      "access lock is already in use");
197   // LCOV_EXCL_STOP
198 
199   ctx->ctx_size = size;
200   ierr = ctx->SetData(ctx, mem_type, copy_mode, data); CeedChk(ierr);
201   ctx->state += 2;
202   return CEED_ERROR_SUCCESS;
203 }
204 
205 /**
206   @brief Get read/write access to a CeedQFunctionContext via the specified memory type.
207            Restore access with @ref CeedQFunctionContextRestoreData().
208 
209   @param ctx        CeedQFunctionContext to access
210   @param mem_type   Memory type on which to access the data. If the backend
211                       uses a different memory type, this will perform a copy.
212   @param[out] data  Data on memory type mem_type
213 
214   @note The CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions
215     provide access to array pointers in the desired memory space. Pairing
216     get/restore allows the Context to track access.
217 
218   @return An error code: 0 - success, otherwise - failure
219 
220   @ref User
221 **/
222 int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type,
223                                 void *data) {
224   int ierr;
225 
226   if (!ctx->GetData)
227     // LCOV_EXCL_START
228     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
229                      "Backend does not support GetData");
230   // LCOV_EXCL_STOP
231 
232   if (ctx->state % 2 == 1)
233     // LCOV_EXCL_START
234     return CeedError(ctx->ceed, 1,
235                      "Cannot grant CeedQFunctionContext data access, the "
236                      "access lock is already in use");
237   // LCOV_EXCL_STOP
238 
239   ierr = ctx->GetData(ctx, mem_type, data); CeedChk(ierr);
240   ctx->state += 1;
241   return CEED_ERROR_SUCCESS;
242 }
243 
244 /**
245   @brief Restore data obtained using @ref CeedQFunctionContextGetData()
246 
247   @param ctx   CeedQFunctionContext to restore
248   @param data  Data to restore
249 
250   @return An error code: 0 - success, otherwise - failure
251 
252   @ref User
253 **/
254 int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) {
255   int ierr;
256 
257   if (!ctx->RestoreData)
258     // LCOV_EXCL_START
259     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
260                      "Backend does not support RestoreData");
261   // LCOV_EXCL_STOP
262 
263   if (ctx->state % 2 != 1)
264     // LCOV_EXCL_START
265     return CeedError(ctx->ceed, 1,
266                      "Cannot restore CeedQFunctionContext array access, "
267                      "access was not granted");
268   // LCOV_EXCL_STOP
269 
270   ierr = ctx->RestoreData(ctx); CeedChk(ierr);
271   *(void **)data = NULL;
272   ctx->state += 1;
273   return CEED_ERROR_SUCCESS;
274 }
275 
276 /**
277   @brief View a CeedQFunctionContext
278 
279   @param[in] ctx     CeedQFunctionContext to view
280   @param[in] stream  Filestream to write to
281 
282   @return An error code: 0 - success, otherwise - failure
283 
284   @ref User
285 **/
286 int CeedQFunctionContextView(CeedQFunctionContext ctx, FILE *stream) {
287   fprintf(stream, "CeedQFunctionContext\n");
288   fprintf(stream, "  Context Data Size: %ld\n", ctx->ctx_size);
289   return CEED_ERROR_SUCCESS;
290 }
291 
292 /**
293   @brief Destroy a CeedQFunctionContext
294 
295   @param ctx  CeedQFunctionContext to destroy
296 
297   @return An error code: 0 - success, otherwise - failure
298 
299   @ref User
300 **/
301 int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx) {
302   int ierr;
303 
304   if (!*ctx || --(*ctx)->ref_count > 0)
305     return CEED_ERROR_SUCCESS;
306 
307   if ((*ctx) && ((*ctx)->state % 2) == 1)
308     // LCOV_EXCL_START
309     return CeedError((*ctx)->ceed, 1,
310                      "Cannot destroy CeedQFunctionContext, the access "
311                      "lock is in use");
312   // LCOV_EXCL_STOP
313 
314   if ((*ctx)->Destroy) {
315     ierr = (*ctx)->Destroy(*ctx); CeedChk(ierr);
316   }
317   ierr = CeedDestroy(&(*ctx)->ceed); CeedChk(ierr);
318   ierr = CeedFree(ctx); CeedChk(ierr);
319   return CEED_ERROR_SUCCESS;
320 }
321 
322 /// @}
323