15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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. 30436c2adSjeremylt // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 50436c2adSjeremylt // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 70436c2adSjeremylt 82b730f8bSJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 11c85e8640SSebastian Grimberg #include <assert.h> 12547d9b97Sjeremylt #include <math.h> 1349aac155SJeremy L Thompson #include <stdbool.h> 143d576824SJeremy L Thompson #include <stdint.h> 153d576824SJeremy L Thompson #include <stdio.h> 160436c2adSjeremylt 177a982d89SJeremy L. Thompson /// @file 187a982d89SJeremy L. Thompson /// Implementation of public CeedVector interfaces 197a982d89SJeremy L. Thompson 200436c2adSjeremylt /// @cond DOXYGEN_SKIP 210436c2adSjeremylt static struct CeedVector_private ceed_vector_active; 220436c2adSjeremylt static struct CeedVector_private ceed_vector_none; 230436c2adSjeremylt /// @endcond 240436c2adSjeremylt 257a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 267a982d89SJeremy L. Thompson /// @{ 277a982d89SJeremy L. Thompson 28ca94c3ddSJeremy L Thompson /// Indicate that vector will be provided as an explicit argument to @ref CeedOperatorApply(). 297a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_ACTIVE = &ceed_vector_active; 307a982d89SJeremy L. Thompson 3196b902e2Sjeremylt /// Indicate that no vector is applicable (i.e., for @ref CEED_EVAL_WEIGHT). 327a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_NONE = &ceed_vector_none; 337a982d89SJeremy L. Thompson 347a982d89SJeremy L. Thompson /// @} 357a982d89SJeremy L. Thompson 367a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 377a982d89SJeremy L. Thompson /// CeedVector Backend API 387a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 397a982d89SJeremy L. Thompson /// @addtogroup CeedVectorBackend 407a982d89SJeremy L. Thompson /// @{ 417a982d89SJeremy L. Thompson 427a982d89SJeremy L. Thompson /** 43ca94c3ddSJeremy L Thompson @brief Check for valid data in a `CeedVector` 449c774eddSJeremy L Thompson 45ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to check validity 469c774eddSJeremy L Thompson @param[out] has_valid_array Variable to store validity 479c774eddSJeremy L Thompson 489c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 499c774eddSJeremy L Thompson 509c774eddSJeremy L Thompson @ref Backend 519c774eddSJeremy L Thompson **/ 529c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) { 531203703bSJeremy L Thompson CeedSize length; 541203703bSJeremy L Thompson 556e536b99SJeremy L Thompson CeedCheck(vec->HasValidArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorHasValidArray"); 561203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 571203703bSJeremy L Thompson if (length == 0) { 58b0976d5aSZach Atkins *has_valid_array = true; 59b0976d5aSZach Atkins return CEED_ERROR_SUCCESS; 60b0976d5aSZach Atkins } 612b730f8bSJeremy L Thompson CeedCall(vec->HasValidArray(vec, has_valid_array)); 629c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 639c774eddSJeremy L Thompson } 649c774eddSJeremy L Thompson 659c774eddSJeremy L Thompson /** 66ca94c3ddSJeremy L Thompson @brief Check for borrowed array of a specific @ref CeedMemType in a `CeedVector` 679c774eddSJeremy L Thompson 68ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to check 69ea61e9acSJeremy L Thompson @param[in] mem_type Memory type to check 709c774eddSJeremy L Thompson @param[out] has_borrowed_array_of_type Variable to store result 719c774eddSJeremy L Thompson 729c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 739c774eddSJeremy L Thompson 749c774eddSJeremy L Thompson @ref Backend 759c774eddSJeremy L Thompson **/ 762b730f8bSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 776e536b99SJeremy L Thompson CeedCheck(vec->HasBorrowedArrayOfType, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, 786e536b99SJeremy L Thompson "Backend does not support CeedVectorHasBorrowedArrayOfType"); 792b730f8bSJeremy L Thompson CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type)); 809c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 819c774eddSJeremy L Thompson } 829c774eddSJeremy L Thompson 839c774eddSJeremy L Thompson /** 84ca94c3ddSJeremy L Thompson @brief Get the state of a `CeedVector` 857a982d89SJeremy L. Thompson 86ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 877a982d89SJeremy L. Thompson @param[out] state Variable to store state 887a982d89SJeremy L. Thompson 897a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 907a982d89SJeremy L. Thompson 917a982d89SJeremy L. Thompson @ref Backend 927a982d89SJeremy L. Thompson **/ 937a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) { 947a982d89SJeremy L. Thompson *state = vec->state; 95e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 967a982d89SJeremy L. Thompson } 977a982d89SJeremy L. Thompson 987a982d89SJeremy L. Thompson /** 99ca94c3ddSJeremy L Thompson @brief Get the backend data of a `CeedVector` 1007a982d89SJeremy L. Thompson 101ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 1027a982d89SJeremy L. Thompson @param[out] data Variable to store data 1037a982d89SJeremy L. Thompson 1047a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1057a982d89SJeremy L. Thompson 1067a982d89SJeremy L. Thompson @ref Backend 1077a982d89SJeremy L. Thompson **/ 108777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) { 109777ff853SJeremy L Thompson *(void **)data = vec->data; 110e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1117a982d89SJeremy L. Thompson } 1127a982d89SJeremy L. Thompson 1137a982d89SJeremy L. Thompson /** 114ca94c3ddSJeremy L Thompson @brief Set the backend data of a `CeedVector` 1157a982d89SJeremy L. Thompson 116ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to retrieve state 117ea61e9acSJeremy L Thompson @param[in] data Data to set 1187a982d89SJeremy L. Thompson 1197a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1207a982d89SJeremy L. Thompson 1217a982d89SJeremy L. Thompson @ref Backend 1227a982d89SJeremy L. Thompson **/ 123777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) { 124777ff853SJeremy L Thompson vec->data = data; 125e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1267a982d89SJeremy L. Thompson } 1277a982d89SJeremy L. Thompson 12834359f16Sjeremylt /** 129ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedVector` 13034359f16Sjeremylt 131ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to increment the reference counter 13234359f16Sjeremylt 13334359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 13434359f16Sjeremylt 13534359f16Sjeremylt @ref Backend 13634359f16Sjeremylt **/ 1379560d06aSjeremylt int CeedVectorReference(CeedVector vec) { 13834359f16Sjeremylt vec->ref_count++; 13934359f16Sjeremylt return CEED_ERROR_SUCCESS; 14034359f16Sjeremylt } 14134359f16Sjeremylt 1427a982d89SJeremy L. Thompson /// @} 1437a982d89SJeremy L. Thompson 1447a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1457a982d89SJeremy L. Thompson /// CeedVector Public API 1467a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1477a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 1480436c2adSjeremylt /// @{ 1490436c2adSjeremylt 1500436c2adSjeremylt /** 151ca94c3ddSJeremy L Thompson @brief Create a `CeedVector` of the specified length (does not allocate memory) 1520436c2adSjeremylt 153ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedVector` 154ea61e9acSJeremy L Thompson @param[in] length Length of vector 155ca94c3ddSJeremy L Thompson @param[out] vec Address of the variable where the newly created `CeedVector` will be stored 1560436c2adSjeremylt 1570436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 1580436c2adSjeremylt 1597a982d89SJeremy L. Thompson @ref User 1600436c2adSjeremylt **/ 1611f9221feSJeremy L Thompson int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) { 1620436c2adSjeremylt if (!ceed->VectorCreate) { 1630436c2adSjeremylt Ceed delegate; 1646574a04fSJeremy L Thompson 1652b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector")); 1661ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement VectorCreate"); 1672b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(delegate, length, vec)); 168e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1690436c2adSjeremylt } 1700436c2adSjeremylt 1712b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, vec)); 172db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*vec)->ceed)); 173d1d35e2fSjeremylt (*vec)->ref_count = 1; 1740436c2adSjeremylt (*vec)->length = length; 1750436c2adSjeremylt (*vec)->state = 0; 1762b730f8bSJeremy L Thompson CeedCall(ceed->VectorCreate(length, *vec)); 177e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1780436c2adSjeremylt } 1790436c2adSjeremylt 1800436c2adSjeremylt /** 181ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedVector`. 1824385fb7fSSebastian Grimberg 183ca94c3ddSJeremy L Thompson Both pointers should be destroyed with @ref CeedVectorDestroy(). 184512bb800SJeremy L Thompson 185ca94c3ddSJeremy L Thompson Note: If the value of `*vec_copy` passed to this function is non-`NULL`, then it is assumed that `*vec_copy` is a pointer to a `CeedVector`. 186ca94c3ddSJeremy L Thompson This `CeedVector` will be destroyed if `*vec_copy` is the only reference to this `CeedVector`. 1879560d06aSjeremylt 188ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to copy reference to 189ea61e9acSJeremy L Thompson @param[in,out] vec_copy Variable to store copied reference 1909560d06aSjeremylt 1919560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 1929560d06aSjeremylt 1939560d06aSjeremylt @ref User 1949560d06aSjeremylt **/ 1959560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) { 196393ac2cdSJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorReference(vec)); 1972b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(vec_copy)); 1989560d06aSjeremylt *vec_copy = vec; 1999560d06aSjeremylt return CEED_ERROR_SUCCESS; 2009560d06aSjeremylt } 2019560d06aSjeremylt 2029560d06aSjeremylt /** 203ca94c3ddSJeremy L Thompson @brief Copy a `CeedVector` into a different `CeedVector`. 2044385fb7fSSebastian Grimberg 205ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to copy 206*e706ae07SJeremy L Thompson @param[in,out] vec_copy `CeedVector` to copy array into 2075fb68f37SKaren (Ren) Stengel 2085fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 2095fb68f37SKaren (Ren) Stengel 2105fb68f37SKaren (Ren) Stengel @ref User 2115fb68f37SKaren (Ren) Stengel **/ 2125fb68f37SKaren (Ren) Stengel int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) { 2135fb68f37SKaren (Ren) Stengel Ceed ceed; 2145fb68f37SKaren (Ren) Stengel CeedMemType mem_type, mem_type_copy; 2155fb68f37SKaren (Ren) Stengel CeedScalar *array; 2165fb68f37SKaren (Ren) Stengel 2175fb68f37SKaren (Ren) Stengel // Get the preferred memory type 218d77c2f5dSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 219d77c2f5dSJeremy L Thompson CeedCall(CeedGetPreferredMemType(ceed, &mem_type)); 2205fb68f37SKaren (Ren) Stengel 2215fb68f37SKaren (Ren) Stengel // Get the preferred memory type 222d77c2f5dSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec_copy, &ceed)); 223d77c2f5dSJeremy L Thompson CeedCall(CeedGetPreferredMemType(ceed, &mem_type_copy)); 2245fb68f37SKaren (Ren) Stengel 2255fb68f37SKaren (Ren) Stengel // Check that both have same memory type 2265fb68f37SKaren (Ren) Stengel if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST; 2275fb68f37SKaren (Ren) Stengel 228*e706ae07SJeremy L Thompson // Check compatible lengths 229*e706ae07SJeremy L Thompson { 230*e706ae07SJeremy L Thompson CeedSize length_vec, length_copy; 231*e706ae07SJeremy L Thompson 232*e706ae07SJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length_vec)); 233*e706ae07SJeremy L Thompson CeedCall(CeedVectorGetLength(vec_copy, &length_copy)); 234*e706ae07SJeremy L Thompson CeedCheck(length_vec == length_copy, ceed, CEED_ERROR_INCOMPATIBLE, "CeedVectors must have the same length to copy"); 235*e706ae07SJeremy L Thompson } 236*e706ae07SJeremy L Thompson 2375fb68f37SKaren (Ren) Stengel // Copy the values from vec to vec_copy 2385fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(vec, mem_type, &array)); 2395fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array)); 2405fb68f37SKaren (Ren) Stengel 2415fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(vec, &array)); 2425fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 2435fb68f37SKaren (Ren) Stengel } 2445fb68f37SKaren (Ren) Stengel 2455fb68f37SKaren (Ren) Stengel /** 2460b8f3c4eSJeremy L Thompson @brief Copy a strided portion of `CeedVector` contents into a different `CeedVector` 2470b8f3c4eSJeremy L Thompson 2480b8f3c4eSJeremy L Thompson @param[in] vec `CeedVector` to copy 2490b8f3c4eSJeremy L Thompson @param[in] start First index to copy 2500b8f3c4eSJeremy L Thompson @param[in] step Stride between indices to copy 2510b8f3c4eSJeremy L Thompson @param[in,out] vec_copy `CeedVector` to copy values to 2520b8f3c4eSJeremy L Thompson 2530b8f3c4eSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2540b8f3c4eSJeremy L Thompson 2550b8f3c4eSJeremy L Thompson @ref User 2560b8f3c4eSJeremy L Thompson **/ 2570b8f3c4eSJeremy L Thompson int CeedVectorCopyStrided(CeedVector vec, CeedSize start, CeedInt step, CeedVector vec_copy) { 2580b8f3c4eSJeremy L Thompson CeedSize length; 2590b8f3c4eSJeremy L Thompson const CeedScalar *array; 2600b8f3c4eSJeremy L Thompson CeedScalar *array_copy; 2610b8f3c4eSJeremy L Thompson 2620b8f3c4eSJeremy L Thompson // Backend version 2630b8f3c4eSJeremy L Thompson if (vec->CopyStrided && vec_copy->CopyStrided) { 2640b8f3c4eSJeremy L Thompson CeedCall(vec->CopyStrided(vec, start, step, vec_copy)); 2650b8f3c4eSJeremy L Thompson vec_copy->state += 2; 2660b8f3c4eSJeremy L Thompson return CEED_ERROR_SUCCESS; 2670b8f3c4eSJeremy L Thompson } 2680b8f3c4eSJeremy L Thompson 2690b8f3c4eSJeremy L Thompson // Get length 2700b8f3c4eSJeremy L Thompson { 2710b8f3c4eSJeremy L Thompson CeedSize length_vec, length_copy; 2720b8f3c4eSJeremy L Thompson 2730b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length_vec)); 2740b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetLength(vec_copy, &length_copy)); 2750b8f3c4eSJeremy L Thompson length = length_vec > length_copy ? length_vec : length_copy; 2760b8f3c4eSJeremy L Thompson } 2770b8f3c4eSJeremy L Thompson 2780b8f3c4eSJeremy L Thompson // Copy 2790b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 2800b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, &array_copy)); 2810b8f3c4eSJeremy L Thompson for (CeedSize i = start; i < length; i += step) array_copy[i] = array[i]; 2820b8f3c4eSJeremy L Thompson 2830b8f3c4eSJeremy L Thompson // Cleanup 2840b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 2850b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec_copy, &array_copy)); 2860b8f3c4eSJeremy L Thompson return CEED_ERROR_SUCCESS; 2870b8f3c4eSJeremy L Thompson } 2880b8f3c4eSJeremy L Thompson 2890b8f3c4eSJeremy L Thompson /** 290ca94c3ddSJeremy L Thompson @brief Set the array used by a `CeedVector`, freeing any previously allocated array if applicable. 2914385fb7fSSebastian Grimberg 292ca94c3ddSJeremy L Thompson The backend may copy values to a different @ref CeedMemType, such as during @ref CeedOperatorApply(). 2936a6c615bSJeremy L Thompson See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 2940436c2adSjeremylt 295ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 296ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the array being passed 297ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the array 298ca94c3ddSJeremy L Thompson @param[in] array Array to be used, or `NULL` with @ref CEED_COPY_VALUES to have the library allocate 2990436c2adSjeremylt 3000436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3010436c2adSjeremylt 3027a982d89SJeremy L. Thompson @ref User 3030436c2adSjeremylt **/ 3042b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 3051203703bSJeremy L Thompson CeedSize length; 3061203703bSJeremy L Thompson Ceed ceed; 3070436c2adSjeremylt 3081203703bSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 3091203703bSJeremy L Thompson 3101203703bSJeremy L Thompson CeedCheck(vec->SetArray, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); 3111203703bSJeremy L Thompson CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 3121203703bSJeremy L Thompson CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3131203703bSJeremy L Thompson 3141203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3151203703bSJeremy L Thompson if (length > 0) CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); 3160436c2adSjeremylt vec->state += 2; 317e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3180436c2adSjeremylt } 3190436c2adSjeremylt 3200436c2adSjeremylt /** 321ca94c3ddSJeremy L Thompson @brief Set the `CeedVector` to a constant value 3220436c2adSjeremylt 323ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 3240436c2adSjeremylt @param[in] value Value to be used 3250436c2adSjeremylt 3260436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3270436c2adSjeremylt 3287a982d89SJeremy L. Thompson @ref User 3290436c2adSjeremylt **/ 3300436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 3311203703bSJeremy L Thompson Ceed ceed; 3321203703bSJeremy L Thompson 3331203703bSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 3341203703bSJeremy L Thompson CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 3351203703bSJeremy L Thompson CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3360436c2adSjeremylt 3370436c2adSjeremylt if (vec->SetValue) { 3382b730f8bSJeremy L Thompson CeedCall(vec->SetValue(vec, value)); 3392d4e0605SJeremy L Thompson vec->state += 2; 3400436c2adSjeremylt } else { 3411203703bSJeremy L Thompson CeedSize length; 3420436c2adSjeremylt CeedScalar *array; 3431203703bSJeremy L Thompson 3442b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 3451203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3461203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) array[i] = value; 3472b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 3480436c2adSjeremylt } 349e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3500436c2adSjeremylt } 3510436c2adSjeremylt 3520436c2adSjeremylt /** 3530b8f3c4eSJeremy L Thompson @brief Set a portion of a `CeedVector` to a constant value. 3540b8f3c4eSJeremy L Thompson 3550b8f3c4eSJeremy L Thompson Note: The `CeedVector` must already have valid data set via @ref CeedVectorSetArray() or similar. 3560b8f3c4eSJeremy L Thompson 3570b8f3c4eSJeremy L Thompson @param[in,out] vec `CeedVector` 3580b8f3c4eSJeremy L Thompson @param[in] start First index to set 3590b8f3c4eSJeremy L Thompson @param[in] step Stride between indices to set 3600b8f3c4eSJeremy L Thompson @param[in] value Value to be used 3610b8f3c4eSJeremy L Thompson 3620b8f3c4eSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3630b8f3c4eSJeremy L Thompson 3640b8f3c4eSJeremy L Thompson @ref User 3650b8f3c4eSJeremy L Thompson **/ 3660b8f3c4eSJeremy L Thompson int CeedVectorSetValueStrided(CeedVector vec, CeedSize start, CeedInt step, CeedScalar value) { 3670b8f3c4eSJeremy L Thompson Ceed ceed; 3680b8f3c4eSJeremy L Thompson 3690b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 3700b8f3c4eSJeremy L Thompson CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 3710b8f3c4eSJeremy L Thompson CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3720b8f3c4eSJeremy L Thompson 3730b8f3c4eSJeremy L Thompson if (vec->SetValueStrided) { 3740b8f3c4eSJeremy L Thompson CeedCall(vec->SetValueStrided(vec, start, step, value)); 3752d4e0605SJeremy L Thompson vec->state += 2; 3760b8f3c4eSJeremy L Thompson } else { 3770b8f3c4eSJeremy L Thompson CeedSize length; 3780b8f3c4eSJeremy L Thompson CeedScalar *array; 3790b8f3c4eSJeremy L Thompson 3800b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array)); 3810b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3820b8f3c4eSJeremy L Thompson for (CeedSize i = start; i < length; i += step) array[i] = value; 3830b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 3840b8f3c4eSJeremy L Thompson } 3850b8f3c4eSJeremy L Thompson return CEED_ERROR_SUCCESS; 3860b8f3c4eSJeremy L Thompson } 3870b8f3c4eSJeremy L Thompson 3880b8f3c4eSJeremy L Thompson /** 389ca94c3ddSJeremy L Thompson @brief Sync the `CeedVector` to a specified `mem_type`. 3904385fb7fSSebastian Grimberg 391ea61e9acSJeremy L Thompson This function is used to force synchronization of arrays set with @ref CeedVectorSetArray(). 392ca94c3ddSJeremy L Thompson If the requested `mem_type` is already synchronized, this function results in a no-op. 3930436c2adSjeremylt 394ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 395ca94c3ddSJeremy L Thompson @param[in] mem_type @ref CeedMemType to be synced 3960436c2adSjeremylt 3970436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3980436c2adSjeremylt 3997a982d89SJeremy L. Thompson @ref User 4000436c2adSjeremylt **/ 401d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { 4021203703bSJeremy L Thompson CeedSize length; 4031203703bSJeremy L Thompson 4046e536b99SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use"); 4050436c2adSjeremylt 406b0976d5aSZach Atkins // Don't sync empty array 4071203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 4081203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 409b0976d5aSZach Atkins 4100436c2adSjeremylt if (vec->SyncArray) { 4112b730f8bSJeremy L Thompson CeedCall(vec->SyncArray(vec, mem_type)); 4120436c2adSjeremylt } else { 4130436c2adSjeremylt const CeedScalar *array; 4141203703bSJeremy L Thompson 4152b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array)); 4162b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 4170436c2adSjeremylt } 418e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4190436c2adSjeremylt } 4200436c2adSjeremylt 4210436c2adSjeremylt /** 422ca94c3ddSJeremy L Thompson @brief Take ownership of the `CeedVector` array set by @ref CeedVectorSetArray() with @ref CEED_USE_POINTER and remove the array from the `CeedVector`. 4234385fb7fSSebastian Grimberg 4249c774eddSJeremy L Thompson The caller is responsible for managing and freeing the array. 425ea61e9acSJeremy L Thompson This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type. 4266a6c615bSJeremy L Thompson 427ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 428ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to take the array. 429ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 430ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type`, or `NULL` if array pointer is not required 4316a6c615bSJeremy L Thompson 4326a6c615bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4336a6c615bSJeremy L Thompson 4346a6c615bSJeremy L Thompson @ref User 4356a6c615bSJeremy L Thompson **/ 4362b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4371203703bSJeremy L Thompson CeedSize length; 4381c66c397SJeremy L Thompson CeedScalar *temp_array = NULL; 4391203703bSJeremy L Thompson Ceed ceed; 4401c66c397SJeremy L Thompson 4411203703bSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 4421203703bSJeremy L Thompson CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); 4431203703bSJeremy L Thompson CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); 4446a6c615bSJeremy L Thompson 4451203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 4461203703bSJeremy L Thompson if (length > 0) { 4471203703bSJeremy L Thompson bool has_borrowed_array_of_type = true, has_valid_array = true; 4481203703bSJeremy L Thompson 4492b730f8bSJeremy L Thompson CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); 4501203703bSJeremy L Thompson CeedCheck(has_borrowed_array_of_type, ceed, CEED_ERROR_BACKEND, "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", 4511203703bSJeremy L Thompson CeedMemTypes[mem_type]); 4529c774eddSJeremy L Thompson 4532b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4541203703bSJeremy L Thompson CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND, 4552b730f8bSJeremy L Thompson "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4569c774eddSJeremy L Thompson 4572b730f8bSJeremy L Thompson CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); 45850c643e1SJed Brown } 459d1d35e2fSjeremylt if (array) (*array) = temp_array; 460e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4616a6c615bSJeremy L Thompson } 4626a6c615bSJeremy L Thompson 4636a6c615bSJeremy L Thompson /** 464ca94c3ddSJeremy L Thompson @brief Get read/write access to a `CeedVector` via the specified memory type. 4654385fb7fSSebastian Grimberg 466b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArray(). 4670436c2adSjeremylt 468ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to access 469ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 470ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 471ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 4720436c2adSjeremylt 473ca94c3ddSJeremy L Thompson @note The @ref CeedVectorGetArray() and @ref CeedVectorRestoreArray() functions provide access to array pointers in the desired memory space. 474ca94c3ddSJeremy L Thompson Pairing get/restore allows the `CeedVector` to track access, thus knowing if norms or other operations may need to be recomputed. 4750436c2adSjeremylt 4760436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4770436c2adSjeremylt 4787a982d89SJeremy L. Thompson @ref User 4790436c2adSjeremylt **/ 4802b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4811203703bSJeremy L Thompson CeedSize length; 4821203703bSJeremy L Thompson Ceed ceed; 4830436c2adSjeremylt 4841203703bSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 4851203703bSJeremy L Thompson CeedCheck(vec->GetArray, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); 4861203703bSJeremy L Thompson CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 4871203703bSJeremy L Thompson CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 4881203703bSJeremy L Thompson 4891203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 4901203703bSJeremy L Thompson if (length > 0) { 4919c774eddSJeremy L Thompson bool has_valid_array = true; 492b0976d5aSZach Atkins 4932b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4941203703bSJeremy L Thompson CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND, 4952b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4969c774eddSJeremy L Thompson 4972b730f8bSJeremy L Thompson CeedCall(vec->GetArray(vec, mem_type, array)); 498b0976d5aSZach Atkins } else { 499b0976d5aSZach Atkins *array = NULL; 500b0976d5aSZach Atkins } 50128bfd0b7SJeremy L Thompson vec->state++; 502e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5030436c2adSjeremylt } 5040436c2adSjeremylt 5050436c2adSjeremylt /** 506ca94c3ddSJeremy L Thompson @brief Get read-only access to a `CeedVector` via the specified memory type. 5074385fb7fSSebastian Grimberg 508b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArrayRead(). 5090436c2adSjeremylt 510ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to access 511ca94c3ddSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 512ca94c3ddSJeremy L Thompson If the backend uses a different memory type, this will perform a copy (possibly cached). 513ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 5140436c2adSjeremylt 5150436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5160436c2adSjeremylt 5177a982d89SJeremy L. Thompson @ref User 5180436c2adSjeremylt **/ 5192b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 5201203703bSJeremy L Thompson CeedSize length; 5211203703bSJeremy L Thompson Ceed ceed; 5220436c2adSjeremylt 5231203703bSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 5241203703bSJeremy L Thompson CeedCheck(vec->GetArrayRead, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); 5251203703bSJeremy L Thompson CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use"); 5261203703bSJeremy L Thompson 5271203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5281203703bSJeremy L Thompson if (length > 0) { 5299c774eddSJeremy L Thompson bool has_valid_array = true; 530b0976d5aSZach Atkins 5312b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5321203703bSJeremy L Thompson CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND, 5332b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5349c774eddSJeremy L Thompson 5352b730f8bSJeremy L Thompson CeedCall(vec->GetArrayRead(vec, mem_type, array)); 53650c643e1SJed Brown } else { 53750c643e1SJed Brown *array = NULL; 53850c643e1SJed Brown } 539d1d35e2fSjeremylt vec->num_readers++; 540e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5410436c2adSjeremylt } 5420436c2adSjeremylt 5430436c2adSjeremylt /** 544ca94c3ddSJeremy L Thompson @brief Get write access to a `CeedVector` via the specified memory type. 5454385fb7fSSebastian Grimberg 546ea61e9acSJeremy L Thompson Restore access with @ref CeedVectorRestoreArray(). 547ea61e9acSJeremy L Thompson All old values should be assumed to be invalid. 5489c774eddSJeremy L Thompson 549ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to access 550ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 551ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 5529c774eddSJeremy L Thompson 5539c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5549c774eddSJeremy L Thompson 5559c774eddSJeremy L Thompson @ref User 5569c774eddSJeremy L Thompson **/ 5572b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 5581203703bSJeremy L Thompson CeedSize length; 5591203703bSJeremy L Thompson Ceed ceed; 5609c774eddSJeremy L Thompson 5611203703bSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 5621203703bSJeremy L Thompson CeedCheck(vec->GetArrayWrite, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorGetArrayWrite"); 5631203703bSJeremy L Thompson CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 5641203703bSJeremy L Thompson CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 5651203703bSJeremy L Thompson 5661203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5671203703bSJeremy L Thompson if (length > 0) { 5682b730f8bSJeremy L Thompson CeedCall(vec->GetArrayWrite(vec, mem_type, array)); 569b0976d5aSZach Atkins } else { 570b0976d5aSZach Atkins *array = NULL; 571b0976d5aSZach Atkins } 57228bfd0b7SJeremy L Thompson vec->state++; 5739c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 5749c774eddSJeremy L Thompson } 5759c774eddSJeremy L Thompson 5769c774eddSJeremy L Thompson /** 577ea61e9acSJeremy L Thompson @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite() 5780436c2adSjeremylt 579ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to restore 580ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 5810436c2adSjeremylt 5820436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5830436c2adSjeremylt 5847a982d89SJeremy L. Thompson @ref User 5850436c2adSjeremylt **/ 5860436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 5871203703bSJeremy L Thompson CeedSize length; 5881203703bSJeremy L Thompson 5896e536b99SJeremy L Thompson CeedCheck(vec->state % 2 == 1, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted"); 5901203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5911203703bSJeremy L Thompson if (length > 0 && vec->RestoreArray) CeedCall(vec->RestoreArray(vec)); 5920436c2adSjeremylt *array = NULL; 59328bfd0b7SJeremy L Thompson vec->state++; 594e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5950436c2adSjeremylt } 5960436c2adSjeremylt 5970436c2adSjeremylt /** 598b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 5990436c2adSjeremylt 600ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to restore 601ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 6020436c2adSjeremylt 6030436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 6040436c2adSjeremylt 6057a982d89SJeremy L. Thompson @ref User 6060436c2adSjeremylt **/ 6070436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 6081203703bSJeremy L Thompson CeedSize length; 6091203703bSJeremy L Thompson 6106e536b99SJeremy L Thompson CeedCheck(vec->num_readers > 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 6116e536b99SJeremy L Thompson "Cannot restore CeedVector array read access, access was not granted"); 61275a19770SJeremy L Thompson vec->num_readers--; 6131203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 6141203703bSJeremy L Thompson if (length > 0 && vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec)); 6150436c2adSjeremylt *array = NULL; 616e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6170436c2adSjeremylt } 6180436c2adSjeremylt 6190436c2adSjeremylt /** 620ca94c3ddSJeremy L Thompson @brief Get the norm of a `CeedVector`. 621171f8ca9Sjeremylt 622ca94c3ddSJeremy L Thompson Note: This operation is local to the `CeedVector`. 623ca94c3ddSJeremy L Thompson This function will likely not provide the desired results for the norm of the libCEED portion of a parallel vector or a `CeedVector` with duplicated or hanging nodes. 624547d9b97Sjeremylt 625ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve maximum value 626ea61e9acSJeremy L Thompson @param[in] norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 627547d9b97Sjeremylt @param[out] norm Variable to store norm value 628547d9b97Sjeremylt 629547d9b97Sjeremylt @return An error code: 0 - success, otherwise - failure 630547d9b97Sjeremylt 6317a982d89SJeremy L. Thompson @ref User 632547d9b97Sjeremylt **/ 633d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) { 6349c774eddSJeremy L Thompson bool has_valid_array = true; 6351203703bSJeremy L Thompson CeedSize length; 6361203703bSJeremy L Thompson 6372b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 6386e536b99SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 6392b730f8bSJeremy L Thompson "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6409c774eddSJeremy L Thompson 6411203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 6421203703bSJeremy L Thompson if (length == 0) { 643b0976d5aSZach Atkins *norm = 0; 644b0976d5aSZach Atkins return CEED_ERROR_SUCCESS; 645b0976d5aSZach Atkins } 646b0976d5aSZach Atkins 647547d9b97Sjeremylt // Backend impl for GPU, if added 648547d9b97Sjeremylt if (vec->Norm) { 6492b730f8bSJeremy L Thompson CeedCall(vec->Norm(vec, norm_type, norm)); 650e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 651547d9b97Sjeremylt } 652547d9b97Sjeremylt 653547d9b97Sjeremylt const CeedScalar *array; 6542b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 655b0976d5aSZach Atkins assert(array); 656547d9b97Sjeremylt 657547d9b97Sjeremylt *norm = 0.; 658d1d35e2fSjeremylt switch (norm_type) { 659547d9b97Sjeremylt case CEED_NORM_1: 6601203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 661547d9b97Sjeremylt *norm += fabs(array[i]); 662547d9b97Sjeremylt } 663547d9b97Sjeremylt break; 664547d9b97Sjeremylt case CEED_NORM_2: 6651203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 666547d9b97Sjeremylt *norm += fabs(array[i]) * fabs(array[i]); 667547d9b97Sjeremylt } 668547d9b97Sjeremylt break; 669547d9b97Sjeremylt case CEED_NORM_MAX: 6701203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 671d1d35e2fSjeremylt const CeedScalar abs_v_i = fabs(array[i]); 672d1d35e2fSjeremylt *norm = *norm > abs_v_i ? *norm : abs_v_i; 673547d9b97Sjeremylt } 674547d9b97Sjeremylt } 6752b730f8bSJeremy L Thompson if (norm_type == CEED_NORM_2) *norm = sqrt(*norm); 676547d9b97Sjeremylt 6772b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 678e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 679547d9b97Sjeremylt } 680547d9b97Sjeremylt 681547d9b97Sjeremylt /** 682ca94c3ddSJeremy L Thompson @brief Compute `x = alpha x` 683e0dd3b27Sjeremylt 684ca94c3ddSJeremy L Thompson @param[in,out] x `CeedVector` for scaling 68596b902e2Sjeremylt @param[in] alpha scaling factor 686e0dd3b27Sjeremylt 687e0dd3b27Sjeremylt @return An error code: 0 - success, otherwise - failure 688e0dd3b27Sjeremylt 689e0dd3b27Sjeremylt @ref User 690e0dd3b27Sjeremylt **/ 691e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) { 6929c774eddSJeremy L Thompson bool has_valid_array = true; 6931203703bSJeremy L Thompson CeedSize length; 6941203703bSJeremy L Thompson CeedScalar *x_array = NULL; 6951c66c397SJeremy L Thompson 6962b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array)); 6976e536b99SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(x), CEED_ERROR_BACKEND, 6982b730f8bSJeremy L Thompson "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6999c774eddSJeremy L Thompson 700b0976d5aSZach Atkins // Return early for empty vector 7011203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length)); 7021203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 703b0976d5aSZach Atkins 704e0dd3b27Sjeremylt // Backend implementation 7052b730f8bSJeremy L Thompson if (x->Scale) return x->Scale(x, alpha); 706e0dd3b27Sjeremylt 707e0dd3b27Sjeremylt // Default implementation 708567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(x, CEED_MEM_HOST, &x_array)); 709b0976d5aSZach Atkins assert(x_array); 7101203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha; 7112b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(x, &x_array)); 712e0dd3b27Sjeremylt return CEED_ERROR_SUCCESS; 713e0dd3b27Sjeremylt } 714e0dd3b27Sjeremylt 715e0dd3b27Sjeremylt /** 716ca94c3ddSJeremy L Thompson @brief Compute `y = alpha x + y` 7170f7fd0f8Sjeremylt 718ca94c3ddSJeremy L Thompson @param[in,out] y target `CeedVector` for sum 71996b902e2Sjeremylt @param[in] alpha scaling factor 720ca94c3ddSJeremy L Thompson @param[in] x second `CeedVector`, must be different than ``y` 7210f7fd0f8Sjeremylt 7220f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 7230f7fd0f8Sjeremylt 7240f7fd0f8Sjeremylt @ref User 7250f7fd0f8Sjeremylt **/ 7260f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { 7271203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 7281203703bSJeremy L Thompson CeedSize length_x, length_y; 72973380422SJeremy L Thompson CeedScalar *y_array = NULL; 73073380422SJeremy L Thompson CeedScalar const *x_array = NULL; 7311203703bSJeremy L Thompson Ceed ceed, ceed_parent_x, ceed_parent_y; 7320f7fd0f8Sjeremylt 7331203703bSJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed)); 7341203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 7351203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 7361203703bSJeremy L Thompson CeedCheck(length_x == length_y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 7371203703bSJeremy L Thompson CeedCheck(x != y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); 7380f7fd0f8Sjeremylt 7392b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 7401203703bSJeremy L Thompson CeedCheck(has_valid_array_x, ceed, CEED_ERROR_BACKEND, 7416574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7422b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 7431203703bSJeremy L Thompson CeedCheck(has_valid_array_y, ceed, CEED_ERROR_BACKEND, 7446574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7459c774eddSJeremy L Thompson 7462b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 7472b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 7481203703bSJeremy L Thompson CeedCheck(ceed_parent_x == ceed_parent_y, ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); 7492d04630dSjeremylt 750b0976d5aSZach Atkins // Return early for empty vectors 7511203703bSJeremy L Thompson if (length_y == 0) return CEED_ERROR_SUCCESS; 752b0976d5aSZach Atkins 7530f7fd0f8Sjeremylt // Backend implementation 754eaf62fffSJeremy L Thompson if (y->AXPY) { 7552b730f8bSJeremy L Thompson CeedCall(y->AXPY(y, alpha, x)); 756eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 757eaf62fffSJeremy L Thompson } 7580f7fd0f8Sjeremylt 7590f7fd0f8Sjeremylt // Default implementation 760567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 7612b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 7620f7fd0f8Sjeremylt 7632b730f8bSJeremy L Thompson assert(x_array); 7642b730f8bSJeremy L Thompson assert(y_array); 76573380422SJeremy L Thompson 7661203703bSJeremy L Thompson for (CeedSize i = 0; i < length_y; i++) y_array[i] += alpha * x_array[i]; 7670f7fd0f8Sjeremylt 7682b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(y, &y_array)); 7692b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 7700f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 7710f7fd0f8Sjeremylt } 7720f7fd0f8Sjeremylt 7730f7fd0f8Sjeremylt /** 774ca94c3ddSJeremy L Thompson @brief Compute `y = alpha x + beta y` 7755fb68f37SKaren (Ren) Stengel 776ca94c3ddSJeremy L Thompson @param[in,out] y target `CeedVector` for sum 7775fb68f37SKaren (Ren) Stengel @param[in] alpha first scaling factor 7785fb68f37SKaren (Ren) Stengel @param[in] beta second scaling factor 779ca94c3ddSJeremy L Thompson @param[in] x second `CeedVector`, must be different than `y` 7805fb68f37SKaren (Ren) Stengel 7815fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 7825fb68f37SKaren (Ren) Stengel 7835fb68f37SKaren (Ren) Stengel @ref User 7845fb68f37SKaren (Ren) Stengel **/ 7855fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 7861203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 7871203703bSJeremy L Thompson CeedSize length_x, length_y; 7885fb68f37SKaren (Ren) Stengel CeedScalar *y_array = NULL; 7895fb68f37SKaren (Ren) Stengel CeedScalar const *x_array = NULL; 7901203703bSJeremy L Thompson Ceed ceed, ceed_parent_x, ceed_parent_y; 7915fb68f37SKaren (Ren) Stengel 7921203703bSJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed)); 7931203703bSJeremy L Thompson 7941203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 7951203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 7961203703bSJeremy L Thompson CeedCheck(length_x == length_y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 7971203703bSJeremy L Thompson CeedCheck(x != y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY"); 7985fb68f37SKaren (Ren) Stengel 7995fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 8001203703bSJeremy L Thompson CeedCheck(has_valid_array_x, ceed, CEED_ERROR_BACKEND, 8016574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8025fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 8031203703bSJeremy L Thompson CeedCheck(has_valid_array_y, ceed, CEED_ERROR_BACKEND, 8046574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8055fb68f37SKaren (Ren) Stengel 8065fb68f37SKaren (Ren) Stengel CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 8075fb68f37SKaren (Ren) Stengel CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 8081203703bSJeremy L Thompson CeedCheck(ceed_parent_x == ceed_parent_y, ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); 8095fb68f37SKaren (Ren) Stengel 810b0976d5aSZach Atkins // Return early for empty vectors 8111203703bSJeremy L Thompson if (length_y == 0) return CEED_ERROR_SUCCESS; 812b0976d5aSZach Atkins 8135fb68f37SKaren (Ren) Stengel // Backend implementation 8145fb68f37SKaren (Ren) Stengel if (y->AXPBY) { 8155fb68f37SKaren (Ren) Stengel CeedCall(y->AXPBY(y, alpha, beta, x)); 8165fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 8175fb68f37SKaren (Ren) Stengel } 8185fb68f37SKaren (Ren) Stengel 8195fb68f37SKaren (Ren) Stengel // Default implementation 8205fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 8215fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 8225fb68f37SKaren (Ren) Stengel 8235fb68f37SKaren (Ren) Stengel assert(x_array); 8245fb68f37SKaren (Ren) Stengel assert(y_array); 8255fb68f37SKaren (Ren) Stengel 8261203703bSJeremy L Thompson for (CeedSize i = 0; i < length_y; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i]; 8275fb68f37SKaren (Ren) Stengel 8285fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(y, &y_array)); 8295fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 8305fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 8315fb68f37SKaren (Ren) Stengel } 8325fb68f37SKaren (Ren) Stengel 8335fb68f37SKaren (Ren) Stengel /** 834ca94c3ddSJeremy L Thompson @brief Compute the pointwise multiplication \f$w = x .* y\f$. 8354385fb7fSSebastian Grimberg 836ca94c3ddSJeremy L Thompson Any subset of `x`, `y`, and `w` may be the same `CeedVector`. 8370f7fd0f8Sjeremylt 838ca94c3ddSJeremy L Thompson @param[out] w target `CeedVector` for the product 839ca94c3ddSJeremy L Thompson @param[in] x first `CeedVector` for product 840ca94c3ddSJeremy L Thompson @param[in] y second `CeedVector` for the product 8410f7fd0f8Sjeremylt 8420f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 8430f7fd0f8Sjeremylt 8440f7fd0f8Sjeremylt @ref User 8450f7fd0f8Sjeremylt **/ 8460f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { 8471203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 84873380422SJeremy L Thompson CeedScalar *w_array = NULL; 84973380422SJeremy L Thompson CeedScalar const *x_array = NULL, *y_array = NULL; 8501203703bSJeremy L Thompson CeedSize length_w, length_x, length_y; 8511203703bSJeremy L Thompson Ceed ceed, ceed_parent_w, ceed_parent_x, ceed_parent_y; 8520f7fd0f8Sjeremylt 8531203703bSJeremy L Thompson CeedCall(CeedVectorGetCeed(w, &ceed)); 8541203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(w, &length_w)); 8551203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 8561203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 8571203703bSJeremy L Thompson CeedCheck(length_w == length_x && length_w == length_y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths"); 8580f7fd0f8Sjeremylt 8592b730f8bSJeremy L Thompson CeedCall(CeedGetParent(w->ceed, &ceed_parent_w)); 8602b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 8612b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 8621203703bSJeremy L Thompson CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, ceed, CEED_ERROR_INCOMPATIBLE, 8636574a04fSJeremy L Thompson "Vectors w, x, and y must be created by the same Ceed context"); 8642d04630dSjeremylt 8652b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 8661203703bSJeremy L Thompson CeedCheck(has_valid_array_x, ceed, CEED_ERROR_BACKEND, 8676574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8682b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 8691203703bSJeremy L Thompson CeedCheck(has_valid_array_y, ceed, CEED_ERROR_BACKEND, 8706574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8719c774eddSJeremy L Thompson 872b0976d5aSZach Atkins // Return early for empty vectors 8731203703bSJeremy L Thompson if (length_w == 0) return CEED_ERROR_SUCCESS; 874b0976d5aSZach Atkins 8750f7fd0f8Sjeremylt // Backend implementation 876eaf62fffSJeremy L Thompson if (w->PointwiseMult) { 8772b730f8bSJeremy L Thompson CeedCall(w->PointwiseMult(w, x, y)); 878eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 879eaf62fffSJeremy L Thompson } 8800f7fd0f8Sjeremylt 8810f7fd0f8Sjeremylt // Default implementation 882b0976d5aSZach Atkins if (x == w || y == w) { 883b0976d5aSZach Atkins CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 884b0976d5aSZach Atkins } else { 885b0976d5aSZach Atkins CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array)); 886b0976d5aSZach Atkins } 8870f7fd0f8Sjeremylt if (x != w) { 8882b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 8890f7fd0f8Sjeremylt } else { 8900f7fd0f8Sjeremylt x_array = w_array; 8910f7fd0f8Sjeremylt } 8920f7fd0f8Sjeremylt if (y != w && y != x) { 8932b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array)); 894c6d796c6SJeremy L Thompson } else if (y == x) { 8950f7fd0f8Sjeremylt y_array = x_array; 896b0976d5aSZach Atkins } else if (y == w) { 897c6d796c6SJeremy L Thompson y_array = w_array; 8980f7fd0f8Sjeremylt } 8990f7fd0f8Sjeremylt 9002b730f8bSJeremy L Thompson assert(w_array); 9012b730f8bSJeremy L Thompson assert(x_array); 9022b730f8bSJeremy L Thompson assert(y_array); 90373380422SJeremy L Thompson 9041203703bSJeremy L Thompson for (CeedSize i = 0; i < length_w; i++) w_array[i] = x_array[i] * y_array[i]; 9050f7fd0f8Sjeremylt 9062b730f8bSJeremy L Thompson if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array)); 9072b730f8bSJeremy L Thompson if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 9082b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(w, &w_array)); 9090f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 9100f7fd0f8Sjeremylt } 9110f7fd0f8Sjeremylt 9120f7fd0f8Sjeremylt /** 913ca94c3ddSJeremy L Thompson @brief Take the reciprocal of a `CeedVector`. 914d99fa3c5SJeremy L Thompson 915ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to take reciprocal 916d99fa3c5SJeremy L Thompson 917d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 918d99fa3c5SJeremy L Thompson 919d99fa3c5SJeremy L Thompson @ref User 920d99fa3c5SJeremy L Thompson **/ 921d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) { 9229c774eddSJeremy L Thompson bool has_valid_array = true; 9231203703bSJeremy L Thompson CeedSize length; 9241203703bSJeremy L Thompson CeedScalar *array; 9251203703bSJeremy L Thompson Ceed ceed; 9262b730f8bSJeremy L Thompson 9271203703bSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 9281c66c397SJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 9291203703bSJeremy L Thompson CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND, 9302b730f8bSJeremy L Thompson "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9319c774eddSJeremy L Thompson 932d99fa3c5SJeremy L Thompson // Check if vector data set 9331203703bSJeremy L Thompson CeedCheck(vec->state > 0, ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); 934d99fa3c5SJeremy L Thompson 935b0976d5aSZach Atkins // Return early for empty vector 9361203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 9371203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 938b0976d5aSZach Atkins 939d99fa3c5SJeremy L Thompson // Backend impl for GPU, if added 940d99fa3c5SJeremy L Thompson if (vec->Reciprocal) { 9412b730f8bSJeremy L Thompson CeedCall(vec->Reciprocal(vec)); 942e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 943d99fa3c5SJeremy L Thompson } 944d99fa3c5SJeremy L Thompson 945567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array)); 9461203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 9472b730f8bSJeremy L Thompson if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i]; 9482b730f8bSJeremy L Thompson } 949d99fa3c5SJeremy L Thompson 9502b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 951e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 952d99fa3c5SJeremy L Thompson } 953d99fa3c5SJeremy L Thompson 954d99fa3c5SJeremy L Thompson /** 955ca94c3ddSJeremy L Thompson @brief View a `CeedVector` 9560436c2adSjeremylt 957acb2c48cSJeremy L Thompson Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`. 958ca94c3ddSJeremy L Thompson Any portion of the provided range that is outside the range of valid indices for the `CeedVector` will be ignored. 959acb2c48cSJeremy L Thompson 960ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to view 961ca94c3ddSJeremy L Thompson @param[in] start Index of first `CeedVector` entry to view 962ca94c3ddSJeremy L Thompson @param[in] stop Index of last `CeedVector` entry to view 963ca94c3ddSJeremy L Thompson @param[in] step Step between `CeedVector` entries to view 964acb2c48cSJeremy L Thompson @param[in] fp_fmt Printing format 965acb2c48cSJeremy L Thompson @param[in] stream Filestream to write to 966acb2c48cSJeremy L Thompson 967acb2c48cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 968acb2c48cSJeremy L Thompson 969acb2c48cSJeremy L Thompson @ref User 970acb2c48cSJeremy L Thompson **/ 971acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) { 972acb2c48cSJeremy L Thompson char fmt[1024]; 9731203703bSJeremy L Thompson CeedSize length; 9741203703bSJeremy L Thompson const CeedScalar *x; 975acb2c48cSJeremy L Thompson 9766e536b99SJeremy L Thompson CeedCheck(step != 0, CeedVectorReturnCeed(vec), CEED_ERROR_MINOR, "View range 'step' must be nonzero"); 977acb2c48cSJeremy L Thompson 9781203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 9791203703bSJeremy L Thompson fprintf(stream, "CeedVector length %" CeedSize_FMT "\n", length); 9801203703bSJeremy L Thompson if (start != 0 || stop != length || step != 1) { 9811203703bSJeremy L Thompson fprintf(stream, " start: %" CeedSize_FMT "\n stop: %" CeedSize_FMT "\n step: %" CeedInt_FMT "\n", start, stop, step); 982acb2c48cSJeremy L Thompson } 9831203703bSJeremy L Thompson if (start > length) start = length; 9841203703bSJeremy L Thompson if (stop > length) stop = length; 985acb2c48cSJeremy L Thompson 986acb2c48cSJeremy L Thompson snprintf(fmt, sizeof fmt, " %s\n", fp_fmt ? fp_fmt : "%g"); 987acb2c48cSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x)); 988b94338b9SJed Brown for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]); 989acb2c48cSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &x)); 9901203703bSJeremy L Thompson if (stop != length) fprintf(stream, " ...\n"); 991acb2c48cSJeremy L Thompson return CEED_ERROR_SUCCESS; 992acb2c48cSJeremy L Thompson } 993acb2c48cSJeremy L Thompson 994acb2c48cSJeremy L Thompson /** 995ca94c3ddSJeremy L Thompson @brief View a `CeedVector` 996acb2c48cSJeremy L Thompson 997ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to view 998d1d35e2fSjeremylt @param[in] fp_fmt Printing format 9990a0da059Sjeremylt @param[in] stream Filestream to write to 10000a0da059Sjeremylt 10010436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 10020436c2adSjeremylt 10037a982d89SJeremy L. Thompson @ref User 10040436c2adSjeremylt **/ 1005d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { 10061203703bSJeremy L Thompson CeedSize length; 10071203703bSJeremy L Thompson 10081203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 10091203703bSJeremy L Thompson CeedCall(CeedVectorViewRange(vec, 0, length, 1, fp_fmt, stream)); 1010e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10110436c2adSjeremylt } 10120436c2adSjeremylt 10130436c2adSjeremylt /** 1014ca94c3ddSJeremy L Thompson @brief Get the `Ceed` associated with a `CeedVector` 1015b7c9bbdaSJeremy L Thompson 1016ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 1017ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 1018b7c9bbdaSJeremy L Thompson 1019b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1020b7c9bbdaSJeremy L Thompson 1021b7c9bbdaSJeremy L Thompson @ref Advanced 1022b7c9bbdaSJeremy L Thompson **/ 1023b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 10246e536b99SJeremy L Thompson *ceed = CeedVectorReturnCeed(vec); 1025b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1026b7c9bbdaSJeremy L Thompson } 1027b7c9bbdaSJeremy L Thompson 1028b7c9bbdaSJeremy L Thompson /** 10296e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedVector` 10306e536b99SJeremy L Thompson 10316e536b99SJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 10326e536b99SJeremy L Thompson 10336e536b99SJeremy L Thompson @return `Ceed` associated with the `vec` 10346e536b99SJeremy L Thompson 10356e536b99SJeremy L Thompson @ref Advanced 10366e536b99SJeremy L Thompson **/ 10376e536b99SJeremy L Thompson Ceed CeedVectorReturnCeed(CeedVector vec) { return vec->ceed; } 10386e536b99SJeremy L Thompson 10396e536b99SJeremy L Thompson /** 1040ca94c3ddSJeremy L Thompson @brief Get the length of a `CeedVector` 10410436c2adSjeremylt 1042ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve length 10437a982d89SJeremy L. Thompson @param[out] length Variable to store length 10440436c2adSjeremylt 10450436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 10460436c2adSjeremylt 10477a982d89SJeremy L. Thompson @ref User 10480436c2adSjeremylt **/ 10491f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) { 10507a982d89SJeremy L. Thompson *length = vec->length; 1051e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10520436c2adSjeremylt } 10530436c2adSjeremylt 10540436c2adSjeremylt /** 1055ca94c3ddSJeremy L Thompson @brief Destroy a `CeedVector` 10560436c2adSjeremylt 1057ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to destroy 10580436c2adSjeremylt 10590436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 10600436c2adSjeremylt 10617a982d89SJeremy L. Thompson @ref User 10620436c2adSjeremylt **/ 10630436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) { 10647425e127SJeremy L Thompson if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) { 1065ad6481ceSJeremy L Thompson *vec = NULL; 1066ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1067ad6481ceSJeremy L Thompson } 10686574a04fSJeremy L Thompson CeedCheck((*vec)->state % 2 == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use"); 10696574a04fSJeremy L Thompson CeedCheck((*vec)->num_readers == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access"); 10700436c2adSjeremylt 10712b730f8bSJeremy L Thompson if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec)); 10722b730f8bSJeremy L Thompson 10732b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*vec)->ceed)); 10742b730f8bSJeremy L Thompson CeedCall(CeedFree(vec)); 1075e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10760436c2adSjeremylt } 10770436c2adSjeremylt 10780436c2adSjeremylt /// @} 1079