xref: /libCEED/interface/ceed-qfunctioncontext.c (revision db002c03923317a1c3814dcd861330002c00a8ea)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3777ff853SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5777ff853SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7777ff853SJeremy L Thompson 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
1149aac155SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <stdint.h>
133d576824SJeremy L Thompson #include <stdio.h>
14cdf32b93SJeremy L Thompson #include <string.h>
15777ff853SJeremy L Thompson 
16777ff853SJeremy L Thompson /// @file
17777ff853SJeremy L Thompson /// Implementation of public CeedQFunctionContext interfaces
18777ff853SJeremy L Thompson 
19777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
20cdf32b93SJeremy L Thompson /// CeedQFunctionContext Library Internal Functions
21cdf32b93SJeremy L Thompson /// ----------------------------------------------------------------------------
22cdf32b93SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
23cdf32b93SJeremy L Thompson /// @{
24cdf32b93SJeremy L Thompson 
25cdf32b93SJeremy L Thompson /**
26cdf32b93SJeremy L Thompson   @brief Get index for QFunctionContext field
27cdf32b93SJeremy L Thompson 
28ea61e9acSJeremy L Thompson   @param[in]  ctx         CeedQFunctionContext
29ea61e9acSJeremy L Thompson   @param[in]  field_name  Name of field
30ea61e9acSJeremy L Thompson   @param[out] field_index Index of field, or -1 if field is not registered
31cdf32b93SJeremy L Thompson 
32cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
33cdf32b93SJeremy L Thompson 
34cdf32b93SJeremy L Thompson   @ref Developer
35cdf32b93SJeremy L Thompson **/
362b730f8bSJeremy L Thompson int CeedQFunctionContextGetFieldIndex(CeedQFunctionContext ctx, const char *field_name, CeedInt *field_index) {
37cdf32b93SJeremy L Thompson   *field_index = -1;
382b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < ctx->num_fields; i++) {
392b730f8bSJeremy L Thompson     if (!strcmp(ctx->field_labels[i]->name, field_name)) *field_index = i;
402b730f8bSJeremy L Thompson   }
41cdf32b93SJeremy L Thompson   return CEED_ERROR_SUCCESS;
42cdf32b93SJeremy L Thompson }
43cdf32b93SJeremy L Thompson 
44cdf32b93SJeremy L Thompson /**
45cdf32b93SJeremy L Thompson   @brief Common function for registering QFunctionContext fields
46cdf32b93SJeremy L Thompson 
47ea61e9acSJeremy L Thompson   @param[in,out] ctx               CeedQFunctionContext
48ea61e9acSJeremy L Thompson   @param[in]     field_name        Name of field to register
49ea61e9acSJeremy L Thompson   @param[in]     field_offset      Offset of field to register
50ea61e9acSJeremy L Thompson   @param[in]     field_description Description of field, or NULL for none
51ea61e9acSJeremy L Thompson   @param[in]     field_type        Field data type, such as double or int32
52ea61e9acSJeremy L Thompson   @param[in]     field_size        Size of field, in bytes
53ea61e9acSJeremy L Thompson   @param[in]     num_values        Number of values to register, must be contiguous in memory
54cdf32b93SJeremy L Thompson 
55cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
56cdf32b93SJeremy L Thompson 
57cdf32b93SJeremy L Thompson   @ref Developer
58cdf32b93SJeremy L Thompson **/
592b730f8bSJeremy L Thompson int CeedQFunctionContextRegisterGeneric(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, const char *field_description,
602b730f8bSJeremy L Thompson                                         CeedContextFieldType field_type, size_t field_size, size_t num_values) {
61cdf32b93SJeremy L Thompson   // Check for duplicate
62cdf32b93SJeremy L Thompson   CeedInt field_index = -1;
632b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index));
646574a04fSJeremy L Thompson   CeedCheck(field_index == -1, ctx->ceed, CEED_ERROR_UNSUPPORTED, "QFunctionContext field with name \"%s\" already registered", field_name);
65cdf32b93SJeremy L Thompson 
66cdf32b93SJeremy L Thompson   // Allocate space for field data
67cdf32b93SJeremy L Thompson   if (ctx->num_fields == 0) {
682b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &ctx->field_labels));
69cdf32b93SJeremy L Thompson     ctx->max_fields = 1;
70cdf32b93SJeremy L Thompson   } else if (ctx->num_fields == ctx->max_fields) {
712b730f8bSJeremy L Thompson     CeedCall(CeedRealloc(2 * ctx->max_fields, &ctx->field_labels));
72cdf32b93SJeremy L Thompson     ctx->max_fields *= 2;
73cdf32b93SJeremy L Thompson   }
742b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &ctx->field_labels[ctx->num_fields]));
75cdf32b93SJeremy L Thompson 
76cdf32b93SJeremy L Thompson   // Copy field data
772b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&ctx->field_labels[ctx->num_fields]->name));
782b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_description, (char **)&ctx->field_labels[ctx->num_fields]->description));
793668ca4bSJeremy L Thompson   ctx->field_labels[ctx->num_fields]->type       = field_type;
803668ca4bSJeremy L Thompson   ctx->field_labels[ctx->num_fields]->offset     = field_offset;
817bfe0f0eSJeremy L Thompson   ctx->field_labels[ctx->num_fields]->size       = field_size * num_values;
827bfe0f0eSJeremy L Thompson   ctx->field_labels[ctx->num_fields]->num_values = num_values;
83cdf32b93SJeremy L Thompson   ctx->num_fields++;
84cdf32b93SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85cdf32b93SJeremy L Thompson }
86cdf32b93SJeremy L Thompson 
873b190ab8SJeremy L Thompson /**
88ea61e9acSJeremy L Thompson   @brief Destroy user data held by CeedQFunctionContext, using function set by CeedQFunctionContextSetDataDestroy, if applicable
893b190ab8SJeremy L Thompson 
903b190ab8SJeremy L Thompson   @param[in,out] ctx CeedQFunctionContext to destroy user data
913b190ab8SJeremy L Thompson 
923b190ab8SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
933b190ab8SJeremy L Thompson 
943b190ab8SJeremy L Thompson   @ref Developer
953b190ab8SJeremy L Thompson **/
963b190ab8SJeremy L Thompson static int CeedQFunctionContextDestroyData(CeedQFunctionContext ctx) {
973b190ab8SJeremy L Thompson   if (ctx->DataDestroy) {
982b730f8bSJeremy L Thompson     CeedCall(ctx->DataDestroy(ctx));
993b190ab8SJeremy L Thompson   } else {
1003b190ab8SJeremy L Thompson     CeedQFunctionContextDataDestroyUser data_destroy_function;
1013b190ab8SJeremy L Thompson     CeedMemType                         data_destroy_mem_type;
1023b190ab8SJeremy L Thompson 
1032b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetDataDestroy(ctx, &data_destroy_mem_type, &data_destroy_function));
1043b190ab8SJeremy L Thompson     if (data_destroy_function) {
1053b190ab8SJeremy L Thompson       void *data;
1063b190ab8SJeremy L Thompson 
1072b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetData(ctx, data_destroy_mem_type, &data));
1082b730f8bSJeremy L Thompson       CeedCall(data_destroy_function(data));
1092b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionContextRestoreData(ctx, &data));
1103b190ab8SJeremy L Thompson     }
1113b190ab8SJeremy L Thompson   }
1123b190ab8SJeremy L Thompson 
1133b190ab8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1143b190ab8SJeremy L Thompson }
1153b190ab8SJeremy L Thompson 
116cdf32b93SJeremy L Thompson /// @}
117cdf32b93SJeremy L Thompson 
118cdf32b93SJeremy L Thompson /// ----------------------------------------------------------------------------
119777ff853SJeremy L Thompson /// CeedQFunctionContext Backend API
120777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
121777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionBackend
122777ff853SJeremy L Thompson /// @{
123777ff853SJeremy L Thompson 
124777ff853SJeremy L Thompson /**
125777ff853SJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunctionContext
126777ff853SJeremy L Thompson 
127ea61e9acSJeremy L Thompson   @param[in]  ctx  CeedQFunctionContext
128777ff853SJeremy L Thompson   @param[out] ceed Variable to store Ceed
129777ff853SJeremy L Thompson 
130777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
131777ff853SJeremy L Thompson 
132777ff853SJeremy L Thompson   @ref Backend
133777ff853SJeremy L Thompson **/
134777ff853SJeremy L Thompson int CeedQFunctionContextGetCeed(CeedQFunctionContext ctx, Ceed *ceed) {
135777ff853SJeremy L Thompson   *ceed = ctx->ceed;
136e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
137777ff853SJeremy L Thompson }
138777ff853SJeremy L Thompson 
139777ff853SJeremy L Thompson /**
1409c774eddSJeremy L Thompson   @brief Check for valid data in a CeedQFunctionContext
1419c774eddSJeremy L Thompson 
142ea61e9acSJeremy L Thompson   @param[in]  ctx            CeedQFunctionContext to check validity
1439c774eddSJeremy L Thompson   @param[out] has_valid_data Variable to store validity
1449c774eddSJeremy L Thompson 
1459c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1469c774eddSJeremy L Thompson 
1479c774eddSJeremy L Thompson   @ref Backend
1489c774eddSJeremy L Thompson **/
1492b730f8bSJeremy L Thompson int CeedQFunctionContextHasValidData(CeedQFunctionContext ctx, bool *has_valid_data) {
1506574a04fSJeremy L Thompson   CeedCheck(ctx->HasValidData, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasValidData");
1512b730f8bSJeremy L Thompson   CeedCall(ctx->HasValidData(ctx, has_valid_data));
1529c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1539c774eddSJeremy L Thompson }
1549c774eddSJeremy L Thompson 
1559c774eddSJeremy L Thompson /**
156ea61e9acSJeremy L Thompson   @brief Check for borrowed data of a specific CeedMemType in a CeedQFunctionContext
1579c774eddSJeremy L Thompson 
158ea61e9acSJeremy L Thompson   @param[in]  ctx                       CeedQFunctionContext to check
159ea61e9acSJeremy L Thompson   @param[in]  mem_type                  Memory type to check
1609c774eddSJeremy L Thompson   @param[out] has_borrowed_data_of_type Variable to store result
1619c774eddSJeremy L Thompson 
1629c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1639c774eddSJeremy L Thompson 
1649c774eddSJeremy L Thompson   @ref Backend
1659c774eddSJeremy L Thompson **/
1662b730f8bSJeremy L Thompson int CeedQFunctionContextHasBorrowedDataOfType(CeedQFunctionContext ctx, CeedMemType mem_type, bool *has_borrowed_data_of_type) {
1676574a04fSJeremy L Thompson   CeedCheck(ctx->HasBorrowedDataOfType, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasBorrowedDataOfType");
1682b730f8bSJeremy L Thompson   CeedCall(ctx->HasBorrowedDataOfType(ctx, mem_type, has_borrowed_data_of_type));
1699c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1709c774eddSJeremy L Thompson }
1719c774eddSJeremy L Thompson 
1729c774eddSJeremy L Thompson /**
173777ff853SJeremy L Thompson   @brief Get the state of a CeedQFunctionContext
174777ff853SJeremy L Thompson 
175ea61e9acSJeremy L Thompson   @param[in]  ctx   CeedQFunctionContext to retrieve state
176777ff853SJeremy L Thompson   @param[out] state Variable to store state
177777ff853SJeremy L Thompson 
178777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
179777ff853SJeremy L Thompson 
180777ff853SJeremy L Thompson   @ref Backend
181777ff853SJeremy L Thompson **/
182777ff853SJeremy L Thompson int CeedQFunctionContextGetState(CeedQFunctionContext ctx, uint64_t *state) {
183777ff853SJeremy L Thompson   *state = ctx->state;
184e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
185777ff853SJeremy L Thompson }
186777ff853SJeremy L Thompson 
187777ff853SJeremy L Thompson /**
188777ff853SJeremy L Thompson   @brief Get backend data of a CeedQFunctionContext
189777ff853SJeremy L Thompson 
190ea61e9acSJeremy L Thompson   @param[in]  ctx  CeedQFunctionContext
191777ff853SJeremy L Thompson   @param[out] data Variable to store data
192777ff853SJeremy L Thompson 
193777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
194777ff853SJeremy L Thompson 
195777ff853SJeremy L Thompson   @ref Backend
196777ff853SJeremy L Thompson **/
197777ff853SJeremy L Thompson int CeedQFunctionContextGetBackendData(CeedQFunctionContext ctx, void *data) {
198777ff853SJeremy L Thompson   *(void **)data = ctx->data;
199e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
200777ff853SJeremy L Thompson }
201777ff853SJeremy L Thompson 
202777ff853SJeremy L Thompson /**
203777ff853SJeremy L Thompson   @brief Set backend data of a CeedQFunctionContext
204777ff853SJeremy L Thompson 
205ea61e9acSJeremy L Thompson   @param[in,out] ctx  CeedQFunctionContext
206ea61e9acSJeremy L Thompson   @param[in]     data Data to set
207777ff853SJeremy L Thompson 
208777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
209777ff853SJeremy L Thompson 
210777ff853SJeremy L Thompson   @ref Backend
211777ff853SJeremy L Thompson **/
212777ff853SJeremy L Thompson int CeedQFunctionContextSetBackendData(CeedQFunctionContext ctx, void *data) {
213777ff853SJeremy L Thompson   ctx->data = data;
214e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
215777ff853SJeremy L Thompson }
216777ff853SJeremy L Thompson 
21734359f16Sjeremylt /**
218ea61e9acSJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`
219e6a0ab89SJeremy L Thompson 
220e6a0ab89SJeremy L Thompson   @param[in]  ctx         CeedQFunctionContext
221e6a0ab89SJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
222e6a0ab89SJeremy L Thompson   @param[out] field_label Variable to field label
223e6a0ab89SJeremy L Thompson 
224e6a0ab89SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
225e6a0ab89SJeremy L Thompson 
2263e1e85abSJeremy L Thompson   @ref Backend
227e6a0ab89SJeremy L Thompson **/
2282b730f8bSJeremy L Thompson int CeedQFunctionContextGetFieldLabel(CeedQFunctionContext ctx, const char *field_name, CeedContextFieldLabel *field_label) {
229e6a0ab89SJeremy L Thompson   CeedInt field_index;
2302b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index));
231e6a0ab89SJeremy L Thompson 
232e6a0ab89SJeremy L Thompson   if (field_index != -1) {
233e6a0ab89SJeremy L Thompson     *field_label = ctx->field_labels[field_index];
234e6a0ab89SJeremy L Thompson   } else {
235e6a0ab89SJeremy L Thompson     *field_label = NULL;
236e6a0ab89SJeremy L Thompson   }
237e6a0ab89SJeremy L Thompson 
238e6a0ab89SJeremy L Thompson   return CEED_ERROR_SUCCESS;
239e6a0ab89SJeremy L Thompson }
240e6a0ab89SJeremy L Thompson 
241e6a0ab89SJeremy L Thompson /**
242d8dd9a91SJeremy L Thompson   @brief Set QFunctionContext field
243d8dd9a91SJeremy L Thompson 
244ea61e9acSJeremy L Thompson   @param[in,out] ctx         CeedQFunctionContext
245ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
246ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
2472788fa27SJeremy L Thompson   @param[in]     values      Value to set
248d8dd9a91SJeremy L Thompson 
249d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
250d8dd9a91SJeremy L Thompson 
2513e1e85abSJeremy L Thompson   @ref Backend
252d8dd9a91SJeremy L Thompson **/
2532788fa27SJeremy L Thompson int CeedQFunctionContextSetGeneric(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
2543668ca4bSJeremy L Thompson   // Check field type
2556574a04fSJeremy L Thompson   CeedCheck(field_label->type == field_type, ctx->ceed, CEED_ERROR_UNSUPPORTED,
2566574a04fSJeremy L Thompson             "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", field_label->name,
2576574a04fSJeremy L Thompson             CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]);
258d8dd9a91SJeremy L Thompson 
259d8dd9a91SJeremy L Thompson   char *data;
2602b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &data));
2612788fa27SJeremy L Thompson   memcpy(&data[field_label->offset], values, field_label->size);
2622b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextRestoreData(ctx, &data));
263d8dd9a91SJeremy L Thompson 
264d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
265d8dd9a91SJeremy L Thompson }
266d8dd9a91SJeremy L Thompson 
267d8dd9a91SJeremy L Thompson /**
2682788fa27SJeremy L Thompson   @brief Get QFunctionContext field data, read-only
2692788fa27SJeremy L Thompson 
2702788fa27SJeremy L Thompson   @param[in]  ctx         CeedQFunctionContext
2712788fa27SJeremy L Thompson   @param[in]  field_label Label of field to read
2722788fa27SJeremy L Thompson   @param[in]  field_type  Type of field to read
2732788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
2742788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
2752788fa27SJeremy L Thompson 
2762788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2772788fa27SJeremy L Thompson 
2782788fa27SJeremy L Thompson   @ref Backend
2792788fa27SJeremy L Thompson **/
2802788fa27SJeremy L Thompson int CeedQFunctionContextGetGenericRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type,
2812788fa27SJeremy L Thompson                                        size_t *num_values, void *values) {
2822788fa27SJeremy L Thompson   // Check field type
2836574a04fSJeremy L Thompson   CeedCheck(field_label->type == field_type, ctx->ceed, CEED_ERROR_UNSUPPORTED,
2846574a04fSJeremy L Thompson             "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", field_label->name,
2856574a04fSJeremy L Thompson             CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]);
2862788fa27SJeremy L Thompson 
2872788fa27SJeremy L Thompson   char *data;
2882788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &data));
2892788fa27SJeremy L Thompson   *(void **)values = &data[field_label->offset];
2902788fa27SJeremy L Thompson   switch (field_type) {
2912788fa27SJeremy L Thompson     case CEED_CONTEXT_FIELD_INT32:
2922788fa27SJeremy L Thompson       *num_values = field_label->size / sizeof(int);
2932788fa27SJeremy L Thompson       break;
2942788fa27SJeremy L Thompson     case CEED_CONTEXT_FIELD_DOUBLE:
2952788fa27SJeremy L Thompson       *num_values = field_label->size / sizeof(double);
2962788fa27SJeremy L Thompson       break;
2972788fa27SJeremy L Thompson   }
2982788fa27SJeremy L Thompson 
2992788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3002788fa27SJeremy L Thompson }
3012788fa27SJeremy L Thompson 
3022788fa27SJeremy L Thompson /**
3032788fa27SJeremy L Thompson   @brief Restore QFunctionContext field data, read-only
3042788fa27SJeremy L Thompson 
3052788fa27SJeremy L Thompson   @param[in]  ctx         CeedQFunctionContext
3062788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
3072788fa27SJeremy L Thompson   @param[in]  field_type  Type of field to restore
3082788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
3092788fa27SJeremy L Thompson 
3102788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3112788fa27SJeremy L Thompson 
3122788fa27SJeremy L Thompson   @ref Backend
3132788fa27SJeremy L Thompson **/
3142788fa27SJeremy L Thompson int CeedQFunctionContextRestoreGenericRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type,
3152788fa27SJeremy L Thompson                                            void *values) {
3162788fa27SJeremy L Thompson   // Check field type
3176574a04fSJeremy L Thompson   CeedCheck(field_label->type == field_type, ctx->ceed, CEED_ERROR_UNSUPPORTED,
3186574a04fSJeremy L Thompson             "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", field_label->name,
3196574a04fSJeremy L Thompson             CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]);
3202788fa27SJeremy L Thompson 
3212788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextRestoreDataRead(ctx, values));
3222788fa27SJeremy L Thompson 
3232788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3242788fa27SJeremy L Thompson }
3252788fa27SJeremy L Thompson 
3262788fa27SJeremy L Thompson /**
327bfacc300SJeremy L Thompson   @brief Set QFunctionContext field holding a double precision value
328bfacc300SJeremy L Thompson 
329ea61e9acSJeremy L Thompson   @param[in,out] ctx         CeedQFunctionContext
3302788fa27SJeremy L Thompson   @param[in]     field_label Label for field to set
331ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
332bfacc300SJeremy L Thompson 
333bfacc300SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
334bfacc300SJeremy L Thompson 
3353e1e85abSJeremy L Thompson   @ref Backend
336bfacc300SJeremy L Thompson **/
3372b730f8bSJeremy L Thompson int CeedQFunctionContextSetDouble(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, double *values) {
3386574a04fSJeremy L Thompson   CeedCheck(field_label, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3392b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, values));
340bfacc300SJeremy L Thompson   return CEED_ERROR_SUCCESS;
341bfacc300SJeremy L Thompson }
342bfacc300SJeremy L Thompson 
343bfacc300SJeremy L Thompson /**
3442788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding a double precision value, read-only
3452788fa27SJeremy L Thompson 
3462788fa27SJeremy L Thompson   @param[in]  ctx         CeedQFunctionContext
3472788fa27SJeremy L Thompson   @param[in]  field_label Label for field to get
3482788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
3492788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
3502788fa27SJeremy L Thompson 
3512788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3522788fa27SJeremy L Thompson 
3532788fa27SJeremy L Thompson   @ref Backend
3542788fa27SJeremy L Thompson **/
3552788fa27SJeremy L Thompson int CeedQFunctionContextGetDoubleRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
3566574a04fSJeremy L Thompson   CeedCheck(field_label, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3572788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values));
3582788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3592788fa27SJeremy L Thompson }
3602788fa27SJeremy L Thompson 
3612788fa27SJeremy L Thompson /**
3622788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding a double precision value, read-only
3632788fa27SJeremy L Thompson 
3642788fa27SJeremy L Thompson   @param[in]  ctx         CeedQFunctionContext
3652788fa27SJeremy L Thompson   @param[in]  field_label Label for field to restore
3662788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
3672788fa27SJeremy L Thompson 
3682788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3692788fa27SJeremy L Thompson 
3702788fa27SJeremy L Thompson   @ref Backend
3712788fa27SJeremy L Thompson **/
3722788fa27SJeremy L Thompson int CeedQFunctionContextRestoreDoubleRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, const double **values) {
3736574a04fSJeremy L Thompson   CeedCheck(field_label, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3742788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, values));
3752788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3762788fa27SJeremy L Thompson }
3772788fa27SJeremy L Thompson 
3782788fa27SJeremy L Thompson /**
379bfacc300SJeremy L Thompson   @brief Set QFunctionContext field holding an int32 value
380bfacc300SJeremy L Thompson 
381ea61e9acSJeremy L Thompson   @param[in,out] ctx         CeedQFunctionContext
3822788fa27SJeremy L Thompson   @param[in]     field_label Label for field to set
383ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
384bfacc300SJeremy L Thompson 
385bfacc300SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
386bfacc300SJeremy L Thompson 
3873e1e85abSJeremy L Thompson   @ref Backend
388bfacc300SJeremy L Thompson **/
3892b730f8bSJeremy L Thompson int CeedQFunctionContextSetInt32(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, int *values) {
3906574a04fSJeremy L Thompson   CeedCheck(field_label, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3912b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_INT32, values));
392bfacc300SJeremy L Thompson   return CEED_ERROR_SUCCESS;
393bfacc300SJeremy L Thompson }
394bfacc300SJeremy L Thompson 
395bfacc300SJeremy L Thompson /**
3962788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding a int32 value, read-only
3972788fa27SJeremy L Thompson 
3982788fa27SJeremy L Thompson   @param[in]  ctx         CeedQFunctionContext
3992788fa27SJeremy L Thompson   @param[in]  field_label Label for field to get
4002788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
4012788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
4022788fa27SJeremy L Thompson 
4032788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4042788fa27SJeremy L Thompson 
4052788fa27SJeremy L Thompson   @ref Backend
4062788fa27SJeremy L Thompson **/
4072788fa27SJeremy L Thompson int CeedQFunctionContextGetInt32Read(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, size_t *num_values, const int **values) {
4086574a04fSJeremy L Thompson   CeedCheck(field_label, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
4092788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values));
4102788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4112788fa27SJeremy L Thompson }
4122788fa27SJeremy L Thompson 
4132788fa27SJeremy L Thompson /**
4142788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding a int32 value, read-only
4152788fa27SJeremy L Thompson 
4162788fa27SJeremy L Thompson   @param[in]  ctx         CeedQFunctionContext
4172788fa27SJeremy L Thompson   @param[in]  field_label Label for field to restore
4182788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
4192788fa27SJeremy L Thompson 
4202788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4212788fa27SJeremy L Thompson 
4222788fa27SJeremy L Thompson   @ref Backend
4232788fa27SJeremy L Thompson **/
4242788fa27SJeremy L Thompson int CeedQFunctionContextRestoreInt32Read(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, const int **values) {
4256574a04fSJeremy L Thompson   CeedCheck(field_label, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
4262788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_INT32, values));
4272788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4282788fa27SJeremy L Thompson }
4292788fa27SJeremy L Thompson 
4302788fa27SJeremy L Thompson /**
4312e64a2b9SJeremy L Thompson   @brief Get additional destroy routine for CeedQFunctionContext user data
4322e64a2b9SJeremy L Thompson 
4332e64a2b9SJeremy L Thompson   @param[in] ctx         CeedQFunctionContext to get user destroy function
4342e64a2b9SJeremy L Thompson   @param[out] f_mem_type Memory type to use when passing data into `f`
4352e64a2b9SJeremy L Thompson   @param[out] f          Additional routine to use to destroy user data
4362e64a2b9SJeremy L Thompson 
4372e64a2b9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4382e64a2b9SJeremy L Thompson 
4392e64a2b9SJeremy L Thompson   @ref Backend
4402e64a2b9SJeremy L Thompson **/
4412b730f8bSJeremy L Thompson int CeedQFunctionContextGetDataDestroy(CeedQFunctionContext ctx, CeedMemType *f_mem_type, CeedQFunctionContextDataDestroyUser *f) {
4422e64a2b9SJeremy L Thompson   if (f_mem_type) *f_mem_type = ctx->data_destroy_mem_type;
4432e64a2b9SJeremy L Thompson   if (f) *f = ctx->data_destroy_function;
4442e64a2b9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4452e64a2b9SJeremy L Thompson }
4462e64a2b9SJeremy L Thompson 
4472e64a2b9SJeremy L Thompson /**
44834359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunctionContext
44934359f16Sjeremylt 
450ea61e9acSJeremy L Thompson   @param[in,out] ctx CeedQFunctionContext to increment the reference counter
45134359f16Sjeremylt 
45234359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
45334359f16Sjeremylt 
45434359f16Sjeremylt   @ref Backend
45534359f16Sjeremylt **/
4569560d06aSjeremylt int CeedQFunctionContextReference(CeedQFunctionContext ctx) {
45734359f16Sjeremylt   ctx->ref_count++;
45834359f16Sjeremylt   return CEED_ERROR_SUCCESS;
45934359f16Sjeremylt }
46034359f16Sjeremylt 
461777ff853SJeremy L Thompson /// @}
462777ff853SJeremy L Thompson 
463777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
464777ff853SJeremy L Thompson /// CeedQFunctionContext Public API
465777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
466777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionUser
467777ff853SJeremy L Thompson /// @{
468777ff853SJeremy L Thompson 
469777ff853SJeremy L Thompson /**
470777ff853SJeremy L Thompson   @brief Create a CeedQFunctionContext for storing CeedQFunction user context data
471777ff853SJeremy L Thompson 
472ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedQFunctionContext will be created
473ea61e9acSJeremy L Thompson   @param[out] ctx  Address of the variable where the newly created CeedQFunctionContext will be stored
474777ff853SJeremy L Thompson 
475777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
476777ff853SJeremy L Thompson 
477777ff853SJeremy L Thompson   @ref User
478777ff853SJeremy L Thompson **/
479777ff853SJeremy L Thompson int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) {
480777ff853SJeremy L Thompson   if (!ceed->QFunctionContextCreate) {
481777ff853SJeremy L Thompson     Ceed delegate;
4826574a04fSJeremy L Thompson 
4832b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Context"));
4846574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support ContextCreate");
4852b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(delegate, ctx));
486e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
487777ff853SJeremy L Thompson   }
488777ff853SJeremy L Thompson 
4892b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, ctx));
490*db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*ctx)->ceed));
491d1d35e2fSjeremylt   (*ctx)->ref_count = 1;
4922b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionContextCreate(*ctx));
493e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
494777ff853SJeremy L Thompson }
495777ff853SJeremy L Thompson 
496777ff853SJeremy L Thompson /**
497ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunctionContext.
4984385fb7fSSebastian Grimberg 
499ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedQFunctionContextDestroy()`.
500512bb800SJeremy L Thompson 
501512bb800SJeremy L Thompson   Note: If the value of `ctx_copy` passed to this function is non-NULL, then it is assumed that `ctx_copy` is a pointer to a
502512bb800SJeremy L Thompson         CeedQFunctionContext. This CeedQFunctionContext will be destroyed if `ctx_copy` is the only reference to this CeedQFunctionContext.
5039560d06aSjeremylt 
504ea61e9acSJeremy L Thompson   @param[in]     ctx      CeedQFunctionContext to copy reference to
505ea61e9acSJeremy L Thompson   @param[in,out] ctx_copy Variable to store copied reference
5069560d06aSjeremylt 
5079560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5089560d06aSjeremylt 
5099560d06aSjeremylt   @ref User
5109560d06aSjeremylt **/
5112b730f8bSJeremy L Thompson int CeedQFunctionContextReferenceCopy(CeedQFunctionContext ctx, CeedQFunctionContext *ctx_copy) {
5122b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextReference(ctx));
5132b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(ctx_copy));
5149560d06aSjeremylt   *ctx_copy = ctx;
5159560d06aSjeremylt   return CEED_ERROR_SUCCESS;
5169560d06aSjeremylt }
5179560d06aSjeremylt 
5189560d06aSjeremylt /**
519ea61e9acSJeremy L Thompson   @brief Set the data used by a CeedQFunctionContext, freeing any previously allocated data if applicable.
5204385fb7fSSebastian Grimberg 
521ea61e9acSJeremy L Thompson   The backend may copy values to a different memtype, such as during @ref CeedQFunctionApply().
522777ff853SJeremy L Thompson   See also @ref CeedQFunctionContextTakeData().
523777ff853SJeremy L Thompson 
524ea61e9acSJeremy L Thompson   @param[in,out] ctx       CeedQFunctionContext
525ea61e9acSJeremy L Thompson   @param[in]     mem_type  Memory type of the data being passed
526ea61e9acSJeremy L Thompson   @param[in]     copy_mode Copy mode for the data
527ea61e9acSJeremy L Thompson   @param[in]     size      Size of data, in bytes
528ea61e9acSJeremy L Thompson   @param[in]     data      Data to be used
529777ff853SJeremy L Thompson 
530777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
531777ff853SJeremy L Thompson 
532777ff853SJeremy L Thompson   @ref User
533777ff853SJeremy L Thompson **/
5342b730f8bSJeremy L Thompson int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, size_t size, void *data) {
5356574a04fSJeremy L Thompson   CeedCheck(ctx->SetData, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support ContextSetData");
5366574a04fSJeremy L Thompson   CeedCheck(ctx->state % 2 == 0, ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
537777ff853SJeremy L Thompson 
5382b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroyData(ctx));
539d1d35e2fSjeremylt   ctx->ctx_size = size;
5402b730f8bSJeremy L Thompson   CeedCall(ctx->SetData(ctx, mem_type, copy_mode, data));
541777ff853SJeremy L Thompson   ctx->state += 2;
542e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
543777ff853SJeremy L Thompson }
544777ff853SJeremy L Thompson 
545777ff853SJeremy L Thompson /**
546891038deSjeremylt   @brief Take ownership of the data in a CeedQFunctionContext via the specified memory type.
5474385fb7fSSebastian Grimberg 
548891038deSjeremylt   The caller is responsible for managing and freeing the memory.
549891038deSjeremylt 
550ea61e9acSJeremy L Thompson   @param[in]  ctx      CeedQFunctionContext to access
551ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
552ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
553891038deSjeremylt   @param[out] data     Data on memory type mem_type
554891038deSjeremylt 
555891038deSjeremylt   @return An error code: 0 - success, otherwise - failure
556891038deSjeremylt 
557891038deSjeremylt   @ref User
558891038deSjeremylt **/
5592b730f8bSJeremy L Thompson int CeedQFunctionContextTakeData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
5609c774eddSJeremy L Thompson   bool has_valid_data = true;
5612b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
5626574a04fSJeremy L Thompson   CeedCheck(has_valid_data, ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to take, must set data");
5639c774eddSJeremy L Thompson 
5646574a04fSJeremy L Thompson   CeedCheck(ctx->TakeData, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support TakeData");
5656574a04fSJeremy L Thompson   CeedCheck(ctx->state % 2 == 0, ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
566891038deSjeremylt 
5679c774eddSJeremy L Thompson   bool has_borrowed_data_of_type = true;
5682b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextHasBorrowedDataOfType(ctx, mem_type, &has_borrowed_data_of_type));
5696574a04fSJeremy L Thompson   CeedCheck(has_borrowed_data_of_type, ctx->ceed, CEED_ERROR_BACKEND,
5706574a04fSJeremy L Thompson             "CeedQFunctionContext has no borrowed %s data, must set data with CeedQFunctionContextSetData", CeedMemTypes[mem_type]);
5719c774eddSJeremy L Thompson 
572891038deSjeremylt   void *temp_data = NULL;
5732b730f8bSJeremy L Thompson   CeedCall(ctx->TakeData(ctx, mem_type, &temp_data));
574891038deSjeremylt   if (data) (*(void **)data) = temp_data;
575891038deSjeremylt   return CEED_ERROR_SUCCESS;
576891038deSjeremylt }
577891038deSjeremylt 
578891038deSjeremylt /**
579777ff853SJeremy L Thompson   @brief Get read/write access to a CeedQFunctionContext via the specified memory type.
5804385fb7fSSebastian Grimberg 
581777ff853SJeremy L Thompson   Restore access with @ref CeedQFunctionContextRestoreData().
582777ff853SJeremy L Thompson 
583ea61e9acSJeremy L Thompson   @param[in]  ctx      CeedQFunctionContext to access
584ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
585ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
586d1d35e2fSjeremylt   @param[out] data     Data on memory type mem_type
587777ff853SJeremy L Thompson 
588ea61e9acSJeremy L Thompson   @note The CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions provide access to array pointers in the desired memory
5899fd66db6SSebastian Grimberg space.
5909fd66db6SSebastian Grimberg         Pairing get/restore allows the Context to track access.
591777ff853SJeremy L Thompson 
592777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
593777ff853SJeremy L Thompson 
594777ff853SJeremy L Thompson   @ref User
595777ff853SJeremy L Thompson **/
5962b730f8bSJeremy L Thompson int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
5976574a04fSJeremy L Thompson   CeedCheck(ctx->GetData, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetData");
5986574a04fSJeremy L Thompson   CeedCheck(ctx->state % 2 == 0, ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
5996574a04fSJeremy L Thompson   CeedCheck(ctx->num_readers == 0, ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, a process has read access");
60028bfd0b7SJeremy L Thompson 
6019c774eddSJeremy L Thompson   bool has_valid_data = true;
6022b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
6036574a04fSJeremy L Thompson   CeedCheck(has_valid_data, ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data");
6049c774eddSJeremy L Thompson 
6052b730f8bSJeremy L Thompson   CeedCall(ctx->GetData(ctx, mem_type, data));
60628bfd0b7SJeremy L Thompson   ctx->state++;
60728bfd0b7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
60828bfd0b7SJeremy L Thompson }
60928bfd0b7SJeremy L Thompson 
61028bfd0b7SJeremy L Thompson /**
61128bfd0b7SJeremy L Thompson   @brief Get read only access to a CeedQFunctionContext via the specified memory type.
6124385fb7fSSebastian Grimberg 
61328bfd0b7SJeremy L Thompson   Restore access with @ref CeedQFunctionContextRestoreData().
61428bfd0b7SJeremy L Thompson 
615ea61e9acSJeremy L Thompson   @param[in]  ctx      CeedQFunctionContext to access
616ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
617ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
61828bfd0b7SJeremy L Thompson   @param[out] data     Data on memory type mem_type
61928bfd0b7SJeremy L Thompson 
620ea61e9acSJeremy L Thompson   @note The CeedQFunctionContextGetDataRead() and @ref CeedQFunctionContextRestoreDataRead() functions provide access to array pointers in the desired
6219fd66db6SSebastian Grimberg memory space.
6229fd66db6SSebastian Grimberg         Pairing get/restore allows the Context to track access.
62328bfd0b7SJeremy L Thompson 
62428bfd0b7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
62528bfd0b7SJeremy L Thompson 
62628bfd0b7SJeremy L Thompson   @ref User
62728bfd0b7SJeremy L Thompson **/
6282b730f8bSJeremy L Thompson int CeedQFunctionContextGetDataRead(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
6296574a04fSJeremy L Thompson   CeedCheck(ctx->GetDataRead, ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetDataRead");
6306574a04fSJeremy L Thompson   CeedCheck(ctx->state % 2 == 0, ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
63128bfd0b7SJeremy L Thompson 
63228bfd0b7SJeremy L Thompson   bool has_valid_data = true;
6332b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
6346574a04fSJeremy L Thompson   CeedCheck(has_valid_data, ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data");
63528bfd0b7SJeremy L Thompson 
6362b730f8bSJeremy L Thompson   CeedCall(ctx->GetDataRead(ctx, mem_type, data));
63728bfd0b7SJeremy L Thompson   ctx->num_readers++;
638e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
639777ff853SJeremy L Thompson }
640777ff853SJeremy L Thompson 
641777ff853SJeremy L Thompson /**
642777ff853SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetData()
643777ff853SJeremy L Thompson 
644ea61e9acSJeremy L Thompson   @param[in]     ctx  CeedQFunctionContext to restore
645ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
646777ff853SJeremy L Thompson 
647777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
648777ff853SJeremy L Thompson 
649777ff853SJeremy L Thompson   @ref User
650777ff853SJeremy L Thompson **/
651777ff853SJeremy L Thompson int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) {
6526574a04fSJeremy L Thompson   CeedCheck(ctx->state % 2 == 1, ctx->ceed, 1, "Cannot restore CeedQFunctionContext array access, access was not granted");
653777ff853SJeremy L Thompson 
6546574a04fSJeremy L Thompson   if (ctx->RestoreData) CeedCall(ctx->RestoreData(ctx));
655777ff853SJeremy L Thompson   *(void **)data = NULL;
65628bfd0b7SJeremy L Thompson   ctx->state++;
65728bfd0b7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
65828bfd0b7SJeremy L Thompson }
65928bfd0b7SJeremy L Thompson 
66028bfd0b7SJeremy L Thompson /**
66128bfd0b7SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetDataRead()
66228bfd0b7SJeremy L Thompson 
663ea61e9acSJeremy L Thompson   @param[in]     ctx  CeedQFunctionContext to restore
664ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
66528bfd0b7SJeremy L Thompson 
66628bfd0b7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
66728bfd0b7SJeremy L Thompson 
66828bfd0b7SJeremy L Thompson   @ref User
66928bfd0b7SJeremy L Thompson **/
67028bfd0b7SJeremy L Thompson int CeedQFunctionContextRestoreDataRead(CeedQFunctionContext ctx, void *data) {
6716574a04fSJeremy L Thompson   CeedCheck(ctx->num_readers > 0, ctx->ceed, 1, "Cannot restore CeedQFunctionContext array access, access was not granted");
67228bfd0b7SJeremy L Thompson 
67375a19770SJeremy L Thompson   ctx->num_readers--;
6746574a04fSJeremy L Thompson   if (ctx->num_readers == 0 && ctx->RestoreDataRead) CeedCall(ctx->RestoreDataRead(ctx));
67528bfd0b7SJeremy L Thompson   *(void **)data = NULL;
67675a19770SJeremy L Thompson 
677e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
678777ff853SJeremy L Thompson }
679777ff853SJeremy L Thompson 
680777ff853SJeremy L Thompson /**
681cdf32b93SJeremy L Thompson   @brief Register QFunctionContext a field holding a double precision value
682cdf32b93SJeremy L Thompson 
683ea61e9acSJeremy L Thompson   @param[in,out] ctx               CeedQFunctionContext
684ea61e9acSJeremy L Thompson   @param[in]     field_name        Name of field to register
685ea61e9acSJeremy L Thompson   @param[in]     field_offset      Offset of field to register
686ea61e9acSJeremy L Thompson   @param[in]     num_values        Number of values to register, must be contiguous in memory
687ea61e9acSJeremy L Thompson   @param[in]     field_description Description of field, or NULL for none
688cdf32b93SJeremy L Thompson 
689cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
690cdf32b93SJeremy L Thompson 
691cdf32b93SJeremy L Thompson   @ref User
692cdf32b93SJeremy L Thompson **/
6932b730f8bSJeremy L Thompson int CeedQFunctionContextRegisterDouble(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values,
694cdf32b93SJeremy L Thompson                                        const char *field_description) {
6952b730f8bSJeremy L Thompson   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_DOUBLE, sizeof(double), num_values);
696cdf32b93SJeremy L Thompson }
697cdf32b93SJeremy L Thompson 
698cdf32b93SJeremy L Thompson /**
699cdf32b93SJeremy L Thompson   @brief Register QFunctionContext a field holding a int32 value
700cdf32b93SJeremy L Thompson 
701ea61e9acSJeremy L Thompson   @param[in,out] ctx               CeedQFunctionContext
702ea61e9acSJeremy L Thompson   @param[in]     field_name        Name of field to register
703ea61e9acSJeremy L Thompson   @param[in]     field_offset      Offset of field to register
704ea61e9acSJeremy L Thompson   @param[in]     num_values        Number of values to register, must be contiguous in memory
705ea61e9acSJeremy L Thompson   @param[in]     field_description Description of field, or NULL for none
706cdf32b93SJeremy L Thompson 
707cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
708cdf32b93SJeremy L Thompson 
709cdf32b93SJeremy L Thompson   @ref User
710cdf32b93SJeremy L Thompson **/
7112b730f8bSJeremy L Thompson int CeedQFunctionContextRegisterInt32(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values,
712cdf32b93SJeremy L Thompson                                       const char *field_description) {
7132b730f8bSJeremy L Thompson   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_INT32, sizeof(int), num_values);
714cdf32b93SJeremy L Thompson }
715cdf32b93SJeremy L Thompson 
716cdf32b93SJeremy L Thompson /**
7173668ca4bSJeremy L Thompson   @brief Get labels for all registered QFunctionContext fields
718cdf32b93SJeremy L Thompson 
719ea61e9acSJeremy L Thompson   @param[in]  ctx          CeedQFunctionContext
7203668ca4bSJeremy L Thompson   @param[out] field_labels Variable to hold array of field labels
721cdf32b93SJeremy L Thompson   @param[out] num_fields   Length of field descriptions array
722cdf32b93SJeremy L Thompson 
723cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
724cdf32b93SJeremy L Thompson 
725cdf32b93SJeremy L Thompson   @ref User
726cdf32b93SJeremy L Thompson **/
7272b730f8bSJeremy L Thompson int CeedQFunctionContextGetAllFieldLabels(CeedQFunctionContext ctx, const CeedContextFieldLabel **field_labels, CeedInt *num_fields) {
7283668ca4bSJeremy L Thompson   *field_labels = ctx->field_labels;
729cdf32b93SJeremy L Thompson   *num_fields   = ctx->num_fields;
730cdf32b93SJeremy L Thompson   return CEED_ERROR_SUCCESS;
731cdf32b93SJeremy L Thompson }
732cdf32b93SJeremy L Thompson 
733cdf32b93SJeremy L Thompson /**
7340f86cbe7SJeremy L Thompson   @brief Get the descriptive information about a CeedContextFieldLabel
7350f86cbe7SJeremy L Thompson 
7360f86cbe7SJeremy L Thompson   @param[in]  label             CeedContextFieldLabel
7370f86cbe7SJeremy L Thompson   @param[out] field_name        Name of labeled field
7380f86cbe7SJeremy L Thompson   @param[out] field_description Description of field, or NULL for none
7397bfe0f0eSJeremy L Thompson   @param[out] num_values        Number of values registered
7400f86cbe7SJeremy L Thompson   @param[out] field_type        CeedContextFieldType
7410f86cbe7SJeremy L Thompson 
7420f86cbe7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7430f86cbe7SJeremy L Thompson 
7440f86cbe7SJeremy L Thompson   @ref User
7450f86cbe7SJeremy L Thompson **/
7462b730f8bSJeremy L Thompson int CeedContextFieldLabelGetDescription(CeedContextFieldLabel label, const char **field_name, const char **field_description, size_t *num_values,
7470f86cbe7SJeremy L Thompson                                         CeedContextFieldType *field_type) {
7480f86cbe7SJeremy L Thompson   if (field_name) *field_name = label->name;
7490f86cbe7SJeremy L Thompson   if (field_description) *field_description = label->description;
7507bfe0f0eSJeremy L Thompson   if (num_values) *num_values = label->num_values;
7510f86cbe7SJeremy L Thompson   if (field_type) *field_type = label->type;
7520f86cbe7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7530f86cbe7SJeremy L Thompson }
7540f86cbe7SJeremy L Thompson 
7550f86cbe7SJeremy L Thompson /**
75680a9ef05SNatalie Beams   @brief Get data size for a Context
75780a9ef05SNatalie Beams 
758ea61e9acSJeremy L Thompson   @param[in]  ctx      CeedQFunctionContext
75980a9ef05SNatalie Beams   @param[out] ctx_size Variable to store size of context data values
76080a9ef05SNatalie Beams 
76180a9ef05SNatalie Beams   @return An error code: 0 - success, otherwise - failure
76280a9ef05SNatalie Beams 
76380a9ef05SNatalie Beams   @ref User
76480a9ef05SNatalie Beams **/
7652b730f8bSJeremy L Thompson int CeedQFunctionContextGetContextSize(CeedQFunctionContext ctx, size_t *ctx_size) {
76680a9ef05SNatalie Beams   *ctx_size = ctx->ctx_size;
76780a9ef05SNatalie Beams   return CEED_ERROR_SUCCESS;
76880a9ef05SNatalie Beams }
76980a9ef05SNatalie Beams 
77080a9ef05SNatalie Beams /**
771777ff853SJeremy L Thompson   @brief View a CeedQFunctionContext
772777ff853SJeremy L Thompson 
773777ff853SJeremy L Thompson   @param[in] ctx    CeedQFunctionContext to view
774777ff853SJeremy L Thompson   @param[in] stream Filestream to write to
775777ff853SJeremy L Thompson 
776777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
777777ff853SJeremy L Thompson 
778777ff853SJeremy L Thompson   @ref User
779777ff853SJeremy L Thompson **/
780777ff853SJeremy L Thompson int CeedQFunctionContextView(CeedQFunctionContext ctx, FILE *stream) {
781777ff853SJeremy L Thompson   fprintf(stream, "CeedQFunctionContext\n");
782d1d35e2fSjeremylt   fprintf(stream, "  Context Data Size: %ld\n", ctx->ctx_size);
7833668ca4bSJeremy L Thompson   for (CeedInt i = 0; i < ctx->num_fields; i++) {
7843668ca4bSJeremy L Thompson     // LCOV_EXCL_START
7852b730f8bSJeremy L Thompson     fprintf(stream, "  Labeled %s field: %s\n", CeedContextFieldTypes[ctx->field_labels[i]->type], ctx->field_labels[i]->name);
7863668ca4bSJeremy L Thompson     // LCOV_EXCL_STOP
7873668ca4bSJeremy L Thompson   }
788e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
789777ff853SJeremy L Thompson }
790777ff853SJeremy L Thompson 
791777ff853SJeremy L Thompson /**
7922790b72bSJeremy L Thompson   @brief Set additional destroy routine for CeedQFunctionContext user data
7932790b72bSJeremy L Thompson 
794ea61e9acSJeremy L Thompson   @param[in,out] ctx        CeedQFunctionContext to set user destroy function
7952e64a2b9SJeremy L Thompson   @param[in]     f_mem_type Memory type to use when passing data into `f`
7962e64a2b9SJeremy L Thompson   @param[in]     f          Additional routine to use to destroy user data
7972790b72bSJeremy L Thompson 
7982790b72bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7992790b72bSJeremy L Thompson 
8002790b72bSJeremy L Thompson   @ref User
8012790b72bSJeremy L Thompson **/
8022b730f8bSJeremy L Thompson int CeedQFunctionContextSetDataDestroy(CeedQFunctionContext ctx, CeedMemType f_mem_type, CeedQFunctionContextDataDestroyUser f) {
8036574a04fSJeremy L Thompson   CeedCheck(f, ctx->ceed, 1, "Must provide valid callback function for destroying user data");
8042790b72bSJeremy L Thompson   ctx->data_destroy_mem_type = f_mem_type;
8052790b72bSJeremy L Thompson   ctx->data_destroy_function = f;
8062790b72bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
8072790b72bSJeremy L Thompson }
8082790b72bSJeremy L Thompson 
8092790b72bSJeremy L Thompson /**
810777ff853SJeremy L Thompson   @brief Destroy a CeedQFunctionContext
811777ff853SJeremy L Thompson 
812ea61e9acSJeremy L Thompson   @param[in,out] ctx CeedQFunctionContext to destroy
813777ff853SJeremy L Thompson 
814777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
815777ff853SJeremy L Thompson 
816777ff853SJeremy L Thompson   @ref User
817777ff853SJeremy L Thompson **/
818777ff853SJeremy L Thompson int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx) {
819ad6481ceSJeremy L Thompson   if (!*ctx || --(*ctx)->ref_count > 0) {
820ad6481ceSJeremy L Thompson     *ctx = NULL;
821ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
822ad6481ceSJeremy L Thompson   }
8236574a04fSJeremy L Thompson   CeedCheck(((*ctx)->state % 2) == 0, (*ctx)->ceed, 1, "Cannot destroy CeedQFunctionContext, the access lock is in use");
824777ff853SJeremy L Thompson 
8252b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroyData(*ctx));
8262b730f8bSJeremy L Thompson   if ((*ctx)->Destroy) CeedCall((*ctx)->Destroy(*ctx));
827cdf32b93SJeremy L Thompson   for (CeedInt i = 0; i < (*ctx)->num_fields; i++) {
8282b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*ctx)->field_labels[i]->name));
8292b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*ctx)->field_labels[i]->description));
8302b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*ctx)->field_labels[i]));
831cdf32b93SJeremy L Thompson   }
8322b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*ctx)->field_labels));
8332b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*ctx)->ceed));
8342b730f8bSJeremy L Thompson   CeedCall(CeedFree(ctx));
835cdf32b93SJeremy L Thompson 
836e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
837777ff853SJeremy L Thompson }
838777ff853SJeremy L Thompson 
839777ff853SJeremy L Thompson /// @}
840