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> 9*49aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 11*49aac155SJeremy 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)); 642b730f8bSJeremy L Thompson if (field_index != -1) { 65cdf32b93SJeremy L Thompson // LCOV_EXCL_START 662b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "QFunctionContext field with name \"%s\" already registered", field_name); 67cdf32b93SJeremy L Thompson // LCOV_EXCL_STOP 682b730f8bSJeremy L Thompson } 69cdf32b93SJeremy L Thompson 70cdf32b93SJeremy L Thompson // Allocate space for field data 71cdf32b93SJeremy L Thompson if (ctx->num_fields == 0) { 722b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &ctx->field_labels)); 73cdf32b93SJeremy L Thompson ctx->max_fields = 1; 74cdf32b93SJeremy L Thompson } else if (ctx->num_fields == ctx->max_fields) { 752b730f8bSJeremy L Thompson CeedCall(CeedRealloc(2 * ctx->max_fields, &ctx->field_labels)); 76cdf32b93SJeremy L Thompson ctx->max_fields *= 2; 77cdf32b93SJeremy L Thompson } 782b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &ctx->field_labels[ctx->num_fields])); 79cdf32b93SJeremy L Thompson 80cdf32b93SJeremy L Thompson // Copy field data 812b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(field_name, (char **)&ctx->field_labels[ctx->num_fields]->name)); 822b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(field_description, (char **)&ctx->field_labels[ctx->num_fields]->description)); 833668ca4bSJeremy L Thompson ctx->field_labels[ctx->num_fields]->type = field_type; 843668ca4bSJeremy L Thompson ctx->field_labels[ctx->num_fields]->offset = field_offset; 857bfe0f0eSJeremy L Thompson ctx->field_labels[ctx->num_fields]->size = field_size * num_values; 867bfe0f0eSJeremy L Thompson ctx->field_labels[ctx->num_fields]->num_values = num_values; 87cdf32b93SJeremy L Thompson ctx->num_fields++; 88cdf32b93SJeremy L Thompson return CEED_ERROR_SUCCESS; 89cdf32b93SJeremy L Thompson } 90cdf32b93SJeremy L Thompson 913b190ab8SJeremy L Thompson /** 92ea61e9acSJeremy L Thompson @brief Destroy user data held by CeedQFunctionContext, using function set by CeedQFunctionContextSetDataDestroy, if applicable 933b190ab8SJeremy L Thompson 943b190ab8SJeremy L Thompson @param[in,out] ctx CeedQFunctionContext to destroy user data 953b190ab8SJeremy L Thompson 963b190ab8SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 973b190ab8SJeremy L Thompson 983b190ab8SJeremy L Thompson @ref Developer 993b190ab8SJeremy L Thompson **/ 1003b190ab8SJeremy L Thompson static int CeedQFunctionContextDestroyData(CeedQFunctionContext ctx) { 1013b190ab8SJeremy L Thompson if (ctx->DataDestroy) { 1022b730f8bSJeremy L Thompson CeedCall(ctx->DataDestroy(ctx)); 1033b190ab8SJeremy L Thompson } else { 1043b190ab8SJeremy L Thompson CeedQFunctionContextDataDestroyUser data_destroy_function; 1053b190ab8SJeremy L Thompson CeedMemType data_destroy_mem_type; 1063b190ab8SJeremy L Thompson 1072b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetDataDestroy(ctx, &data_destroy_mem_type, &data_destroy_function)); 1083b190ab8SJeremy L Thompson if (data_destroy_function) { 1093b190ab8SJeremy L Thompson void *data; 1103b190ab8SJeremy L Thompson 1112b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetData(ctx, data_destroy_mem_type, &data)); 1122b730f8bSJeremy L Thompson CeedCall(data_destroy_function(data)); 1132b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextRestoreData(ctx, &data)); 1143b190ab8SJeremy L Thompson } 1153b190ab8SJeremy L Thompson } 1163b190ab8SJeremy L Thompson 1173b190ab8SJeremy L Thompson return CEED_ERROR_SUCCESS; 1183b190ab8SJeremy L Thompson } 1193b190ab8SJeremy L Thompson 120cdf32b93SJeremy L Thompson /// @} 121cdf32b93SJeremy L Thompson 122cdf32b93SJeremy L Thompson /// ---------------------------------------------------------------------------- 123777ff853SJeremy L Thompson /// CeedQFunctionContext Backend API 124777ff853SJeremy L Thompson /// ---------------------------------------------------------------------------- 125777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionBackend 126777ff853SJeremy L Thompson /// @{ 127777ff853SJeremy L Thompson 128777ff853SJeremy L Thompson /** 129777ff853SJeremy L Thompson @brief Get the Ceed associated with a CeedQFunctionContext 130777ff853SJeremy L Thompson 131ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext 132777ff853SJeremy L Thompson @param[out] ceed Variable to store Ceed 133777ff853SJeremy L Thompson 134777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 135777ff853SJeremy L Thompson 136777ff853SJeremy L Thompson @ref Backend 137777ff853SJeremy L Thompson **/ 138777ff853SJeremy L Thompson int CeedQFunctionContextGetCeed(CeedQFunctionContext ctx, Ceed *ceed) { 139777ff853SJeremy L Thompson *ceed = ctx->ceed; 140e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 141777ff853SJeremy L Thompson } 142777ff853SJeremy L Thompson 143777ff853SJeremy L Thompson /** 1449c774eddSJeremy L Thompson @brief Check for valid data in a CeedQFunctionContext 1459c774eddSJeremy L Thompson 146ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext to check validity 1479c774eddSJeremy L Thompson @param[out] has_valid_data Variable to store validity 1489c774eddSJeremy L Thompson 1499c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1509c774eddSJeremy L Thompson 1519c774eddSJeremy L Thompson @ref Backend 1529c774eddSJeremy L Thompson **/ 1532b730f8bSJeremy L Thompson int CeedQFunctionContextHasValidData(CeedQFunctionContext ctx, bool *has_valid_data) { 1542b730f8bSJeremy L Thompson if (!ctx->HasValidData) { 1559c774eddSJeremy L Thompson // LCOV_EXCL_START 1562b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasValidData"); 1579c774eddSJeremy L Thompson // LCOV_EXCL_STOP 1582b730f8bSJeremy L Thompson } 1599c774eddSJeremy L Thompson 1602b730f8bSJeremy L Thompson CeedCall(ctx->HasValidData(ctx, has_valid_data)); 1619c774eddSJeremy L Thompson 1629c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 1639c774eddSJeremy L Thompson } 1649c774eddSJeremy L Thompson 1659c774eddSJeremy L Thompson /** 166ea61e9acSJeremy L Thompson @brief Check for borrowed data of a specific CeedMemType in a CeedQFunctionContext 1679c774eddSJeremy L Thompson 168ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext to check 169ea61e9acSJeremy L Thompson @param[in] mem_type Memory type to check 1709c774eddSJeremy L Thompson @param[out] has_borrowed_data_of_type Variable to store result 1719c774eddSJeremy L Thompson 1729c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1739c774eddSJeremy L Thompson 1749c774eddSJeremy L Thompson @ref Backend 1759c774eddSJeremy L Thompson **/ 1762b730f8bSJeremy L Thompson int CeedQFunctionContextHasBorrowedDataOfType(CeedQFunctionContext ctx, CeedMemType mem_type, bool *has_borrowed_data_of_type) { 1772b730f8bSJeremy L Thompson if (!ctx->HasBorrowedDataOfType) { 1789c774eddSJeremy L Thompson // LCOV_EXCL_START 1792b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasBorrowedDataOfType"); 1809c774eddSJeremy L Thompson // LCOV_EXCL_STOP 1812b730f8bSJeremy L Thompson } 1829c774eddSJeremy L Thompson 1832b730f8bSJeremy L Thompson CeedCall(ctx->HasBorrowedDataOfType(ctx, mem_type, has_borrowed_data_of_type)); 1849c774eddSJeremy L Thompson 1859c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 1869c774eddSJeremy L Thompson } 1879c774eddSJeremy L Thompson 1889c774eddSJeremy L Thompson /** 189777ff853SJeremy L Thompson @brief Get the state of a CeedQFunctionContext 190777ff853SJeremy L Thompson 191ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext to retrieve state 192777ff853SJeremy L Thompson @param[out] state Variable to store state 193777ff853SJeremy L Thompson 194777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 195777ff853SJeremy L Thompson 196777ff853SJeremy L Thompson @ref Backend 197777ff853SJeremy L Thompson **/ 198777ff853SJeremy L Thompson int CeedQFunctionContextGetState(CeedQFunctionContext ctx, uint64_t *state) { 199777ff853SJeremy L Thompson *state = ctx->state; 200e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 201777ff853SJeremy L Thompson } 202777ff853SJeremy L Thompson 203777ff853SJeremy L Thompson /** 204777ff853SJeremy L Thompson @brief Get backend data of a CeedQFunctionContext 205777ff853SJeremy L Thompson 206ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext 207777ff853SJeremy L Thompson @param[out] data Variable to store data 208777ff853SJeremy L Thompson 209777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 210777ff853SJeremy L Thompson 211777ff853SJeremy L Thompson @ref Backend 212777ff853SJeremy L Thompson **/ 213777ff853SJeremy L Thompson int CeedQFunctionContextGetBackendData(CeedQFunctionContext ctx, void *data) { 214777ff853SJeremy L Thompson *(void **)data = ctx->data; 215e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 216777ff853SJeremy L Thompson } 217777ff853SJeremy L Thompson 218777ff853SJeremy L Thompson /** 219777ff853SJeremy L Thompson @brief Set backend data of a CeedQFunctionContext 220777ff853SJeremy L Thompson 221ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext 222ea61e9acSJeremy L Thompson @param[in] data Data to set 223777ff853SJeremy L Thompson 224777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 225777ff853SJeremy L Thompson 226777ff853SJeremy L Thompson @ref Backend 227777ff853SJeremy L Thompson **/ 228777ff853SJeremy L Thompson int CeedQFunctionContextSetBackendData(CeedQFunctionContext ctx, void *data) { 229777ff853SJeremy L Thompson ctx->data = data; 230e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 231777ff853SJeremy L Thompson } 232777ff853SJeremy L Thompson 23334359f16Sjeremylt /** 234ea61e9acSJeremy L Thompson @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name` 235e6a0ab89SJeremy L Thompson 236e6a0ab89SJeremy L Thompson @param[in] ctx CeedQFunctionContext 237e6a0ab89SJeremy L Thompson @param[in] field_name Name of field to retrieve label 238e6a0ab89SJeremy L Thompson @param[out] field_label Variable to field label 239e6a0ab89SJeremy L Thompson 240e6a0ab89SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 241e6a0ab89SJeremy L Thompson 2423e1e85abSJeremy L Thompson @ref Backend 243e6a0ab89SJeremy L Thompson **/ 2442b730f8bSJeremy L Thompson int CeedQFunctionContextGetFieldLabel(CeedQFunctionContext ctx, const char *field_name, CeedContextFieldLabel *field_label) { 245e6a0ab89SJeremy L Thompson CeedInt field_index; 2462b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index)); 247e6a0ab89SJeremy L Thompson 248e6a0ab89SJeremy L Thompson if (field_index != -1) { 249e6a0ab89SJeremy L Thompson *field_label = ctx->field_labels[field_index]; 250e6a0ab89SJeremy L Thompson } else { 251e6a0ab89SJeremy L Thompson *field_label = NULL; 252e6a0ab89SJeremy L Thompson } 253e6a0ab89SJeremy L Thompson 254e6a0ab89SJeremy L Thompson return CEED_ERROR_SUCCESS; 255e6a0ab89SJeremy L Thompson } 256e6a0ab89SJeremy L Thompson 257e6a0ab89SJeremy L Thompson /** 258d8dd9a91SJeremy L Thompson @brief Set QFunctionContext field 259d8dd9a91SJeremy L Thompson 260ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext 261ea61e9acSJeremy L Thompson @param[in] field_label Label of field to set 262ea61e9acSJeremy L Thompson @param[in] field_type Type of field to set 2632788fa27SJeremy L Thompson @param[in] values Value to set 264d8dd9a91SJeremy L Thompson 265d8dd9a91SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 266d8dd9a91SJeremy L Thompson 2673e1e85abSJeremy L Thompson @ref Backend 268d8dd9a91SJeremy L Thompson **/ 2692788fa27SJeremy L Thompson int CeedQFunctionContextSetGeneric(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) { 2703668ca4bSJeremy L Thompson // Check field type 2712b730f8bSJeremy L Thompson if (field_label->type != field_type) { 272d8dd9a91SJeremy L Thompson // LCOV_EXCL_START 2732b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", 2742b730f8bSJeremy L Thompson field_label->name, CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]); 275d8dd9a91SJeremy L Thompson // LCOV_EXCL_STOP 2762b730f8bSJeremy L Thompson } 277d8dd9a91SJeremy L Thompson 278d8dd9a91SJeremy L Thompson char *data; 2792b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &data)); 2802788fa27SJeremy L Thompson memcpy(&data[field_label->offset], values, field_label->size); 2812b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextRestoreData(ctx, &data)); 282d8dd9a91SJeremy L Thompson 283d8dd9a91SJeremy L Thompson return CEED_ERROR_SUCCESS; 284d8dd9a91SJeremy L Thompson } 285d8dd9a91SJeremy L Thompson 286d8dd9a91SJeremy L Thompson /** 2872788fa27SJeremy L Thompson @brief Get QFunctionContext field data, read-only 2882788fa27SJeremy L Thompson 2892788fa27SJeremy L Thompson @param[in] ctx CeedQFunctionContext 2902788fa27SJeremy L Thompson @param[in] field_label Label of field to read 2912788fa27SJeremy L Thompson @param[in] field_type Type of field to read 2922788fa27SJeremy L Thompson @param[out] num_values Number of values in the field label 2932788fa27SJeremy L Thompson @param[out] values Pointer to context values 2942788fa27SJeremy L Thompson 2952788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2962788fa27SJeremy L Thompson 2972788fa27SJeremy L Thompson @ref Backend 2982788fa27SJeremy L Thompson **/ 2992788fa27SJeremy L Thompson int CeedQFunctionContextGetGenericRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type, 3002788fa27SJeremy L Thompson size_t *num_values, void *values) { 3012788fa27SJeremy L Thompson // Check field type 3022788fa27SJeremy L Thompson if (field_label->type != field_type) { 3032788fa27SJeremy L Thompson // LCOV_EXCL_START 3042788fa27SJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", 3052788fa27SJeremy L Thompson field_label->name, CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]); 3062788fa27SJeremy L Thompson // LCOV_EXCL_STOP 3072788fa27SJeremy L Thompson } 3082788fa27SJeremy L Thompson 3092788fa27SJeremy L Thompson char *data; 3102788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &data)); 3112788fa27SJeremy L Thompson *(void **)values = &data[field_label->offset]; 3122788fa27SJeremy L Thompson switch (field_type) { 3132788fa27SJeremy L Thompson case CEED_CONTEXT_FIELD_INT32: 3142788fa27SJeremy L Thompson *num_values = field_label->size / sizeof(int); 3152788fa27SJeremy L Thompson break; 3162788fa27SJeremy L Thompson case CEED_CONTEXT_FIELD_DOUBLE: 3172788fa27SJeremy L Thompson *num_values = field_label->size / sizeof(double); 3182788fa27SJeremy L Thompson break; 3192788fa27SJeremy L Thompson } 3202788fa27SJeremy L Thompson 3212788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 3222788fa27SJeremy L Thompson } 3232788fa27SJeremy L Thompson 3242788fa27SJeremy L Thompson /** 3252788fa27SJeremy L Thompson @brief Restore QFunctionContext field data, read-only 3262788fa27SJeremy L Thompson 3272788fa27SJeremy L Thompson @param[in] ctx CeedQFunctionContext 3282788fa27SJeremy L Thompson @param[in] field_label Label of field to restore 3292788fa27SJeremy L Thompson @param[in] field_type Type of field to restore 3302788fa27SJeremy L Thompson @param[out] values Pointer to context values 3312788fa27SJeremy L Thompson 3322788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3332788fa27SJeremy L Thompson 3342788fa27SJeremy L Thompson @ref Backend 3352788fa27SJeremy L Thompson **/ 3362788fa27SJeremy L Thompson int CeedQFunctionContextRestoreGenericRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type, 3372788fa27SJeremy L Thompson void *values) { 3382788fa27SJeremy L Thompson // Check field type 3392788fa27SJeremy L Thompson if (field_label->type != field_type) { 3402788fa27SJeremy L Thompson // LCOV_EXCL_START 3412788fa27SJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", 3422788fa27SJeremy L Thompson field_label->name, CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]); 3432788fa27SJeremy L Thompson // LCOV_EXCL_STOP 3442788fa27SJeremy L Thompson } 3452788fa27SJeremy L Thompson 3462788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextRestoreDataRead(ctx, values)); 3472788fa27SJeremy L Thompson 3482788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 3492788fa27SJeremy L Thompson } 3502788fa27SJeremy L Thompson 3512788fa27SJeremy L Thompson /** 352bfacc300SJeremy L Thompson @brief Set QFunctionContext field holding a double precision value 353bfacc300SJeremy L Thompson 354ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext 3552788fa27SJeremy L Thompson @param[in] field_label Label for field to set 356ea61e9acSJeremy L Thompson @param[in] values Values to set 357bfacc300SJeremy L Thompson 358bfacc300SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 359bfacc300SJeremy L Thompson 3603e1e85abSJeremy L Thompson @ref Backend 361bfacc300SJeremy L Thompson **/ 3622b730f8bSJeremy L Thompson int CeedQFunctionContextSetDouble(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, double *values) { 3632b730f8bSJeremy L Thompson if (!field_label) { 364bfacc300SJeremy L Thompson // LCOV_EXCL_START 3652b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 366bfacc300SJeremy L Thompson // LCOV_EXCL_STOP 3672b730f8bSJeremy L Thompson } 368bfacc300SJeremy L Thompson 3692b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, values)); 370bfacc300SJeremy L Thompson 371bfacc300SJeremy L Thompson return CEED_ERROR_SUCCESS; 372bfacc300SJeremy L Thompson } 373bfacc300SJeremy L Thompson 374bfacc300SJeremy L Thompson /** 3752788fa27SJeremy L Thompson @brief Get QFunctionContext field holding a double precision value, read-only 3762788fa27SJeremy L Thompson 3772788fa27SJeremy L Thompson @param[in] ctx CeedQFunctionContext 3782788fa27SJeremy L Thompson @param[in] field_label Label for field to get 3792788fa27SJeremy L Thompson @param[out] num_values Number of values in the field label 3802788fa27SJeremy L Thompson @param[out] values Pointer to context values 3812788fa27SJeremy L Thompson 3822788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3832788fa27SJeremy L Thompson 3842788fa27SJeremy L Thompson @ref Backend 3852788fa27SJeremy L Thompson **/ 3862788fa27SJeremy L Thompson int CeedQFunctionContextGetDoubleRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, size_t *num_values, const double **values) { 3872788fa27SJeremy L Thompson if (!field_label) { 3882788fa27SJeremy L Thompson // LCOV_EXCL_START 3892788fa27SJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 3902788fa27SJeremy L Thompson // LCOV_EXCL_STOP 3912788fa27SJeremy L Thompson } 3922788fa27SJeremy L Thompson 3932788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values)); 3942788fa27SJeremy L Thompson 3952788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 3962788fa27SJeremy L Thompson } 3972788fa27SJeremy L Thompson 3982788fa27SJeremy L Thompson /** 3992788fa27SJeremy L Thompson @brief Restore QFunctionContext field holding a double precision value, read-only 4002788fa27SJeremy L Thompson 4012788fa27SJeremy L Thompson @param[in] ctx CeedQFunctionContext 4022788fa27SJeremy L Thompson @param[in] field_label Label for field to restore 4032788fa27SJeremy L Thompson @param[out] values Pointer to context values 4042788fa27SJeremy L Thompson 4052788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4062788fa27SJeremy L Thompson 4072788fa27SJeremy L Thompson @ref Backend 4082788fa27SJeremy L Thompson **/ 4092788fa27SJeremy L Thompson int CeedQFunctionContextRestoreDoubleRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, const double **values) { 4102788fa27SJeremy L Thompson if (!field_label) { 4112788fa27SJeremy L Thompson // LCOV_EXCL_START 4122788fa27SJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 4132788fa27SJeremy L Thompson // LCOV_EXCL_STOP 4142788fa27SJeremy L Thompson } 4152788fa27SJeremy L Thompson 4162788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, values)); 4172788fa27SJeremy L Thompson 4182788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 4192788fa27SJeremy L Thompson } 4202788fa27SJeremy L Thompson 4212788fa27SJeremy L Thompson /** 422bfacc300SJeremy L Thompson @brief Set QFunctionContext field holding an int32 value 423bfacc300SJeremy L Thompson 424ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext 4252788fa27SJeremy L Thompson @param[in] field_label Label for field to set 426ea61e9acSJeremy L Thompson @param[in] values Values to set 427bfacc300SJeremy L Thompson 428bfacc300SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 429bfacc300SJeremy L Thompson 4303e1e85abSJeremy L Thompson @ref Backend 431bfacc300SJeremy L Thompson **/ 4322b730f8bSJeremy L Thompson int CeedQFunctionContextSetInt32(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, int *values) { 4332b730f8bSJeremy L Thompson if (!field_label) { 434bfacc300SJeremy L Thompson // LCOV_EXCL_START 4352b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 436bfacc300SJeremy L Thompson // LCOV_EXCL_STOP 4372b730f8bSJeremy L Thompson } 438bfacc300SJeremy L Thompson 4392b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_INT32, values)); 440bfacc300SJeremy L Thompson 441bfacc300SJeremy L Thompson return CEED_ERROR_SUCCESS; 442bfacc300SJeremy L Thompson } 443bfacc300SJeremy L Thompson 444bfacc300SJeremy L Thompson /** 4452788fa27SJeremy L Thompson @brief Get QFunctionContext field holding a int32 value, read-only 4462788fa27SJeremy L Thompson 4472788fa27SJeremy L Thompson @param[in] ctx CeedQFunctionContext 4482788fa27SJeremy L Thompson @param[in] field_label Label for field to get 4492788fa27SJeremy L Thompson @param[out] num_values Number of values in the field label 4502788fa27SJeremy L Thompson @param[out] values Pointer to context values 4512788fa27SJeremy L Thompson 4522788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4532788fa27SJeremy L Thompson 4542788fa27SJeremy L Thompson @ref Backend 4552788fa27SJeremy L Thompson **/ 4562788fa27SJeremy L Thompson int CeedQFunctionContextGetInt32Read(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, size_t *num_values, const int **values) { 4572788fa27SJeremy L Thompson if (!field_label) { 4582788fa27SJeremy L Thompson // LCOV_EXCL_START 4592788fa27SJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 4602788fa27SJeremy L Thompson // LCOV_EXCL_STOP 4612788fa27SJeremy L Thompson } 4622788fa27SJeremy L Thompson 4632788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values)); 4642788fa27SJeremy L Thompson 4652788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 4662788fa27SJeremy L Thompson } 4672788fa27SJeremy L Thompson 4682788fa27SJeremy L Thompson /** 4692788fa27SJeremy L Thompson @brief Restore QFunctionContext field holding a int32 value, read-only 4702788fa27SJeremy L Thompson 4712788fa27SJeremy L Thompson @param[in] ctx CeedQFunctionContext 4722788fa27SJeremy L Thompson @param[in] field_label Label for field to restore 4732788fa27SJeremy L Thompson @param[out] values Pointer to context values 4742788fa27SJeremy L Thompson 4752788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4762788fa27SJeremy L Thompson 4772788fa27SJeremy L Thompson @ref Backend 4782788fa27SJeremy L Thompson **/ 4792788fa27SJeremy L Thompson int CeedQFunctionContextRestoreInt32Read(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, const int **values) { 4802788fa27SJeremy L Thompson if (!field_label) { 4812788fa27SJeremy L Thompson // LCOV_EXCL_START 4822788fa27SJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 4832788fa27SJeremy L Thompson // LCOV_EXCL_STOP 4842788fa27SJeremy L Thompson } 4852788fa27SJeremy L Thompson 4862788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_INT32, values)); 4872788fa27SJeremy L Thompson 4882788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 4892788fa27SJeremy L Thompson } 4902788fa27SJeremy L Thompson 4912788fa27SJeremy L Thompson /** 4922e64a2b9SJeremy L Thompson @brief Get additional destroy routine for CeedQFunctionContext user data 4932e64a2b9SJeremy L Thompson 4942e64a2b9SJeremy L Thompson @param[in] ctx CeedQFunctionContext to get user destroy function 4952e64a2b9SJeremy L Thompson @param[out] f_mem_type Memory type to use when passing data into `f` 4962e64a2b9SJeremy L Thompson @param[out] f Additional routine to use to destroy user data 4972e64a2b9SJeremy L Thompson 4982e64a2b9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4992e64a2b9SJeremy L Thompson 5002e64a2b9SJeremy L Thompson @ref Backend 5012e64a2b9SJeremy L Thompson **/ 5022b730f8bSJeremy L Thompson int CeedQFunctionContextGetDataDestroy(CeedQFunctionContext ctx, CeedMemType *f_mem_type, CeedQFunctionContextDataDestroyUser *f) { 5032e64a2b9SJeremy L Thompson if (f_mem_type) *f_mem_type = ctx->data_destroy_mem_type; 5042e64a2b9SJeremy L Thompson if (f) *f = ctx->data_destroy_function; 5052e64a2b9SJeremy L Thompson return CEED_ERROR_SUCCESS; 5062e64a2b9SJeremy L Thompson } 5072e64a2b9SJeremy L Thompson 5082e64a2b9SJeremy L Thompson /** 50934359f16Sjeremylt @brief Increment the reference counter for a CeedQFunctionContext 51034359f16Sjeremylt 511ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext to increment the reference counter 51234359f16Sjeremylt 51334359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 51434359f16Sjeremylt 51534359f16Sjeremylt @ref Backend 51634359f16Sjeremylt **/ 5179560d06aSjeremylt int CeedQFunctionContextReference(CeedQFunctionContext ctx) { 51834359f16Sjeremylt ctx->ref_count++; 51934359f16Sjeremylt return CEED_ERROR_SUCCESS; 52034359f16Sjeremylt } 52134359f16Sjeremylt 522777ff853SJeremy L Thompson /// @} 523777ff853SJeremy L Thompson 524777ff853SJeremy L Thompson /// ---------------------------------------------------------------------------- 525777ff853SJeremy L Thompson /// CeedQFunctionContext Public API 526777ff853SJeremy L Thompson /// ---------------------------------------------------------------------------- 527777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionUser 528777ff853SJeremy L Thompson /// @{ 529777ff853SJeremy L Thompson 530777ff853SJeremy L Thompson /** 531777ff853SJeremy L Thompson @brief Create a CeedQFunctionContext for storing CeedQFunction user context data 532777ff853SJeremy L Thompson 533ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedQFunctionContext will be created 534ea61e9acSJeremy L Thompson @param[out] ctx Address of the variable where the newly created CeedQFunctionContext will be stored 535777ff853SJeremy L Thompson 536777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 537777ff853SJeremy L Thompson 538777ff853SJeremy L Thompson @ref User 539777ff853SJeremy L Thompson **/ 540777ff853SJeremy L Thompson int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) { 541777ff853SJeremy L Thompson if (!ceed->QFunctionContextCreate) { 542777ff853SJeremy L Thompson Ceed delegate; 5432b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Context")); 544777ff853SJeremy L Thompson 5452b730f8bSJeremy L Thompson if (!delegate) { 546777ff853SJeremy L Thompson // LCOV_EXCL_START 5472b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support ContextCreate"); 548777ff853SJeremy L Thompson // LCOV_EXCL_STOP 5492b730f8bSJeremy L Thompson } 550777ff853SJeremy L Thompson 5512b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(delegate, ctx)); 552e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 553777ff853SJeremy L Thompson } 554777ff853SJeremy L Thompson 5552b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, ctx)); 556777ff853SJeremy L Thompson (*ctx)->ceed = ceed; 5572b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 558d1d35e2fSjeremylt (*ctx)->ref_count = 1; 5592b730f8bSJeremy L Thompson CeedCall(ceed->QFunctionContextCreate(*ctx)); 560e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 561777ff853SJeremy L Thompson } 562777ff853SJeremy L Thompson 563777ff853SJeremy L Thompson /** 564ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedQFunctionContext. 565ea61e9acSJeremy L Thompson Both pointers should be destroyed with `CeedQFunctionContextDestroy()`. 566512bb800SJeremy L Thompson 567512bb800SJeremy 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 568512bb800SJeremy L Thompson CeedQFunctionContext. This CeedQFunctionContext will be destroyed if `ctx_copy` is the only reference to this CeedQFunctionContext. 5699560d06aSjeremylt 570ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext to copy reference to 571ea61e9acSJeremy L Thompson @param[in,out] ctx_copy Variable to store copied reference 5729560d06aSjeremylt 5739560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 5749560d06aSjeremylt 5759560d06aSjeremylt @ref User 5769560d06aSjeremylt **/ 5772b730f8bSJeremy L Thompson int CeedQFunctionContextReferenceCopy(CeedQFunctionContext ctx, CeedQFunctionContext *ctx_copy) { 5782b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextReference(ctx)); 5792b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(ctx_copy)); 5809560d06aSjeremylt *ctx_copy = ctx; 5819560d06aSjeremylt return CEED_ERROR_SUCCESS; 5829560d06aSjeremylt } 5839560d06aSjeremylt 5849560d06aSjeremylt /** 585ea61e9acSJeremy L Thompson @brief Set the data used by a CeedQFunctionContext, freeing any previously allocated data if applicable. 586ea61e9acSJeremy L Thompson The backend may copy values to a different memtype, such as during @ref CeedQFunctionApply(). 587777ff853SJeremy L Thompson See also @ref CeedQFunctionContextTakeData(). 588777ff853SJeremy L Thompson 589ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext 590ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the data being passed 591ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the data 592ea61e9acSJeremy L Thompson @param[in] size Size of data, in bytes 593ea61e9acSJeremy L Thompson @param[in] data Data to be used 594777ff853SJeremy L Thompson 595777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 596777ff853SJeremy L Thompson 597777ff853SJeremy L Thompson @ref User 598777ff853SJeremy L Thompson **/ 5992b730f8bSJeremy L Thompson int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, size_t size, void *data) { 6002b730f8bSJeremy L Thompson if (!ctx->SetData) { 601777ff853SJeremy L Thompson // LCOV_EXCL_START 6022b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support ContextSetData"); 603777ff853SJeremy L Thompson // LCOV_EXCL_STOP 6042b730f8bSJeremy L Thompson } 6052b730f8bSJeremy L Thompson if (ctx->state % 2 == 1) { 606777ff853SJeremy L Thompson // LCOV_EXCL_START 6072b730f8bSJeremy L Thompson return CeedError(ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); 608777ff853SJeremy L Thompson // LCOV_EXCL_STOP 6092b730f8bSJeremy L Thompson } 610777ff853SJeremy L Thompson 6112b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroyData(ctx)); 612d1d35e2fSjeremylt ctx->ctx_size = size; 6132b730f8bSJeremy L Thompson CeedCall(ctx->SetData(ctx, mem_type, copy_mode, data)); 614777ff853SJeremy L Thompson ctx->state += 2; 615e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 616777ff853SJeremy L Thompson } 617777ff853SJeremy L Thompson 618777ff853SJeremy L Thompson /** 619891038deSjeremylt @brief Take ownership of the data in a CeedQFunctionContext via the specified memory type. 620891038deSjeremylt The caller is responsible for managing and freeing the memory. 621891038deSjeremylt 622ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext to access 623ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the data. 624ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 625891038deSjeremylt @param[out] data Data on memory type mem_type 626891038deSjeremylt 627891038deSjeremylt @return An error code: 0 - success, otherwise - failure 628891038deSjeremylt 629891038deSjeremylt @ref User 630891038deSjeremylt **/ 6312b730f8bSJeremy L Thompson int CeedQFunctionContextTakeData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 6329c774eddSJeremy L Thompson bool has_valid_data = true; 6332b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data)); 6342b730f8bSJeremy L Thompson if (!has_valid_data) { 6359c774eddSJeremy L Thompson // LCOV_EXCL_START 6362b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to take, must set data"); 6379c774eddSJeremy L Thompson // LCOV_EXCL_STOP 6382b730f8bSJeremy L Thompson } 6399c774eddSJeremy L Thompson 6402b730f8bSJeremy L Thompson if (!ctx->TakeData) { 641891038deSjeremylt // LCOV_EXCL_START 6422b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support TakeData"); 643891038deSjeremylt // LCOV_EXCL_STOP 6442b730f8bSJeremy L Thompson } 6452b730f8bSJeremy L Thompson if (ctx->state % 2 == 1) { 646891038deSjeremylt // LCOV_EXCL_START 6472b730f8bSJeremy L Thompson return CeedError(ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); 648891038deSjeremylt // LCOV_EXCL_STOP 6492b730f8bSJeremy L Thompson } 650891038deSjeremylt 6519c774eddSJeremy L Thompson bool has_borrowed_data_of_type = true; 6522b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextHasBorrowedDataOfType(ctx, mem_type, &has_borrowed_data_of_type)); 6532b730f8bSJeremy L Thompson if (!has_borrowed_data_of_type) { 6549c774eddSJeremy L Thompson // LCOV_EXCL_START 6556ed4cbd1SJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no borrowed %s data, must set data with CeedQFunctionContextSetData", 6569c774eddSJeremy L Thompson CeedMemTypes[mem_type]); 6579c774eddSJeremy L Thompson // LCOV_EXCL_STOP 6582b730f8bSJeremy L Thompson } 6599c774eddSJeremy L Thompson 660891038deSjeremylt void *temp_data = NULL; 6612b730f8bSJeremy L Thompson CeedCall(ctx->TakeData(ctx, mem_type, &temp_data)); 662891038deSjeremylt if (data) (*(void **)data) = temp_data; 663891038deSjeremylt return CEED_ERROR_SUCCESS; 664891038deSjeremylt } 665891038deSjeremylt 666891038deSjeremylt /** 667777ff853SJeremy L Thompson @brief Get read/write access to a CeedQFunctionContext via the specified memory type. 668777ff853SJeremy L Thompson Restore access with @ref CeedQFunctionContextRestoreData(). 669777ff853SJeremy L Thompson 670ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext to access 671ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the data. 672ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 673d1d35e2fSjeremylt @param[out] data Data on memory type mem_type 674777ff853SJeremy L Thompson 675ea61e9acSJeremy L Thompson @note The CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions provide access to array pointers in the desired memory 676ea61e9acSJeremy L Thompson space. Pairing get/restore allows the Context to track access. 677777ff853SJeremy L Thompson 678777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 679777ff853SJeremy L Thompson 680777ff853SJeremy L Thompson @ref User 681777ff853SJeremy L Thompson **/ 6822b730f8bSJeremy L Thompson int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 6832b730f8bSJeremy L Thompson if (!ctx->GetData) { 684777ff853SJeremy L Thompson // LCOV_EXCL_START 6852b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetData"); 686777ff853SJeremy L Thompson // LCOV_EXCL_STOP 6872b730f8bSJeremy L Thompson } 6882b730f8bSJeremy L Thompson if (ctx->state % 2 == 1) { 689777ff853SJeremy L Thompson // LCOV_EXCL_START 6902b730f8bSJeremy L Thompson return CeedError(ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); 691777ff853SJeremy L Thompson // LCOV_EXCL_STOP 6922b730f8bSJeremy L Thompson } 6932b730f8bSJeremy L Thompson if (ctx->num_readers > 0) { 69428bfd0b7SJeremy L Thompson // LCOV_EXCL_START 6952b730f8bSJeremy L Thompson return CeedError(ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, a process has read access"); 69628bfd0b7SJeremy L Thompson // LCOV_EXCL_STOP 6972b730f8bSJeremy L Thompson } 69828bfd0b7SJeremy L Thompson 6999c774eddSJeremy L Thompson bool has_valid_data = true; 7002b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data)); 7012b730f8bSJeremy L Thompson if (!has_valid_data) { 7029c774eddSJeremy L Thompson // LCOV_EXCL_START 7032b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data"); 7049c774eddSJeremy L Thompson // LCOV_EXCL_STOP 7052b730f8bSJeremy L Thompson } 7069c774eddSJeremy L Thompson 7072b730f8bSJeremy L Thompson CeedCall(ctx->GetData(ctx, mem_type, data)); 70828bfd0b7SJeremy L Thompson ctx->state++; 70928bfd0b7SJeremy L Thompson return CEED_ERROR_SUCCESS; 71028bfd0b7SJeremy L Thompson } 71128bfd0b7SJeremy L Thompson 71228bfd0b7SJeremy L Thompson /** 71328bfd0b7SJeremy L Thompson @brief Get read only access to a CeedQFunctionContext via the specified memory type. 71428bfd0b7SJeremy L Thompson Restore access with @ref CeedQFunctionContextRestoreData(). 71528bfd0b7SJeremy L Thompson 716ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext to access 717ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the data. 718ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 71928bfd0b7SJeremy L Thompson @param[out] data Data on memory type mem_type 72028bfd0b7SJeremy L Thompson 721ea61e9acSJeremy L Thompson @note The CeedQFunctionContextGetDataRead() and @ref CeedQFunctionContextRestoreDataRead() functions provide access to array pointers in the desired 722ea61e9acSJeremy L Thompson memory space. Pairing get/restore allows the Context to track access. 72328bfd0b7SJeremy L Thompson 72428bfd0b7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 72528bfd0b7SJeremy L Thompson 72628bfd0b7SJeremy L Thompson @ref User 72728bfd0b7SJeremy L Thompson **/ 7282b730f8bSJeremy L Thompson int CeedQFunctionContextGetDataRead(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 7292b730f8bSJeremy L Thompson if (!ctx->GetDataRead) { 73028bfd0b7SJeremy L Thompson // LCOV_EXCL_START 7312b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetDataRead"); 73228bfd0b7SJeremy L Thompson // LCOV_EXCL_STOP 7332b730f8bSJeremy L Thompson } 7342b730f8bSJeremy L Thompson if (ctx->state % 2 == 1) { 73528bfd0b7SJeremy L Thompson // LCOV_EXCL_START 7362b730f8bSJeremy L Thompson return CeedError(ctx->ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); 73728bfd0b7SJeremy L Thompson // LCOV_EXCL_STOP 7382b730f8bSJeremy L Thompson } 73928bfd0b7SJeremy L Thompson 74028bfd0b7SJeremy L Thompson bool has_valid_data = true; 7412b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data)); 7422b730f8bSJeremy L Thompson if (!has_valid_data) { 74328bfd0b7SJeremy L Thompson // LCOV_EXCL_START 7442b730f8bSJeremy L Thompson return CeedError(ctx->ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data"); 74528bfd0b7SJeremy L Thompson // LCOV_EXCL_STOP 7462b730f8bSJeremy L Thompson } 74728bfd0b7SJeremy L Thompson 7482b730f8bSJeremy L Thompson CeedCall(ctx->GetDataRead(ctx, mem_type, data)); 74928bfd0b7SJeremy L Thompson ctx->num_readers++; 750e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 751777ff853SJeremy L Thompson } 752777ff853SJeremy L Thompson 753777ff853SJeremy L Thompson /** 754777ff853SJeremy L Thompson @brief Restore data obtained using @ref CeedQFunctionContextGetData() 755777ff853SJeremy L Thompson 756ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext to restore 757ea61e9acSJeremy L Thompson @param[in,out] data Data to restore 758777ff853SJeremy L Thompson 759777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 760777ff853SJeremy L Thompson 761777ff853SJeremy L Thompson @ref User 762777ff853SJeremy L Thompson **/ 763777ff853SJeremy L Thompson int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) { 7642b730f8bSJeremy L Thompson if (ctx->state % 2 != 1) { 765777ff853SJeremy L Thompson // LCOV_EXCL_START 7662b730f8bSJeremy L Thompson return CeedError(ctx->ceed, 1, "Cannot restore CeedQFunctionContext array access, access was not granted"); 767777ff853SJeremy L Thompson // LCOV_EXCL_STOP 7682b730f8bSJeremy L Thompson } 769777ff853SJeremy L Thompson 770706efda3SJeremy L Thompson if (ctx->RestoreData) { 7712b730f8bSJeremy L Thompson CeedCall(ctx->RestoreData(ctx)); 772706efda3SJeremy L Thompson } 773777ff853SJeremy L Thompson *(void **)data = NULL; 77428bfd0b7SJeremy L Thompson ctx->state++; 77528bfd0b7SJeremy L Thompson return CEED_ERROR_SUCCESS; 77628bfd0b7SJeremy L Thompson } 77728bfd0b7SJeremy L Thompson 77828bfd0b7SJeremy L Thompson /** 77928bfd0b7SJeremy L Thompson @brief Restore data obtained using @ref CeedQFunctionContextGetDataRead() 78028bfd0b7SJeremy L Thompson 781ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext to restore 782ea61e9acSJeremy L Thompson @param[in,out] data Data to restore 78328bfd0b7SJeremy L Thompson 78428bfd0b7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 78528bfd0b7SJeremy L Thompson 78628bfd0b7SJeremy L Thompson @ref User 78728bfd0b7SJeremy L Thompson **/ 78828bfd0b7SJeremy L Thompson int CeedQFunctionContextRestoreDataRead(CeedQFunctionContext ctx, void *data) { 7892b730f8bSJeremy L Thompson if (ctx->num_readers == 0) { 79028bfd0b7SJeremy L Thompson // LCOV_EXCL_START 7912b730f8bSJeremy L Thompson return CeedError(ctx->ceed, 1, "Cannot restore CeedQFunctionContext array access, access was not granted"); 79228bfd0b7SJeremy L Thompson // LCOV_EXCL_STOP 7932b730f8bSJeremy L Thompson } 79428bfd0b7SJeremy L Thompson 79575a19770SJeremy L Thompson ctx->num_readers--; 79675a19770SJeremy L Thompson if (ctx->num_readers == 0 && ctx->RestoreDataRead) { 7972b730f8bSJeremy L Thompson CeedCall(ctx->RestoreDataRead(ctx)); 79828bfd0b7SJeremy L Thompson } 79928bfd0b7SJeremy L Thompson *(void **)data = NULL; 80075a19770SJeremy L Thompson 801e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 802777ff853SJeremy L Thompson } 803777ff853SJeremy L Thompson 804777ff853SJeremy L Thompson /** 805cdf32b93SJeremy L Thompson @brief Register QFunctionContext a field holding a double precision value 806cdf32b93SJeremy L Thompson 807ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext 808ea61e9acSJeremy L Thompson @param[in] field_name Name of field to register 809ea61e9acSJeremy L Thompson @param[in] field_offset Offset of field to register 810ea61e9acSJeremy L Thompson @param[in] num_values Number of values to register, must be contiguous in memory 811ea61e9acSJeremy L Thompson @param[in] field_description Description of field, or NULL for none 812cdf32b93SJeremy L Thompson 813cdf32b93SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 814cdf32b93SJeremy L Thompson 815cdf32b93SJeremy L Thompson @ref User 816cdf32b93SJeremy L Thompson **/ 8172b730f8bSJeremy L Thompson int CeedQFunctionContextRegisterDouble(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values, 818cdf32b93SJeremy L Thompson const char *field_description) { 8192b730f8bSJeremy L Thompson return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_DOUBLE, sizeof(double), num_values); 820cdf32b93SJeremy L Thompson } 821cdf32b93SJeremy L Thompson 822cdf32b93SJeremy L Thompson /** 823cdf32b93SJeremy L Thompson @brief Register QFunctionContext a field holding a int32 value 824cdf32b93SJeremy L Thompson 825ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext 826ea61e9acSJeremy L Thompson @param[in] field_name Name of field to register 827ea61e9acSJeremy L Thompson @param[in] field_offset Offset of field to register 828ea61e9acSJeremy L Thompson @param[in] num_values Number of values to register, must be contiguous in memory 829ea61e9acSJeremy L Thompson @param[in] field_description Description of field, or NULL for none 830cdf32b93SJeremy L Thompson 831cdf32b93SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 832cdf32b93SJeremy L Thompson 833cdf32b93SJeremy L Thompson @ref User 834cdf32b93SJeremy L Thompson **/ 8352b730f8bSJeremy L Thompson int CeedQFunctionContextRegisterInt32(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values, 836cdf32b93SJeremy L Thompson const char *field_description) { 8372b730f8bSJeremy L Thompson return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_INT32, sizeof(int), num_values); 838cdf32b93SJeremy L Thompson } 839cdf32b93SJeremy L Thompson 840cdf32b93SJeremy L Thompson /** 8413668ca4bSJeremy L Thompson @brief Get labels for all registered QFunctionContext fields 842cdf32b93SJeremy L Thompson 843ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext 8443668ca4bSJeremy L Thompson @param[out] field_labels Variable to hold array of field labels 845cdf32b93SJeremy L Thompson @param[out] num_fields Length of field descriptions array 846cdf32b93SJeremy L Thompson 847cdf32b93SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 848cdf32b93SJeremy L Thompson 849cdf32b93SJeremy L Thompson @ref User 850cdf32b93SJeremy L Thompson **/ 8512b730f8bSJeremy L Thompson int CeedQFunctionContextGetAllFieldLabels(CeedQFunctionContext ctx, const CeedContextFieldLabel **field_labels, CeedInt *num_fields) { 8523668ca4bSJeremy L Thompson *field_labels = ctx->field_labels; 853cdf32b93SJeremy L Thompson *num_fields = ctx->num_fields; 854cdf32b93SJeremy L Thompson return CEED_ERROR_SUCCESS; 855cdf32b93SJeremy L Thompson } 856cdf32b93SJeremy L Thompson 857cdf32b93SJeremy L Thompson /** 8580f86cbe7SJeremy L Thompson @brief Get the descriptive information about a CeedContextFieldLabel 8590f86cbe7SJeremy L Thompson 8600f86cbe7SJeremy L Thompson @param[in] label CeedContextFieldLabel 8610f86cbe7SJeremy L Thompson @param[out] field_name Name of labeled field 8620f86cbe7SJeremy L Thompson @param[out] field_description Description of field, or NULL for none 8637bfe0f0eSJeremy L Thompson @param[out] num_values Number of values registered 8640f86cbe7SJeremy L Thompson @param[out] field_type CeedContextFieldType 8650f86cbe7SJeremy L Thompson 8660f86cbe7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 8670f86cbe7SJeremy L Thompson 8680f86cbe7SJeremy L Thompson @ref User 8690f86cbe7SJeremy L Thompson **/ 8702b730f8bSJeremy L Thompson int CeedContextFieldLabelGetDescription(CeedContextFieldLabel label, const char **field_name, const char **field_description, size_t *num_values, 8710f86cbe7SJeremy L Thompson CeedContextFieldType *field_type) { 8720f86cbe7SJeremy L Thompson if (field_name) *field_name = label->name; 8730f86cbe7SJeremy L Thompson if (field_description) *field_description = label->description; 8747bfe0f0eSJeremy L Thompson if (num_values) *num_values = label->num_values; 8750f86cbe7SJeremy L Thompson if (field_type) *field_type = label->type; 8760f86cbe7SJeremy L Thompson return CEED_ERROR_SUCCESS; 8770f86cbe7SJeremy L Thompson } 8780f86cbe7SJeremy L Thompson 8790f86cbe7SJeremy L Thompson /** 88080a9ef05SNatalie Beams @brief Get data size for a Context 88180a9ef05SNatalie Beams 882ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext 88380a9ef05SNatalie Beams @param[out] ctx_size Variable to store size of context data values 88480a9ef05SNatalie Beams 88580a9ef05SNatalie Beams @return An error code: 0 - success, otherwise - failure 88680a9ef05SNatalie Beams 88780a9ef05SNatalie Beams @ref User 88880a9ef05SNatalie Beams **/ 8892b730f8bSJeremy L Thompson int CeedQFunctionContextGetContextSize(CeedQFunctionContext ctx, size_t *ctx_size) { 89080a9ef05SNatalie Beams *ctx_size = ctx->ctx_size; 89180a9ef05SNatalie Beams return CEED_ERROR_SUCCESS; 89280a9ef05SNatalie Beams } 89380a9ef05SNatalie Beams 89480a9ef05SNatalie Beams /** 895777ff853SJeremy L Thompson @brief View a CeedQFunctionContext 896777ff853SJeremy L Thompson 897777ff853SJeremy L Thompson @param[in] ctx CeedQFunctionContext to view 898777ff853SJeremy L Thompson @param[in] stream Filestream to write to 899777ff853SJeremy L Thompson 900777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 901777ff853SJeremy L Thompson 902777ff853SJeremy L Thompson @ref User 903777ff853SJeremy L Thompson **/ 904777ff853SJeremy L Thompson int CeedQFunctionContextView(CeedQFunctionContext ctx, FILE *stream) { 905777ff853SJeremy L Thompson fprintf(stream, "CeedQFunctionContext\n"); 906d1d35e2fSjeremylt fprintf(stream, " Context Data Size: %ld\n", ctx->ctx_size); 9073668ca4bSJeremy L Thompson for (CeedInt i = 0; i < ctx->num_fields; i++) { 9083668ca4bSJeremy L Thompson // LCOV_EXCL_START 9092b730f8bSJeremy L Thompson fprintf(stream, " Labeled %s field: %s\n", CeedContextFieldTypes[ctx->field_labels[i]->type], ctx->field_labels[i]->name); 9103668ca4bSJeremy L Thompson // LCOV_EXCL_STOP 9113668ca4bSJeremy L Thompson } 912e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 913777ff853SJeremy L Thompson } 914777ff853SJeremy L Thompson 915777ff853SJeremy L Thompson /** 9162790b72bSJeremy L Thompson @brief Set additional destroy routine for CeedQFunctionContext user data 9172790b72bSJeremy L Thompson 918ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext to set user destroy function 9192e64a2b9SJeremy L Thompson @param[in] f_mem_type Memory type to use when passing data into `f` 9202e64a2b9SJeremy L Thompson @param[in] f Additional routine to use to destroy user data 9212790b72bSJeremy L Thompson 9222790b72bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 9232790b72bSJeremy L Thompson 9242790b72bSJeremy L Thompson @ref User 9252790b72bSJeremy L Thompson **/ 9262b730f8bSJeremy L Thompson int CeedQFunctionContextSetDataDestroy(CeedQFunctionContext ctx, CeedMemType f_mem_type, CeedQFunctionContextDataDestroyUser f) { 9272b730f8bSJeremy L Thompson if (!f) { 9282790b72bSJeremy L Thompson // LCOV_EXCL_START 9292b730f8bSJeremy L Thompson return CeedError(ctx->ceed, 1, "Must provide valid callback function for destroying user data"); 9302790b72bSJeremy L Thompson // LCOV_EXCL_STOP 9312b730f8bSJeremy L Thompson } 9322790b72bSJeremy L Thompson ctx->data_destroy_mem_type = f_mem_type; 9332790b72bSJeremy L Thompson ctx->data_destroy_function = f; 9342790b72bSJeremy L Thompson return CEED_ERROR_SUCCESS; 9352790b72bSJeremy L Thompson } 9362790b72bSJeremy L Thompson 9372790b72bSJeremy L Thompson /** 938777ff853SJeremy L Thompson @brief Destroy a CeedQFunctionContext 939777ff853SJeremy L Thompson 940ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext to destroy 941777ff853SJeremy L Thompson 942777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 943777ff853SJeremy L Thompson 944777ff853SJeremy L Thompson @ref User 945777ff853SJeremy L Thompson **/ 946777ff853SJeremy L Thompson int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx) { 947ad6481ceSJeremy L Thompson if (!*ctx || --(*ctx)->ref_count > 0) { 948ad6481ceSJeremy L Thompson *ctx = NULL; 949ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 950ad6481ceSJeremy L Thompson } 9512b730f8bSJeremy L Thompson if ((*ctx) && ((*ctx)->state % 2) == 1) { 952777ff853SJeremy L Thompson // LCOV_EXCL_START 9532b730f8bSJeremy L Thompson return CeedError((*ctx)->ceed, 1, "Cannot destroy CeedQFunctionContext, the access lock is in use"); 954777ff853SJeremy L Thompson // LCOV_EXCL_STOP 9552b730f8bSJeremy L Thompson } 956777ff853SJeremy L Thompson 9572b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroyData(*ctx)); 9582b730f8bSJeremy L Thompson if ((*ctx)->Destroy) CeedCall((*ctx)->Destroy(*ctx)); 959cdf32b93SJeremy L Thompson for (CeedInt i = 0; i < (*ctx)->num_fields; i++) { 9602b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ctx)->field_labels[i]->name)); 9612b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ctx)->field_labels[i]->description)); 9622b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ctx)->field_labels[i])); 963cdf32b93SJeremy L Thompson } 9642b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ctx)->field_labels)); 9652b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*ctx)->ceed)); 9662b730f8bSJeremy L Thompson CeedCall(CeedFree(ctx)); 967cdf32b93SJeremy L Thompson 968e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 969777ff853SJeremy L Thompson } 970777ff853SJeremy L Thompson 971777ff853SJeremy L Thompson /// @} 972