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 /** 43ca94c3ddSJeremy L Thompson @brief Check for valid data in a `CeedVector` 449c774eddSJeremy L Thompson 45ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to check validity 469c774eddSJeremy L Thompson @param[out] has_valid_array Variable to store validity 479c774eddSJeremy L Thompson 489c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 499c774eddSJeremy L Thompson 509c774eddSJeremy L Thompson @ref Backend 519c774eddSJeremy L Thompson **/ 529c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) { 531203703bSJeremy L Thompson CeedSize length; 541203703bSJeremy L Thompson 556e536b99SJeremy L Thompson CeedCheck(vec->HasValidArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorHasValidArray"); 561203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 571203703bSJeremy L Thompson if (length == 0) { 58b0976d5aSZach Atkins *has_valid_array = true; 59b0976d5aSZach Atkins return CEED_ERROR_SUCCESS; 60b0976d5aSZach Atkins } 612b730f8bSJeremy L Thompson CeedCall(vec->HasValidArray(vec, has_valid_array)); 629c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 639c774eddSJeremy L Thompson } 649c774eddSJeremy L Thompson 659c774eddSJeremy L Thompson /** 66ca94c3ddSJeremy L Thompson @brief Check for borrowed array of a specific @ref CeedMemType in a `CeedVector` 679c774eddSJeremy L Thompson 68ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to check 69ea61e9acSJeremy L Thompson @param[in] mem_type Memory type to check 709c774eddSJeremy L Thompson @param[out] has_borrowed_array_of_type Variable to store result 719c774eddSJeremy L Thompson 729c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 739c774eddSJeremy L Thompson 749c774eddSJeremy L Thompson @ref Backend 759c774eddSJeremy L Thompson **/ 762b730f8bSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 776e536b99SJeremy L Thompson CeedCheck(vec->HasBorrowedArrayOfType, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, 786e536b99SJeremy L Thompson "Backend does not support CeedVectorHasBorrowedArrayOfType"); 792b730f8bSJeremy L Thompson CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type)); 809c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 819c774eddSJeremy L Thompson } 829c774eddSJeremy L Thompson 839c774eddSJeremy L Thompson /** 84ca94c3ddSJeremy L Thompson @brief Get the state of a `CeedVector` 857a982d89SJeremy L. Thompson 86ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 877a982d89SJeremy L. Thompson @param[out] state Variable to store state 887a982d89SJeremy L. Thompson 897a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 907a982d89SJeremy L. Thompson 917a982d89SJeremy L. Thompson @ref Backend 927a982d89SJeremy L. Thompson **/ 937a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) { 947a982d89SJeremy L. Thompson *state = vec->state; 95e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 967a982d89SJeremy L. Thompson } 977a982d89SJeremy L. Thompson 987a982d89SJeremy L. Thompson /** 99ca94c3ddSJeremy L Thompson @brief Get the backend data of a `CeedVector` 1007a982d89SJeremy L. Thompson 101ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 1027a982d89SJeremy L. Thompson @param[out] data Variable to store data 1037a982d89SJeremy L. Thompson 1047a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1057a982d89SJeremy L. Thompson 1067a982d89SJeremy L. Thompson @ref Backend 1077a982d89SJeremy L. Thompson **/ 108777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) { 109777ff853SJeremy L Thompson *(void **)data = vec->data; 110e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1117a982d89SJeremy L. Thompson } 1127a982d89SJeremy L. Thompson 1137a982d89SJeremy L. Thompson /** 114ca94c3ddSJeremy L Thompson @brief Set the backend data of a `CeedVector` 1157a982d89SJeremy L. Thompson 116ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to retrieve state 117ea61e9acSJeremy L Thompson @param[in] data Data to set 1187a982d89SJeremy L. Thompson 1197a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1207a982d89SJeremy L. Thompson 1217a982d89SJeremy L. Thompson @ref Backend 1227a982d89SJeremy L. Thompson **/ 123777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) { 124777ff853SJeremy L Thompson vec->data = data; 125e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1267a982d89SJeremy L. Thompson } 1277a982d89SJeremy L. Thompson 12834359f16Sjeremylt /** 129ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedVector` 13034359f16Sjeremylt 131ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to increment the reference counter 13234359f16Sjeremylt 13334359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 13434359f16Sjeremylt 13534359f16Sjeremylt @ref Backend 13634359f16Sjeremylt **/ 1379560d06aSjeremylt int CeedVectorReference(CeedVector vec) { 13834359f16Sjeremylt vec->ref_count++; 13934359f16Sjeremylt return CEED_ERROR_SUCCESS; 14034359f16Sjeremylt } 14134359f16Sjeremylt 1427a982d89SJeremy L. Thompson /// @} 1437a982d89SJeremy L. Thompson 1447a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1457a982d89SJeremy L. Thompson /// CeedVector Public API 1467a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1477a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 1480436c2adSjeremylt /// @{ 1490436c2adSjeremylt 1500436c2adSjeremylt /** 151ca94c3ddSJeremy L Thompson @brief Create a `CeedVector` of the specified length (does not allocate memory) 1520436c2adSjeremylt 153ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedVector` 154ea61e9acSJeremy L Thompson @param[in] length Length of vector 155ca94c3ddSJeremy L Thompson @param[out] vec Address of the variable where the newly created `CeedVector` will be stored 1560436c2adSjeremylt 1570436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 1580436c2adSjeremylt 1597a982d89SJeremy L. Thompson @ref User 1600436c2adSjeremylt **/ 1611f9221feSJeremy L Thompson int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) { 162*a5ef892dSJames Wright CeedCheck(length >= 0, ceed, CEED_ERROR_UNSUPPORTED, "CeedVector must have length >= 0, recieved %" CeedSize_FMT, length); 1630436c2adSjeremylt if (!ceed->VectorCreate) { 1640436c2adSjeremylt Ceed delegate; 1656574a04fSJeremy L Thompson 1662b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector")); 1671ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement VectorCreate"); 1682b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(delegate, length, vec)); 1699bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 170e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1710436c2adSjeremylt } 1720436c2adSjeremylt 1732b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, vec)); 174db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*vec)->ceed)); 175d1d35e2fSjeremylt (*vec)->ref_count = 1; 1760436c2adSjeremylt (*vec)->length = length; 1770436c2adSjeremylt (*vec)->state = 0; 1782b730f8bSJeremy L Thompson CeedCall(ceed->VectorCreate(length, *vec)); 179e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1800436c2adSjeremylt } 1810436c2adSjeremylt 1820436c2adSjeremylt /** 183ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedVector`. 1844385fb7fSSebastian Grimberg 185ca94c3ddSJeremy L Thompson Both pointers should be destroyed with @ref CeedVectorDestroy(). 186512bb800SJeremy L Thompson 187ca94c3ddSJeremy 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`. 188ca94c3ddSJeremy L Thompson This `CeedVector` will be destroyed if `*vec_copy` is the only reference to this `CeedVector`. 1899560d06aSjeremylt 190ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to copy reference to 191ea61e9acSJeremy L Thompson @param[in,out] vec_copy Variable to store copied reference 1929560d06aSjeremylt 1939560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 1949560d06aSjeremylt 1959560d06aSjeremylt @ref User 1969560d06aSjeremylt **/ 1979560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) { 198393ac2cdSJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorReference(vec)); 1992b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(vec_copy)); 2009560d06aSjeremylt *vec_copy = vec; 2019560d06aSjeremylt return CEED_ERROR_SUCCESS; 2029560d06aSjeremylt } 2039560d06aSjeremylt 2049560d06aSjeremylt /** 205ca94c3ddSJeremy L Thompson @brief Copy a `CeedVector` into a different `CeedVector`. 2064385fb7fSSebastian Grimberg 207ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to copy 208e706ae07SJeremy L Thompson @param[in,out] vec_copy `CeedVector` to copy array into 2095fb68f37SKaren (Ren) Stengel 2105fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 2115fb68f37SKaren (Ren) Stengel 2125fb68f37SKaren (Ren) Stengel @ref User 2135fb68f37SKaren (Ren) Stengel **/ 2145fb68f37SKaren (Ren) Stengel int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) { 2155fb68f37SKaren (Ren) Stengel CeedMemType mem_type, mem_type_copy; 2165fb68f37SKaren (Ren) Stengel CeedScalar *array; 2175fb68f37SKaren (Ren) Stengel 2189bc66399SJeremy L Thompson // Get the preferred memory types 2199bc66399SJeremy L Thompson { 2209bc66399SJeremy L Thompson Ceed ceed; 2219bc66399SJeremy L Thompson 222d77c2f5dSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec, &ceed)); 223d77c2f5dSJeremy L Thompson CeedCall(CeedGetPreferredMemType(ceed, &mem_type)); 2249bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 2255fb68f37SKaren (Ren) Stengel 226d77c2f5dSJeremy L Thompson CeedCall(CeedVectorGetCeed(vec_copy, &ceed)); 227d77c2f5dSJeremy L Thompson CeedCall(CeedGetPreferredMemType(ceed, &mem_type_copy)); 2289bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 2299bc66399SJeremy L Thompson } 2305fb68f37SKaren (Ren) Stengel 2315fb68f37SKaren (Ren) Stengel // Check that both have same memory type 2325fb68f37SKaren (Ren) Stengel if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST; 2335fb68f37SKaren (Ren) Stengel 234e706ae07SJeremy L Thompson // Check compatible lengths 235e706ae07SJeremy L Thompson { 236e706ae07SJeremy L Thompson CeedSize length_vec, length_copy; 237e706ae07SJeremy L Thompson 238e706ae07SJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length_vec)); 239e706ae07SJeremy L Thompson CeedCall(CeedVectorGetLength(vec_copy, &length_copy)); 2409bc66399SJeremy L Thompson CeedCheck(length_vec == length_copy, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPATIBLE, "CeedVectors must have the same length to copy"); 241e706ae07SJeremy L Thompson } 242e706ae07SJeremy L Thompson 2435fb68f37SKaren (Ren) Stengel // Copy the values from vec to vec_copy 2445fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(vec, mem_type, &array)); 2455fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array)); 2465fb68f37SKaren (Ren) Stengel 2475fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(vec, &array)); 2485fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 2495fb68f37SKaren (Ren) Stengel } 2505fb68f37SKaren (Ren) Stengel 2515fb68f37SKaren (Ren) Stengel /** 2520b8f3c4eSJeremy L Thompson @brief Copy a strided portion of `CeedVector` contents into a different `CeedVector` 2530b8f3c4eSJeremy L Thompson 2540b8f3c4eSJeremy L Thompson @param[in] vec `CeedVector` to copy 255832a6d73SJeremy L Thompson @param[in] start First index to copy in the range `[start, stop)` 256832a6d73SJeremy L Thompson @param[in] stop One past the last element to copy in the range, or `-1` for `length` 2570b8f3c4eSJeremy L Thompson @param[in] step Stride between indices to copy 2580b8f3c4eSJeremy L Thompson @param[in,out] vec_copy `CeedVector` to copy values to 2590b8f3c4eSJeremy L Thompson 2600b8f3c4eSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2610b8f3c4eSJeremy L Thompson 2620b8f3c4eSJeremy L Thompson @ref User 2630b8f3c4eSJeremy L Thompson **/ 264832a6d73SJeremy L Thompson int CeedVectorCopyStrided(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedVector vec_copy) { 2650b8f3c4eSJeremy L Thompson CeedSize length; 266bb03490dSJeremy L Thompson const CeedScalar *array = NULL; 267bb03490dSJeremy L Thompson CeedScalar *array_copy = NULL; 2680b8f3c4eSJeremy L Thompson 269832a6d73SJeremy L Thompson // Check length 2700b8f3c4eSJeremy L Thompson { 2710b8f3c4eSJeremy L Thompson CeedSize length_vec, length_copy; 2720b8f3c4eSJeremy L Thompson 2730b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length_vec)); 2740b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetLength(vec_copy, &length_copy)); 275bb03490dSJeremy L Thompson if (length_vec <= 0 || length_copy <= 0) return CEED_ERROR_SUCCESS; 276a7efc114SJeremy L Thompson length = length_vec < length_copy ? length_vec : length_copy; 2770b8f3c4eSJeremy L Thompson } 278832a6d73SJeremy L Thompson CeedCheck(stop >= -1 && stop <= length, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 279832a6d73SJeremy L Thompson "Invalid value for stop %" CeedSize_FMT ", must be in the range [-1, length]", stop); 280832a6d73SJeremy L Thompson CeedCheck(start >= 0 && start <= length && (start <= stop || stop == -1), CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 281832a6d73SJeremy L Thompson "Invalid value for start %" CeedSize_FMT ", must be in the range [0, stop]", start); 282832a6d73SJeremy L Thompson 283832a6d73SJeremy L Thompson // Backend version 284832a6d73SJeremy L Thompson if (vec->CopyStrided && vec_copy->CopyStrided) { 285832a6d73SJeremy L Thompson CeedCall(vec->CopyStrided(vec, start, stop, step, vec_copy)); 286832a6d73SJeremy L Thompson vec_copy->state += 2; 287832a6d73SJeremy L Thompson return CEED_ERROR_SUCCESS; 288832a6d73SJeremy L Thompson } 2890b8f3c4eSJeremy L Thompson 2900b8f3c4eSJeremy L Thompson // Copy 2910b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 2920b8f3c4eSJeremy L Thompson CeedCall(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, &array_copy)); 293832a6d73SJeremy L Thompson if (stop == -1) stop = length; 294832a6d73SJeremy L Thompson for (CeedSize i = start; i < stop; i += step) array_copy[i] = array[i]; 2950b8f3c4eSJeremy L Thompson 2960b8f3c4eSJeremy L Thompson // Cleanup 2970b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 2980b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec_copy, &array_copy)); 2990b8f3c4eSJeremy L Thompson return CEED_ERROR_SUCCESS; 3000b8f3c4eSJeremy L Thompson } 3010b8f3c4eSJeremy L Thompson 3020b8f3c4eSJeremy L Thompson /** 303ca94c3ddSJeremy L Thompson @brief Set the array used by a `CeedVector`, freeing any previously allocated array if applicable. 3044385fb7fSSebastian Grimberg 305ca94c3ddSJeremy L Thompson The backend may copy values to a different @ref CeedMemType, such as during @ref CeedOperatorApply(). 3066a6c615bSJeremy L Thompson See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 3070436c2adSjeremylt 308ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 309ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the array being passed 310ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the array 311ca94c3ddSJeremy L Thompson @param[in] array Array to be used, or `NULL` with @ref CEED_COPY_VALUES to have the library allocate 3120436c2adSjeremylt 3130436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3140436c2adSjeremylt 3157a982d89SJeremy L. Thompson @ref User 3160436c2adSjeremylt **/ 3172b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 3181203703bSJeremy L Thompson CeedSize length; 3190436c2adSjeremylt 3209bc66399SJeremy L Thompson CeedCheck(vec->SetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); 3219bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 3229bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 3239bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3241203703bSJeremy L Thompson 3251203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3261203703bSJeremy L Thompson if (length > 0) CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); 3270436c2adSjeremylt vec->state += 2; 328e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3290436c2adSjeremylt } 3300436c2adSjeremylt 3310436c2adSjeremylt /** 332ca94c3ddSJeremy L Thompson @brief Set the `CeedVector` to a constant value 3330436c2adSjeremylt 334ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 3350436c2adSjeremylt @param[in] value Value to be used 3360436c2adSjeremylt 3370436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3380436c2adSjeremylt 3397a982d89SJeremy L. Thompson @ref User 3400436c2adSjeremylt **/ 3410436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 3429bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 3439bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 3449bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3450436c2adSjeremylt 3460436c2adSjeremylt if (vec->SetValue) { 3472b730f8bSJeremy L Thompson CeedCall(vec->SetValue(vec, value)); 3482d4e0605SJeremy L Thompson vec->state += 2; 3490436c2adSjeremylt } else { 3501203703bSJeremy L Thompson CeedSize length; 3510436c2adSjeremylt CeedScalar *array; 3521203703bSJeremy L Thompson 3532b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 3541203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3551203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) array[i] = value; 3562b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 3570436c2adSjeremylt } 358e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3590436c2adSjeremylt } 3600436c2adSjeremylt 3610436c2adSjeremylt /** 3620b8f3c4eSJeremy L Thompson @brief Set a portion of a `CeedVector` to a constant value. 3630b8f3c4eSJeremy L Thompson 3640b8f3c4eSJeremy L Thompson Note: The `CeedVector` must already have valid data set via @ref CeedVectorSetArray() or similar. 3650b8f3c4eSJeremy L Thompson 3660b8f3c4eSJeremy L Thompson @param[in,out] vec `CeedVector` 367ff90b007SJeremy L Thompson @param[in] start First index to set in range `[start, stop)` 36812634730SJeremy L Thompson @param[in] stop One past the last element to set in the range, or `-1` for `length` 3690b8f3c4eSJeremy L Thompson @param[in] step Stride between indices to set 3700b8f3c4eSJeremy L Thompson @param[in] value Value to be used 3710b8f3c4eSJeremy L Thompson 3720b8f3c4eSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3730b8f3c4eSJeremy L Thompson 3740b8f3c4eSJeremy L Thompson @ref User 3750b8f3c4eSJeremy L Thompson **/ 376ff90b007SJeremy L Thompson int CeedVectorSetValueStrided(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedScalar value) { 377b1a610efSJeremy L Thompson CeedSize length; 378b1a610efSJeremy L Thompson 3799bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 3809bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 3819bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 382b1a610efSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 3837a747cf1SJeremy L Thompson CeedCheck(stop >= -1 && stop <= length, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 3847a747cf1SJeremy L Thompson "Invalid value for stop %" CeedSize_FMT ", must be in the range [-1, length]", stop); 3850b8f3c4eSJeremy L Thompson 3860b8f3c4eSJeremy L Thompson if (vec->SetValueStrided) { 387ff90b007SJeremy L Thompson CeedCall(vec->SetValueStrided(vec, start, stop, step, value)); 3882d4e0605SJeremy L Thompson vec->state += 2; 3890b8f3c4eSJeremy L Thompson } else { 3900b8f3c4eSJeremy L Thompson CeedScalar *array; 3910b8f3c4eSJeremy L Thompson 392bb03490dSJeremy L Thompson if (length <= 0) return CEED_ERROR_SUCCESS; 393ff90b007SJeremy L Thompson if (stop == -1) stop = length; 394bb03490dSJeremy L Thompson CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array)); 395ff90b007SJeremy L Thompson for (CeedSize i = start; i < stop; i += step) array[i] = value; 3960b8f3c4eSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 3970b8f3c4eSJeremy L Thompson } 3980b8f3c4eSJeremy L Thompson return CEED_ERROR_SUCCESS; 3990b8f3c4eSJeremy L Thompson } 4000b8f3c4eSJeremy L Thompson 4010b8f3c4eSJeremy L Thompson /** 402ca94c3ddSJeremy L Thompson @brief Sync the `CeedVector` to a specified `mem_type`. 4034385fb7fSSebastian Grimberg 404ea61e9acSJeremy L Thompson This function is used to force synchronization of arrays set with @ref CeedVectorSetArray(). 405ca94c3ddSJeremy L Thompson If the requested `mem_type` is already synchronized, this function results in a no-op. 4060436c2adSjeremylt 407ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 408ca94c3ddSJeremy L Thompson @param[in] mem_type @ref CeedMemType to be synced 4090436c2adSjeremylt 4100436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4110436c2adSjeremylt 4127a982d89SJeremy L. Thompson @ref User 4130436c2adSjeremylt **/ 414d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { 4151203703bSJeremy L Thompson CeedSize length; 4161203703bSJeremy L Thompson 4176e536b99SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use"); 4180436c2adSjeremylt 419b0976d5aSZach Atkins // Don't sync empty array 4201203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 4211203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 422b0976d5aSZach Atkins 4230436c2adSjeremylt if (vec->SyncArray) { 4242b730f8bSJeremy L Thompson CeedCall(vec->SyncArray(vec, mem_type)); 4250436c2adSjeremylt } else { 4260436c2adSjeremylt const CeedScalar *array; 4271203703bSJeremy L Thompson 4282b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array)); 4292b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 4300436c2adSjeremylt } 431e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4320436c2adSjeremylt } 4330436c2adSjeremylt 4340436c2adSjeremylt /** 435ca94c3ddSJeremy 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`. 4364385fb7fSSebastian Grimberg 4379c774eddSJeremy L Thompson The caller is responsible for managing and freeing the array. 438ea61e9acSJeremy L Thompson This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type. 4396a6c615bSJeremy L Thompson 440ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` 441ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to take the array. 442ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 443ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type`, or `NULL` if array pointer is not required 4446a6c615bSJeremy L Thompson 4456a6c615bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4466a6c615bSJeremy L Thompson 4476a6c615bSJeremy L Thompson @ref User 4486a6c615bSJeremy L Thompson **/ 4492b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4501203703bSJeremy L Thompson CeedSize length; 4511c66c397SJeremy L Thompson CeedScalar *temp_array = NULL; 4521c66c397SJeremy L Thompson 4539bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); 4549bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); 4556a6c615bSJeremy L Thompson 4561203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 4571203703bSJeremy L Thompson if (length > 0) { 4581203703bSJeremy L Thompson bool has_borrowed_array_of_type = true, has_valid_array = true; 4591203703bSJeremy L Thompson 4602b730f8bSJeremy L Thompson CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); 4619bc66399SJeremy L Thompson CeedCheck(has_borrowed_array_of_type, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 4629bc66399SJeremy L Thompson "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", CeedMemTypes[mem_type]); 4639c774eddSJeremy L Thompson 4642b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4659bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 4662b730f8bSJeremy L Thompson "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4679c774eddSJeremy L Thompson 4682b730f8bSJeremy L Thompson CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); 46950c643e1SJed Brown } 470d1d35e2fSjeremylt if (array) (*array) = temp_array; 471e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4726a6c615bSJeremy L Thompson } 4736a6c615bSJeremy L Thompson 4746a6c615bSJeremy L Thompson /** 475ca94c3ddSJeremy L Thompson @brief Get read/write access to a `CeedVector` via the specified memory type. 4764385fb7fSSebastian Grimberg 477b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArray(). 4780436c2adSjeremylt 479ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to access 480ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 481ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 482ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 4830436c2adSjeremylt 484ca94c3ddSJeremy L Thompson @note The @ref CeedVectorGetArray() and @ref CeedVectorRestoreArray() functions provide access to array pointers in the desired memory space. 485ca94c3ddSJeremy L Thompson Pairing get/restore allows the `CeedVector` to track access, thus knowing if norms or other operations may need to be recomputed. 4860436c2adSjeremylt 4870436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4880436c2adSjeremylt 4897a982d89SJeremy L. Thompson @ref User 4900436c2adSjeremylt **/ 4912b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4921203703bSJeremy L Thompson CeedSize length; 4930436c2adSjeremylt 4949bc66399SJeremy L Thompson CeedCheck(vec->GetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); 4959bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 4969bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 4979bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 4981203703bSJeremy L Thompson 4991203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5001203703bSJeremy L Thompson if (length > 0) { 5019c774eddSJeremy L Thompson bool has_valid_array = true; 502b0976d5aSZach Atkins 5032b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5049bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 5052b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5069c774eddSJeremy L Thompson 5072b730f8bSJeremy L Thompson CeedCall(vec->GetArray(vec, mem_type, array)); 508b0976d5aSZach Atkins } else { 509b0976d5aSZach Atkins *array = NULL; 510b0976d5aSZach Atkins } 51128bfd0b7SJeremy L Thompson vec->state++; 512e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5130436c2adSjeremylt } 5140436c2adSjeremylt 5150436c2adSjeremylt /** 516ca94c3ddSJeremy L Thompson @brief Get read-only access to a `CeedVector` via the specified memory type. 5174385fb7fSSebastian Grimberg 518b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArrayRead(). 5190436c2adSjeremylt 520ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to access 521ca94c3ddSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 522ca94c3ddSJeremy L Thompson If the backend uses a different memory type, this will perform a copy (possibly cached). 523ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 5240436c2adSjeremylt 5250436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5260436c2adSjeremylt 5277a982d89SJeremy L. Thompson @ref User 5280436c2adSjeremylt **/ 5292b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 5301203703bSJeremy L Thompson CeedSize length; 5310436c2adSjeremylt 5329bc66399SJeremy L Thompson CeedCheck(vec->GetArrayRead, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); 5339bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 5349bc66399SJeremy L Thompson "Cannot grant CeedVector read-only array access, the access lock is already in use"); 5351203703bSJeremy L Thompson 5361203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5371203703bSJeremy L Thompson if (length > 0) { 5389c774eddSJeremy L Thompson bool has_valid_array = true; 539b0976d5aSZach Atkins 5402b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5419bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 5422b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5439c774eddSJeremy L Thompson 5442b730f8bSJeremy L Thompson CeedCall(vec->GetArrayRead(vec, mem_type, array)); 54550c643e1SJed Brown } else { 54650c643e1SJed Brown *array = NULL; 54750c643e1SJed Brown } 548d1d35e2fSjeremylt vec->num_readers++; 549e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5500436c2adSjeremylt } 5510436c2adSjeremylt 5520436c2adSjeremylt /** 553ca94c3ddSJeremy L Thompson @brief Get write access to a `CeedVector` via the specified memory type. 5544385fb7fSSebastian Grimberg 555ea61e9acSJeremy L Thompson Restore access with @ref CeedVectorRestoreArray(). 556ea61e9acSJeremy L Thompson All old values should be assumed to be invalid. 5579c774eddSJeremy L Thompson 558ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to access 559ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 560ca94c3ddSJeremy L Thompson @param[out] array Array on memory type `mem_type` 5619c774eddSJeremy L Thompson 5629c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5639c774eddSJeremy L Thompson 5649c774eddSJeremy L Thompson @ref User 5659c774eddSJeremy L Thompson **/ 5662b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 5671203703bSJeremy L Thompson CeedSize length; 5689c774eddSJeremy L Thompson 5699bc66399SJeremy L Thompson CeedCheck(vec->GetArrayWrite, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorGetArrayWrite"); 5709bc66399SJeremy L Thompson CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 5719bc66399SJeremy L Thompson "Cannot grant CeedVector array access, the access lock is already in use"); 5729bc66399SJeremy L Thompson CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 5731203703bSJeremy L Thompson 5741203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5751203703bSJeremy L Thompson if (length > 0) { 5762b730f8bSJeremy L Thompson CeedCall(vec->GetArrayWrite(vec, mem_type, array)); 577b0976d5aSZach Atkins } else { 578b0976d5aSZach Atkins *array = NULL; 579b0976d5aSZach Atkins } 58028bfd0b7SJeremy L Thompson vec->state++; 5819c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 5829c774eddSJeremy L Thompson } 5839c774eddSJeremy L Thompson 5849c774eddSJeremy L Thompson /** 585ea61e9acSJeremy L Thompson @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite() 5860436c2adSjeremylt 587ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to restore 588ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 5890436c2adSjeremylt 5900436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5910436c2adSjeremylt 5927a982d89SJeremy L. Thompson @ref User 5930436c2adSjeremylt **/ 5940436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 5951203703bSJeremy L Thompson CeedSize length; 5961203703bSJeremy L Thompson 5976e536b99SJeremy L Thompson CeedCheck(vec->state % 2 == 1, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted"); 5981203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 5991203703bSJeremy L Thompson if (length > 0 && vec->RestoreArray) CeedCall(vec->RestoreArray(vec)); 6000436c2adSjeremylt *array = NULL; 60128bfd0b7SJeremy L Thompson vec->state++; 602e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6030436c2adSjeremylt } 6040436c2adSjeremylt 6050436c2adSjeremylt /** 606b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 6070436c2adSjeremylt 608ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to restore 609ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 6100436c2adSjeremylt 6110436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 6120436c2adSjeremylt 6137a982d89SJeremy L. Thompson @ref User 6140436c2adSjeremylt **/ 6150436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 6161203703bSJeremy L Thompson CeedSize length; 6171203703bSJeremy L Thompson 6186e536b99SJeremy L Thompson CeedCheck(vec->num_readers > 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, 6196e536b99SJeremy L Thompson "Cannot restore CeedVector array read access, access was not granted"); 62075a19770SJeremy L Thompson vec->num_readers--; 6211203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 6221203703bSJeremy L Thompson if (length > 0 && vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec)); 6230436c2adSjeremylt *array = NULL; 624e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6250436c2adSjeremylt } 6260436c2adSjeremylt 6270436c2adSjeremylt /** 628ca94c3ddSJeremy L Thompson @brief Get the norm of a `CeedVector`. 629171f8ca9Sjeremylt 630ca94c3ddSJeremy L Thompson Note: This operation is local to the `CeedVector`. 631ca94c3ddSJeremy 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. 632547d9b97Sjeremylt 633ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve maximum value 634ea61e9acSJeremy L Thompson @param[in] norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 635547d9b97Sjeremylt @param[out] norm Variable to store norm value 636547d9b97Sjeremylt 637547d9b97Sjeremylt @return An error code: 0 - success, otherwise - failure 638547d9b97Sjeremylt 6397a982d89SJeremy L. Thompson @ref User 640547d9b97Sjeremylt **/ 641d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) { 6429c774eddSJeremy L Thompson bool has_valid_array = true; 6431203703bSJeremy L Thompson CeedSize length; 6441203703bSJeremy L Thompson 6452b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 6466e536b99SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 6472b730f8bSJeremy L Thompson "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6489c774eddSJeremy L Thompson 6491203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 6501203703bSJeremy L Thompson if (length == 0) { 651b0976d5aSZach Atkins *norm = 0; 652b0976d5aSZach Atkins return CEED_ERROR_SUCCESS; 653b0976d5aSZach Atkins } 654b0976d5aSZach Atkins 655547d9b97Sjeremylt // Backend impl for GPU, if added 656547d9b97Sjeremylt if (vec->Norm) { 6572b730f8bSJeremy L Thompson CeedCall(vec->Norm(vec, norm_type, norm)); 658e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 659547d9b97Sjeremylt } 660547d9b97Sjeremylt 661547d9b97Sjeremylt const CeedScalar *array; 6622b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 663b0976d5aSZach Atkins assert(array); 664547d9b97Sjeremylt 665547d9b97Sjeremylt *norm = 0.; 666d1d35e2fSjeremylt switch (norm_type) { 667547d9b97Sjeremylt case CEED_NORM_1: 6681203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 669547d9b97Sjeremylt *norm += fabs(array[i]); 670547d9b97Sjeremylt } 671547d9b97Sjeremylt break; 672547d9b97Sjeremylt case CEED_NORM_2: 6731203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 674547d9b97Sjeremylt *norm += fabs(array[i]) * fabs(array[i]); 675547d9b97Sjeremylt } 676547d9b97Sjeremylt break; 677547d9b97Sjeremylt case CEED_NORM_MAX: 6781203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 679d1d35e2fSjeremylt const CeedScalar abs_v_i = fabs(array[i]); 680d1d35e2fSjeremylt *norm = *norm > abs_v_i ? *norm : abs_v_i; 681547d9b97Sjeremylt } 682547d9b97Sjeremylt } 6832b730f8bSJeremy L Thompson if (norm_type == CEED_NORM_2) *norm = sqrt(*norm); 684547d9b97Sjeremylt 6852b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 686e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 687547d9b97Sjeremylt } 688547d9b97Sjeremylt 689547d9b97Sjeremylt /** 690ca94c3ddSJeremy L Thompson @brief Compute `x = alpha x` 691e0dd3b27Sjeremylt 692ca94c3ddSJeremy L Thompson @param[in,out] x `CeedVector` for scaling 69396b902e2Sjeremylt @param[in] alpha scaling factor 694e0dd3b27Sjeremylt 695e0dd3b27Sjeremylt @return An error code: 0 - success, otherwise - failure 696e0dd3b27Sjeremylt 697e0dd3b27Sjeremylt @ref User 698e0dd3b27Sjeremylt **/ 699e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) { 7009c774eddSJeremy L Thompson bool has_valid_array = true; 7011203703bSJeremy L Thompson CeedSize length; 7021203703bSJeremy L Thompson CeedScalar *x_array = NULL; 7031c66c397SJeremy L Thompson 7042b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array)); 7056e536b99SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(x), CEED_ERROR_BACKEND, 7062b730f8bSJeremy L Thompson "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7079c774eddSJeremy L Thompson 708b0976d5aSZach Atkins // Return early for empty vector 7091203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length)); 7101203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 711b0976d5aSZach Atkins 712e0dd3b27Sjeremylt // Backend implementation 7132b730f8bSJeremy L Thompson if (x->Scale) return x->Scale(x, alpha); 714e0dd3b27Sjeremylt 715e0dd3b27Sjeremylt // Default implementation 716567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(x, CEED_MEM_HOST, &x_array)); 717b0976d5aSZach Atkins assert(x_array); 7181203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha; 7192b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(x, &x_array)); 720e0dd3b27Sjeremylt return CEED_ERROR_SUCCESS; 721e0dd3b27Sjeremylt } 722e0dd3b27Sjeremylt 723e0dd3b27Sjeremylt /** 724ca94c3ddSJeremy L Thompson @brief Compute `y = alpha x + y` 7250f7fd0f8Sjeremylt 726ca94c3ddSJeremy L Thompson @param[in,out] y target `CeedVector` for sum 72796b902e2Sjeremylt @param[in] alpha scaling factor 728ca94c3ddSJeremy L Thompson @param[in] x second `CeedVector`, must be different than ``y` 7290f7fd0f8Sjeremylt 7300f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 7310f7fd0f8Sjeremylt 7320f7fd0f8Sjeremylt @ref User 7330f7fd0f8Sjeremylt **/ 7340f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { 7351203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 7361203703bSJeremy L Thompson CeedSize length_x, length_y; 73773380422SJeremy L Thompson CeedScalar *y_array = NULL; 73873380422SJeremy L Thompson CeedScalar const *x_array = NULL; 7390f7fd0f8Sjeremylt 7401203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 7411203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 7429bc66399SJeremy L Thompson CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, 7433f08121cSJeremy L Thompson "Cannot add vector of different lengths." 7443f08121cSJeremy L Thompson " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, 7453f08121cSJeremy L Thompson length_x, length_y); 7469bc66399SJeremy L Thompson CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); 7470f7fd0f8Sjeremylt 7482b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 7499bc66399SJeremy L Thompson CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 7506574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7512b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 7529bc66399SJeremy L Thompson CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 7536574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7549c774eddSJeremy L Thompson 7559bc66399SJeremy L Thompson { 7569bc66399SJeremy L Thompson Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y; 7579bc66399SJeremy L Thompson 7589bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed_y)); 7599bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(x, &ceed_x)); 7609bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); 7619bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); 7629bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_x)); 7639bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_y)); 7649bc66399SJeremy L Thompson CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE, 7659bc66399SJeremy L Thompson "Vectors x and y must be created by the same Ceed context"); 7669bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_x)); 7679bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_y)); 7689bc66399SJeremy L Thompson } 7692d04630dSjeremylt 770b0976d5aSZach Atkins // Return early for empty vectors 7711203703bSJeremy L Thompson if (length_y == 0) return CEED_ERROR_SUCCESS; 772b0976d5aSZach Atkins 7730f7fd0f8Sjeremylt // Backend implementation 774eaf62fffSJeremy L Thompson if (y->AXPY) { 7752b730f8bSJeremy L Thompson CeedCall(y->AXPY(y, alpha, x)); 776eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 777eaf62fffSJeremy L Thompson } 7780f7fd0f8Sjeremylt 7790f7fd0f8Sjeremylt // Default implementation 780567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 7812b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 7820f7fd0f8Sjeremylt 7832b730f8bSJeremy L Thompson assert(x_array); 7842b730f8bSJeremy L Thompson assert(y_array); 78573380422SJeremy L Thompson 7861203703bSJeremy L Thompson for (CeedSize i = 0; i < length_y; i++) y_array[i] += alpha * x_array[i]; 7870f7fd0f8Sjeremylt 7882b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(y, &y_array)); 7892b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 7900f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 7910f7fd0f8Sjeremylt } 7920f7fd0f8Sjeremylt 7930f7fd0f8Sjeremylt /** 794ca94c3ddSJeremy L Thompson @brief Compute `y = alpha x + beta y` 7955fb68f37SKaren (Ren) Stengel 796ca94c3ddSJeremy L Thompson @param[in,out] y target `CeedVector` for sum 7975fb68f37SKaren (Ren) Stengel @param[in] alpha first scaling factor 7985fb68f37SKaren (Ren) Stengel @param[in] beta second scaling factor 799ca94c3ddSJeremy L Thompson @param[in] x second `CeedVector`, must be different than `y` 8005fb68f37SKaren (Ren) Stengel 8015fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 8025fb68f37SKaren (Ren) Stengel 8035fb68f37SKaren (Ren) Stengel @ref User 8045fb68f37SKaren (Ren) Stengel **/ 8055fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 8061203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 8071203703bSJeremy L Thompson CeedSize length_x, length_y; 8085fb68f37SKaren (Ren) Stengel CeedScalar *y_array = NULL; 8095fb68f37SKaren (Ren) Stengel CeedScalar const *x_array = NULL; 8101203703bSJeremy L Thompson 8111203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 8121203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 8139bc66399SJeremy L Thompson CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, 8143f08121cSJeremy L Thompson "Cannot add vector of different lengths." 8153f08121cSJeremy L Thompson " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, 8163f08121cSJeremy L Thompson length_x, length_y); 8179bc66399SJeremy L Thompson CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY"); 8185fb68f37SKaren (Ren) Stengel 8195fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 8209bc66399SJeremy L Thompson CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 8216574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8225fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 8239bc66399SJeremy L Thompson CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, 8246574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8255fb68f37SKaren (Ren) Stengel 8269bc66399SJeremy L Thompson { 8279bc66399SJeremy L Thompson Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y; 8289bc66399SJeremy L Thompson 8299bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed_y)); 8309bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(x, &ceed_x)); 8319bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); 8329bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); 8339bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_x)); 8349bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_y)); 8359bc66399SJeremy L Thompson CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE, 8369bc66399SJeremy L Thompson "Vectors x and y must be created by the same Ceed context"); 8379bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_x)); 8389bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_y)); 8399bc66399SJeremy L Thompson } 8405fb68f37SKaren (Ren) Stengel 841b0976d5aSZach Atkins // Return early for empty vectors 8421203703bSJeremy L Thompson if (length_y == 0) return CEED_ERROR_SUCCESS; 843b0976d5aSZach Atkins 8445fb68f37SKaren (Ren) Stengel // Backend implementation 8455fb68f37SKaren (Ren) Stengel if (y->AXPBY) { 8465fb68f37SKaren (Ren) Stengel CeedCall(y->AXPBY(y, alpha, beta, x)); 8475fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 8485fb68f37SKaren (Ren) Stengel } 8495fb68f37SKaren (Ren) Stengel 8505fb68f37SKaren (Ren) Stengel // Default implementation 8515fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 8525fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 8535fb68f37SKaren (Ren) Stengel 8545fb68f37SKaren (Ren) Stengel assert(x_array); 8555fb68f37SKaren (Ren) Stengel assert(y_array); 8565fb68f37SKaren (Ren) Stengel 8571203703bSJeremy L Thompson for (CeedSize i = 0; i < length_y; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i]; 8585fb68f37SKaren (Ren) Stengel 8595fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(y, &y_array)); 8605fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 8615fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 8625fb68f37SKaren (Ren) Stengel } 8635fb68f37SKaren (Ren) Stengel 8645fb68f37SKaren (Ren) Stengel /** 865ca94c3ddSJeremy L Thompson @brief Compute the pointwise multiplication \f$w = x .* y\f$. 8664385fb7fSSebastian Grimberg 867ca94c3ddSJeremy L Thompson Any subset of `x`, `y`, and `w` may be the same `CeedVector`. 8680f7fd0f8Sjeremylt 869ca94c3ddSJeremy L Thompson @param[out] w target `CeedVector` for the product 870ca94c3ddSJeremy L Thompson @param[in] x first `CeedVector` for product 871ca94c3ddSJeremy L Thompson @param[in] y second `CeedVector` for the product 8720f7fd0f8Sjeremylt 8730f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 8740f7fd0f8Sjeremylt 8750f7fd0f8Sjeremylt @ref User 8760f7fd0f8Sjeremylt **/ 8770f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { 8781203703bSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 87973380422SJeremy L Thompson CeedScalar *w_array = NULL; 88073380422SJeremy L Thompson CeedScalar const *x_array = NULL, *y_array = NULL; 8811203703bSJeremy L Thompson CeedSize length_w, length_x, length_y; 8820f7fd0f8Sjeremylt 8831203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(w, &length_w)); 8841203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &length_x)); 8851203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &length_y)); 8869bc66399SJeremy L Thompson CeedCheck(length_x >= length_w && length_y >= length_w, CeedVectorReturnCeed(w), CEED_ERROR_UNSUPPORTED, 887ccad5cb9SJeremy L Thompson "Cannot pointwise multiply vectors of incompatible lengths." 888ccad5cb9SJeremy L Thompson " w length: %" CeedSize_FMT " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, 889ccad5cb9SJeremy L Thompson length_w, length_x, length_y); 8900f7fd0f8Sjeremylt 8919bc66399SJeremy L Thompson { 8929bc66399SJeremy L Thompson Ceed ceed_w, ceed_x, ceed_y, ceed_parent_w, ceed_parent_x, ceed_parent_y; 8939bc66399SJeremy L Thompson 8949bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(w, &ceed_w)); 8959bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(x, &ceed_x)); 8969bc66399SJeremy L Thompson CeedCall(CeedVectorGetCeed(y, &ceed_y)); 8979bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_w, &ceed_parent_w)); 8989bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); 8999bc66399SJeremy L Thompson CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); 9009bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_w)); 9019bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_x)); 9029bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_y)); 9039bc66399SJeremy L Thompson CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, CeedVectorReturnCeed(w), CEED_ERROR_INCOMPATIBLE, 9046574a04fSJeremy L Thompson "Vectors w, x, and y must be created by the same Ceed context"); 9059bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_w)); 9069bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_x)); 9079bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent_y)); 9089bc66399SJeremy L Thompson } 9092d04630dSjeremylt 9102b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 9119bc66399SJeremy L Thompson CeedCheck(has_valid_array_x, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND, 9126574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9132b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 9149bc66399SJeremy L Thompson CeedCheck(has_valid_array_y, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND, 9156574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9169c774eddSJeremy L Thompson 917b0976d5aSZach Atkins // Return early for empty vectors 9181203703bSJeremy L Thompson if (length_w == 0) return CEED_ERROR_SUCCESS; 919b0976d5aSZach Atkins 9200f7fd0f8Sjeremylt // Backend implementation 921eaf62fffSJeremy L Thompson if (w->PointwiseMult) { 9222b730f8bSJeremy L Thompson CeedCall(w->PointwiseMult(w, x, y)); 923eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 924eaf62fffSJeremy L Thompson } 9250f7fd0f8Sjeremylt 9260f7fd0f8Sjeremylt // Default implementation 927b0976d5aSZach Atkins if (x == w || y == w) { 928b0976d5aSZach Atkins CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 929b0976d5aSZach Atkins } else { 930b0976d5aSZach Atkins CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array)); 931b0976d5aSZach Atkins } 9320f7fd0f8Sjeremylt if (x != w) { 9332b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 9340f7fd0f8Sjeremylt } else { 9350f7fd0f8Sjeremylt x_array = w_array; 9360f7fd0f8Sjeremylt } 9370f7fd0f8Sjeremylt if (y != w && y != x) { 9382b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array)); 939c6d796c6SJeremy L Thompson } else if (y == x) { 9400f7fd0f8Sjeremylt y_array = x_array; 941b0976d5aSZach Atkins } else if (y == w) { 942c6d796c6SJeremy L Thompson y_array = w_array; 9430f7fd0f8Sjeremylt } 9440f7fd0f8Sjeremylt 9452b730f8bSJeremy L Thompson assert(w_array); 9462b730f8bSJeremy L Thompson assert(x_array); 9472b730f8bSJeremy L Thompson assert(y_array); 94873380422SJeremy L Thompson 9491203703bSJeremy L Thompson for (CeedSize i = 0; i < length_w; i++) w_array[i] = x_array[i] * y_array[i]; 9500f7fd0f8Sjeremylt 9512b730f8bSJeremy L Thompson if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array)); 9522b730f8bSJeremy L Thompson if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 9532b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(w, &w_array)); 9540f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 9550f7fd0f8Sjeremylt } 9560f7fd0f8Sjeremylt 9570f7fd0f8Sjeremylt /** 958ca94c3ddSJeremy L Thompson @brief Take the reciprocal of a `CeedVector`. 959d99fa3c5SJeremy L Thompson 960ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to take reciprocal 961d99fa3c5SJeremy L Thompson 962d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 963d99fa3c5SJeremy L Thompson 964d99fa3c5SJeremy L Thompson @ref User 965d99fa3c5SJeremy L Thompson **/ 966d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) { 9679c774eddSJeremy L Thompson bool has_valid_array = true; 9681203703bSJeremy L Thompson CeedSize length; 9691203703bSJeremy L Thompson CeedScalar *array; 9702b730f8bSJeremy L Thompson 9711c66c397SJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 9729bc66399SJeremy L Thompson CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, 9732b730f8bSJeremy L Thompson "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9749c774eddSJeremy L Thompson 975d99fa3c5SJeremy L Thompson // Check if vector data set 9769bc66399SJeremy L Thompson CeedCheck(vec->state > 0, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); 977d99fa3c5SJeremy L Thompson 978b0976d5aSZach Atkins // Return early for empty vector 9791203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 9801203703bSJeremy L Thompson if (length == 0) return CEED_ERROR_SUCCESS; 981b0976d5aSZach Atkins 982d99fa3c5SJeremy L Thompson // Backend impl for GPU, if added 983d99fa3c5SJeremy L Thompson if (vec->Reciprocal) { 9842b730f8bSJeremy L Thompson CeedCall(vec->Reciprocal(vec)); 985e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 986d99fa3c5SJeremy L Thompson } 987d99fa3c5SJeremy L Thompson 988567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array)); 9891203703bSJeremy L Thompson for (CeedSize i = 0; i < length; i++) { 9902b730f8bSJeremy L Thompson if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i]; 9912b730f8bSJeremy L Thompson } 992d99fa3c5SJeremy L Thompson 9932b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 994e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 995d99fa3c5SJeremy L Thompson } 996d99fa3c5SJeremy L Thompson 997d99fa3c5SJeremy L Thompson /** 998ca94c3ddSJeremy L Thompson @brief View a `CeedVector` 9990436c2adSjeremylt 1000acb2c48cSJeremy L Thompson Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`. 1001ca94c3ddSJeremy L Thompson Any portion of the provided range that is outside the range of valid indices for the `CeedVector` will be ignored. 1002acb2c48cSJeremy L Thompson 1003ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to view 1004f52f9f6cSJeremy L Thompson @param[in] start Index of first `CeedVector` entry to view in the range `[start, stop)` 1005f52f9f6cSJeremy L Thompson @param[in] stop One past the last element to view in the range, or `-1` for `length` 1006ca94c3ddSJeremy L Thompson @param[in] step Step between `CeedVector` entries to view 1007acb2c48cSJeremy L Thompson @param[in] fp_fmt Printing format 1008acb2c48cSJeremy L Thompson @param[in] stream Filestream to write to 1009acb2c48cSJeremy L Thompson 1010acb2c48cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1011acb2c48cSJeremy L Thompson 1012acb2c48cSJeremy L Thompson @ref User 1013acb2c48cSJeremy L Thompson **/ 1014acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) { 1015acb2c48cSJeremy L Thompson char fmt[1024]; 10161203703bSJeremy L Thompson CeedSize length; 10171203703bSJeremy L Thompson const CeedScalar *x; 1018acb2c48cSJeremy L Thompson 10196e536b99SJeremy L Thompson CeedCheck(step != 0, CeedVectorReturnCeed(vec), CEED_ERROR_MINOR, "View range 'step' must be nonzero"); 1020acb2c48cSJeremy L Thompson 10211203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 10221203703bSJeremy L Thompson fprintf(stream, "CeedVector length %" CeedSize_FMT "\n", length); 10231203703bSJeremy L Thompson if (start != 0 || stop != length || step != 1) { 10241203703bSJeremy L Thompson fprintf(stream, " start: %" CeedSize_FMT "\n stop: %" CeedSize_FMT "\n step: %" CeedInt_FMT "\n", start, stop, step); 1025acb2c48cSJeremy L Thompson } 10261203703bSJeremy L Thompson if (start > length) start = length; 1027f52f9f6cSJeremy L Thompson if (stop == -1 || stop > length) stop = length; 1028acb2c48cSJeremy L Thompson 1029acb2c48cSJeremy L Thompson snprintf(fmt, sizeof fmt, " %s\n", fp_fmt ? fp_fmt : "%g"); 1030acb2c48cSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x)); 1031b94338b9SJed Brown for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]); 1032acb2c48cSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &x)); 10331203703bSJeremy L Thompson if (stop != length) fprintf(stream, " ...\n"); 1034acb2c48cSJeremy L Thompson return CEED_ERROR_SUCCESS; 1035acb2c48cSJeremy L Thompson } 1036acb2c48cSJeremy L Thompson 1037acb2c48cSJeremy L Thompson /** 1038ca94c3ddSJeremy L Thompson @brief View a `CeedVector` 1039acb2c48cSJeremy L Thompson 1040ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to view 1041d1d35e2fSjeremylt @param[in] fp_fmt Printing format 10420a0da059Sjeremylt @param[in] stream Filestream to write to 10430a0da059Sjeremylt 10440436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 10450436c2adSjeremylt 10467a982d89SJeremy L. Thompson @ref User 10470436c2adSjeremylt **/ 1048d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { 10491203703bSJeremy L Thompson CeedSize length; 10501203703bSJeremy L Thompson 10511203703bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &length)); 10521203703bSJeremy L Thompson CeedCall(CeedVectorViewRange(vec, 0, length, 1, fp_fmt, stream)); 1053e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10540436c2adSjeremylt } 10550436c2adSjeremylt 10560436c2adSjeremylt /** 1057ca94c3ddSJeremy L Thompson @brief Get the `Ceed` associated with a `CeedVector` 1058b7c9bbdaSJeremy L Thompson 1059ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 1060ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 1061b7c9bbdaSJeremy L Thompson 1062b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1063b7c9bbdaSJeremy L Thompson 1064b7c9bbdaSJeremy L Thompson @ref Advanced 1065b7c9bbdaSJeremy L Thompson **/ 1066b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 10679bc66399SJeremy L Thompson *ceed = NULL; 10689bc66399SJeremy L Thompson CeedCall(CeedReferenceCopy(CeedVectorReturnCeed(vec), ceed)); 1069b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1070b7c9bbdaSJeremy L Thompson } 1071b7c9bbdaSJeremy L Thompson 1072b7c9bbdaSJeremy L Thompson /** 10736e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedVector` 10746e536b99SJeremy L Thompson 10756e536b99SJeremy L Thompson @param[in] vec `CeedVector` to retrieve state 10766e536b99SJeremy L Thompson 10776e536b99SJeremy L Thompson @return `Ceed` associated with the `vec` 10786e536b99SJeremy L Thompson 10796e536b99SJeremy L Thompson @ref Advanced 10806e536b99SJeremy L Thompson **/ 10816e536b99SJeremy L Thompson Ceed CeedVectorReturnCeed(CeedVector vec) { return vec->ceed; } 10826e536b99SJeremy L Thompson 10836e536b99SJeremy L Thompson /** 1084ca94c3ddSJeremy L Thompson @brief Get the length of a `CeedVector` 10850436c2adSjeremylt 1086ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to retrieve length 10877a982d89SJeremy L. Thompson @param[out] length Variable to store length 10880436c2adSjeremylt 10890436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 10900436c2adSjeremylt 10917a982d89SJeremy L. Thompson @ref User 10920436c2adSjeremylt **/ 10931f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) { 10947a982d89SJeremy L. Thompson *length = vec->length; 1095e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10960436c2adSjeremylt } 10970436c2adSjeremylt 10980436c2adSjeremylt /** 1099ca94c3ddSJeremy L Thompson @brief Destroy a `CeedVector` 11000436c2adSjeremylt 1101ca94c3ddSJeremy L Thompson @param[in,out] vec `CeedVector` to destroy 11020436c2adSjeremylt 11030436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 11040436c2adSjeremylt 11057a982d89SJeremy L. Thompson @ref User 11060436c2adSjeremylt **/ 11070436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) { 11087425e127SJeremy L Thompson if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) { 1109ad6481ceSJeremy L Thompson *vec = NULL; 1110ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1111ad6481ceSJeremy L Thompson } 11126574a04fSJeremy L Thompson CeedCheck((*vec)->state % 2 == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use"); 11136574a04fSJeremy L Thompson CeedCheck((*vec)->num_readers == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access"); 11140436c2adSjeremylt 11152b730f8bSJeremy L Thompson if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec)); 11162b730f8bSJeremy L Thompson 11172b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*vec)->ceed)); 11182b730f8bSJeremy L Thompson CeedCall(CeedFree(vec)); 1119e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11200436c2adSjeremylt } 11210436c2adSjeremylt 11220436c2adSjeremylt /// @} 1123