1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, 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 /** 43*4c789ea2SJeremy L Thompson @brief Get the number of tabs to indent for @ref CeedVectorView() output 44*4c789ea2SJeremy L Thompson 45*4c789ea2SJeremy L Thompson @param[in] vec `CeedVector` to get the number of view tabs 46*4c789ea2SJeremy L Thompson @param[out] num_tabs Number of view tabs 47*4c789ea2SJeremy L Thompson 48*4c789ea2SJeremy L Thompson @return Error code: 0 - success, otherwise - failure 49*4c789ea2SJeremy L Thompson 50*4c789ea2SJeremy L Thompson @ref Backend 51*4c789ea2SJeremy L Thompson **/ 52*4c789ea2SJeremy L Thompson int CeedVectorGetNumViewTabs(CeedVector vec, CeedInt *num_tabs) { 53*4c789ea2SJeremy L Thompson *num_tabs = vec->num_tabs; 54*4c789ea2SJeremy L Thompson return CEED_ERROR_SUCCESS; 55*4c789ea2SJeremy L Thompson } 56*4c789ea2SJeremy L Thompson 57*4c789ea2SJeremy L Thompson /** 58ca94c3ddSJeremy L Thompson @brief Check for valid data in a `CeedVector` 599c774eddSJeremy L Thompson 60ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to check validity 619c774eddSJeremy L Thompson @param[out] has_valid_array Variable to store validity 629c774eddSJeremy L Thompson 639c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 649c774eddSJeremy L Thompson 659c774eddSJeremy L Thompson @ref Backend 669c774eddSJeremy L Thompson **/ 679c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) { 681203703bSJeremy L Thompson CeedSize length; 691203703bSJeremy L Thompson 706e536b99SJeremy L Thompson CeedCheck(vec->HasValidArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorHasValidArray"); 711203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 721203703bSJeremy L Thompson if (length == 0) { 73b0976d5aSZach Atkins *has_valid_array = true; 74b0976d5aSZach Atkins return CEED_ERROR_SUCCESS; 75b0976d5aSZach Atkins } 762b730f8bSJeremy L Thompson CeedCall(vec->HasValidArray(vec, has_valid_array)); 779c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 789c774eddSJeremy L Thompson } 799c774eddSJeremy L Thompson 809c774eddSJeremy L Thompson /** 81ca94c3ddSJeremy L Thompson @brief Check for borrowed array of a specific @ref CeedMemType in a `CeedVector` 829c774eddSJeremy L Thompson 83ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to check 84ea61e9acSJeremy L Thompson @param[in] mem_type Memory type to check 859c774eddSJeremy L Thompson @param[out] has_borrowed_array_of_type Variable to store result 869c774eddSJeremy L Thompson 879c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 889c774eddSJeremy L Thompson 899c774eddSJeremy L Thompson @ref Backend 909c774eddSJeremy L Thompson **/ 912b730f8bSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 926e536b99SJeremy L Thompson CeedCheck(vec->HasBorrowedArrayOfType, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, 936e536b99SJeremy L Thompson "Backend does not support CeedVectorHasBorrowedArrayOfType"); 942b730f8bSJeremy L Thompson CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type)); 959c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 969c774eddSJeremy L Thompson } 979c774eddSJeremy L Thompson 989c774eddSJeremy L Thompson /** 99ca94c3ddSJeremy L Thompson @brief Get the state of a `CeedVector` 1007a982d89SJeremy L. Thompson 101ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 1027a982d89SJeremy L. Thompson @param[out] state Variable to store state 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 **/ 1087a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) { 1097a982d89SJeremy L. Thompson *state = vec->state; 110e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1117a982d89SJeremy L. Thompson } 1127a982d89SJeremy L. Thompson 1137a982d89SJeremy L. Thompson /** 114ca94c3ddSJeremy L Thompson @brief Get the backend data of a `CeedVector` 1157a982d89SJeremy L. Thompson 116ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 1177a982d89SJeremy L. Thompson @param[out] data Variable to store data 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 CeedVectorGetData(CeedVector vec, void *data) { 124777ff853SJeremy L Thompson *(void **)data = vec->data; 125e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1267a982d89SJeremy L. Thompson } 1277a982d89SJeremy L. Thompson 1287a982d89SJeremy L. Thompson /** 129ca94c3ddSJeremy L Thompson @brief Set the backend data of a `CeedVector` 1307a982d89SJeremy L. Thompson 131ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to retrieve state 132ea61e9acSJeremy L Thompson @param[in] data Data to set 1337a982d89SJeremy L. Thompson 1347a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1357a982d89SJeremy L. Thompson 1367a982d89SJeremy L. Thompson @ref Backend 1377a982d89SJeremy L. Thompson **/ 138777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) { 139777ff853SJeremy L Thompson vec->data = data; 140e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1417a982d89SJeremy L. Thompson } 1427a982d89SJeremy L. Thompson 14334359f16Sjeremylt /** 144ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedVector` 14534359f16Sjeremylt 146ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to increment the reference counter 14734359f16Sjeremylt 14834359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 14934359f16Sjeremylt 15034359f16Sjeremylt @ref Backend 15134359f16Sjeremylt **/ 1529560d06aSjeremylt int CeedVectorReference(CeedVector vec) { 15334359f16Sjeremylt vec->ref_count++; 15434359f16Sjeremylt return CEED_ERROR_SUCCESS; 15534359f16Sjeremylt } 15634359f16Sjeremylt 1577a982d89SJeremy L. Thompson /// @} 1587a982d89SJeremy L. Thompson 1597a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1607a982d89SJeremy L. Thompson /// CeedVector Public API 1617a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1627a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 1630436c2adSjeremylt /// @{ 1640436c2adSjeremylt 1650436c2adSjeremylt /** 166ca94c3ddSJeremy L Thompson @brief Create a `CeedVector` of the specified length (does not allocate memory) 1670436c2adSjeremylt 168ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedVector` 169ea61e9acSJeremy L Thompson @param[in] length Length of vector 170ca94c3ddSJeremy L Thompson @param[out] vec Address of the variable where the newly created `CeedVector` will be stored 1710436c2adSjeremylt 1720436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 1730436c2adSjeremylt 1747a982d89SJeremy L. Thompson @ref User 1750436c2adSjeremylt **/ 1761f9221feSJeremy L Thompson int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) { 177a5ef892dSJames Wright CeedCheck(length >= 0, ceed, CEED_ERROR_UNSUPPORTED, "CeedVector must have length >= 0, recieved %" CeedSize_FMT, length); 1780436c2adSjeremylt if (!ceed->VectorCreate) { 1790436c2adSjeremylt Ceed delegate; 1806574a04fSJeremy L Thompson 1812b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector")); 1821ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement VectorCreate"); 1832b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(delegate, length, vec)); 1849bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 185e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1860436c2adSjeremylt } 1870436c2adSjeremylt 1882b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, vec)); 189db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*vec)->ceed)); 190d1d35e2fSjeremylt (*vec)->ref_count = 1; 1910436c2adSjeremylt (*vec)->length = length; 1920436c2adSjeremylt (*vec)->state = 0; 1932b730f8bSJeremy L Thompson CeedCall(ceed->VectorCreate(length, *vec)); 194e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1950436c2adSjeremylt } 1960436c2adSjeremylt 1970436c2adSjeremylt /** 198ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedVector`. 1994385fb7fSSebastian Grimberg 200ca94c3ddSJeremy L Thompson Both pointers should be destroyed with @ref CeedVectorDestroy(). 201512bb800SJeremy L Thompson 202ca94c3ddSJeremy 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`. 203ca94c3ddSJeremy L Thompson This `CeedVector` will be destroyed if `*vec_copy` is the only reference to this `CeedVector`. 2049560d06aSjeremylt 205ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to copy reference to 206ea61e9acSJeremy L Thompson @param[in,out] vec_copy Variable to store copied reference 2079560d06aSjeremylt 2089560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 2099560d06aSjeremylt 2109560d06aSjeremylt @ref User 2119560d06aSjeremylt **/ 2129560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) { 213393ac2cdSJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorReference(vec)); 2142b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(vec_copy)); 2159560d06aSjeremylt *vec_copy = vec; 2169560d06aSjeremylt return CEED_ERROR_SUCCESS; 2179560d06aSjeremylt } 2189560d06aSjeremylt 2199560d06aSjeremylt /** 220ca94c3ddSJeremy L Thompson @brief Copy a `CeedVector` into a different `CeedVector`. 2214385fb7fSSebastian Grimberg 222ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to copy 223e706ae07SJeremy L Thompson @param[in,out] vec_copy `CeedVector` to copy array into 2245fb68f37SKaren (Ren) Stengel 2255fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 2265fb68f37SKaren (Ren) Stengel 2275fb68f37SKaren (Ren) Stengel @ref User 2285fb68f37SKaren (Ren) Stengel **/ 2295fb68f37SKaren (Ren) Stengel int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) { 2305fb68f37SKaren (Ren) Stengel CeedMemType mem_type, mem_type_copy; 2315fb68f37SKaren (Ren) Stengel CeedScalar *array; 2325fb68f37SKaren (Ren) Stengel 2339bc66399SJeremy L Thompson // Get the preferred memory types 2349bc66399SJeremy L Thompson { 2359bc66399SJeremy L Thompson Ceed ceed; 2369bc66399SJeremy L Thompson 237d77c2f5dSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 238d77c2f5dSJeremy L Thompson CeedCall(CeedGetPreferredMemType(ceed, &mem_type)); 2399bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 2405fb68f37SKaren (Ren) Stengel 241d77c2f5dSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec_copy, &ceed)); 242d77c2f5dSJeremy L Thompson CeedCall(CeedGetPreferredMemType(ceed, &mem_type_copy)); 2439bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 2449bc66399SJeremy L Thompson } 2455fb68f37SKaren (Ren) Stengel 2465fb68f37SKaren (Ren) Stengel // Check that both have same memory type 2475fb68f37SKaren (Ren) Stengel if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST; 2485fb68f37SKaren (Ren) Stengel 249e706ae07SJeremy L Thompson // Check compatible lengths 250e706ae07SJeremy L Thompson { 251e706ae07SJeremy L Thompson CeedSize length_vec, length_copy; 252e706ae07SJeremy L Thompson 253e706ae07SJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length_vec)); 254e706ae07SJeremy L Thompson CeedCall(CeedVectorGetLength(vec_copy, &length_copy)); 2559bc66399SJeremy L Thompson CeedCheck(length_vec == length_copy, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPATIBLE, "CeedVectors must have the same length to copy"); 256e706ae07SJeremy L Thompson } 257e706ae07SJeremy L Thompson 2585fb68f37SKaren (Ren) Stengel // Copy the values from vec to vec_copy 2595fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(vec, mem_type, &array)); 2605fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array)); 2615fb68f37SKaren (Ren) Stengel 2625fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(vec, &array)); 2635fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 2645fb68f37SKaren (Ren) Stengel } 2655fb68f37SKaren (Ren) Stengel 2665fb68f37SKaren (Ren) Stengel /** 2670b8f3c4eSJeremy L Thompson @brief Copy a strided portion of `CeedVector` contents into a different `CeedVector` 2680b8f3c4eSJeremy L Thompson 2690b8f3c4eSJeremy L Thompson @param[in] vec `CeedVector` to copy 270832a6d73SJeremy L Thompson @param[in] start First index to copy in the range `[start, stop)` 271832a6d73SJeremy L Thompson @param[in] stop One past the last element to copy in the range, or `-1` for `length` 2720b8f3c4eSJeremy L Thompson @param[in] step Stride between indices to copy 2730b8f3c4eSJeremy L Thompson @param[in,out] vec_copy `CeedVector` to copy values to 2740b8f3c4eSJeremy L Thompson 2750b8f3c4eSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2760b8f3c4eSJeremy L Thompson 2770b8f3c4eSJeremy L Thompson @ref User 2780b8f3c4eSJeremy L Thompson **/ 279832a6d73SJeremy L Thompson int CeedVectorCopyStrided(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedVector vec_copy) { 2800b8f3c4eSJeremy L Thompson CeedSize length; 281bb03490dSJeremy L Thompson const CeedScalar *array = NULL; 282bb03490dSJeremy L Thompson CeedScalar *array_copy = NULL; 2830b8f3c4eSJeremy L Thompson 284832a6d73SJeremy L Thompson // Check length 2850b8f3c4eSJeremy L Thompson { 2860b8f3c4eSJeremy L Thompson CeedSize length_vec, length_copy; 2870b8f3c4eSJeremy L Thompson 2880b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length_vec)); 2890b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetLength(vec_copy, &length_copy)); 290bb03490dSJeremy L Thompson if (length_vec <= 0 || length_copy <= 0) return CEED_ERROR_SUCCESS; 291a7efc114SJeremy L Thompson length = length_vec < length_copy ? length_vec : length_copy; 2920b8f3c4eSJeremy L Thompson } 293832a6d73SJeremy L Thompson CeedCheck(stop >= -1 && stop <= length, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 294832a6d73SJeremy L Thompson "Invalid value for stop %" CeedSize_FMT ", must be in the range [-1, length]", stop); 295832a6d73SJeremy L Thompson CeedCheck(start >= 0 && start <= length && (start <= stop || stop == -1), CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 296832a6d73SJeremy L Thompson "Invalid value for start %" CeedSize_FMT ", must be in the range [0, stop]", start); 297832a6d73SJeremy L Thompson 298832a6d73SJeremy L Thompson // Backend version 299832a6d73SJeremy L Thompson if (vec->CopyStrided && vec_copy->CopyStrided) { 300832a6d73SJeremy L Thompson CeedCall(vec->CopyStrided(vec, start, stop, step, vec_copy)); 301832a6d73SJeremy L Thompson vec_copy->state += 2; 302832a6d73SJeremy L Thompson return CEED_ERROR_SUCCESS; 303832a6d73SJeremy L Thompson } 3040b8f3c4eSJeremy L Thompson 3050b8f3c4eSJeremy L Thompson // Copy 3060b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 3070b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, &array_copy)); 308832a6d73SJeremy L Thompson if (stop == -1) stop = length; 309832a6d73SJeremy L Thompson for (CeedSize i = start; i < stop; i += step) array_copy[i] = array[i]; 3100b8f3c4eSJeremy L Thompson 3110b8f3c4eSJeremy L Thompson // Cleanup 3120b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 3130b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec_copy, &array_copy)); 3140b8f3c4eSJeremy L Thompson return CEED_ERROR_SUCCESS; 3150b8f3c4eSJeremy L Thompson } 3160b8f3c4eSJeremy L Thompson 3170b8f3c4eSJeremy L Thompson /** 318ca94c3ddSJeremy L Thompson @brief Set the array used by a `CeedVector`, freeing any previously allocated array if applicable. 3194385fb7fSSebastian Grimberg 320ca94c3ddSJeremy L Thompson The backend may copy values to a different @ref CeedMemType, such as during @ref CeedOperatorApply(). 3216a6c615bSJeremy L Thompson See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 3220436c2adSjeremylt 323ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 324ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the array being passed 325ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the array 326ca94c3ddSJeremy L Thompson @param[in] array Array to be used, or `NULL` with @ref CEED_COPY_VALUES to have the library allocate 3270436c2adSjeremylt 3280436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3290436c2adSjeremylt 3307a982d89SJeremy L. Thompson @ref User 3310436c2adSjeremylt **/ 3322b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 3331203703bSJeremy L Thompson CeedSize length; 3340436c2adSjeremylt 3359bc66399SJeremy L Thompson CeedCheck(vec->SetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); 3369bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 3379bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 3389bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3391203703bSJeremy L Thompson 3401203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3411203703bSJeremy L Thompson if (length > 0) CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); 3420436c2adSjeremylt vec->state += 2; 343e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3440436c2adSjeremylt } 3450436c2adSjeremylt 3460436c2adSjeremylt /** 347ca94c3ddSJeremy L Thompson @brief Set the `CeedVector` to a constant value 3480436c2adSjeremylt 349ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 3500436c2adSjeremylt @param[in] value Value to be used 3510436c2adSjeremylt 3520436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3530436c2adSjeremylt 3547a982d89SJeremy L. Thompson @ref User 3550436c2adSjeremylt **/ 3560436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 3579bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 3589bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 3599bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3600436c2adSjeremylt 3610436c2adSjeremylt if (vec->SetValue) { 3622b730f8bSJeremy L Thompson CeedCall(vec->SetValue(vec, value)); 3632d4e0605SJeremy L Thompson vec->state += 2; 3640436c2adSjeremylt } else { 3651203703bSJeremy L Thompson CeedSize length; 3660436c2adSjeremylt CeedScalar *array; 3671203703bSJeremy L Thompson 3682b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 3691203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3701203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) array[i] = value; 3712b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 3720436c2adSjeremylt } 373e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3740436c2adSjeremylt } 3750436c2adSjeremylt 3760436c2adSjeremylt /** 3770b8f3c4eSJeremy L Thompson @brief Set a portion of a `CeedVector` to a constant value. 3780b8f3c4eSJeremy L Thompson 3790b8f3c4eSJeremy L Thompson Note: The `CeedVector` must already have valid data set via @ref CeedVectorSetArray() or similar. 3800b8f3c4eSJeremy L Thompson 3810b8f3c4eSJeremy L Thompson @param[in,out] vec `CeedVector` 382ff90b007SJeremy L Thompson @param[in] start First index to set in range `[start, stop)` 38312634730SJeremy L Thompson @param[in] stop One past the last element to set in the range, or `-1` for `length` 3840b8f3c4eSJeremy L Thompson @param[in] step Stride between indices to set 3850b8f3c4eSJeremy L Thompson @param[in] value Value to be used 3860b8f3c4eSJeremy L Thompson 3870b8f3c4eSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3880b8f3c4eSJeremy L Thompson 3890b8f3c4eSJeremy L Thompson @ref User 3900b8f3c4eSJeremy L Thompson **/ 391ff90b007SJeremy L Thompson int CeedVectorSetValueStrided(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedScalar value) { 392b1a610efSJeremy L Thompson CeedSize length; 393b1a610efSJeremy L Thompson 3949bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 3959bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 3969bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 397b1a610efSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3987a747cf1SJeremy L Thompson CeedCheck(stop >= -1 && stop <= length, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 3997a747cf1SJeremy L Thompson "Invalid value for stop %" CeedSize_FMT ", must be in the range [-1, length]", stop); 4000b8f3c4eSJeremy L Thompson 4010b8f3c4eSJeremy L Thompson if (vec->SetValueStrided) { 402ff90b007SJeremy L Thompson CeedCall(vec->SetValueStrided(vec, start, stop, step, value)); 4032d4e0605SJeremy L Thompson vec->state += 2; 4040b8f3c4eSJeremy L Thompson } else { 4050b8f3c4eSJeremy L Thompson CeedScalar *array; 4060b8f3c4eSJeremy L Thompson 407bb03490dSJeremy L Thompson if (length <= 0) return CEED_ERROR_SUCCESS; 408ff90b007SJeremy L Thompson if (stop == -1) stop = length; 409bb03490dSJeremy L Thompson CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array)); 410ff90b007SJeremy L Thompson for (CeedSize i = start; i < stop; i += step) array[i] = value; 4110b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 4120b8f3c4eSJeremy L Thompson } 4130b8f3c4eSJeremy L Thompson return CEED_ERROR_SUCCESS; 4140b8f3c4eSJeremy L Thompson } 4150b8f3c4eSJeremy L Thompson 4160b8f3c4eSJeremy L Thompson /** 417ca94c3ddSJeremy L Thompson @brief Sync the `CeedVector` to a specified `mem_type`. 4184385fb7fSSebastian Grimberg 419ea61e9acSJeremy L Thompson This function is used to force synchronization of arrays set with @ref CeedVectorSetArray(). 420ca94c3ddSJeremy L Thompson If the requested `mem_type` is already synchronized, this function results in a no-op. 4210436c2adSjeremylt 422ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 423ca94c3ddSJeremy L Thompson @param[in] mem_type @ref CeedMemType to be synced 4240436c2adSjeremylt 4250436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4260436c2adSjeremylt 4277a982d89SJeremy L. Thompson @ref User 4280436c2adSjeremylt **/ 429d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { 4301203703bSJeremy L Thompson CeedSize length; 4311203703bSJeremy L Thompson 4326e536b99SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use"); 4330436c2adSjeremylt 434b0976d5aSZach Atkins // Don't sync empty array 4351203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 4361203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 437b0976d5aSZach Atkins 4380436c2adSjeremylt if (vec->SyncArray) { 4392b730f8bSJeremy L Thompson CeedCall(vec->SyncArray(vec, mem_type)); 4400436c2adSjeremylt } else { 4410436c2adSjeremylt const CeedScalar *array; 4421203703bSJeremy L Thompson 4432b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array)); 4442b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 4450436c2adSjeremylt } 446e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4470436c2adSjeremylt } 4480436c2adSjeremylt 4490436c2adSjeremylt /** 450ca94c3ddSJeremy 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`. 4514385fb7fSSebastian Grimberg 4529c774eddSJeremy L Thompson The caller is responsible for managing and freeing the array. 453ea61e9acSJeremy L Thompson This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type. 4546a6c615bSJeremy L Thompson 455ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 456ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to take the array. 457ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 458ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type`, or `NULL` if array pointer is not required 4596a6c615bSJeremy L Thompson 4606a6c615bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4616a6c615bSJeremy L Thompson 4626a6c615bSJeremy L Thompson @ref User 4636a6c615bSJeremy L Thompson **/ 4642b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4651203703bSJeremy L Thompson CeedSize length; 4661c66c397SJeremy L Thompson CeedScalar *temp_array = NULL; 4671c66c397SJeremy L Thompson 4689bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); 4699bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); 4706a6c615bSJeremy L Thompson 4711203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 4721203703bSJeremy L Thompson if (length > 0) { 4731203703bSJeremy L Thompson bool has_borrowed_array_of_type = true, has_valid_array = true; 4741203703bSJeremy L Thompson 4752b730f8bSJeremy L Thompson CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); 4769bc66399SJeremy L Thompson CeedCheck(has_borrowed_array_of_type, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 4779bc66399SJeremy L Thompson "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", CeedMemTypes[mem_type]); 4789c774eddSJeremy L Thompson 4792b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4809bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 4812b730f8bSJeremy L Thompson "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4829c774eddSJeremy L Thompson 4832b730f8bSJeremy L Thompson CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); 48450c643e1SJed Brown } 485d1d35e2fSjeremylt if (array) (*array) = temp_array; 486e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4876a6c615bSJeremy L Thompson } 4886a6c615bSJeremy L Thompson 4896a6c615bSJeremy L Thompson /** 490ca94c3ddSJeremy L Thompson @brief Get read/write access to a `CeedVector` via the specified memory type. 4914385fb7fSSebastian Grimberg 492b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArray(). 4930436c2adSjeremylt 494ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to access 495ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 496ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 497ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 4980436c2adSjeremylt 499ca94c3ddSJeremy L Thompson @note The @ref CeedVectorGetArray() and @ref CeedVectorRestoreArray() functions provide access to array pointers in the desired memory space. 500ca94c3ddSJeremy L Thompson Pairing get/restore allows the `CeedVector` to track access, thus knowing if norms or other operations may need to be recomputed. 5010436c2adSjeremylt 5020436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5030436c2adSjeremylt 5047a982d89SJeremy L. Thompson @ref User 5050436c2adSjeremylt **/ 5062b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 5071203703bSJeremy L Thompson CeedSize length; 5080436c2adSjeremylt 5099bc66399SJeremy L Thompson CeedCheck(vec->GetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); 5109bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 5119bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 5129bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 5131203703bSJeremy L Thompson 5141203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5151203703bSJeremy L Thompson if (length > 0) { 5169c774eddSJeremy L Thompson bool has_valid_array = true; 517b0976d5aSZach Atkins 5182b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5199bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 5202b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5219c774eddSJeremy L Thompson 5222b730f8bSJeremy L Thompson CeedCall(vec->GetArray(vec, mem_type, array)); 523b0976d5aSZach Atkins } else { 524b0976d5aSZach Atkins *array = NULL; 525b0976d5aSZach Atkins } 52628bfd0b7SJeremy L Thompson vec->state++; 527e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5280436c2adSjeremylt } 5290436c2adSjeremylt 5300436c2adSjeremylt /** 531ca94c3ddSJeremy L Thompson @brief Get read-only access to a `CeedVector` via the specified memory type. 5324385fb7fSSebastian Grimberg 533b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArrayRead(). 5340436c2adSjeremylt 535ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to access 536ca94c3ddSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 537ca94c3ddSJeremy L Thompson If the backend uses a different memory type, this will perform a copy (possibly cached). 538ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 5390436c2adSjeremylt 5400436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5410436c2adSjeremylt 5427a982d89SJeremy L. Thompson @ref User 5430436c2adSjeremylt **/ 5442b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 5451203703bSJeremy L Thompson CeedSize length; 5460436c2adSjeremylt 5479bc66399SJeremy L Thompson CeedCheck(vec->GetArrayRead, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); 5489bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 5499bc66399SJeremy L Thompson "Cannot grant CeedVector read-only array access, the access lock is already in use"); 5501203703bSJeremy L Thompson 5511203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5521203703bSJeremy L Thompson if (length > 0) { 5539c774eddSJeremy L Thompson bool has_valid_array = true; 554b0976d5aSZach Atkins 5552b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5569bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 5572b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5589c774eddSJeremy L Thompson 5592b730f8bSJeremy L Thompson CeedCall(vec->GetArrayRead(vec, mem_type, array)); 56050c643e1SJed Brown } else { 56150c643e1SJed Brown *array = NULL; 56250c643e1SJed Brown } 563d1d35e2fSjeremylt vec->num_readers++; 564e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5650436c2adSjeremylt } 5660436c2adSjeremylt 5670436c2adSjeremylt /** 568ca94c3ddSJeremy L Thompson @brief Get write access to a `CeedVector` via the specified memory type. 5694385fb7fSSebastian Grimberg 570ea61e9acSJeremy L Thompson Restore access with @ref CeedVectorRestoreArray(). 571ea61e9acSJeremy L Thompson All old values should be assumed to be invalid. 5729c774eddSJeremy L Thompson 573ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to access 574ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 575ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 5769c774eddSJeremy L Thompson 5779c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5789c774eddSJeremy L Thompson 5799c774eddSJeremy L Thompson @ref User 5809c774eddSJeremy L Thompson **/ 5812b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 5821203703bSJeremy L Thompson CeedSize length; 5839c774eddSJeremy L Thompson 5849bc66399SJeremy L Thompson CeedCheck(vec->GetArrayWrite, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorGetArrayWrite"); 5859bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 5869bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 5879bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 5881203703bSJeremy L Thompson 5891203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5901203703bSJeremy L Thompson if (length > 0) { 5912b730f8bSJeremy L Thompson CeedCall(vec->GetArrayWrite(vec, mem_type, array)); 592b0976d5aSZach Atkins } else { 593b0976d5aSZach Atkins *array = NULL; 594b0976d5aSZach Atkins } 59528bfd0b7SJeremy L Thompson vec->state++; 5969c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 5979c774eddSJeremy L Thompson } 5989c774eddSJeremy L Thompson 5999c774eddSJeremy L Thompson /** 600ea61e9acSJeremy L Thompson @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite() 6010436c2adSjeremylt 602ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to restore 603ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 6040436c2adSjeremylt 6050436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 6060436c2adSjeremylt 6077a982d89SJeremy L. Thompson @ref User 6080436c2adSjeremylt **/ 6090436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 6101203703bSJeremy L Thompson CeedSize length; 6111203703bSJeremy L Thompson 6126e536b99SJeremy L Thompson CeedCheck(vec->state % 2 == 1, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted"); 6131203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 6141203703bSJeremy L Thompson if (length > 0 && vec->RestoreArray) CeedCall(vec->RestoreArray(vec)); 6150436c2adSjeremylt *array = NULL; 61628bfd0b7SJeremy L Thompson vec->state++; 617e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6180436c2adSjeremylt } 6190436c2adSjeremylt 6200436c2adSjeremylt /** 621b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 6220436c2adSjeremylt 623ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to restore 624ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 6250436c2adSjeremylt 6260436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 6270436c2adSjeremylt 6287a982d89SJeremy L. Thompson @ref User 6290436c2adSjeremylt **/ 6300436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 6311203703bSJeremy L Thompson CeedSize length; 6321203703bSJeremy L Thompson 6336e536b99SJeremy L Thompson CeedCheck(vec->num_readers > 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 6346e536b99SJeremy L Thompson "Cannot restore CeedVector array read access, access was not granted"); 63575a19770SJeremy L Thompson vec->num_readers--; 6361203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 6371203703bSJeremy L Thompson if (length > 0 && vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec)); 6380436c2adSjeremylt *array = NULL; 639e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6400436c2adSjeremylt } 6410436c2adSjeremylt 6420436c2adSjeremylt /** 643ca94c3ddSJeremy L Thompson @brief Get the norm of a `CeedVector`. 644171f8ca9Sjeremylt 645ca94c3ddSJeremy L Thompson Note: This operation is local to the `CeedVector`. 646ca94c3ddSJeremy 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. 647547d9b97Sjeremylt 648ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve maximum value 649ea61e9acSJeremy L Thompson @param[in] norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 650547d9b97Sjeremylt @param[out] norm Variable to store norm value 651547d9b97Sjeremylt 652547d9b97Sjeremylt @return An error code: 0 - success, otherwise - failure 653547d9b97Sjeremylt 6547a982d89SJeremy L. Thompson @ref User 655547d9b97Sjeremylt **/ 656d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) { 6579c774eddSJeremy L Thompson bool has_valid_array = true; 6581203703bSJeremy L Thompson CeedSize length; 6591203703bSJeremy L Thompson 6602b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 6616e536b99SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 6622b730f8bSJeremy L Thompson "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6639c774eddSJeremy L Thompson 6641203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 6651203703bSJeremy L Thompson if (length == 0) { 666b0976d5aSZach Atkins *norm = 0; 667b0976d5aSZach Atkins return CEED_ERROR_SUCCESS; 668b0976d5aSZach Atkins } 669b0976d5aSZach Atkins 670547d9b97Sjeremylt // Backend impl for GPU, if added 671547d9b97Sjeremylt if (vec->Norm) { 6722b730f8bSJeremy L Thompson CeedCall(vec->Norm(vec, norm_type, norm)); 673e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 674547d9b97Sjeremylt } 675547d9b97Sjeremylt 676547d9b97Sjeremylt const CeedScalar *array; 6772b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 678b0976d5aSZach Atkins assert(array); 679547d9b97Sjeremylt 680547d9b97Sjeremylt *norm = 0.; 681d1d35e2fSjeremylt switch (norm_type) { 682547d9b97Sjeremylt case CEED_NORM_1: 6831203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 684547d9b97Sjeremylt *norm += fabs(array[i]); 685547d9b97Sjeremylt } 686547d9b97Sjeremylt break; 687547d9b97Sjeremylt case CEED_NORM_2: 6881203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 689547d9b97Sjeremylt *norm += fabs(array[i]) * fabs(array[i]); 690547d9b97Sjeremylt } 691547d9b97Sjeremylt break; 692547d9b97Sjeremylt case CEED_NORM_MAX: 6931203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 694d1d35e2fSjeremylt const CeedScalar abs_v_i = fabs(array[i]); 695d1d35e2fSjeremylt *norm = *norm > abs_v_i ? *norm : abs_v_i; 696547d9b97Sjeremylt } 697547d9b97Sjeremylt } 6982b730f8bSJeremy L Thompson if (norm_type == CEED_NORM_2) *norm = sqrt(*norm); 699547d9b97Sjeremylt 7002b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 701e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 702547d9b97Sjeremylt } 703547d9b97Sjeremylt 704547d9b97Sjeremylt /** 705ca94c3ddSJeremy L Thompson @brief Compute `x = alpha x` 706e0dd3b27Sjeremylt 707ca94c3ddSJeremy L Thompson @param[in,out] x `CeedVector` for scaling 70896b902e2Sjeremylt @param[in] alpha scaling factor 709e0dd3b27Sjeremylt 710e0dd3b27Sjeremylt @return An error code: 0 - success, otherwise - failure 711e0dd3b27Sjeremylt 712e0dd3b27Sjeremylt @ref User 713e0dd3b27Sjeremylt **/ 714e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) { 7159c774eddSJeremy L Thompson bool has_valid_array = true; 7161203703bSJeremy L Thompson CeedSize length; 7171203703bSJeremy L Thompson CeedScalar *x_array = NULL; 7181c66c397SJeremy L Thompson 7192b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array)); 7206e536b99SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(x), CEED_ERROR_BACKEND, 7212b730f8bSJeremy L Thompson "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7229c774eddSJeremy L Thompson 723b0976d5aSZach Atkins // Return early for empty vector 7241203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length)); 7251203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 726b0976d5aSZach Atkins 727e0dd3b27Sjeremylt // Backend implementation 7282b730f8bSJeremy L Thompson if (x->Scale) return x->Scale(x, alpha); 729e0dd3b27Sjeremylt 730e0dd3b27Sjeremylt // Default implementation 731567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(x, CEED_MEM_HOST, &x_array)); 732b0976d5aSZach Atkins assert(x_array); 7331203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha; 7342b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(x, &x_array)); 735e0dd3b27Sjeremylt return CEED_ERROR_SUCCESS; 736e0dd3b27Sjeremylt } 737e0dd3b27Sjeremylt 738e0dd3b27Sjeremylt /** 739ca94c3ddSJeremy L Thompson @brief Compute `y = alpha x + y` 7400f7fd0f8Sjeremylt 741ca94c3ddSJeremy L Thompson @param[in,out] y target `CeedVector` for sum 74296b902e2Sjeremylt @param[in] alpha scaling factor 743ca94c3ddSJeremy L Thompson @param[in] x second `CeedVector`, must be different than ``y` 7440f7fd0f8Sjeremylt 7450f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 7460f7fd0f8Sjeremylt 7470f7fd0f8Sjeremylt @ref User 7480f7fd0f8Sjeremylt **/ 7490f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { 7501203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 7511203703bSJeremy L Thompson CeedSize length_x, length_y; 75273380422SJeremy L Thompson CeedScalar *y_array = NULL; 75373380422SJeremy L Thompson CeedScalar const *x_array = NULL; 7540f7fd0f8Sjeremylt 7551203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 7561203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 7579bc66399SJeremy L Thompson CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, 7583f08121cSJeremy L Thompson "Cannot add vector of different lengths." 7593f08121cSJeremy L Thompson " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, 7603f08121cSJeremy L Thompson length_x, length_y); 7619bc66399SJeremy L Thompson CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); 7620f7fd0f8Sjeremylt 7632b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 7649bc66399SJeremy L Thompson CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 7656574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7662b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 7679bc66399SJeremy L Thompson CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 7686574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7699c774eddSJeremy L Thompson 7709bc66399SJeremy L Thompson { 7719bc66399SJeremy L Thompson Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y; 7729bc66399SJeremy L Thompson 7739bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed_y)); 7749bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(x, &ceed_x)); 7759bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); 7769bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); 7779bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_x)); 7789bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_y)); 7799bc66399SJeremy L Thompson CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE, 7809bc66399SJeremy L Thompson "Vectors x and y must be created by the same Ceed context"); 7819bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_x)); 7829bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_y)); 7839bc66399SJeremy L Thompson } 7842d04630dSjeremylt 785b0976d5aSZach Atkins // Return early for empty vectors 7861203703bSJeremy L Thompson if (length_y == 0) return CEED_ERROR_SUCCESS; 787b0976d5aSZach Atkins 7880f7fd0f8Sjeremylt // Backend implementation 789eaf62fffSJeremy L Thompson if (y->AXPY) { 7902b730f8bSJeremy L Thompson CeedCall(y->AXPY(y, alpha, x)); 791eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 792eaf62fffSJeremy L Thompson } 7930f7fd0f8Sjeremylt 7940f7fd0f8Sjeremylt // Default implementation 795567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 7962b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 7970f7fd0f8Sjeremylt 7982b730f8bSJeremy L Thompson assert(x_array); 7992b730f8bSJeremy L Thompson assert(y_array); 80073380422SJeremy L Thompson 8011203703bSJeremy L Thompson for (CeedSize i = 0; i < length_y; i++) y_array[i] += alpha * x_array[i]; 8020f7fd0f8Sjeremylt 8032b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(y, &y_array)); 8042b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 8050f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 8060f7fd0f8Sjeremylt } 8070f7fd0f8Sjeremylt 8080f7fd0f8Sjeremylt /** 809ca94c3ddSJeremy L Thompson @brief Compute `y = alpha x + beta y` 8105fb68f37SKaren (Ren) Stengel 811ca94c3ddSJeremy L Thompson @param[in,out] y target `CeedVector` for sum 8125fb68f37SKaren (Ren) Stengel @param[in] alpha first scaling factor 8135fb68f37SKaren (Ren) Stengel @param[in] beta second scaling factor 814ca94c3ddSJeremy L Thompson @param[in] x second `CeedVector`, must be different than `y` 8155fb68f37SKaren (Ren) Stengel 8165fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 8175fb68f37SKaren (Ren) Stengel 8185fb68f37SKaren (Ren) Stengel @ref User 8195fb68f37SKaren (Ren) Stengel **/ 8205fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 8211203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 8221203703bSJeremy L Thompson CeedSize length_x, length_y; 8235fb68f37SKaren (Ren) Stengel CeedScalar *y_array = NULL; 8245fb68f37SKaren (Ren) Stengel CeedScalar const *x_array = NULL; 8251203703bSJeremy L Thompson 8261203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 8271203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 8289bc66399SJeremy L Thompson CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, 8293f08121cSJeremy L Thompson "Cannot add vector of different lengths." 8303f08121cSJeremy L Thompson " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, 8313f08121cSJeremy L Thompson length_x, length_y); 8329bc66399SJeremy L Thompson CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY"); 8335fb68f37SKaren (Ren) Stengel 8345fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 8359bc66399SJeremy L Thompson CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 8366574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8375fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 8389bc66399SJeremy L Thompson CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 8396574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8405fb68f37SKaren (Ren) Stengel 8419bc66399SJeremy L Thompson { 8429bc66399SJeremy L Thompson Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y; 8439bc66399SJeremy L Thompson 8449bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed_y)); 8459bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(x, &ceed_x)); 8469bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); 8479bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); 8489bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_x)); 8499bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_y)); 8509bc66399SJeremy L Thompson CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE, 8519bc66399SJeremy L Thompson "Vectors x and y must be created by the same Ceed context"); 8529bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_x)); 8539bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_y)); 8549bc66399SJeremy L Thompson } 8555fb68f37SKaren (Ren) Stengel 856b0976d5aSZach Atkins // Return early for empty vectors 8571203703bSJeremy L Thompson if (length_y == 0) return CEED_ERROR_SUCCESS; 858b0976d5aSZach Atkins 8595fb68f37SKaren (Ren) Stengel // Backend implementation 8605fb68f37SKaren (Ren) Stengel if (y->AXPBY) { 8615fb68f37SKaren (Ren) Stengel CeedCall(y->AXPBY(y, alpha, beta, x)); 8625fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 8635fb68f37SKaren (Ren) Stengel } 8645fb68f37SKaren (Ren) Stengel 8655fb68f37SKaren (Ren) Stengel // Default implementation 8665fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 8675fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 8685fb68f37SKaren (Ren) Stengel 8695fb68f37SKaren (Ren) Stengel assert(x_array); 8705fb68f37SKaren (Ren) Stengel assert(y_array); 8715fb68f37SKaren (Ren) Stengel 8721203703bSJeremy L Thompson for (CeedSize i = 0; i < length_y; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i]; 8735fb68f37SKaren (Ren) Stengel 8745fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(y, &y_array)); 8755fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 8765fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 8775fb68f37SKaren (Ren) Stengel } 8785fb68f37SKaren (Ren) Stengel 8795fb68f37SKaren (Ren) Stengel /** 880ca94c3ddSJeremy L Thompson @brief Compute the pointwise multiplication \f$w = x .* y\f$. 8814385fb7fSSebastian Grimberg 882ca94c3ddSJeremy L Thompson Any subset of `x`, `y`, and `w` may be the same `CeedVector`. 8830f7fd0f8Sjeremylt 884ca94c3ddSJeremy L Thompson @param[out] w target `CeedVector` for the product 885ca94c3ddSJeremy L Thompson @param[in] x first `CeedVector` for product 886ca94c3ddSJeremy L Thompson @param[in] y second `CeedVector` for the product 8870f7fd0f8Sjeremylt 8880f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 8890f7fd0f8Sjeremylt 8900f7fd0f8Sjeremylt @ref User 8910f7fd0f8Sjeremylt **/ 8920f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { 8931203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 89473380422SJeremy L Thompson CeedScalar *w_array = NULL; 89573380422SJeremy L Thompson CeedScalar const *x_array = NULL, *y_array = NULL; 8961203703bSJeremy L Thompson CeedSize length_w, length_x, length_y; 8970f7fd0f8Sjeremylt 8981203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(w, &length_w)); 8991203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 9001203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 9019bc66399SJeremy L Thompson CeedCheck(length_x >= length_w && length_y >= length_w, CeedVectorReturnCeed(w), CEED_ERROR_UNSUPPORTED, 902ccad5cb9SJeremy L Thompson "Cannot pointwise multiply vectors of incompatible lengths." 903ccad5cb9SJeremy L Thompson " w length: %" CeedSize_FMT " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, 904ccad5cb9SJeremy L Thompson length_w, length_x, length_y); 9050f7fd0f8Sjeremylt 9069bc66399SJeremy L Thompson { 9079bc66399SJeremy L Thompson Ceed ceed_w, ceed_x, ceed_y, ceed_parent_w, ceed_parent_x, ceed_parent_y; 9089bc66399SJeremy L Thompson 9099bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(w, &ceed_w)); 9109bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(x, &ceed_x)); 9119bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed_y)); 9129bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_w, &ceed_parent_w)); 9139bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); 9149bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); 9159bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_w)); 9169bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_x)); 9179bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_y)); 9189bc66399SJeremy L Thompson CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, CeedVectorReturnCeed(w), CEED_ERROR_INCOMPATIBLE, 9196574a04fSJeremy L Thompson "Vectors w, x, and y must be created by the same Ceed context"); 9209bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_w)); 9219bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_x)); 9229bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_y)); 9239bc66399SJeremy L Thompson } 9242d04630dSjeremylt 9252b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 9269bc66399SJeremy L Thompson CeedCheck(has_valid_array_x, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND, 9276574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9282b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 9299bc66399SJeremy L Thompson CeedCheck(has_valid_array_y, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND, 9306574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9319c774eddSJeremy L Thompson 932b0976d5aSZach Atkins // Return early for empty vectors 9331203703bSJeremy L Thompson if (length_w == 0) return CEED_ERROR_SUCCESS; 934b0976d5aSZach Atkins 9350f7fd0f8Sjeremylt // Backend implementation 936eaf62fffSJeremy L Thompson if (w->PointwiseMult) { 9372b730f8bSJeremy L Thompson CeedCall(w->PointwiseMult(w, x, y)); 938eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 939eaf62fffSJeremy L Thompson } 9400f7fd0f8Sjeremylt 9410f7fd0f8Sjeremylt // Default implementation 942b0976d5aSZach Atkins if (x == w || y == w) { 943b0976d5aSZach Atkins CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 944b0976d5aSZach Atkins } else { 945b0976d5aSZach Atkins CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array)); 946b0976d5aSZach Atkins } 9470f7fd0f8Sjeremylt if (x != w) { 9482b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 9490f7fd0f8Sjeremylt } else { 9500f7fd0f8Sjeremylt x_array = w_array; 9510f7fd0f8Sjeremylt } 9520f7fd0f8Sjeremylt if (y != w && y != x) { 9532b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array)); 954c6d796c6SJeremy L Thompson } else if (y == x) { 9550f7fd0f8Sjeremylt y_array = x_array; 956b0976d5aSZach Atkins } else if (y == w) { 957c6d796c6SJeremy L Thompson y_array = w_array; 9580f7fd0f8Sjeremylt } 9590f7fd0f8Sjeremylt 9602b730f8bSJeremy L Thompson assert(w_array); 9612b730f8bSJeremy L Thompson assert(x_array); 9622b730f8bSJeremy L Thompson assert(y_array); 96373380422SJeremy L Thompson 9641203703bSJeremy L Thompson for (CeedSize i = 0; i < length_w; i++) w_array[i] = x_array[i] * y_array[i]; 9650f7fd0f8Sjeremylt 9662b730f8bSJeremy L Thompson if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array)); 9672b730f8bSJeremy L Thompson if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 9682b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(w, &w_array)); 9690f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 9700f7fd0f8Sjeremylt } 9710f7fd0f8Sjeremylt 9720f7fd0f8Sjeremylt /** 973ca94c3ddSJeremy L Thompson @brief Take the reciprocal of a `CeedVector`. 974d99fa3c5SJeremy L Thompson 975ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to take reciprocal 976d99fa3c5SJeremy L Thompson 977d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 978d99fa3c5SJeremy L Thompson 979d99fa3c5SJeremy L Thompson @ref User 980d99fa3c5SJeremy L Thompson **/ 981d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) { 9829c774eddSJeremy L Thompson bool has_valid_array = true; 9831203703bSJeremy L Thompson CeedSize length; 9841203703bSJeremy L Thompson CeedScalar *array; 9852b730f8bSJeremy L Thompson 9861c66c397SJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 9879bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 9882b730f8bSJeremy L Thompson "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9899c774eddSJeremy L Thompson 990d99fa3c5SJeremy L Thompson // Check if vector data set 9919bc66399SJeremy L Thompson CeedCheck(vec->state > 0, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); 992d99fa3c5SJeremy L Thompson 993b0976d5aSZach Atkins // Return early for empty vector 9941203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 9951203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 996b0976d5aSZach Atkins 997d99fa3c5SJeremy L Thompson // Backend impl for GPU, if added 998d99fa3c5SJeremy L Thompson if (vec->Reciprocal) { 9992b730f8bSJeremy L Thompson CeedCall(vec->Reciprocal(vec)); 1000e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1001d99fa3c5SJeremy L Thompson } 1002d99fa3c5SJeremy L Thompson 1003567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array)); 10041203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 10052b730f8bSJeremy L Thompson if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i]; 10062b730f8bSJeremy L Thompson } 1007d99fa3c5SJeremy L Thompson 10082b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 1009e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1010d99fa3c5SJeremy L Thompson } 1011d99fa3c5SJeremy L Thompson 1012d99fa3c5SJeremy L Thompson /** 1013*4c789ea2SJeremy L Thompson @brief Set the number of tabs to indent for @ref CeedVectorView() output 1014*4c789ea2SJeremy L Thompson 1015*4c789ea2SJeremy L Thompson @param[in] vec `CeedVector` to set the number of view tabs 1016*4c789ea2SJeremy L Thompson @param[in] num_tabs Number of view tabs to set 1017*4c789ea2SJeremy L Thompson 1018*4c789ea2SJeremy L Thompson @return Error code: 0 - success, otherwise - failure 1019*4c789ea2SJeremy L Thompson 1020*4c789ea2SJeremy L Thompson @ref User 1021*4c789ea2SJeremy L Thompson **/ 1022*4c789ea2SJeremy L Thompson int CeedVectorSetNumViewTabs(CeedVector vec, CeedInt num_tabs) { 1023*4c789ea2SJeremy L Thompson CeedCheck(num_tabs >= 0, CeedVectorReturnCeed(vec), CEED_ERROR_MINOR, "Number of view tabs must be non-negative"); 1024*4c789ea2SJeremy L Thompson vec->num_tabs = num_tabs; 1025*4c789ea2SJeremy L Thompson return CEED_ERROR_SUCCESS; 1026*4c789ea2SJeremy L Thompson } 1027*4c789ea2SJeremy L Thompson 1028*4c789ea2SJeremy L Thompson /** 1029ca94c3ddSJeremy L Thompson @brief View a `CeedVector` 10300436c2adSjeremylt 1031acb2c48cSJeremy L Thompson Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`. 1032ca94c3ddSJeremy L Thompson Any portion of the provided range that is outside the range of valid indices for the `CeedVector` will be ignored. 1033acb2c48cSJeremy L Thompson 1034ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to view 1035f52f9f6cSJeremy L Thompson @param[in] start Index of first `CeedVector` entry to view in the range `[start, stop)` 1036f52f9f6cSJeremy L Thompson @param[in] stop One past the last element to view in the range, or `-1` for `length` 1037ca94c3ddSJeremy L Thompson @param[in] step Step between `CeedVector` entries to view 1038acb2c48cSJeremy L Thompson @param[in] fp_fmt Printing format 1039acb2c48cSJeremy L Thompson @param[in] stream Filestream to write to 1040acb2c48cSJeremy L Thompson 1041acb2c48cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1042acb2c48cSJeremy L Thompson 1043acb2c48cSJeremy L Thompson @ref User 1044acb2c48cSJeremy L Thompson **/ 1045acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) { 1046acb2c48cSJeremy L Thompson char fmt[1024]; 1047*4c789ea2SJeremy L Thompson char *tabs = NULL; 10481203703bSJeremy L Thompson CeedSize length; 10491203703bSJeremy L Thompson const CeedScalar *x; 1050acb2c48cSJeremy L Thompson 10516e536b99SJeremy L Thompson CeedCheck(step != 0, CeedVectorReturnCeed(vec), CEED_ERROR_MINOR, "View range 'step' must be nonzero"); 1052acb2c48cSJeremy L Thompson 1053*4c789ea2SJeremy L Thompson { 1054*4c789ea2SJeremy L Thompson CeedInt num_tabs = 0; 1055*4c789ea2SJeremy L Thompson 1056*4c789ea2SJeremy L Thompson CeedCall(CeedVectorGetNumViewTabs(vec, &num_tabs)); 1057*4c789ea2SJeremy L Thompson CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs)); 1058*4c789ea2SJeremy L Thompson for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' '; 1059*4c789ea2SJeremy L Thompson } 1060*4c789ea2SJeremy L Thompson 10611203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 1062*4c789ea2SJeremy L Thompson fprintf(stream, "%sCeedVector length %" CeedSize_FMT "\n", tabs, length); 10631203703bSJeremy L Thompson if (start != 0 || stop != length || step != 1) { 1064*4c789ea2SJeremy 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); 1065acb2c48cSJeremy L Thompson } 10661203703bSJeremy L Thompson if (start > length) start = length; 1067f52f9f6cSJeremy L Thompson if (stop == -1 || stop > length) stop = length; 1068acb2c48cSJeremy L Thompson 1069*4c789ea2SJeremy L Thompson snprintf(fmt, sizeof fmt, "%s %s\n", tabs, fp_fmt ? fp_fmt : "%g"); 1070acb2c48cSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x)); 1071b94338b9SJed Brown for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]); 1072acb2c48cSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &x)); 1073*4c789ea2SJeremy L Thompson if (stop != length) fprintf(stream, "%s ...\n", tabs); 1074*4c789ea2SJeremy L Thompson CeedCall(CeedFree(&tabs)); 1075acb2c48cSJeremy L Thompson return CEED_ERROR_SUCCESS; 1076acb2c48cSJeremy L Thompson } 1077acb2c48cSJeremy L Thompson 1078acb2c48cSJeremy L Thompson /** 1079ca94c3ddSJeremy L Thompson @brief View a `CeedVector` 1080acb2c48cSJeremy L Thompson 1081ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to view 1082d1d35e2fSjeremylt @param[in] fp_fmt Printing format 10830a0da059Sjeremylt @param[in] stream Filestream to write to 10840a0da059Sjeremylt 10850436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 10860436c2adSjeremylt 10877a982d89SJeremy L. Thompson @ref User 10880436c2adSjeremylt **/ 1089d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { 10901203703bSJeremy L Thompson CeedSize length; 10911203703bSJeremy L Thompson 10921203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 10931203703bSJeremy L Thompson CeedCall(CeedVectorViewRange(vec, 0, length, 1, fp_fmt, stream)); 1094e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10950436c2adSjeremylt } 10960436c2adSjeremylt 10970436c2adSjeremylt /** 1098ca94c3ddSJeremy L Thompson @brief Get the `Ceed` associated with a `CeedVector` 1099b7c9bbdaSJeremy L Thompson 1100ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 1101ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 1102b7c9bbdaSJeremy L Thompson 1103b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1104b7c9bbdaSJeremy L Thompson 1105b7c9bbdaSJeremy L Thompson @ref Advanced 1106b7c9bbdaSJeremy L Thompson **/ 1107b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 11089bc66399SJeremy L Thompson *ceed = NULL; 11099bc66399SJeremy L Thompson CeedCall(CeedReferenceCopy(CeedVectorReturnCeed(vec), ceed)); 1110b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1111b7c9bbdaSJeremy L Thompson } 1112b7c9bbdaSJeremy L Thompson 1113b7c9bbdaSJeremy L Thompson /** 11146e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedVector` 11156e536b99SJeremy L Thompson 11166e536b99SJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 11176e536b99SJeremy L Thompson 11186e536b99SJeremy L Thompson @return `Ceed` associated with the `vec` 11196e536b99SJeremy L Thompson 11206e536b99SJeremy L Thompson @ref Advanced 11216e536b99SJeremy L Thompson **/ 11226e536b99SJeremy L Thompson Ceed CeedVectorReturnCeed(CeedVector vec) { return vec->ceed; } 11236e536b99SJeremy L Thompson 11246e536b99SJeremy L Thompson /** 1125ca94c3ddSJeremy L Thompson @brief Get the length of a `CeedVector` 11260436c2adSjeremylt 1127ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve length 11287a982d89SJeremy L. Thompson @param[out] length Variable to store length 11290436c2adSjeremylt 11300436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 11310436c2adSjeremylt 11327a982d89SJeremy L. Thompson @ref User 11330436c2adSjeremylt **/ 11341f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) { 11357a982d89SJeremy L. Thompson *length = vec->length; 1136e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11370436c2adSjeremylt } 11380436c2adSjeremylt 11390436c2adSjeremylt /** 1140ca94c3ddSJeremy L Thompson @brief Destroy a `CeedVector` 11410436c2adSjeremylt 1142ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to destroy 11430436c2adSjeremylt 11440436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 11450436c2adSjeremylt 11467a982d89SJeremy L. Thompson @ref User 11470436c2adSjeremylt **/ 11480436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) { 11497425e127SJeremy L Thompson if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) { 1150ad6481ceSJeremy L Thompson *vec = NULL; 1151ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1152ad6481ceSJeremy L Thompson } 11536574a04fSJeremy L Thompson CeedCheck((*vec)->state % 2 == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use"); 11546574a04fSJeremy L Thompson CeedCheck((*vec)->num_readers == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access"); 11550436c2adSjeremylt 11562b730f8bSJeremy L Thompson if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec)); 11572b730f8bSJeremy L Thompson 11582b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*vec)->ceed)); 11592b730f8bSJeremy L Thompson CeedCall(CeedFree(vec)); 1160e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11610436c2adSjeremylt } 11620436c2adSjeremylt 11630436c2adSjeremylt /// @} 1164