19ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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 /// ---------------------------------------------------------------------------- 37*b0f67a9cSJeremy L Thompson /// CeedVector Internal Functions 38*b0f67a9cSJeremy L Thompson /// ---------------------------------------------------------------------------- 39*b0f67a9cSJeremy L Thompson /// @addtogroup CeedVectorDeveloper 40*b0f67a9cSJeremy L Thompson /// @{ 41*b0f67a9cSJeremy L Thompson 42*b0f67a9cSJeremy L Thompson /** 43*b0f67a9cSJeremy L Thompson @brief View a `CeedVector` passed as a `CeedObject` 44*b0f67a9cSJeremy L Thompson 45*b0f67a9cSJeremy L Thompson @param[in] vec `CeedVector` to view 46*b0f67a9cSJeremy L Thompson @param[in] stream Filestream to write to 47*b0f67a9cSJeremy L Thompson 48*b0f67a9cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 49*b0f67a9cSJeremy L Thompson 50*b0f67a9cSJeremy L Thompson @ref Developer 51*b0f67a9cSJeremy L Thompson **/ 52*b0f67a9cSJeremy L Thompson static int CeedVectorView_Object(CeedObject vec, FILE *stream) { 53*b0f67a9cSJeremy L Thompson CeedCall(CeedVectorView((CeedVector)vec, "%12.8f", stream)); 54*b0f67a9cSJeremy L Thompson return CEED_ERROR_SUCCESS; 55*b0f67a9cSJeremy L Thompson } 56*b0f67a9cSJeremy L Thompson 57*b0f67a9cSJeremy L Thompson /// @} 58*b0f67a9cSJeremy L Thompson 59*b0f67a9cSJeremy L Thompson /// ---------------------------------------------------------------------------- 607a982d89SJeremy L. Thompson /// CeedVector Backend API 617a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 627a982d89SJeremy L. Thompson /// @addtogroup CeedVectorBackend 637a982d89SJeremy L. Thompson /// @{ 647a982d89SJeremy L. Thompson 657a982d89SJeremy L. Thompson /** 66ca94c3ddSJeremy L Thompson @brief Check for valid data in a `CeedVector` 679c774eddSJeremy L Thompson 68ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to check validity 699c774eddSJeremy L Thompson @param[out] has_valid_array Variable to store validity 709c774eddSJeremy L Thompson 719c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 729c774eddSJeremy L Thompson 739c774eddSJeremy L Thompson @ref Backend 749c774eddSJeremy L Thompson **/ 759c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) { 761203703bSJeremy L Thompson CeedSize length; 771203703bSJeremy L Thompson 786e536b99SJeremy L Thompson CeedCheck(vec->HasValidArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorHasValidArray"); 791203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 801203703bSJeremy L Thompson if (length == 0) { 81b0976d5aSZach Atkins *has_valid_array = true; 82b0976d5aSZach Atkins return CEED_ERROR_SUCCESS; 83b0976d5aSZach Atkins } 842b730f8bSJeremy L Thompson CeedCall(vec->HasValidArray(vec, has_valid_array)); 859c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 869c774eddSJeremy L Thompson } 879c774eddSJeremy L Thompson 889c774eddSJeremy L Thompson /** 89ca94c3ddSJeremy L Thompson @brief Check for borrowed array of a specific @ref CeedMemType in a `CeedVector` 909c774eddSJeremy L Thompson 91ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to check 92ea61e9acSJeremy L Thompson @param[in] mem_type Memory type to check 939c774eddSJeremy L Thompson @param[out] has_borrowed_array_of_type Variable to store result 949c774eddSJeremy L Thompson 959c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 969c774eddSJeremy L Thompson 979c774eddSJeremy L Thompson @ref Backend 989c774eddSJeremy L Thompson **/ 992b730f8bSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 1006e536b99SJeremy L Thompson CeedCheck(vec->HasBorrowedArrayOfType, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, 1016e536b99SJeremy L Thompson "Backend does not support CeedVectorHasBorrowedArrayOfType"); 1022b730f8bSJeremy L Thompson CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type)); 1039c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 1049c774eddSJeremy L Thompson } 1059c774eddSJeremy L Thompson 1069c774eddSJeremy L Thompson /** 107ca94c3ddSJeremy L Thompson @brief Get the state of a `CeedVector` 1087a982d89SJeremy L. Thompson 109ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 1107a982d89SJeremy L. Thompson @param[out] state Variable to store state 1117a982d89SJeremy L. Thompson 1127a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1137a982d89SJeremy L. Thompson 1147a982d89SJeremy L. Thompson @ref Backend 1157a982d89SJeremy L. Thompson **/ 1167a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) { 1177a982d89SJeremy L. Thompson *state = vec->state; 118e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1197a982d89SJeremy L. Thompson } 1207a982d89SJeremy L. Thompson 1217a982d89SJeremy L. Thompson /** 122ca94c3ddSJeremy L Thompson @brief Get the backend data of a `CeedVector` 1237a982d89SJeremy L. Thompson 124ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 1257a982d89SJeremy L. Thompson @param[out] data Variable to store data 1267a982d89SJeremy L. Thompson 1277a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1287a982d89SJeremy L. Thompson 1297a982d89SJeremy L. Thompson @ref Backend 1307a982d89SJeremy L. Thompson **/ 131777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) { 132777ff853SJeremy L Thompson *(void **)data = vec->data; 133e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1347a982d89SJeremy L. Thompson } 1357a982d89SJeremy L. Thompson 1367a982d89SJeremy L. Thompson /** 137ca94c3ddSJeremy L Thompson @brief Set the backend data of a `CeedVector` 1387a982d89SJeremy L. Thompson 139ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to retrieve state 140ea61e9acSJeremy L Thompson @param[in] data Data to set 1417a982d89SJeremy L. Thompson 1427a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1437a982d89SJeremy L. Thompson 1447a982d89SJeremy L. Thompson @ref Backend 1457a982d89SJeremy L. Thompson **/ 146777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) { 147777ff853SJeremy L Thompson vec->data = data; 148e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1497a982d89SJeremy L. Thompson } 1507a982d89SJeremy L. Thompson 15134359f16Sjeremylt /** 152ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedVector` 15334359f16Sjeremylt 154ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to increment the reference counter 15534359f16Sjeremylt 15634359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 15734359f16Sjeremylt 15834359f16Sjeremylt @ref Backend 15934359f16Sjeremylt **/ 1609560d06aSjeremylt int CeedVectorReference(CeedVector vec) { 161*b0f67a9cSJeremy L Thompson CeedCall(CeedObjectReference((CeedObject)vec)); 16234359f16Sjeremylt return CEED_ERROR_SUCCESS; 16334359f16Sjeremylt } 16434359f16Sjeremylt 1657a982d89SJeremy L. Thompson /// @} 1667a982d89SJeremy L. Thompson 1677a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1687a982d89SJeremy L. Thompson /// CeedVector Public API 1697a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1707a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 1710436c2adSjeremylt /// @{ 1720436c2adSjeremylt 1730436c2adSjeremylt /** 174ca94c3ddSJeremy L Thompson @brief Create a `CeedVector` of the specified length (does not allocate memory) 1750436c2adSjeremylt 176ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedVector` 177ea61e9acSJeremy L Thompson @param[in] length Length of vector 178ca94c3ddSJeremy L Thompson @param[out] vec Address of the variable where the newly created `CeedVector` will be stored 1790436c2adSjeremylt 1800436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 1810436c2adSjeremylt 1827a982d89SJeremy L. Thompson @ref User 1830436c2adSjeremylt **/ 1841f9221feSJeremy L Thompson int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) { 185a5ef892dSJames Wright CeedCheck(length >= 0, ceed, CEED_ERROR_UNSUPPORTED, "CeedVector must have length >= 0, recieved %" CeedSize_FMT, length); 1860436c2adSjeremylt if (!ceed->VectorCreate) { 1870436c2adSjeremylt Ceed delegate; 1886574a04fSJeremy L Thompson 1892b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector")); 1901ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement VectorCreate"); 1912b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(delegate, length, vec)); 1929bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 193e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1940436c2adSjeremylt } 1950436c2adSjeremylt 1962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, vec)); 197*b0f67a9cSJeremy L Thompson CeedCall(CeedObjectCreate(ceed, CeedVectorView_Object, &(*vec)->obj)); 1980436c2adSjeremylt (*vec)->length = length; 1990436c2adSjeremylt (*vec)->state = 0; 2002b730f8bSJeremy L Thompson CeedCall(ceed->VectorCreate(length, *vec)); 201e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2020436c2adSjeremylt } 2030436c2adSjeremylt 2040436c2adSjeremylt /** 205ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedVector`. 2064385fb7fSSebastian Grimberg 207ca94c3ddSJeremy L Thompson Both pointers should be destroyed with @ref CeedVectorDestroy(). 208512bb800SJeremy L Thompson 209ca94c3ddSJeremy 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`. 210ca94c3ddSJeremy L Thompson This `CeedVector` will be destroyed if `*vec_copy` is the only reference to this `CeedVector`. 2119560d06aSjeremylt 212ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to copy reference to 213ea61e9acSJeremy L Thompson @param[in,out] vec_copy Variable to store copied reference 2149560d06aSjeremylt 2159560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 2169560d06aSjeremylt 2179560d06aSjeremylt @ref User 2189560d06aSjeremylt **/ 2199560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) { 220393ac2cdSJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorReference(vec)); 2212b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(vec_copy)); 2229560d06aSjeremylt *vec_copy = vec; 2239560d06aSjeremylt return CEED_ERROR_SUCCESS; 2249560d06aSjeremylt } 2259560d06aSjeremylt 2269560d06aSjeremylt /** 227ca94c3ddSJeremy L Thompson @brief Copy a `CeedVector` into a different `CeedVector`. 2284385fb7fSSebastian Grimberg 229ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to copy 230e706ae07SJeremy L Thompson @param[in,out] vec_copy `CeedVector` to copy array into 2315fb68f37SKaren (Ren) Stengel 2325fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 2335fb68f37SKaren (Ren) Stengel 2345fb68f37SKaren (Ren) Stengel @ref User 2355fb68f37SKaren (Ren) Stengel **/ 2365fb68f37SKaren (Ren) Stengel int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) { 2375fb68f37SKaren (Ren) Stengel CeedMemType mem_type, mem_type_copy; 2385fb68f37SKaren (Ren) Stengel CeedScalar *array; 2395fb68f37SKaren (Ren) Stengel 2409bc66399SJeremy L Thompson // Get the preferred memory types 2419bc66399SJeremy L Thompson { 2429bc66399SJeremy L Thompson Ceed ceed; 2439bc66399SJeremy L Thompson 244d77c2f5dSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 245d77c2f5dSJeremy L Thompson CeedCall(CeedGetPreferredMemType(ceed, &mem_type)); 2469bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 2475fb68f37SKaren (Ren) Stengel 248d77c2f5dSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec_copy, &ceed)); 249d77c2f5dSJeremy L Thompson CeedCall(CeedGetPreferredMemType(ceed, &mem_type_copy)); 2509bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 2519bc66399SJeremy L Thompson } 2525fb68f37SKaren (Ren) Stengel 2535fb68f37SKaren (Ren) Stengel // Check that both have same memory type 2545fb68f37SKaren (Ren) Stengel if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST; 2555fb68f37SKaren (Ren) Stengel 256e706ae07SJeremy L Thompson // Check compatible lengths 257e706ae07SJeremy L Thompson { 258e706ae07SJeremy L Thompson CeedSize length_vec, length_copy; 259e706ae07SJeremy L Thompson 260e706ae07SJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length_vec)); 261e706ae07SJeremy L Thompson CeedCall(CeedVectorGetLength(vec_copy, &length_copy)); 2629bc66399SJeremy L Thompson CeedCheck(length_vec == length_copy, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPATIBLE, "CeedVectors must have the same length to copy"); 263e706ae07SJeremy L Thompson } 264e706ae07SJeremy L Thompson 2655fb68f37SKaren (Ren) Stengel // Copy the values from vec to vec_copy 2665fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(vec, mem_type, &array)); 2675fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array)); 2685fb68f37SKaren (Ren) Stengel 2695fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(vec, &array)); 2705fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 2715fb68f37SKaren (Ren) Stengel } 2725fb68f37SKaren (Ren) Stengel 2735fb68f37SKaren (Ren) Stengel /** 2740b8f3c4eSJeremy L Thompson @brief Copy a strided portion of `CeedVector` contents into a different `CeedVector` 2750b8f3c4eSJeremy L Thompson 2760b8f3c4eSJeremy L Thompson @param[in] vec `CeedVector` to copy 277832a6d73SJeremy L Thompson @param[in] start First index to copy in the range `[start, stop)` 278832a6d73SJeremy L Thompson @param[in] stop One past the last element to copy in the range, or `-1` for `length` 2790b8f3c4eSJeremy L Thompson @param[in] step Stride between indices to copy 2800b8f3c4eSJeremy L Thompson @param[in,out] vec_copy `CeedVector` to copy values to 2810b8f3c4eSJeremy L Thompson 2820b8f3c4eSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2830b8f3c4eSJeremy L Thompson 2840b8f3c4eSJeremy L Thompson @ref User 2850b8f3c4eSJeremy L Thompson **/ 286832a6d73SJeremy L Thompson int CeedVectorCopyStrided(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedVector vec_copy) { 2870b8f3c4eSJeremy L Thompson CeedSize length; 288bb03490dSJeremy L Thompson const CeedScalar *array = NULL; 289bb03490dSJeremy L Thompson CeedScalar *array_copy = NULL; 2900b8f3c4eSJeremy L Thompson 291832a6d73SJeremy L Thompson // Check length 2920b8f3c4eSJeremy L Thompson { 2930b8f3c4eSJeremy L Thompson CeedSize length_vec, length_copy; 2940b8f3c4eSJeremy L Thompson 2950b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length_vec)); 2960b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetLength(vec_copy, &length_copy)); 297bb03490dSJeremy L Thompson if (length_vec <= 0 || length_copy <= 0) return CEED_ERROR_SUCCESS; 298a7efc114SJeremy L Thompson length = length_vec < length_copy ? length_vec : length_copy; 2990b8f3c4eSJeremy L Thompson } 300832a6d73SJeremy L Thompson CeedCheck(stop >= -1 && stop <= length, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 301832a6d73SJeremy L Thompson "Invalid value for stop %" CeedSize_FMT ", must be in the range [-1, length]", stop); 302832a6d73SJeremy L Thompson CeedCheck(start >= 0 && start <= length && (start <= stop || stop == -1), CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 303832a6d73SJeremy L Thompson "Invalid value for start %" CeedSize_FMT ", must be in the range [0, stop]", start); 304832a6d73SJeremy L Thompson 305832a6d73SJeremy L Thompson // Backend version 306832a6d73SJeremy L Thompson if (vec->CopyStrided && vec_copy->CopyStrided) { 307832a6d73SJeremy L Thompson CeedCall(vec->CopyStrided(vec, start, stop, step, vec_copy)); 308832a6d73SJeremy L Thompson vec_copy->state += 2; 309832a6d73SJeremy L Thompson return CEED_ERROR_SUCCESS; 310832a6d73SJeremy L Thompson } 3110b8f3c4eSJeremy L Thompson 3120b8f3c4eSJeremy L Thompson // Copy 3130b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 3140b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, &array_copy)); 315832a6d73SJeremy L Thompson if (stop == -1) stop = length; 316832a6d73SJeremy L Thompson for (CeedSize i = start; i < stop; i += step) array_copy[i] = array[i]; 3170b8f3c4eSJeremy L Thompson 3180b8f3c4eSJeremy L Thompson // Cleanup 3190b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 3200b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec_copy, &array_copy)); 3210b8f3c4eSJeremy L Thompson return CEED_ERROR_SUCCESS; 3220b8f3c4eSJeremy L Thompson } 3230b8f3c4eSJeremy L Thompson 3240b8f3c4eSJeremy L Thompson /** 325ca94c3ddSJeremy L Thompson @brief Set the array used by a `CeedVector`, freeing any previously allocated array if applicable. 3264385fb7fSSebastian Grimberg 327ca94c3ddSJeremy L Thompson The backend may copy values to a different @ref CeedMemType, such as during @ref CeedOperatorApply(). 3286a6c615bSJeremy L Thompson See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 3290436c2adSjeremylt 330ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 331ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the array being passed 332ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the array 333ca94c3ddSJeremy L Thompson @param[in] array Array to be used, or `NULL` with @ref CEED_COPY_VALUES to have the library allocate 3340436c2adSjeremylt 3350436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3360436c2adSjeremylt 3377a982d89SJeremy L. Thompson @ref User 3380436c2adSjeremylt **/ 3392b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 3401203703bSJeremy L Thompson CeedSize length; 3410436c2adSjeremylt 3429bc66399SJeremy L Thompson CeedCheck(vec->SetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); 3439bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 3449bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 3459bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3461203703bSJeremy L Thompson 3471203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3481203703bSJeremy L Thompson if (length > 0) CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); 3490436c2adSjeremylt vec->state += 2; 350e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3510436c2adSjeremylt } 3520436c2adSjeremylt 3530436c2adSjeremylt /** 354ca94c3ddSJeremy L Thompson @brief Set the `CeedVector` to a constant value 3550436c2adSjeremylt 356ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 3570436c2adSjeremylt @param[in] value Value to be used 3580436c2adSjeremylt 3590436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3600436c2adSjeremylt 3617a982d89SJeremy L. Thompson @ref User 3620436c2adSjeremylt **/ 3630436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 3649bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 3659bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 3669bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3670436c2adSjeremylt 3680436c2adSjeremylt if (vec->SetValue) { 3692b730f8bSJeremy L Thompson CeedCall(vec->SetValue(vec, value)); 3702d4e0605SJeremy L Thompson vec->state += 2; 3710436c2adSjeremylt } else { 3721203703bSJeremy L Thompson CeedSize length; 3730436c2adSjeremylt CeedScalar *array; 3741203703bSJeremy L Thompson 3752b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 3761203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3771203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) array[i] = value; 3782b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 3790436c2adSjeremylt } 380e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3810436c2adSjeremylt } 3820436c2adSjeremylt 3830436c2adSjeremylt /** 3840b8f3c4eSJeremy L Thompson @brief Set a portion of a `CeedVector` to a constant value. 3850b8f3c4eSJeremy L Thompson 3860b8f3c4eSJeremy L Thompson Note: The `CeedVector` must already have valid data set via @ref CeedVectorSetArray() or similar. 3870b8f3c4eSJeremy L Thompson 3880b8f3c4eSJeremy L Thompson @param[in,out] vec `CeedVector` 389ff90b007SJeremy L Thompson @param[in] start First index to set in range `[start, stop)` 39012634730SJeremy L Thompson @param[in] stop One past the last element to set in the range, or `-1` for `length` 3910b8f3c4eSJeremy L Thompson @param[in] step Stride between indices to set 3920b8f3c4eSJeremy L Thompson @param[in] value Value to be used 3930b8f3c4eSJeremy L Thompson 3940b8f3c4eSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3950b8f3c4eSJeremy L Thompson 3960b8f3c4eSJeremy L Thompson @ref User 3970b8f3c4eSJeremy L Thompson **/ 398ff90b007SJeremy L Thompson int CeedVectorSetValueStrided(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedScalar value) { 399b1a610efSJeremy L Thompson CeedSize length; 400b1a610efSJeremy L Thompson 4019bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 4029bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 4039bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 404b1a610efSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 4057a747cf1SJeremy L Thompson CeedCheck(stop >= -1 && stop <= length, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 4067a747cf1SJeremy L Thompson "Invalid value for stop %" CeedSize_FMT ", must be in the range [-1, length]", stop); 4070b8f3c4eSJeremy L Thompson 4080b8f3c4eSJeremy L Thompson if (vec->SetValueStrided) { 409ff90b007SJeremy L Thompson CeedCall(vec->SetValueStrided(vec, start, stop, step, value)); 4102d4e0605SJeremy L Thompson vec->state += 2; 4110b8f3c4eSJeremy L Thompson } else { 4120b8f3c4eSJeremy L Thompson CeedScalar *array; 4130b8f3c4eSJeremy L Thompson 414bb03490dSJeremy L Thompson if (length <= 0) return CEED_ERROR_SUCCESS; 415ff90b007SJeremy L Thompson if (stop == -1) stop = length; 416bb03490dSJeremy L Thompson CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array)); 417ff90b007SJeremy L Thompson for (CeedSize i = start; i < stop; i += step) array[i] = value; 4180b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 4190b8f3c4eSJeremy L Thompson } 4200b8f3c4eSJeremy L Thompson return CEED_ERROR_SUCCESS; 4210b8f3c4eSJeremy L Thompson } 4220b8f3c4eSJeremy L Thompson 4230b8f3c4eSJeremy L Thompson /** 424ca94c3ddSJeremy L Thompson @brief Sync the `CeedVector` to a specified `mem_type`. 4254385fb7fSSebastian Grimberg 426ea61e9acSJeremy L Thompson This function is used to force synchronization of arrays set with @ref CeedVectorSetArray(). 427ca94c3ddSJeremy L Thompson If the requested `mem_type` is already synchronized, this function results in a no-op. 4280436c2adSjeremylt 429ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 430ca94c3ddSJeremy L Thompson @param[in] mem_type @ref CeedMemType to be synced 4310436c2adSjeremylt 4320436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4330436c2adSjeremylt 4347a982d89SJeremy L. Thompson @ref User 4350436c2adSjeremylt **/ 436d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { 4371203703bSJeremy L Thompson CeedSize length; 4381203703bSJeremy L Thompson 4396e536b99SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use"); 4400436c2adSjeremylt 441b0976d5aSZach Atkins // Don't sync empty array 4421203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 4431203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 444b0976d5aSZach Atkins 4450436c2adSjeremylt if (vec->SyncArray) { 4462b730f8bSJeremy L Thompson CeedCall(vec->SyncArray(vec, mem_type)); 4470436c2adSjeremylt } else { 4480436c2adSjeremylt const CeedScalar *array; 4491203703bSJeremy L Thompson 4502b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array)); 4512b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 4520436c2adSjeremylt } 453e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4540436c2adSjeremylt } 4550436c2adSjeremylt 4560436c2adSjeremylt /** 457ca94c3ddSJeremy 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`. 4584385fb7fSSebastian Grimberg 4599c774eddSJeremy L Thompson The caller is responsible for managing and freeing the array. 460ea61e9acSJeremy L Thompson This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type. 4616a6c615bSJeremy L Thompson 462ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 463ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to take the array. 464ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 465ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type`, or `NULL` if array pointer is not required 4666a6c615bSJeremy L Thompson 4676a6c615bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4686a6c615bSJeremy L Thompson 4696a6c615bSJeremy L Thompson @ref User 4706a6c615bSJeremy L Thompson **/ 4712b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4721203703bSJeremy L Thompson CeedSize length; 4731c66c397SJeremy L Thompson CeedScalar *temp_array = NULL; 4741c66c397SJeremy L Thompson 4759bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); 4769bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); 4776a6c615bSJeremy L Thompson 4781203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 4791203703bSJeremy L Thompson if (length > 0) { 4801203703bSJeremy L Thompson bool has_borrowed_array_of_type = true, has_valid_array = true; 4811203703bSJeremy L Thompson 4822b730f8bSJeremy L Thompson CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); 4839bc66399SJeremy L Thompson CeedCheck(has_borrowed_array_of_type, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 4849bc66399SJeremy L Thompson "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", CeedMemTypes[mem_type]); 4859c774eddSJeremy L Thompson 4862b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4879bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 4882b730f8bSJeremy L Thompson "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4899c774eddSJeremy L Thompson 4902b730f8bSJeremy L Thompson CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); 49150c643e1SJed Brown } 492d1d35e2fSjeremylt if (array) (*array) = temp_array; 493e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4946a6c615bSJeremy L Thompson } 4956a6c615bSJeremy L Thompson 4966a6c615bSJeremy L Thompson /** 497ca94c3ddSJeremy L Thompson @brief Get read/write access to a `CeedVector` via the specified memory type. 4984385fb7fSSebastian Grimberg 499b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArray(). 5000436c2adSjeremylt 501ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to access 502ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 503ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 504ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 5050436c2adSjeremylt 506ca94c3ddSJeremy L Thompson @note The @ref CeedVectorGetArray() and @ref CeedVectorRestoreArray() functions provide access to array pointers in the desired memory space. 507ca94c3ddSJeremy L Thompson Pairing get/restore allows the `CeedVector` to track access, thus knowing if norms or other operations may need to be recomputed. 5080436c2adSjeremylt 5090436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5100436c2adSjeremylt 5117a982d89SJeremy L. Thompson @ref User 5120436c2adSjeremylt **/ 5132b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 5141203703bSJeremy L Thompson CeedSize length; 5150436c2adSjeremylt 5169bc66399SJeremy L Thompson CeedCheck(vec->GetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); 5179bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 5189bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 5199bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 5201203703bSJeremy L Thompson 5211203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5221203703bSJeremy L Thompson if (length > 0) { 5239c774eddSJeremy L Thompson bool has_valid_array = true; 524b0976d5aSZach Atkins 5252b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5269bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 5272b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5289c774eddSJeremy L Thompson 5292b730f8bSJeremy L Thompson CeedCall(vec->GetArray(vec, mem_type, array)); 530b0976d5aSZach Atkins } else { 531b0976d5aSZach Atkins *array = NULL; 532b0976d5aSZach Atkins } 53328bfd0b7SJeremy L Thompson vec->state++; 534e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5350436c2adSjeremylt } 5360436c2adSjeremylt 5370436c2adSjeremylt /** 538ca94c3ddSJeremy L Thompson @brief Get read-only access to a `CeedVector` via the specified memory type. 5394385fb7fSSebastian Grimberg 540b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArrayRead(). 5410436c2adSjeremylt 542ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to access 543ca94c3ddSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 544ca94c3ddSJeremy L Thompson If the backend uses a different memory type, this will perform a copy (possibly cached). 545ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 5460436c2adSjeremylt 5470436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5480436c2adSjeremylt 5497a982d89SJeremy L. Thompson @ref User 5500436c2adSjeremylt **/ 5512b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 5521203703bSJeremy L Thompson CeedSize length; 5530436c2adSjeremylt 5549bc66399SJeremy L Thompson CeedCheck(vec->GetArrayRead, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); 5559bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 5569bc66399SJeremy L Thompson "Cannot grant CeedVector read-only array access, the access lock is already in use"); 5571203703bSJeremy L Thompson 5581203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5591203703bSJeremy L Thompson if (length > 0) { 5609c774eddSJeremy L Thompson bool has_valid_array = true; 561b0976d5aSZach Atkins 5622b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5639bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 5642b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5659c774eddSJeremy L Thompson 5662b730f8bSJeremy L Thompson CeedCall(vec->GetArrayRead(vec, mem_type, array)); 56750c643e1SJed Brown } else { 56850c643e1SJed Brown *array = NULL; 56950c643e1SJed Brown } 570d1d35e2fSjeremylt vec->num_readers++; 571e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5720436c2adSjeremylt } 5730436c2adSjeremylt 5740436c2adSjeremylt /** 575ca94c3ddSJeremy L Thompson @brief Get write access to a `CeedVector` via the specified memory type. 5764385fb7fSSebastian Grimberg 577ea61e9acSJeremy L Thompson Restore access with @ref CeedVectorRestoreArray(). 578ea61e9acSJeremy L Thompson All old values should be assumed to be invalid. 5799c774eddSJeremy L Thompson 580ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to access 581ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 582ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 5839c774eddSJeremy L Thompson 5849c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5859c774eddSJeremy L Thompson 5869c774eddSJeremy L Thompson @ref User 5879c774eddSJeremy L Thompson **/ 5882b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 5891203703bSJeremy L Thompson CeedSize length; 5909c774eddSJeremy L Thompson 5919bc66399SJeremy L Thompson CeedCheck(vec->GetArrayWrite, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorGetArrayWrite"); 5929bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 5939bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 5949bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 5951203703bSJeremy L Thompson 5961203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5971203703bSJeremy L Thompson if (length > 0) { 5982b730f8bSJeremy L Thompson CeedCall(vec->GetArrayWrite(vec, mem_type, array)); 599b0976d5aSZach Atkins } else { 600b0976d5aSZach Atkins *array = NULL; 601b0976d5aSZach Atkins } 60228bfd0b7SJeremy L Thompson vec->state++; 6039c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 6049c774eddSJeremy L Thompson } 6059c774eddSJeremy L Thompson 6069c774eddSJeremy L Thompson /** 607ea61e9acSJeremy L Thompson @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite() 6080436c2adSjeremylt 609ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to restore 610ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 6110436c2adSjeremylt 6120436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 6130436c2adSjeremylt 6147a982d89SJeremy L. Thompson @ref User 6150436c2adSjeremylt **/ 6160436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 6171203703bSJeremy L Thompson CeedSize length; 6181203703bSJeremy L Thompson 6196e536b99SJeremy L Thompson CeedCheck(vec->state % 2 == 1, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted"); 6201203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 6211203703bSJeremy L Thompson if (length > 0 && vec->RestoreArray) CeedCall(vec->RestoreArray(vec)); 6220436c2adSjeremylt *array = NULL; 62328bfd0b7SJeremy L Thompson vec->state++; 624e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6250436c2adSjeremylt } 6260436c2adSjeremylt 6270436c2adSjeremylt /** 628b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 6290436c2adSjeremylt 630ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to restore 631ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 6320436c2adSjeremylt 6330436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 6340436c2adSjeremylt 6357a982d89SJeremy L. Thompson @ref User 6360436c2adSjeremylt **/ 6370436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 6381203703bSJeremy L Thompson CeedSize length; 6391203703bSJeremy L Thompson 6406e536b99SJeremy L Thompson CeedCheck(vec->num_readers > 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 6416e536b99SJeremy L Thompson "Cannot restore CeedVector array read access, access was not granted"); 64275a19770SJeremy L Thompson vec->num_readers--; 6431203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 6441203703bSJeremy L Thompson if (length > 0 && vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec)); 6450436c2adSjeremylt *array = NULL; 646e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6470436c2adSjeremylt } 6480436c2adSjeremylt 6490436c2adSjeremylt /** 650ca94c3ddSJeremy L Thompson @brief Get the norm of a `CeedVector`. 651171f8ca9Sjeremylt 652ca94c3ddSJeremy L Thompson Note: This operation is local to the `CeedVector`. 653ca94c3ddSJeremy 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. 654547d9b97Sjeremylt 655ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve maximum value 656ea61e9acSJeremy L Thompson @param[in] norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 657547d9b97Sjeremylt @param[out] norm Variable to store norm value 658547d9b97Sjeremylt 659547d9b97Sjeremylt @return An error code: 0 - success, otherwise - failure 660547d9b97Sjeremylt 6617a982d89SJeremy L. Thompson @ref User 662547d9b97Sjeremylt **/ 663d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) { 6649c774eddSJeremy L Thompson bool has_valid_array = true; 6651203703bSJeremy L Thompson CeedSize length; 6661203703bSJeremy L Thompson 6672b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 6686e536b99SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 6692b730f8bSJeremy L Thompson "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6709c774eddSJeremy L Thompson 6711203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 6721203703bSJeremy L Thompson if (length == 0) { 673b0976d5aSZach Atkins *norm = 0; 674b0976d5aSZach Atkins return CEED_ERROR_SUCCESS; 675b0976d5aSZach Atkins } 676b0976d5aSZach Atkins 677547d9b97Sjeremylt // Backend impl for GPU, if added 678547d9b97Sjeremylt if (vec->Norm) { 6792b730f8bSJeremy L Thompson CeedCall(vec->Norm(vec, norm_type, norm)); 680e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 681547d9b97Sjeremylt } 682547d9b97Sjeremylt 683547d9b97Sjeremylt const CeedScalar *array; 6842b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 685b0976d5aSZach Atkins assert(array); 686547d9b97Sjeremylt 687547d9b97Sjeremylt *norm = 0.; 688d1d35e2fSjeremylt switch (norm_type) { 689547d9b97Sjeremylt case CEED_NORM_1: 6901203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 691547d9b97Sjeremylt *norm += fabs(array[i]); 692547d9b97Sjeremylt } 693547d9b97Sjeremylt break; 694547d9b97Sjeremylt case CEED_NORM_2: 6951203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 696547d9b97Sjeremylt *norm += fabs(array[i]) * fabs(array[i]); 697547d9b97Sjeremylt } 698547d9b97Sjeremylt break; 699547d9b97Sjeremylt case CEED_NORM_MAX: 7001203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 701d1d35e2fSjeremylt const CeedScalar abs_v_i = fabs(array[i]); 702d1d35e2fSjeremylt *norm = *norm > abs_v_i ? *norm : abs_v_i; 703547d9b97Sjeremylt } 704547d9b97Sjeremylt } 7052b730f8bSJeremy L Thompson if (norm_type == CEED_NORM_2) *norm = sqrt(*norm); 706547d9b97Sjeremylt 7072b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 708e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 709547d9b97Sjeremylt } 710547d9b97Sjeremylt 711547d9b97Sjeremylt /** 712ca94c3ddSJeremy L Thompson @brief Compute `x = alpha x` 713e0dd3b27Sjeremylt 714ca94c3ddSJeremy L Thompson @param[in,out] x `CeedVector` for scaling 71596b902e2Sjeremylt @param[in] alpha scaling factor 716e0dd3b27Sjeremylt 717e0dd3b27Sjeremylt @return An error code: 0 - success, otherwise - failure 718e0dd3b27Sjeremylt 719e0dd3b27Sjeremylt @ref User 720e0dd3b27Sjeremylt **/ 721e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) { 7229c774eddSJeremy L Thompson bool has_valid_array = true; 7231203703bSJeremy L Thompson CeedSize length; 7241203703bSJeremy L Thompson CeedScalar *x_array = NULL; 7251c66c397SJeremy L Thompson 7262b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array)); 7276e536b99SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(x), CEED_ERROR_BACKEND, 7282b730f8bSJeremy L Thompson "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7299c774eddSJeremy L Thompson 730b0976d5aSZach Atkins // Return early for empty vector 7311203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length)); 7321203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 733b0976d5aSZach Atkins 734e0dd3b27Sjeremylt // Backend implementation 7352b730f8bSJeremy L Thompson if (x->Scale) return x->Scale(x, alpha); 736e0dd3b27Sjeremylt 737e0dd3b27Sjeremylt // Default implementation 738567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(x, CEED_MEM_HOST, &x_array)); 739b0976d5aSZach Atkins assert(x_array); 7401203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha; 7412b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(x, &x_array)); 742e0dd3b27Sjeremylt return CEED_ERROR_SUCCESS; 743e0dd3b27Sjeremylt } 744e0dd3b27Sjeremylt 745e0dd3b27Sjeremylt /** 746ca94c3ddSJeremy L Thompson @brief Compute `y = alpha x + y` 7470f7fd0f8Sjeremylt 748ca94c3ddSJeremy L Thompson @param[in,out] y target `CeedVector` for sum 74996b902e2Sjeremylt @param[in] alpha scaling factor 750ca94c3ddSJeremy L Thompson @param[in] x second `CeedVector`, must be different than ``y` 7510f7fd0f8Sjeremylt 7520f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 7530f7fd0f8Sjeremylt 7540f7fd0f8Sjeremylt @ref User 7550f7fd0f8Sjeremylt **/ 7560f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { 7571203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 7581203703bSJeremy L Thompson CeedSize length_x, length_y; 75973380422SJeremy L Thompson CeedScalar *y_array = NULL; 76073380422SJeremy L Thompson CeedScalar const *x_array = NULL; 7610f7fd0f8Sjeremylt 7621203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 7631203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 7649bc66399SJeremy L Thompson CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, 7653f08121cSJeremy L Thompson "Cannot add vector of different lengths." 7663f08121cSJeremy L Thompson " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, 7673f08121cSJeremy L Thompson length_x, length_y); 7689bc66399SJeremy L Thompson CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); 7690f7fd0f8Sjeremylt 7702b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 7719bc66399SJeremy L Thompson CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 7726574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7732b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 7749bc66399SJeremy L Thompson CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 7756574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7769c774eddSJeremy L Thompson 7779bc66399SJeremy L Thompson { 7789bc66399SJeremy L Thompson Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y; 7799bc66399SJeremy L Thompson 7809bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed_y)); 7819bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(x, &ceed_x)); 7829bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); 7839bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); 7849bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_x)); 7859bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_y)); 7869bc66399SJeremy L Thompson CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE, 7879bc66399SJeremy L Thompson "Vectors x and y must be created by the same Ceed context"); 7889bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_x)); 7899bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_y)); 7909bc66399SJeremy L Thompson } 7912d04630dSjeremylt 792b0976d5aSZach Atkins // Return early for empty vectors 7931203703bSJeremy L Thompson if (length_y == 0) return CEED_ERROR_SUCCESS; 794b0976d5aSZach Atkins 7950f7fd0f8Sjeremylt // Backend implementation 796eaf62fffSJeremy L Thompson if (y->AXPY) { 7972b730f8bSJeremy L Thompson CeedCall(y->AXPY(y, alpha, x)); 798eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 799eaf62fffSJeremy L Thompson } 8000f7fd0f8Sjeremylt 8010f7fd0f8Sjeremylt // Default implementation 802567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 8032b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 8040f7fd0f8Sjeremylt 8052b730f8bSJeremy L Thompson assert(x_array); 8062b730f8bSJeremy L Thompson assert(y_array); 80773380422SJeremy L Thompson 8081203703bSJeremy L Thompson for (CeedSize i = 0; i < length_y; i++) y_array[i] += alpha * x_array[i]; 8090f7fd0f8Sjeremylt 8102b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(y, &y_array)); 8112b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 8120f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 8130f7fd0f8Sjeremylt } 8140f7fd0f8Sjeremylt 8150f7fd0f8Sjeremylt /** 816ca94c3ddSJeremy L Thompson @brief Compute `y = alpha x + beta y` 8175fb68f37SKaren (Ren) Stengel 818ca94c3ddSJeremy L Thompson @param[in,out] y target `CeedVector` for sum 8195fb68f37SKaren (Ren) Stengel @param[in] alpha first scaling factor 8205fb68f37SKaren (Ren) Stengel @param[in] beta second scaling factor 821ca94c3ddSJeremy L Thompson @param[in] x second `CeedVector`, must be different than `y` 8225fb68f37SKaren (Ren) Stengel 8235fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 8245fb68f37SKaren (Ren) Stengel 8255fb68f37SKaren (Ren) Stengel @ref User 8265fb68f37SKaren (Ren) Stengel **/ 8275fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 8281203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 8291203703bSJeremy L Thompson CeedSize length_x, length_y; 8305fb68f37SKaren (Ren) Stengel CeedScalar *y_array = NULL; 8315fb68f37SKaren (Ren) Stengel CeedScalar const *x_array = NULL; 8321203703bSJeremy L Thompson 8331203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 8341203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 8359bc66399SJeremy L Thompson CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, 8363f08121cSJeremy L Thompson "Cannot add vector of different lengths." 8373f08121cSJeremy L Thompson " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, 8383f08121cSJeremy L Thompson length_x, length_y); 8399bc66399SJeremy L Thompson CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY"); 8405fb68f37SKaren (Ren) Stengel 8415fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 8429bc66399SJeremy L Thompson CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 8436574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8445fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 8459bc66399SJeremy L Thompson CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 8466574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8475fb68f37SKaren (Ren) Stengel 8489bc66399SJeremy L Thompson { 8499bc66399SJeremy L Thompson Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y; 8509bc66399SJeremy L Thompson 8519bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed_y)); 8529bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(x, &ceed_x)); 8539bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); 8549bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); 8559bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_x)); 8569bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_y)); 8579bc66399SJeremy L Thompson CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE, 8589bc66399SJeremy L Thompson "Vectors x and y must be created by the same Ceed context"); 8599bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_x)); 8609bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_y)); 8619bc66399SJeremy L Thompson } 8625fb68f37SKaren (Ren) Stengel 863b0976d5aSZach Atkins // Return early for empty vectors 8641203703bSJeremy L Thompson if (length_y == 0) return CEED_ERROR_SUCCESS; 865b0976d5aSZach Atkins 8665fb68f37SKaren (Ren) Stengel // Backend implementation 8675fb68f37SKaren (Ren) Stengel if (y->AXPBY) { 8685fb68f37SKaren (Ren) Stengel CeedCall(y->AXPBY(y, alpha, beta, x)); 8695fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 8705fb68f37SKaren (Ren) Stengel } 8715fb68f37SKaren (Ren) Stengel 8725fb68f37SKaren (Ren) Stengel // Default implementation 8735fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 8745fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 8755fb68f37SKaren (Ren) Stengel 8765fb68f37SKaren (Ren) Stengel assert(x_array); 8775fb68f37SKaren (Ren) Stengel assert(y_array); 8785fb68f37SKaren (Ren) Stengel 8791203703bSJeremy L Thompson for (CeedSize i = 0; i < length_y; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i]; 8805fb68f37SKaren (Ren) Stengel 8815fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(y, &y_array)); 8825fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 8835fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 8845fb68f37SKaren (Ren) Stengel } 8855fb68f37SKaren (Ren) Stengel 8865fb68f37SKaren (Ren) Stengel /** 887ca94c3ddSJeremy L Thompson @brief Compute the pointwise multiplication \f$w = x .* y\f$. 8884385fb7fSSebastian Grimberg 889ca94c3ddSJeremy L Thompson Any subset of `x`, `y`, and `w` may be the same `CeedVector`. 8900f7fd0f8Sjeremylt 891ca94c3ddSJeremy L Thompson @param[out] w target `CeedVector` for the product 892ca94c3ddSJeremy L Thompson @param[in] x first `CeedVector` for product 893ca94c3ddSJeremy L Thompson @param[in] y second `CeedVector` for the product 8940f7fd0f8Sjeremylt 8950f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 8960f7fd0f8Sjeremylt 8970f7fd0f8Sjeremylt @ref User 8980f7fd0f8Sjeremylt **/ 8990f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { 9001203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 90173380422SJeremy L Thompson CeedScalar *w_array = NULL; 90273380422SJeremy L Thompson CeedScalar const *x_array = NULL, *y_array = NULL; 9031203703bSJeremy L Thompson CeedSize length_w, length_x, length_y; 9040f7fd0f8Sjeremylt 9051203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(w, &length_w)); 9061203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 9071203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 9089bc66399SJeremy L Thompson CeedCheck(length_x >= length_w && length_y >= length_w, CeedVectorReturnCeed(w), CEED_ERROR_UNSUPPORTED, 909ccad5cb9SJeremy L Thompson "Cannot pointwise multiply vectors of incompatible lengths." 910ccad5cb9SJeremy L Thompson " w length: %" CeedSize_FMT " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, 911ccad5cb9SJeremy L Thompson length_w, length_x, length_y); 9120f7fd0f8Sjeremylt 9139bc66399SJeremy L Thompson { 9149bc66399SJeremy L Thompson Ceed ceed_w, ceed_x, ceed_y, ceed_parent_w, ceed_parent_x, ceed_parent_y; 9159bc66399SJeremy L Thompson 9169bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(w, &ceed_w)); 9179bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(x, &ceed_x)); 9189bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed_y)); 9199bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_w, &ceed_parent_w)); 9209bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); 9219bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); 9229bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_w)); 9239bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_x)); 9249bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_y)); 9259bc66399SJeremy L Thompson CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, CeedVectorReturnCeed(w), CEED_ERROR_INCOMPATIBLE, 9266574a04fSJeremy L Thompson "Vectors w, x, and y must be created by the same Ceed context"); 9279bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_w)); 9289bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_x)); 9299bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_y)); 9309bc66399SJeremy L Thompson } 9312d04630dSjeremylt 9322b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 9339bc66399SJeremy L Thompson CeedCheck(has_valid_array_x, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND, 9346574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9352b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 9369bc66399SJeremy L Thompson CeedCheck(has_valid_array_y, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND, 9376574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9389c774eddSJeremy L Thompson 939b0976d5aSZach Atkins // Return early for empty vectors 9401203703bSJeremy L Thompson if (length_w == 0) return CEED_ERROR_SUCCESS; 941b0976d5aSZach Atkins 9420f7fd0f8Sjeremylt // Backend implementation 943eaf62fffSJeremy L Thompson if (w->PointwiseMult) { 9442b730f8bSJeremy L Thompson CeedCall(w->PointwiseMult(w, x, y)); 945eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 946eaf62fffSJeremy L Thompson } 9470f7fd0f8Sjeremylt 9480f7fd0f8Sjeremylt // Default implementation 949b0976d5aSZach Atkins if (x == w || y == w) { 950b0976d5aSZach Atkins CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 951b0976d5aSZach Atkins } else { 952b0976d5aSZach Atkins CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array)); 953b0976d5aSZach Atkins } 9540f7fd0f8Sjeremylt if (x != w) { 9552b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 9560f7fd0f8Sjeremylt } else { 9570f7fd0f8Sjeremylt x_array = w_array; 9580f7fd0f8Sjeremylt } 9590f7fd0f8Sjeremylt if (y != w && y != x) { 9602b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array)); 961c6d796c6SJeremy L Thompson } else if (y == x) { 9620f7fd0f8Sjeremylt y_array = x_array; 963b0976d5aSZach Atkins } else if (y == w) { 964c6d796c6SJeremy L Thompson y_array = w_array; 9650f7fd0f8Sjeremylt } 9660f7fd0f8Sjeremylt 9672b730f8bSJeremy L Thompson assert(w_array); 9682b730f8bSJeremy L Thompson assert(x_array); 9692b730f8bSJeremy L Thompson assert(y_array); 97073380422SJeremy L Thompson 9711203703bSJeremy L Thompson for (CeedSize i = 0; i < length_w; i++) w_array[i] = x_array[i] * y_array[i]; 9720f7fd0f8Sjeremylt 9732b730f8bSJeremy L Thompson if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array)); 9742b730f8bSJeremy L Thompson if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 9752b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(w, &w_array)); 9760f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 9770f7fd0f8Sjeremylt } 9780f7fd0f8Sjeremylt 9790f7fd0f8Sjeremylt /** 980ca94c3ddSJeremy L Thompson @brief Take the reciprocal of a `CeedVector`. 981d99fa3c5SJeremy L Thompson 982ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to take reciprocal 983d99fa3c5SJeremy L Thompson 984d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 985d99fa3c5SJeremy L Thompson 986d99fa3c5SJeremy L Thompson @ref User 987d99fa3c5SJeremy L Thompson **/ 988d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) { 9899c774eddSJeremy L Thompson bool has_valid_array = true; 9901203703bSJeremy L Thompson CeedSize length; 9911203703bSJeremy L Thompson CeedScalar *array; 9922b730f8bSJeremy L Thompson 9931c66c397SJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 9949bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 9952b730f8bSJeremy L Thompson "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9969c774eddSJeremy L Thompson 997d99fa3c5SJeremy L Thompson // Check if vector data set 9989bc66399SJeremy L Thompson CeedCheck(vec->state > 0, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); 999d99fa3c5SJeremy L Thompson 1000b0976d5aSZach Atkins // Return early for empty vector 10011203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 10021203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 1003b0976d5aSZach Atkins 1004d99fa3c5SJeremy L Thompson // Backend impl for GPU, if added 1005d99fa3c5SJeremy L Thompson if (vec->Reciprocal) { 10062b730f8bSJeremy L Thompson CeedCall(vec->Reciprocal(vec)); 1007e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1008d99fa3c5SJeremy L Thompson } 1009d99fa3c5SJeremy L Thompson 1010567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array)); 10111203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 10122b730f8bSJeremy L Thompson if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i]; 10132b730f8bSJeremy L Thompson } 1014d99fa3c5SJeremy L Thompson 10152b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 1016e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1017d99fa3c5SJeremy L Thompson } 1018d99fa3c5SJeremy L Thompson 1019d99fa3c5SJeremy L Thompson /** 10204c789ea2SJeremy L Thompson @brief Set the number of tabs to indent for @ref CeedVectorView() output 10214c789ea2SJeremy L Thompson 10224c789ea2SJeremy L Thompson @param[in] vec `CeedVector` to set the number of view tabs 10234c789ea2SJeremy L Thompson @param[in] num_tabs Number of view tabs to set 10244c789ea2SJeremy L Thompson 10254c789ea2SJeremy L Thompson @return Error code: 0 - success, otherwise - failure 10264c789ea2SJeremy L Thompson 10274c789ea2SJeremy L Thompson @ref User 10284c789ea2SJeremy L Thompson **/ 10294c789ea2SJeremy L Thompson int CeedVectorSetNumViewTabs(CeedVector vec, CeedInt num_tabs) { 10304c789ea2SJeremy L Thompson CeedCheck(num_tabs >= 0, CeedVectorReturnCeed(vec), CEED_ERROR_MINOR, "Number of view tabs must be non-negative"); 10314c789ea2SJeremy L Thompson vec->num_tabs = num_tabs; 10324c789ea2SJeremy L Thompson return CEED_ERROR_SUCCESS; 10334c789ea2SJeremy L Thompson } 10344c789ea2SJeremy L Thompson 10354c789ea2SJeremy L Thompson /** 1036690992b2SZach Atkins @brief Get the number of tabs to indent for @ref CeedVectorView() output 1037690992b2SZach Atkins 1038690992b2SZach Atkins @param[in] vec `CeedVector` to get the number of view tabs 1039690992b2SZach Atkins @param[out] num_tabs Number of view tabs 1040690992b2SZach Atkins 1041690992b2SZach Atkins @return Error code: 0 - success, otherwise - failure 1042690992b2SZach Atkins 1043690992b2SZach Atkins @ref User 1044690992b2SZach Atkins **/ 1045690992b2SZach Atkins int CeedVectorGetNumViewTabs(CeedVector vec, CeedInt *num_tabs) { 1046690992b2SZach Atkins *num_tabs = vec->num_tabs; 1047690992b2SZach Atkins return CEED_ERROR_SUCCESS; 1048690992b2SZach Atkins } 1049690992b2SZach Atkins 1050690992b2SZach Atkins /** 1051ca94c3ddSJeremy L Thompson @brief View a `CeedVector` 10520436c2adSjeremylt 1053acb2c48cSJeremy L Thompson Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`. 1054ca94c3ddSJeremy L Thompson Any portion of the provided range that is outside the range of valid indices for the `CeedVector` will be ignored. 1055acb2c48cSJeremy L Thompson 1056ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to view 1057f52f9f6cSJeremy L Thompson @param[in] start Index of first `CeedVector` entry to view in the range `[start, stop)` 1058f52f9f6cSJeremy L Thompson @param[in] stop One past the last element to view in the range, or `-1` for `length` 1059ca94c3ddSJeremy L Thompson @param[in] step Step between `CeedVector` entries to view 1060acb2c48cSJeremy L Thompson @param[in] fp_fmt Printing format 1061acb2c48cSJeremy L Thompson @param[in] stream Filestream to write to 1062acb2c48cSJeremy L Thompson 1063acb2c48cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1064acb2c48cSJeremy L Thompson 1065acb2c48cSJeremy L Thompson @ref User 1066acb2c48cSJeremy L Thompson **/ 1067acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) { 1068acb2c48cSJeremy L Thompson char fmt[1024]; 10694c789ea2SJeremy L Thompson char *tabs = NULL; 10701203703bSJeremy L Thompson CeedSize length; 10711203703bSJeremy L Thompson const CeedScalar *x; 1072acb2c48cSJeremy L Thompson 10736e536b99SJeremy L Thompson CeedCheck(step != 0, CeedVectorReturnCeed(vec), CEED_ERROR_MINOR, "View range 'step' must be nonzero"); 1074acb2c48cSJeremy L Thompson 10754c789ea2SJeremy L Thompson { 10764c789ea2SJeremy L Thompson CeedInt num_tabs = 0; 10774c789ea2SJeremy L Thompson 10784c789ea2SJeremy L Thompson CeedCall(CeedVectorGetNumViewTabs(vec, &num_tabs)); 10794c789ea2SJeremy L Thompson CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs)); 10804c789ea2SJeremy L Thompson for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' '; 10814c789ea2SJeremy L Thompson } 10824c789ea2SJeremy L Thompson 10831203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 10844c789ea2SJeremy L Thompson fprintf(stream, "%sCeedVector length %" CeedSize_FMT "\n", tabs, length); 10851203703bSJeremy L Thompson if (start != 0 || stop != length || step != 1) { 10864c789ea2SJeremy L Thompson fprintf(stream, "%s start: %" CeedSize_FMT "\n%s stop: %" CeedSize_FMT "\n%s step: %" CeedInt_FMT "\n", tabs, start, tabs, stop, tabs, step); 1087acb2c48cSJeremy L Thompson } 10881203703bSJeremy L Thompson if (start > length) start = length; 1089f52f9f6cSJeremy L Thompson if (stop == -1 || stop > length) stop = length; 1090acb2c48cSJeremy L Thompson 10914c789ea2SJeremy L Thompson snprintf(fmt, sizeof fmt, "%s %s\n", tabs, fp_fmt ? fp_fmt : "%g"); 1092acb2c48cSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x)); 1093b94338b9SJed Brown for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]); 1094acb2c48cSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &x)); 10954c789ea2SJeremy L Thompson if (stop != length) fprintf(stream, "%s ...\n", tabs); 10964c789ea2SJeremy L Thompson CeedCall(CeedFree(&tabs)); 1097acb2c48cSJeremy L Thompson return CEED_ERROR_SUCCESS; 1098acb2c48cSJeremy L Thompson } 1099acb2c48cSJeremy L Thompson 1100acb2c48cSJeremy L Thompson /** 1101ca94c3ddSJeremy L Thompson @brief View a `CeedVector` 1102acb2c48cSJeremy L Thompson 1103ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to view 1104d1d35e2fSjeremylt @param[in] fp_fmt Printing format 11050a0da059Sjeremylt @param[in] stream Filestream to write to 11060a0da059Sjeremylt 11070436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 11080436c2adSjeremylt 11097a982d89SJeremy L. Thompson @ref User 11100436c2adSjeremylt **/ 1111d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { 11121203703bSJeremy L Thompson CeedSize length; 11131203703bSJeremy L Thompson 11141203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 11151203703bSJeremy L Thompson CeedCall(CeedVectorViewRange(vec, 0, length, 1, fp_fmt, stream)); 1116e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11170436c2adSjeremylt } 11180436c2adSjeremylt 11190436c2adSjeremylt /** 1120ca94c3ddSJeremy L Thompson @brief Get the `Ceed` associated with a `CeedVector` 1121b7c9bbdaSJeremy L Thompson 1122ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 1123ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 1124b7c9bbdaSJeremy L Thompson 1125b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1126b7c9bbdaSJeremy L Thompson 1127b7c9bbdaSJeremy L Thompson @ref Advanced 1128b7c9bbdaSJeremy L Thompson **/ 1129b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 1130*b0f67a9cSJeremy L Thompson CeedCall(CeedObjectGetCeed((CeedObject)vec, ceed)); 1131b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1132b7c9bbdaSJeremy L Thompson } 1133b7c9bbdaSJeremy L Thompson 1134b7c9bbdaSJeremy L Thompson /** 11356e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedVector` 11366e536b99SJeremy L Thompson 11376e536b99SJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 11386e536b99SJeremy L Thompson 11396e536b99SJeremy L Thompson @return `Ceed` associated with the `vec` 11406e536b99SJeremy L Thompson 11416e536b99SJeremy L Thompson @ref Advanced 11426e536b99SJeremy L Thompson **/ 1143*b0f67a9cSJeremy L Thompson Ceed CeedVectorReturnCeed(CeedVector vec) { return CeedObjectReturnCeed((CeedObject)vec); } 11446e536b99SJeremy L Thompson 11456e536b99SJeremy L Thompson /** 1146ca94c3ddSJeremy L Thompson @brief Get the length of a `CeedVector` 11470436c2adSjeremylt 1148ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve length 11497a982d89SJeremy L. Thompson @param[out] length Variable to store length 11500436c2adSjeremylt 11510436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 11520436c2adSjeremylt 11537a982d89SJeremy L. Thompson @ref User 11540436c2adSjeremylt **/ 11551f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) { 11567a982d89SJeremy L. Thompson *length = vec->length; 1157e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11580436c2adSjeremylt } 11590436c2adSjeremylt 11600436c2adSjeremylt /** 1161ca94c3ddSJeremy L Thompson @brief Destroy a `CeedVector` 11620436c2adSjeremylt 1163ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to destroy 11640436c2adSjeremylt 11650436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 11660436c2adSjeremylt 11677a982d89SJeremy L. Thompson @ref User 11680436c2adSjeremylt **/ 11690436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) { 1170*b0f67a9cSJeremy L Thompson if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || CeedObjectDereference((CeedObject)*vec) > 0) { 1171ad6481ceSJeremy L Thompson *vec = NULL; 1172ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1173ad6481ceSJeremy L Thompson } 1174*b0f67a9cSJeremy L Thompson CeedCheck((*vec)->state % 2 == 0, CeedVectorReturnCeed(*vec), CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use"); 1175*b0f67a9cSJeremy L Thompson CeedCheck((*vec)->num_readers == 0, CeedVectorReturnCeed(*vec), CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access"); 11760436c2adSjeremylt 11772b730f8bSJeremy L Thompson if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec)); 1178*b0f67a9cSJeremy L Thompson CeedCall(CeedObjectDestroy(&(*vec)->obj)); 11792b730f8bSJeremy L Thompson CeedCall(CeedFree(vec)); 1180e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11810436c2adSjeremylt } 11820436c2adSjeremylt 11830436c2adSjeremylt /// @} 1184