xref: /libCEED/interface/ceed-qfunctioncontext.c (revision 751b7b16b4c6f23d06daa251e4825bc6151d97db)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed-impl.h>
9 #include <ceed/backend.h>
10 #include <ceed/ceed.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 
15 /// @file
16 /// Implementation of public CeedQFunctionContext interfaces
17 
18 /// ----------------------------------------------------------------------------
19 /// CeedQFunctionContext Library Internal Functions
20 /// ----------------------------------------------------------------------------
21 /// @addtogroup CeedQFunctionDeveloper
22 /// @{
23 
24 /**
25   @brief Get index for QFunctionContext field
26 
27   @param[in]  ctx         CeedQFunctionContext
28   @param[in]  field_name  Name of field
29   @param[out] field_index Index of field, or -1 if field is not registered
30 
31   @return An error code: 0 - success, otherwise - failure
32 
33   @ref Developer
34 **/
35 int CeedQFunctionContextGetFieldIndex(CeedQFunctionContext ctx, const char *field_name, CeedInt *field_index) {
36   *field_index = -1;
37   for (CeedInt i = 0; i < ctx->num_fields; i++) {
38     if (!strcmp(ctx->field_labels[i]->name, field_name)) *field_index = i;
39   }
40   return CEED_ERROR_SUCCESS;
41 }
42 
43 /**
44   @brief Common function for registering QFunctionContext fields
45 
46   @param[in,out] ctx               CeedQFunctionContext
47   @param[in]     field_name        Name of field to register
48   @param[in]     field_offset      Offset of field to register
49   @param[in]     field_description Description of field, or NULL for none
50   @param[in]     field_type        Field data type, such as double or int32
51   @param[in]     field_size        Size of field, in bytes
52   @param[in]     num_values        Number of values to register, must be contiguous in memory
53 
54   @return An error code: 0 - success, otherwise - failure
55 
56   @ref Developer
57 **/
58 int CeedQFunctionContextRegisterGeneric(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, const char *field_description,
59                                         CeedContextFieldType field_type, size_t field_size, size_t num_values) {
60   // Check for duplicate
61   CeedInt field_index = -1;
62   CeedCall(CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index));
63   if (field_index != -1) {
64     // LCOV_EXCL_START
65     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "QFunctionContext field with name \"%s\" already registered", field_name);
66     // LCOV_EXCL_STOP
67   }
68 
69   // Allocate space for field data
70   if (ctx->num_fields == 0) {
71     CeedCall(CeedCalloc(1, &ctx->field_labels));
72     ctx->max_fields = 1;
73   } else if (ctx->num_fields == ctx->max_fields) {
74     CeedCall(CeedRealloc(2 * ctx->max_fields, &ctx->field_labels));
75     ctx->max_fields *= 2;
76   }
77   CeedCall(CeedCalloc(1, &ctx->field_labels[ctx->num_fields]));
78 
79   // Copy field data
80   CeedCall(CeedStringAllocCopy(field_name, (char **)&ctx->field_labels[ctx->num_fields]->name));
81   CeedCall(CeedStringAllocCopy(field_description, (char **)&ctx->field_labels[ctx->num_fields]->description));
82   ctx->field_labels[ctx->num_fields]->type       = field_type;
83   ctx->field_labels[ctx->num_fields]->offset     = field_offset;
84   ctx->field_labels[ctx->num_fields]->size       = field_size * num_values;
85   ctx->field_labels[ctx->num_fields]->num_values = num_values;
86   ctx->num_fields++;
87   return CEED_ERROR_SUCCESS;
88 }
89 
90 /**
91   @brief Destroy user data held by CeedQFunctionContext, using function set by CeedQFunctionContextSetDataDestroy, if applicable
92 
93   @param[in,out] ctx CeedQFunctionContext to destroy user data
94 
95   @return An error code: 0 - success, otherwise - failure
96 
97   @ref Developer
98 **/
99 static int CeedQFunctionContextDestroyData(CeedQFunctionContext ctx) {
100   if (ctx->DataDestroy) {
101     CeedCall(ctx->DataDestroy(ctx));
102   } else {
103     CeedQFunctionContextDataDestroyUser data_destroy_function;
104     CeedMemType                         data_destroy_mem_type;
105 
106     CeedCall(CeedQFunctionContextGetDataDestroy(ctx, &data_destroy_mem_type, &data_destroy_function));
107     if (data_destroy_function) {
108       void *data;
109 
110       CeedCall(CeedQFunctionContextGetData(ctx, data_destroy_mem_type, &data));
111       CeedCall(data_destroy_function(data));
112       CeedCall(CeedQFunctionContextRestoreData(ctx, &data));
113     }
114   }
115 
116   return CEED_ERROR_SUCCESS;
117 }
118 
119 /// @}
120 
121 /// ----------------------------------------------------------------------------
122 /// CeedQFunctionContext Backend API
123 /// ----------------------------------------------------------------------------
124 /// @addtogroup CeedQFunctionBackend
125 /// @{
126 
127 /**
128   @brief Get the Ceed associated with a CeedQFunctionContext
129 
130   @param[in]  ctx  CeedQFunctionContext
131   @param[out] ceed Variable to store Ceed
132 
133   @return An error code: 0 - success, otherwise - failure
134 
135   @ref Backend
136 **/
137 int CeedQFunctionContextGetCeed(CeedQFunctionContext ctx, Ceed *ceed) {
138   *ceed = ctx->ceed;
139   return CEED_ERROR_SUCCESS;
140 }
141 
142 /**
143   @brief Check for valid data in a CeedQFunctionContext
144 
145   @param[in]  ctx            CeedQFunctionContext to check validity
146   @param[out] has_valid_data Variable to store validity
147 
148   @return An error code: 0 - success, otherwise - failure
149 
150   @ref Backend
151 **/
152 int CeedQFunctionContextHasValidData(CeedQFunctionContext ctx, bool *has_valid_data) {
153   if (!ctx->HasValidData) {
154     // LCOV_EXCL_START
155     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasValidData");
156     // LCOV_EXCL_STOP
157   }
158 
159   CeedCall(ctx->HasValidData(ctx, has_valid_data));
160 
161   return CEED_ERROR_SUCCESS;
162 }
163 
164 /**
165   @brief Check for borrowed data of a specific CeedMemType in a CeedQFunctionContext
166 
167   @param[in]  ctx                       CeedQFunctionContext to check
168   @param[in]  mem_type                  Memory type to check
169   @param[out] has_borrowed_data_of_type Variable to store result
170 
171   @return An error code: 0 - success, otherwise - failure
172 
173   @ref Backend
174 **/
175 int CeedQFunctionContextHasBorrowedDataOfType(CeedQFunctionContext ctx, CeedMemType mem_type, bool *has_borrowed_data_of_type) {
176   if (!ctx->HasBorrowedDataOfType) {
177     // LCOV_EXCL_START
178     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasBorrowedDataOfType");
179     // LCOV_EXCL_STOP
180   }
181 
182   CeedCall(ctx->HasBorrowedDataOfType(ctx, mem_type, has_borrowed_data_of_type));
183 
184   return CEED_ERROR_SUCCESS;
185 }
186 
187 /**
188   @brief Get the state of a CeedQFunctionContext
189 
190   @param[in]  ctx   CeedQFunctionContext to retrieve state
191   @param[out] state Variable to store state
192 
193   @return An error code: 0 - success, otherwise - failure
194 
195   @ref Backend
196 **/
197 int CeedQFunctionContextGetState(CeedQFunctionContext ctx, uint64_t *state) {
198   *state = ctx->state;
199   return CEED_ERROR_SUCCESS;
200 }
201 
202 /**
203   @brief Get backend data of a CeedQFunctionContext
204 
205   @param[in]  ctx  CeedQFunctionContext
206   @param[out] data Variable to store data
207 
208   @return An error code: 0 - success, otherwise - failure
209 
210   @ref Backend
211 **/
212 int CeedQFunctionContextGetBackendData(CeedQFunctionContext ctx, void *data) {
213   *(void **)data = ctx->data;
214   return CEED_ERROR_SUCCESS;
215 }
216 
217 /**
218   @brief Set backend data of a CeedQFunctionContext
219 
220   @param[in,out] ctx  CeedQFunctionContext
221   @param[in]     data Data to set
222 
223   @return An error code: 0 - success, otherwise - failure
224 
225   @ref Backend
226 **/
227 int CeedQFunctionContextSetBackendData(CeedQFunctionContext ctx, void *data) {
228   ctx->data = data;
229   return CEED_ERROR_SUCCESS;
230 }
231 
232 /**
233   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`
234 
235   @param[in]  ctx         CeedQFunctionContext
236   @param[in]  field_name  Name of field to retrieve label
237   @param[out] field_label Variable to field label
238 
239   @return An error code: 0 - success, otherwise - failure
240 
241   @ref Backend
242 **/
243 int CeedQFunctionContextGetFieldLabel(CeedQFunctionContext ctx, const char *field_name, CeedContextFieldLabel *field_label) {
244   CeedInt field_index;
245   CeedCall(CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index));
246 
247   if (field_index != -1) {
248     *field_label = ctx->field_labels[field_index];
249   } else {
250     *field_label = NULL;
251   }
252 
253   return CEED_ERROR_SUCCESS;
254 }
255 
256 /**
257   @brief Set QFunctionContext field
258 
259   @param[in,out] ctx         CeedQFunctionContext
260   @param[in]     field_label Label of field to set
261   @param[in]     field_type  Type of field to set
262   @param[in]     value       Value to set
263 
264   @return An error code: 0 - success, otherwise - failure
265 
266   @ref Backend
267 **/
268 int CeedQFunctionContextSetGeneric(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *value) {
269   // Check field type
270   if (field_label->type != field_type) {
271     // LCOV_EXCL_START
272     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "QFunctionContext field with name \"%s\" registered as %s, not registered as %s",
273                      field_label->name, CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]);
274     // LCOV_EXCL_STOP
275   }
276 
277   char *data;
278   CeedCall(CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &data));
279   memcpy(&data[field_label->offset], value, field_label->size);
280   CeedCall(CeedQFunctionContextRestoreData(ctx, &data));
281 
282   return CEED_ERROR_SUCCESS;
283 }
284 
285 /**
286   @brief Set QFunctionContext field holding a double precision value
287 
288   @param[in,out] ctx         CeedQFunctionContext
289   @param[in]     field_label Label for field to register
290   @param[in]     values      Values to set
291 
292   @return An error code: 0 - success, otherwise - failure
293 
294   @ref Backend
295 **/
296 int CeedQFunctionContextSetDouble(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, double *values) {
297   if (!field_label) {
298     // LCOV_EXCL_START
299     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
300     // LCOV_EXCL_STOP
301   }
302 
303   CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, values));
304 
305   return CEED_ERROR_SUCCESS;
306 }
307 
308 /**
309   @brief Set QFunctionContext field holding an int32 value
310 
311   @param[in,out] ctx         CeedQFunctionContext
312   @param[in]     field_label Label for field to register
313   @param[in]     values      Values to set
314 
315   @return An error code: 0 - success, otherwise - failure
316 
317   @ref Backend
318 **/
319 int CeedQFunctionContextSetInt32(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, int *values) {
320   if (!field_label) {
321     // LCOV_EXCL_START
322     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
323     // LCOV_EXCL_STOP
324   }
325 
326   CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_INT32, values));
327 
328   return CEED_ERROR_SUCCESS;
329 }
330 
331 /**
332   @brief Get additional destroy routine for CeedQFunctionContext user data
333 
334   @param[in] ctx         CeedQFunctionContext to get user destroy function
335   @param[out] f_mem_type Memory type to use when passing data into `f`
336   @param[out] f          Additional routine to use to destroy user data
337 
338   @return An error code: 0 - success, otherwise - failure
339 
340   @ref Backend
341 **/
342 int CeedQFunctionContextGetDataDestroy(CeedQFunctionContext ctx, CeedMemType *f_mem_type, CeedQFunctionContextDataDestroyUser *f) {
343   if (f_mem_type) *f_mem_type = ctx->data_destroy_mem_type;
344   if (f) *f = ctx->data_destroy_function;
345   return CEED_ERROR_SUCCESS;
346 }
347 
348 /**
349   @brief Increment the reference counter for a CeedQFunctionContext
350 
351   @param[in,out] ctx CeedQFunctionContext to increment the reference counter
352 
353   @return An error code: 0 - success, otherwise - failure
354 
355   @ref Backend
356 **/
357 int CeedQFunctionContextReference(CeedQFunctionContext ctx) {
358   ctx->ref_count++;
359   return CEED_ERROR_SUCCESS;
360 }
361 
362 /// @}
363 
364 /// ----------------------------------------------------------------------------
365 /// CeedQFunctionContext Public API
366 /// ----------------------------------------------------------------------------
367 /// @addtogroup CeedQFunctionUser
368 /// @{
369 
370 /**
371   @brief Create a CeedQFunctionContext for storing CeedQFunction user context data
372 
373   @param[in]  ceed Ceed object where the CeedQFunctionContext will be created
374   @param[out] ctx  Address of the variable where the newly created CeedQFunctionContext will be stored
375 
376   @return An error code: 0 - success, otherwise - failure
377 
378   @ref User
379 **/
380 int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) {
381   if (!ceed->QFunctionContextCreate) {
382     Ceed delegate;
383     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Context"));
384 
385     if (!delegate) {
386       // LCOV_EXCL_START
387       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support ContextCreate");
388       // LCOV_EXCL_STOP
389     }
390 
391     CeedCall(CeedQFunctionContextCreate(delegate, ctx));
392     return CEED_ERROR_SUCCESS;
393   }
394 
395   CeedCall(CeedCalloc(1, ctx));
396   (*ctx)->ceed = ceed;
397   CeedCall(CeedReference(ceed));
398   (*ctx)->ref_count = 1;
399   CeedCall(ceed->QFunctionContextCreate(*ctx));
400   return CEED_ERROR_SUCCESS;
401 }
402 
403 /**
404   @brief Copy the pointer to a CeedQFunctionContext.
405            Both pointers should be destroyed with `CeedQFunctionContextDestroy()`.
406            Note: If `*ctx_copy` is non-NULL, then it is assumed that `*ctx_copy` is a pointer to a CeedQFunctionContext.
407              This CeedQFunctionContext will be destroyed if `*ctx_copy` is the only reference to this CeedQFunctionContext.
408 
409   @param[in]     ctx      CeedQFunctionContext to copy reference to
410   @param[in,out] ctx_copy Variable to store copied reference
411 
412   @return An error code: 0 - success, otherwise - failure
413 
414   @ref User
415 **/
416 int CeedQFunctionContextReferenceCopy(CeedQFunctionContext ctx, CeedQFunctionContext *ctx_copy) {
417   CeedCall(CeedQFunctionContextReference(ctx));
418   CeedCall(CeedQFunctionContextDestroy(ctx_copy));
419   *ctx_copy = ctx;
420   return CEED_ERROR_SUCCESS;
421 }
422 
423 /**
424   @brief Set the data used by a CeedQFunctionContext, freeing any previously allocated data if applicable.
425            The backend may copy values to a different memtype, such as during @ref CeedQFunctionApply().
426            See also @ref CeedQFunctionContextTakeData().
427 
428   @param[in,out] ctx       CeedQFunctionContext
429   @param[in]     mem_type  Memory type of the data being passed
430   @param[in]     copy_mode Copy mode for the data
431   @param[in]     size      Size of data, in bytes
432   @param[in]     data      Data to be used
433 
434   @return An error code: 0 - success, otherwise - failure
435 
436   @ref User
437 **/
438 int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, size_t size, void *data) {
439   if (!ctx->SetData) {
440     // LCOV_EXCL_START
441     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support ContextSetData");
442     // LCOV_EXCL_STOP
443   }
444   if (ctx->state % 2 == 1) {
445     // LCOV_EXCL_START
446     return CeedError(ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
447     // LCOV_EXCL_STOP
448   }
449 
450   CeedCall(CeedQFunctionContextDestroyData(ctx));
451   ctx->ctx_size = size;
452   CeedCall(ctx->SetData(ctx, mem_type, copy_mode, data));
453   ctx->state += 2;
454   return CEED_ERROR_SUCCESS;
455 }
456 
457 /**
458   @brief Take ownership of the data in a CeedQFunctionContext via the specified memory type.
459            The caller is responsible for managing and freeing the memory.
460 
461   @param[in]  ctx      CeedQFunctionContext to access
462   @param[in]  mem_type Memory type on which to access the data.
463                          If the backend uses a different memory type, this will perform a copy.
464   @param[out] data     Data on memory type mem_type
465 
466   @return An error code: 0 - success, otherwise - failure
467 
468   @ref User
469 **/
470 int CeedQFunctionContextTakeData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
471   bool has_valid_data = true;
472   CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
473   if (!has_valid_data) {
474     // LCOV_EXCL_START
475     return CeedError(ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to take, must set data");
476     // LCOV_EXCL_STOP
477   }
478 
479   if (!ctx->TakeData) {
480     // LCOV_EXCL_START
481     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support TakeData");
482     // LCOV_EXCL_STOP
483   }
484   if (ctx->state % 2 == 1) {
485     // LCOV_EXCL_START
486     return CeedError(ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
487     // LCOV_EXCL_STOP
488   }
489 
490   bool has_borrowed_data_of_type = true;
491   CeedCall(CeedQFunctionContextHasBorrowedDataOfType(ctx, mem_type, &has_borrowed_data_of_type));
492   if (!has_borrowed_data_of_type) {
493     // LCOV_EXCL_START
494     return CeedError(ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no borowed %s data, must set data with CeedQFunctionContextSetData",
495                      CeedMemTypes[mem_type]);
496     // LCOV_EXCL_STOP
497   }
498 
499   void *temp_data = NULL;
500   CeedCall(ctx->TakeData(ctx, mem_type, &temp_data));
501   if (data) (*(void **)data) = temp_data;
502   return CEED_ERROR_SUCCESS;
503 }
504 
505 /**
506   @brief Get read/write access to a CeedQFunctionContext via the specified memory type.
507            Restore access with @ref CeedQFunctionContextRestoreData().
508 
509   @param[in]  ctx      CeedQFunctionContext to access
510   @param[in]  mem_type Memory type on which to access the data.
511                          If the backend uses a different memory type, this will perform a copy.
512   @param[out] data     Data on memory type mem_type
513 
514   @note The CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions provide access to array pointers in the desired memory
515 space. Pairing get/restore allows the Context to track access.
516 
517   @return An error code: 0 - success, otherwise - failure
518 
519   @ref User
520 **/
521 int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
522   if (!ctx->GetData) {
523     // LCOV_EXCL_START
524     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetData");
525     // LCOV_EXCL_STOP
526   }
527   if (ctx->state % 2 == 1) {
528     // LCOV_EXCL_START
529     return CeedError(ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
530     // LCOV_EXCL_STOP
531   }
532   if (ctx->num_readers > 0) {
533     // LCOV_EXCL_START
534     return CeedError(ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, a process has read access");
535     // LCOV_EXCL_STOP
536   }
537 
538   bool has_valid_data = true;
539   CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
540   if (!has_valid_data) {
541     // LCOV_EXCL_START
542     return CeedError(ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data");
543     // LCOV_EXCL_STOP
544   }
545 
546   CeedCall(ctx->GetData(ctx, mem_type, data));
547   ctx->state++;
548   return CEED_ERROR_SUCCESS;
549 }
550 
551 /**
552   @brief Get read only access to a CeedQFunctionContext via the specified memory type.
553            Restore access with @ref CeedQFunctionContextRestoreData().
554 
555   @param[in]  ctx      CeedQFunctionContext to access
556   @param[in]  mem_type Memory type on which to access the data.
557                          If the backend uses a different memory type, this will perform a copy.
558   @param[out] data     Data on memory type mem_type
559 
560   @note The CeedQFunctionContextGetDataRead() and @ref CeedQFunctionContextRestoreDataRead() functions provide access to array pointers in the desired
561 memory space. Pairing get/restore allows the Context to track access.
562 
563   @return An error code: 0 - success, otherwise - failure
564 
565   @ref User
566 **/
567 int CeedQFunctionContextGetDataRead(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
568   if (!ctx->GetDataRead) {
569     // LCOV_EXCL_START
570     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetDataRead");
571     // LCOV_EXCL_STOP
572   }
573   if (ctx->state % 2 == 1) {
574     // LCOV_EXCL_START
575     return CeedError(ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
576     // LCOV_EXCL_STOP
577   }
578 
579   bool has_valid_data = true;
580   CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
581   if (!has_valid_data) {
582     // LCOV_EXCL_START
583     return CeedError(ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data");
584     // LCOV_EXCL_STOP
585   }
586 
587   CeedCall(ctx->GetDataRead(ctx, mem_type, data));
588   ctx->num_readers++;
589   return CEED_ERROR_SUCCESS;
590 }
591 
592 /**
593   @brief Restore data obtained using @ref CeedQFunctionContextGetData()
594 
595   @param[in]     ctx  CeedQFunctionContext to restore
596   @param[in,out] data Data to restore
597 
598   @return An error code: 0 - success, otherwise - failure
599 
600   @ref User
601 **/
602 int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) {
603   if (ctx->state % 2 != 1) {
604     // LCOV_EXCL_START
605     return CeedError(ctx->ceed, 1, "Cannot restore CeedQFunctionContext array access, access was not granted");
606     // LCOV_EXCL_STOP
607   }
608 
609   if (ctx->RestoreData) {
610     CeedCall(ctx->RestoreData(ctx));
611   }
612   *(void **)data = NULL;
613   ctx->state++;
614   return CEED_ERROR_SUCCESS;
615 }
616 
617 /**
618   @brief Restore data obtained using @ref CeedQFunctionContextGetDataRead()
619 
620   @param[in]     ctx  CeedQFunctionContext to restore
621   @param[in,out] data Data to restore
622 
623   @return An error code: 0 - success, otherwise - failure
624 
625   @ref User
626 **/
627 int CeedQFunctionContextRestoreDataRead(CeedQFunctionContext ctx, void *data) {
628   if (ctx->num_readers == 0) {
629     // LCOV_EXCL_START
630     return CeedError(ctx->ceed, 1, "Cannot restore CeedQFunctionContext array access, access was not granted");
631     // LCOV_EXCL_STOP
632   }
633 
634   ctx->num_readers--;
635   if (ctx->num_readers == 0 && ctx->RestoreDataRead) {
636     CeedCall(ctx->RestoreDataRead(ctx));
637   }
638   *(void **)data = NULL;
639 
640   return CEED_ERROR_SUCCESS;
641 }
642 
643 /**
644   @brief Register QFunctionContext a field holding a double precision value
645 
646   @param[in,out] ctx               CeedQFunctionContext
647   @param[in]     field_name        Name of field to register
648   @param[in]     field_offset      Offset of field to register
649   @param[in]     num_values        Number of values to register, must be contiguous in memory
650   @param[in]     field_description Description of field, or NULL for none
651 
652   @return An error code: 0 - success, otherwise - failure
653 
654   @ref User
655 **/
656 int CeedQFunctionContextRegisterDouble(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values,
657                                        const char *field_description) {
658   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_DOUBLE, sizeof(double), num_values);
659 }
660 
661 /**
662   @brief Register QFunctionContext a field holding a int32 value
663 
664   @param[in,out] ctx               CeedQFunctionContext
665   @param[in]     field_name        Name of field to register
666   @param[in]     field_offset      Offset of field to register
667   @param[in]     num_values        Number of values to register, must be contiguous in memory
668   @param[in]     field_description Description of field, or NULL for none
669 
670   @return An error code: 0 - success, otherwise - failure
671 
672   @ref User
673 **/
674 int CeedQFunctionContextRegisterInt32(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values,
675                                       const char *field_description) {
676   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_INT32, sizeof(int), num_values);
677 }
678 
679 /**
680   @brief Get labels for all registered QFunctionContext fields
681 
682   @param[in]  ctx          CeedQFunctionContext
683   @param[out] field_labels Variable to hold array of field labels
684   @param[out] num_fields   Length of field descriptions array
685 
686   @return An error code: 0 - success, otherwise - failure
687 
688   @ref User
689 **/
690 int CeedQFunctionContextGetAllFieldLabels(CeedQFunctionContext ctx, const CeedContextFieldLabel **field_labels, CeedInt *num_fields) {
691   *field_labels = ctx->field_labels;
692   *num_fields   = ctx->num_fields;
693   return CEED_ERROR_SUCCESS;
694 }
695 
696 /**
697   @brief Get the descriptive information about a CeedContextFieldLabel
698 
699   @param[in]  label             CeedContextFieldLabel
700   @param[out] field_name        Name of labeled field
701   @param[out] field_description Description of field, or NULL for none
702   @param[out] num_values        Number of values registered
703   @param[out] field_type        CeedContextFieldType
704 
705   @return An error code: 0 - success, otherwise - failure
706 
707   @ref User
708 **/
709 int CeedContextFieldLabelGetDescription(CeedContextFieldLabel label, const char **field_name, const char **field_description, size_t *num_values,
710                                         CeedContextFieldType *field_type) {
711   if (field_name) *field_name = label->name;
712   if (field_description) *field_description = label->description;
713   if (num_values) *num_values = label->num_values;
714   if (field_type) *field_type = label->type;
715   return CEED_ERROR_SUCCESS;
716 }
717 
718 /**
719   @brief Get data size for a Context
720 
721   @param[in]  ctx      CeedQFunctionContext
722   @param[out] ctx_size Variable to store size of context data values
723 
724   @return An error code: 0 - success, otherwise - failure
725 
726   @ref User
727 **/
728 int CeedQFunctionContextGetContextSize(CeedQFunctionContext ctx, size_t *ctx_size) {
729   *ctx_size = ctx->ctx_size;
730   return CEED_ERROR_SUCCESS;
731 }
732 
733 /**
734   @brief View a CeedQFunctionContext
735 
736   @param[in] ctx    CeedQFunctionContext to view
737   @param[in] stream Filestream to write to
738 
739   @return An error code: 0 - success, otherwise - failure
740 
741   @ref User
742 **/
743 int CeedQFunctionContextView(CeedQFunctionContext ctx, FILE *stream) {
744   fprintf(stream, "CeedQFunctionContext\n");
745   fprintf(stream, "  Context Data Size: %ld\n", ctx->ctx_size);
746   for (CeedInt i = 0; i < ctx->num_fields; i++) {
747     // LCOV_EXCL_START
748     fprintf(stream, "  Labeled %s field: %s\n", CeedContextFieldTypes[ctx->field_labels[i]->type], ctx->field_labels[i]->name);
749     // LCOV_EXCL_STOP
750   }
751   return CEED_ERROR_SUCCESS;
752 }
753 
754 /**
755   @brief Set additional destroy routine for CeedQFunctionContext user data
756 
757   @param[in,out] ctx        CeedQFunctionContext to set user destroy function
758   @param[in]     f_mem_type Memory type to use when passing data into `f`
759   @param[in]     f          Additional routine to use to destroy user data
760 
761   @return An error code: 0 - success, otherwise - failure
762 
763   @ref User
764 **/
765 int CeedQFunctionContextSetDataDestroy(CeedQFunctionContext ctx, CeedMemType f_mem_type, CeedQFunctionContextDataDestroyUser f) {
766   if (!f) {
767     // LCOV_EXCL_START
768     return CeedError(ctx->ceed, 1, "Must provide valid callback function for destroying user data");
769     // LCOV_EXCL_STOP
770   }
771   ctx->data_destroy_mem_type = f_mem_type;
772   ctx->data_destroy_function = f;
773   return CEED_ERROR_SUCCESS;
774 }
775 
776 /**
777   @brief Destroy a CeedQFunctionContext
778 
779   @param[in,out] ctx CeedQFunctionContext to destroy
780 
781   @return An error code: 0 - success, otherwise - failure
782 
783   @ref User
784 **/
785 int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx) {
786   if (!*ctx || --(*ctx)->ref_count > 0) return CEED_ERROR_SUCCESS;
787 
788   if ((*ctx) && ((*ctx)->state % 2) == 1) {
789     // LCOV_EXCL_START
790     return CeedError((*ctx)->ceed, 1, "Cannot destroy CeedQFunctionContext, the access lock is in use");
791     // LCOV_EXCL_STOP
792   }
793 
794   CeedCall(CeedQFunctionContextDestroyData(*ctx));
795   if ((*ctx)->Destroy) CeedCall((*ctx)->Destroy(*ctx));
796   for (CeedInt i = 0; i < (*ctx)->num_fields; i++) {
797     CeedCall(CeedFree(&(*ctx)->field_labels[i]->name));
798     CeedCall(CeedFree(&(*ctx)->field_labels[i]->description));
799     CeedCall(CeedFree(&(*ctx)->field_labels[i]));
800   }
801   CeedCall(CeedFree(&(*ctx)->field_labels));
802   CeedCall(CeedDestroy(&(*ctx)->ceed));
803   CeedCall(CeedFree(ctx));
804 
805   return CEED_ERROR_SUCCESS;
806 }
807 
808 /// @}
809