13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 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 873380422SJeremy L Thompson #include <assert.h> 92b730f8bSJeremy L Thompson #include <ceed-impl.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 112b730f8bSJeremy L Thompson #include <ceed/ceed.h> 12547d9b97Sjeremylt #include <math.h> 133d576824SJeremy L Thompson #include <stdint.h> 143d576824SJeremy L Thompson #include <stdio.h> 150436c2adSjeremylt 167a982d89SJeremy L. Thompson /// @file 177a982d89SJeremy L. Thompson /// Implementation of public CeedVector interfaces 187a982d89SJeremy L. Thompson 190436c2adSjeremylt /// @cond DOXYGEN_SKIP 200436c2adSjeremylt static struct CeedVector_private ceed_vector_active; 210436c2adSjeremylt static struct CeedVector_private ceed_vector_none; 220436c2adSjeremylt /// @endcond 230436c2adSjeremylt 247a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 257a982d89SJeremy L. Thompson /// @{ 267a982d89SJeremy L. Thompson 27ea61e9acSJeremy L Thompson /// Indicate that vector will be provided as an explicit argument to CeedOperatorApply(). 287a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_ACTIVE = &ceed_vector_active; 297a982d89SJeremy L. Thompson 3096b902e2Sjeremylt /// Indicate that no vector is applicable (i.e., for @ref CEED_EVAL_WEIGHT). 317a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_NONE = &ceed_vector_none; 327a982d89SJeremy L. Thompson 337a982d89SJeremy L. Thompson /// @} 347a982d89SJeremy L. Thompson 357a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 367a982d89SJeremy L. Thompson /// CeedVector Backend API 377a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 387a982d89SJeremy L. Thompson /// @addtogroup CeedVectorBackend 397a982d89SJeremy L. Thompson /// @{ 407a982d89SJeremy L. Thompson 417a982d89SJeremy L. Thompson /** 429c774eddSJeremy L Thompson @brief Check for valid data in a CeedVector 439c774eddSJeremy L Thompson 44ea61e9acSJeremy L Thompson @param[in] vec CeedVector to check validity 459c774eddSJeremy L Thompson @param[out] has_valid_array Variable to store validity 469c774eddSJeremy L Thompson 479c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 489c774eddSJeremy L Thompson 499c774eddSJeremy L Thompson @ref Backend 509c774eddSJeremy L Thompson **/ 519c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) { 522b730f8bSJeremy L Thompson if (!vec->HasValidArray) { 539c774eddSJeremy L Thompson // LCOV_EXCL_START 542b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasValidArray"); 559c774eddSJeremy L Thompson // LCOV_EXCL_STOP 562b730f8bSJeremy L Thompson } 579c774eddSJeremy L Thompson 582b730f8bSJeremy L Thompson CeedCall(vec->HasValidArray(vec, has_valid_array)); 599c774eddSJeremy L Thompson 609c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 619c774eddSJeremy L Thompson } 629c774eddSJeremy L Thompson 639c774eddSJeremy L Thompson /** 649c774eddSJeremy L Thompson @brief Check for borrowed array of a specific CeedMemType in a CeedVector 659c774eddSJeremy L Thompson 66ea61e9acSJeremy L Thompson @param[in] vec CeedVector to check 67ea61e9acSJeremy L Thompson @param[in] mem_type Memory type to check 689c774eddSJeremy L Thompson @param[out] has_borrowed_array_of_type Variable to store result 699c774eddSJeremy L Thompson 709c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 719c774eddSJeremy L Thompson 729c774eddSJeremy L Thompson @ref Backend 739c774eddSJeremy L Thompson **/ 742b730f8bSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 752b730f8bSJeremy L Thompson if (!vec->HasBorrowedArrayOfType) { 769c774eddSJeremy L Thompson // LCOV_EXCL_START 772b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasBorrowedArrayOfType"); 789c774eddSJeremy L Thompson // LCOV_EXCL_STOP 792b730f8bSJeremy L Thompson } 809c774eddSJeremy L Thompson 812b730f8bSJeremy L Thompson CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type)); 829c774eddSJeremy L Thompson 839c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 849c774eddSJeremy L Thompson } 859c774eddSJeremy L Thompson 869c774eddSJeremy L Thompson /** 877a982d89SJeremy L. Thompson @brief Get the state of a CeedVector 887a982d89SJeremy L. Thompson 89ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 907a982d89SJeremy L. Thompson @param[out] state Variable to store state 917a982d89SJeremy L. Thompson 927a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 937a982d89SJeremy L. Thompson 947a982d89SJeremy L. Thompson @ref Backend 957a982d89SJeremy L. Thompson **/ 967a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) { 977a982d89SJeremy L. Thompson *state = vec->state; 98e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 997a982d89SJeremy L. Thompson } 1007a982d89SJeremy L. Thompson 1017a982d89SJeremy L. Thompson /** 10227312b0fSJed Brown @brief Add a reference to a CeedVector 1037a982d89SJeremy L. Thompson 104ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to increment reference counter 1057a982d89SJeremy L. Thompson 1067a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1077a982d89SJeremy L. Thompson 1087a982d89SJeremy L. Thompson @ref Backend 1097a982d89SJeremy L. Thompson **/ 1107a982d89SJeremy L. Thompson int CeedVectorAddReference(CeedVector vec) { 111d1d35e2fSjeremylt vec->ref_count++; 112e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1137a982d89SJeremy L. Thompson } 1147a982d89SJeremy L. Thompson 1157a982d89SJeremy L. Thompson /** 1167a982d89SJeremy L. Thompson @brief Get the backend data of a CeedVector 1177a982d89SJeremy L. Thompson 118ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 1197a982d89SJeremy L. Thompson @param[out] data Variable to store data 1207a982d89SJeremy L. Thompson 1217a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1227a982d89SJeremy L. Thompson 1237a982d89SJeremy L. Thompson @ref Backend 1247a982d89SJeremy L. Thompson **/ 125777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) { 126777ff853SJeremy L Thompson *(void **)data = vec->data; 127e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1287a982d89SJeremy L. Thompson } 1297a982d89SJeremy L. Thompson 1307a982d89SJeremy L. Thompson /** 1317a982d89SJeremy L. Thompson @brief Set the backend data of a CeedVector 1327a982d89SJeremy L. Thompson 133ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to retrieve state 134ea61e9acSJeremy L Thompson @param[in] data Data to set 1357a982d89SJeremy L. Thompson 1367a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1377a982d89SJeremy L. Thompson 1387a982d89SJeremy L. Thompson @ref Backend 1397a982d89SJeremy L. Thompson **/ 140777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) { 141777ff853SJeremy L Thompson vec->data = data; 142e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1437a982d89SJeremy L. Thompson } 1447a982d89SJeremy L. Thompson 14534359f16Sjeremylt /** 14634359f16Sjeremylt @brief Increment the reference counter for a CeedVector 14734359f16Sjeremylt 148ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to increment the reference counter 14934359f16Sjeremylt 15034359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 15134359f16Sjeremylt 15234359f16Sjeremylt @ref Backend 15334359f16Sjeremylt **/ 1549560d06aSjeremylt int CeedVectorReference(CeedVector vec) { 15534359f16Sjeremylt vec->ref_count++; 15634359f16Sjeremylt return CEED_ERROR_SUCCESS; 15734359f16Sjeremylt } 15834359f16Sjeremylt 1597a982d89SJeremy L. Thompson /// @} 1607a982d89SJeremy L. Thompson 1617a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1627a982d89SJeremy L. Thompson /// CeedVector Public API 1637a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1647a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 1650436c2adSjeremylt /// @{ 1660436c2adSjeremylt 1670436c2adSjeremylt /** 1680436c2adSjeremylt @brief Create a CeedVector of the specified length (does not allocate memory) 1690436c2adSjeremylt 170ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedVector will be created 171ea61e9acSJeremy L Thompson @param[in] length Length of vector 172ea61e9acSJeremy L Thompson @param[out] vec Address of the variable where the newly created CeedVector will be stored 1730436c2adSjeremylt 1740436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 1750436c2adSjeremylt 1767a982d89SJeremy L. Thompson @ref User 1770436c2adSjeremylt **/ 1781f9221feSJeremy L Thompson int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) { 1790436c2adSjeremylt if (!ceed->VectorCreate) { 1800436c2adSjeremylt Ceed delegate; 1812b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector")); 1820436c2adSjeremylt 1832b730f8bSJeremy L Thompson if (!delegate) { 1840436c2adSjeremylt // LCOV_EXCL_START 1852b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorCreate"); 1860436c2adSjeremylt // LCOV_EXCL_STOP 1872b730f8bSJeremy L Thompson } 1880436c2adSjeremylt 1892b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(delegate, length, vec)); 190e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1910436c2adSjeremylt } 1920436c2adSjeremylt 1932b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, vec)); 1940436c2adSjeremylt (*vec)->ceed = ceed; 1952b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 196d1d35e2fSjeremylt (*vec)->ref_count = 1; 1970436c2adSjeremylt (*vec)->length = length; 1980436c2adSjeremylt (*vec)->state = 0; 1992b730f8bSJeremy L Thompson CeedCall(ceed->VectorCreate(length, *vec)); 200e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2010436c2adSjeremylt } 2020436c2adSjeremylt 2030436c2adSjeremylt /** 204ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedVector. 205ea61e9acSJeremy L Thompson Both pointers should be destroyed with `CeedVectorDestroy()`. 206*512bb800SJeremy L Thompson 207*512bb800SJeremy 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. 208*512bb800SJeremy L Thompson This CeedVector will be destroyed if `vec_copy` is the only reference to this CeedVector. 2099560d06aSjeremylt 210ea61e9acSJeremy L Thompson @param[in] vec CeedVector to copy reference to 211ea61e9acSJeremy L Thompson @param[in,out] vec_copy Variable to store copied reference 2129560d06aSjeremylt 2139560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 2149560d06aSjeremylt 2159560d06aSjeremylt @ref User 2169560d06aSjeremylt **/ 2179560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) { 2182b730f8bSJeremy L Thompson CeedCall(CeedVectorReference(vec)); 2192b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(vec_copy)); 2209560d06aSjeremylt *vec_copy = vec; 2219560d06aSjeremylt return CEED_ERROR_SUCCESS; 2229560d06aSjeremylt } 2239560d06aSjeremylt 2249560d06aSjeremylt /** 225ea61e9acSJeremy L Thompson @brief Set the array used by a CeedVector, freeing any previously allocated array if applicable. 226ea61e9acSJeremy L Thompson The backend may copy values to a different memtype, such as during @ref CeedOperatorApply(). 2276a6c615bSJeremy L Thompson See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 2280436c2adSjeremylt 229ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 230ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the array being passed 231ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the array 232ea61e9acSJeremy L Thompson @param[in] array Array to be used, or NULL with @ref CEED_COPY_VALUES to have the library allocate 2330436c2adSjeremylt 2340436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2350436c2adSjeremylt 2367a982d89SJeremy L. Thompson @ref User 2370436c2adSjeremylt **/ 2382b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 2392b730f8bSJeremy L Thompson if (!vec->SetArray) { 2400436c2adSjeremylt // LCOV_EXCL_START 2412b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); 2420436c2adSjeremylt // LCOV_EXCL_STOP 2432b730f8bSJeremy L Thompson } 2442b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 2452b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 2462b730f8bSJeremy L Thompson } 2472b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 2482b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 2492b730f8bSJeremy L Thompson } 2500436c2adSjeremylt 2512b730f8bSJeremy L Thompson CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); 2520436c2adSjeremylt vec->state += 2; 253e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2540436c2adSjeremylt } 2550436c2adSjeremylt 2560436c2adSjeremylt /** 2570436c2adSjeremylt @brief Set the CeedVector to a constant value 2580436c2adSjeremylt 259ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 2600436c2adSjeremylt @param[in] value Value to be used 2610436c2adSjeremylt 2620436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2630436c2adSjeremylt 2647a982d89SJeremy L. Thompson @ref User 2650436c2adSjeremylt **/ 2660436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 2672b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 2689c774eddSJeremy L Thompson // LCOV_EXCL_START 2692b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 2709c774eddSJeremy L Thompson // LCOV_EXCL_STOP 2712b730f8bSJeremy L Thompson } 2722b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 2739c774eddSJeremy L Thompson // LCOV_EXCL_START 2742b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 2759c774eddSJeremy L Thompson // LCOV_EXCL_STOP 2762b730f8bSJeremy L Thompson } 2770436c2adSjeremylt 2780436c2adSjeremylt if (vec->SetValue) { 2792b730f8bSJeremy L Thompson CeedCall(vec->SetValue(vec, value)); 2800436c2adSjeremylt } else { 2810436c2adSjeremylt CeedScalar *array; 2822b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 28392ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) array[i] = value; 2842b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 2850436c2adSjeremylt } 2860436c2adSjeremylt vec->state += 2; 287e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2880436c2adSjeremylt } 2890436c2adSjeremylt 2900436c2adSjeremylt /** 291ea61e9acSJeremy L Thompson @brief Sync the CeedVector to a specified memtype. 292ea61e9acSJeremy L Thompson This function is used to force synchronization of arrays set with @ref CeedVectorSetArray(). 293ea61e9acSJeremy L Thompson If the requested memtype is already synchronized, this function results in a no-op. 2940436c2adSjeremylt 295ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 296ea61e9acSJeremy L Thompson @param[in] mem_type Memtype to be synced 2970436c2adSjeremylt 2980436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2990436c2adSjeremylt 3007a982d89SJeremy L. Thompson @ref User 3010436c2adSjeremylt **/ 302d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { 3032b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 3042b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use"); 3052b730f8bSJeremy L Thompson } 3060436c2adSjeremylt 3070436c2adSjeremylt if (vec->SyncArray) { 3082b730f8bSJeremy L Thompson CeedCall(vec->SyncArray(vec, mem_type)); 3090436c2adSjeremylt } else { 3100436c2adSjeremylt const CeedScalar *array; 3112b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array)); 3122b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 3130436c2adSjeremylt } 314e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3150436c2adSjeremylt } 3160436c2adSjeremylt 3170436c2adSjeremylt /** 318ea61e9acSJeremy 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. 3199c774eddSJeremy L Thompson The caller is responsible for managing and freeing the array. 320ea61e9acSJeremy L Thompson This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type. 3216a6c615bSJeremy L Thompson 322ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 323ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to take the array. 324ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 325ea61e9acSJeremy L Thompson @param[out] array Array on memory type mem_type, or NULL if array pointer is not required 3266a6c615bSJeremy L Thompson 3276a6c615bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3286a6c615bSJeremy L Thompson 3296a6c615bSJeremy L Thompson @ref User 3306a6c615bSJeremy L Thompson **/ 3312b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 3322b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 3332b730f8bSJeremy L Thompson // LCOV_EXCL_START 3342b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); 3352b730f8bSJeremy L Thompson // LCOV_EXCL_STOP 3362b730f8bSJeremy L Thompson } 3372b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 3382b730f8bSJeremy L Thompson // LCOV_EXCL_START 3392b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); 3402b730f8bSJeremy L Thompson // LCOV_EXCL_STOP 3412b730f8bSJeremy L Thompson } 3426a6c615bSJeremy L Thompson 34350c643e1SJed Brown CeedScalar *temp_array = NULL; 34450c643e1SJed Brown if (vec->length > 0) { 3459c774eddSJeremy L Thompson bool has_borrowed_array_of_type = true; 3462b730f8bSJeremy L Thompson CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); 3472b730f8bSJeremy L Thompson if (!has_borrowed_array_of_type) { 3489c774eddSJeremy L Thompson // LCOV_EXCL_START 3492b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", 3502b730f8bSJeremy L Thompson CeedMemTypes[mem_type]); 3519c774eddSJeremy L Thompson // LCOV_EXCL_STOP 3522b730f8bSJeremy L Thompson } 3539c774eddSJeremy L Thompson 3549c774eddSJeremy L Thompson bool has_valid_array = true; 3552b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 3562b730f8bSJeremy L Thompson if (!has_valid_array) { 3579c774eddSJeremy L Thompson // LCOV_EXCL_START 3589c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 3592b730f8bSJeremy L Thompson "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); 3609c774eddSJeremy L Thompson // LCOV_EXCL_STOP 3612b730f8bSJeremy L Thompson } 3629c774eddSJeremy L Thompson 3632b730f8bSJeremy L Thompson CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); 36450c643e1SJed Brown } 365d1d35e2fSjeremylt if (array) (*array) = temp_array; 366e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3676a6c615bSJeremy L Thompson } 3686a6c615bSJeremy L Thompson 3696a6c615bSJeremy L Thompson /** 370b3cf021fSjeremylt @brief Get read/write access to a CeedVector via the specified memory type. 371b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArray(). 3720436c2adSjeremylt 373ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to access 374ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 375ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 376d1d35e2fSjeremylt @param[out] array Array on memory type mem_type 3770436c2adSjeremylt 378ea61e9acSJeremy L Thompson @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide access to array pointers in the desired memory space. 379ea61e9acSJeremy L Thompson Pairing get/restore allows the Vector to track access, thus knowing if norms or other operations may need to be recomputed. 3800436c2adSjeremylt 3810436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3820436c2adSjeremylt 3837a982d89SJeremy L. Thompson @ref User 3840436c2adSjeremylt **/ 3852b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 3862b730f8bSJeremy L Thompson if (!vec->GetArray) { 3870436c2adSjeremylt // LCOV_EXCL_START 3882b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); 3890436c2adSjeremylt // LCOV_EXCL_STOP 3902b730f8bSJeremy L Thompson } 3912b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 3922b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 3932b730f8bSJeremy L Thompson } 3942b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 3952b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3962b730f8bSJeremy L Thompson } 3970436c2adSjeremylt 3989c774eddSJeremy L Thompson bool has_valid_array = true; 3992b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4002b730f8bSJeremy L Thompson if (!has_valid_array) { 4019c774eddSJeremy L Thompson // LCOV_EXCL_START 4029c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 4032b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4049c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4052b730f8bSJeremy L Thompson } 4069c774eddSJeremy L Thompson 4072b730f8bSJeremy L Thompson CeedCall(vec->GetArray(vec, mem_type, array)); 40828bfd0b7SJeremy L Thompson vec->state++; 409e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4100436c2adSjeremylt } 4110436c2adSjeremylt 4120436c2adSjeremylt /** 413b3cf021fSjeremylt @brief Get read-only access to a CeedVector via the specified memory type. 414b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArrayRead(). 4150436c2adSjeremylt 416ea61e9acSJeremy L Thompson @param[in] vec CeedVector to access 417ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 418ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy (possibly cached). 419d1d35e2fSjeremylt @param[out] array Array on memory type mem_type 4200436c2adSjeremylt 4210436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4220436c2adSjeremylt 4237a982d89SJeremy L. Thompson @ref User 4240436c2adSjeremylt **/ 4252b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 4262b730f8bSJeremy L Thompson if (!vec->GetArrayRead) { 4270436c2adSjeremylt // LCOV_EXCL_START 4282b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); 4290436c2adSjeremylt // LCOV_EXCL_STOP 4302b730f8bSJeremy L Thompson } 4312b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 4322b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use"); 4332b730f8bSJeremy L Thompson } 4340436c2adSjeremylt 43550c643e1SJed Brown if (vec->length > 0) { 4369c774eddSJeremy L Thompson bool has_valid_array = true; 4372b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4382b730f8bSJeremy L Thompson if (!has_valid_array) { 4399c774eddSJeremy L Thompson // LCOV_EXCL_START 4409c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 4412b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4429c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4432b730f8bSJeremy L Thompson } 4449c774eddSJeremy L Thompson 4452b730f8bSJeremy L Thompson CeedCall(vec->GetArrayRead(vec, mem_type, array)); 44650c643e1SJed Brown } else { 44750c643e1SJed Brown *array = NULL; 44850c643e1SJed Brown } 449d1d35e2fSjeremylt vec->num_readers++; 450e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4510436c2adSjeremylt } 4520436c2adSjeremylt 4530436c2adSjeremylt /** 4549c774eddSJeremy L Thompson @brief Get write access to a CeedVector via the specified memory type. 455ea61e9acSJeremy L Thompson Restore access with @ref CeedVectorRestoreArray(). 456ea61e9acSJeremy L Thompson All old values should be assumed to be invalid. 4579c774eddSJeremy L Thompson 458ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to access 459ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 4609c774eddSJeremy L Thompson @param[out] array Array on memory type mem_type 4619c774eddSJeremy L Thompson 4629c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4639c774eddSJeremy L Thompson 4649c774eddSJeremy L Thompson @ref User 4659c774eddSJeremy L Thompson **/ 4662b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4672b730f8bSJeremy L Thompson if (!vec->GetArrayWrite) { 4689c774eddSJeremy L Thompson // LCOV_EXCL_START 4692b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayWrite"); 4709c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4712b730f8bSJeremy L Thompson } 4722b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 4739c774eddSJeremy L Thompson // LCOV_EXCL_START 4742b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 4759c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4762b730f8bSJeremy L Thompson } 4772b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 4789c774eddSJeremy L Thompson // LCOV_EXCL_START 4792b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 4809c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4812b730f8bSJeremy L Thompson } 4829c774eddSJeremy L Thompson 4832b730f8bSJeremy L Thompson CeedCall(vec->GetArrayWrite(vec, mem_type, array)); 48428bfd0b7SJeremy L Thompson vec->state++; 4859c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 4869c774eddSJeremy L Thompson } 4879c774eddSJeremy L Thompson 4889c774eddSJeremy L Thompson /** 489ea61e9acSJeremy L Thompson @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite() 4900436c2adSjeremylt 491ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to restore 492ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 4930436c2adSjeremylt 4940436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4950436c2adSjeremylt 4967a982d89SJeremy L. Thompson @ref User 4970436c2adSjeremylt **/ 4980436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 4992b730f8bSJeremy L Thompson if (vec->state % 2 != 1) { 5002b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted"); 501706efda3SJeremy L Thompson } 5022b730f8bSJeremy L Thompson if (vec->RestoreArray) CeedCall(vec->RestoreArray(vec)); 5030436c2adSjeremylt *array = NULL; 50428bfd0b7SJeremy L Thompson vec->state++; 505e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5060436c2adSjeremylt } 5070436c2adSjeremylt 5080436c2adSjeremylt /** 509b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 5100436c2adSjeremylt 511ea61e9acSJeremy L Thompson @param[in] vec CeedVector to restore 512ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 5130436c2adSjeremylt 5140436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5150436c2adSjeremylt 5167a982d89SJeremy L. Thompson @ref User 5170436c2adSjeremylt **/ 5180436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 5192b730f8bSJeremy L Thompson if (vec->num_readers == 0) { 5209c774eddSJeremy L Thompson // LCOV_EXCL_START 5212b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted"); 5229c774eddSJeremy L Thompson // LCOV_EXCL_STOP 5232b730f8bSJeremy L Thompson } 5249c774eddSJeremy L Thompson 52575a19770SJeremy L Thompson vec->num_readers--; 5262b730f8bSJeremy L Thompson if (vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec)); 5270436c2adSjeremylt *array = NULL; 52875a19770SJeremy L Thompson 529e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5300436c2adSjeremylt } 5310436c2adSjeremylt 5320436c2adSjeremylt /** 533171f8ca9Sjeremylt @brief Get the norm of a CeedVector. 534171f8ca9Sjeremylt 535ea61e9acSJeremy L Thompson Note: This operation is local to the CeedVector. 536ea61e9acSJeremy 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 537ea61e9acSJeremy L Thompson duplicated or hanging nodes. 538547d9b97Sjeremylt 539ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve maximum value 540ea61e9acSJeremy L Thompson @param[in] norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 541547d9b97Sjeremylt @param[out] norm Variable to store norm value 542547d9b97Sjeremylt 543547d9b97Sjeremylt @return An error code: 0 - success, otherwise - failure 544547d9b97Sjeremylt 5457a982d89SJeremy L. Thompson @ref User 546547d9b97Sjeremylt **/ 547d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) { 5489c774eddSJeremy L Thompson bool has_valid_array = true; 5492b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5502b730f8bSJeremy L Thompson if (!has_valid_array) { 5519c774eddSJeremy L Thompson // LCOV_EXCL_START 5529c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 5532b730f8bSJeremy L Thompson "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5549c774eddSJeremy L Thompson // LCOV_EXCL_STOP 5552b730f8bSJeremy L Thompson } 5569c774eddSJeremy L Thompson 557547d9b97Sjeremylt // Backend impl for GPU, if added 558547d9b97Sjeremylt if (vec->Norm) { 5592b730f8bSJeremy L Thompson CeedCall(vec->Norm(vec, norm_type, norm)); 560e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 561547d9b97Sjeremylt } 562547d9b97Sjeremylt 563547d9b97Sjeremylt const CeedScalar *array; 5642b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 565547d9b97Sjeremylt 566547d9b97Sjeremylt *norm = 0.; 567d1d35e2fSjeremylt switch (norm_type) { 568547d9b97Sjeremylt case CEED_NORM_1: 56992ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) { 570547d9b97Sjeremylt *norm += fabs(array[i]); 571547d9b97Sjeremylt } 572547d9b97Sjeremylt break; 573547d9b97Sjeremylt case CEED_NORM_2: 57492ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) { 575547d9b97Sjeremylt *norm += fabs(array[i]) * fabs(array[i]); 576547d9b97Sjeremylt } 577547d9b97Sjeremylt break; 578547d9b97Sjeremylt case CEED_NORM_MAX: 57992ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) { 580d1d35e2fSjeremylt const CeedScalar abs_v_i = fabs(array[i]); 581d1d35e2fSjeremylt *norm = *norm > abs_v_i ? *norm : abs_v_i; 582547d9b97Sjeremylt } 583547d9b97Sjeremylt } 5842b730f8bSJeremy L Thompson if (norm_type == CEED_NORM_2) *norm = sqrt(*norm); 585547d9b97Sjeremylt 5862b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 587e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 588547d9b97Sjeremylt } 589547d9b97Sjeremylt 590547d9b97Sjeremylt /** 591e0dd3b27Sjeremylt @brief Compute x = alpha x 592e0dd3b27Sjeremylt 59396b902e2Sjeremylt @param[in,out] x vector for scaling 59496b902e2Sjeremylt @param[in] alpha scaling factor 595e0dd3b27Sjeremylt 596e0dd3b27Sjeremylt @return An error code: 0 - success, otherwise - failure 597e0dd3b27Sjeremylt 598e0dd3b27Sjeremylt @ref User 599e0dd3b27Sjeremylt **/ 600e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) { 60173380422SJeremy L Thompson CeedScalar *x_array = NULL; 6021f9221feSJeremy L Thompson CeedSize n_x; 603e0dd3b27Sjeremylt 6049c774eddSJeremy L Thompson bool has_valid_array = true; 6052b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array)); 6062b730f8bSJeremy L Thompson if (!has_valid_array) { 6079c774eddSJeremy L Thompson // LCOV_EXCL_START 6089c774eddSJeremy L Thompson return CeedError(x->ceed, CEED_ERROR_BACKEND, 6092b730f8bSJeremy L Thompson "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6109c774eddSJeremy L Thompson // LCOV_EXCL_STOP 6112b730f8bSJeremy L Thompson } 6129c774eddSJeremy L Thompson 6132b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 614e0dd3b27Sjeremylt 615e0dd3b27Sjeremylt // Backend implementation 6162b730f8bSJeremy L Thompson if (x->Scale) return x->Scale(x, alpha); 617e0dd3b27Sjeremylt 618e0dd3b27Sjeremylt // Default implementation 6192b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array)); 6202b730f8bSJeremy L Thompson for (CeedInt i = 0; i < n_x; i++) x_array[i] *= alpha; 6212b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(x, &x_array)); 622e0dd3b27Sjeremylt 623e0dd3b27Sjeremylt return CEED_ERROR_SUCCESS; 624e0dd3b27Sjeremylt } 625e0dd3b27Sjeremylt 626e0dd3b27Sjeremylt /** 6270f7fd0f8Sjeremylt @brief Compute y = alpha x + y 6280f7fd0f8Sjeremylt 62996b902e2Sjeremylt @param[in,out] y target vector for sum 63096b902e2Sjeremylt @param[in] alpha scaling factor 63196b902e2Sjeremylt @param[in] x second vector, must be different than y 6320f7fd0f8Sjeremylt 6330f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 6340f7fd0f8Sjeremylt 6350f7fd0f8Sjeremylt @ref User 6360f7fd0f8Sjeremylt **/ 6370f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { 63873380422SJeremy L Thompson CeedScalar *y_array = NULL; 63973380422SJeremy L Thompson CeedScalar const *x_array = NULL; 6401f9221feSJeremy L Thompson CeedSize n_x, n_y; 6410f7fd0f8Sjeremylt 6422b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &n_y)); 6432b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 6442b730f8bSJeremy L Thompson if (n_x != n_y) { 6450f7fd0f8Sjeremylt // LCOV_EXCL_START 6462b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 6470f7fd0f8Sjeremylt // LCOV_EXCL_STOP 6482b730f8bSJeremy L Thompson } 6492b730f8bSJeremy L Thompson if (x == y) { 6500f7fd0f8Sjeremylt // LCOV_EXCL_START 6512b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); 6520f7fd0f8Sjeremylt // LCOV_EXCL_STOP 6532b730f8bSJeremy L Thompson } 6540f7fd0f8Sjeremylt 6559c774eddSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 6562b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 6572b730f8bSJeremy L Thompson if (!has_valid_array_x) { 6589c774eddSJeremy L Thompson // LCOV_EXCL_START 6592b730f8bSJeremy L Thompson return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6609c774eddSJeremy L Thompson // LCOV_EXCL_STOP 6612b730f8bSJeremy L Thompson } 6622b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 6632b730f8bSJeremy L Thompson if (!has_valid_array_y) { 6649c774eddSJeremy L Thompson // LCOV_EXCL_START 6652b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6669c774eddSJeremy L Thompson // LCOV_EXCL_STOP 6672b730f8bSJeremy L Thompson } 6689c774eddSJeremy L Thompson 6692d04630dSjeremylt Ceed ceed_parent_x, ceed_parent_y; 6702b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 6712b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 6722b730f8bSJeremy L Thompson if (ceed_parent_x != ceed_parent_y) { 6732d04630dSjeremylt // LCOV_EXCL_START 6742b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); 6752d04630dSjeremylt // LCOV_EXCL_STOP 6762b730f8bSJeremy L Thompson } 6772d04630dSjeremylt 6780f7fd0f8Sjeremylt // Backend implementation 679eaf62fffSJeremy L Thompson if (y->AXPY) { 6802b730f8bSJeremy L Thompson CeedCall(y->AXPY(y, alpha, x)); 681eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 682eaf62fffSJeremy L Thompson } 6830f7fd0f8Sjeremylt 6840f7fd0f8Sjeremylt // Default implementation 6852b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array)); 6862b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 6870f7fd0f8Sjeremylt 6882b730f8bSJeremy L Thompson assert(x_array); 6892b730f8bSJeremy L Thompson assert(y_array); 69073380422SJeremy L Thompson 6912b730f8bSJeremy L Thompson for (CeedInt i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i]; 6920f7fd0f8Sjeremylt 6932b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(y, &y_array)); 6942b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 6950f7fd0f8Sjeremylt 6960f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 6970f7fd0f8Sjeremylt } 6980f7fd0f8Sjeremylt 6990f7fd0f8Sjeremylt /** 700ea61e9acSJeremy L Thompson @brief Compute the pointwise multiplication w = x .* y. 701ea61e9acSJeremy L Thompson Any subset of x, y, and w may be the same vector. 7020f7fd0f8Sjeremylt 70396b902e2Sjeremylt @param[out] w target vector for the product 70496b902e2Sjeremylt @param[in] x first vector for product 70596b902e2Sjeremylt @param[in] y second vector for the product 7060f7fd0f8Sjeremylt 7070f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 7080f7fd0f8Sjeremylt 7090f7fd0f8Sjeremylt @ ref User 7100f7fd0f8Sjeremylt **/ 7110f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { 71273380422SJeremy L Thompson CeedScalar *w_array = NULL; 71373380422SJeremy L Thompson CeedScalar const *x_array = NULL, *y_array = NULL; 7141f9221feSJeremy L Thompson CeedSize n_w, n_x, n_y; 7150f7fd0f8Sjeremylt 7162b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(w, &n_w)); 7172b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 7182b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &n_y)); 7192b730f8bSJeremy L Thompson if (n_w != n_x || n_w != n_y) { 7200f7fd0f8Sjeremylt // LCOV_EXCL_START 7212b730f8bSJeremy L Thompson return CeedError(w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths"); 7220f7fd0f8Sjeremylt // LCOV_EXCL_STOP 7232b730f8bSJeremy L Thompson } 7240f7fd0f8Sjeremylt 7252d04630dSjeremylt Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y; 7262b730f8bSJeremy L Thompson CeedCall(CeedGetParent(w->ceed, &ceed_parent_w)); 7272b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 7282b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 7292b730f8bSJeremy L Thompson if ((ceed_parent_w != ceed_parent_x) || (ceed_parent_w != ceed_parent_y)) { 7302d04630dSjeremylt // LCOV_EXCL_START 7312b730f8bSJeremy L Thompson return CeedError(w->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors w, x, and y must be created by the same Ceed context"); 7322d04630dSjeremylt // LCOV_EXCL_STOP 7332b730f8bSJeremy L Thompson } 7342d04630dSjeremylt 7359c774eddSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 7362b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 7372b730f8bSJeremy L Thompson if (!has_valid_array_x) { 7389c774eddSJeremy L Thompson // LCOV_EXCL_START 7392b730f8bSJeremy L Thompson return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7409c774eddSJeremy L Thompson // LCOV_EXCL_STOP 7412b730f8bSJeremy L Thompson } 7422b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 7432b730f8bSJeremy L Thompson if (!has_valid_array_y) { 7449c774eddSJeremy L Thompson // LCOV_EXCL_START 7452b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7469c774eddSJeremy L Thompson // LCOV_EXCL_STOP 7472b730f8bSJeremy L Thompson } 7489c774eddSJeremy L Thompson 7490f7fd0f8Sjeremylt // Backend implementation 750eaf62fffSJeremy L Thompson if (w->PointwiseMult) { 7512b730f8bSJeremy L Thompson CeedCall(w->PointwiseMult(w, x, y)); 752eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 753eaf62fffSJeremy L Thompson } 7540f7fd0f8Sjeremylt 7550f7fd0f8Sjeremylt // Default implementation 7560f7fd0f8Sjeremylt if (x != w) { 7572b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 7580f7fd0f8Sjeremylt } else { 759c6d796c6SJeremy L Thompson CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 7600f7fd0f8Sjeremylt x_array = w_array; 7610f7fd0f8Sjeremylt } 7620f7fd0f8Sjeremylt if (y != w && y != x) { 7632b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array)); 764c6d796c6SJeremy L Thompson } else if (y == x) { 7650f7fd0f8Sjeremylt y_array = x_array; 766c6d796c6SJeremy L Thompson } else { 767c6d796c6SJeremy L Thompson CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 768c6d796c6SJeremy L Thompson y_array = w_array; 7690f7fd0f8Sjeremylt } 770c6d796c6SJeremy L Thompson if (!w_array) CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array)); 7710f7fd0f8Sjeremylt 7722b730f8bSJeremy L Thompson assert(w_array); 7732b730f8bSJeremy L Thompson assert(x_array); 7742b730f8bSJeremy L Thompson assert(y_array); 77573380422SJeremy L Thompson 7762b730f8bSJeremy L Thompson for (CeedInt i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i]; 7770f7fd0f8Sjeremylt 7782b730f8bSJeremy L Thompson if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array)); 7792b730f8bSJeremy L Thompson if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 7802b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(w, &w_array)); 7810f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 7820f7fd0f8Sjeremylt } 7830f7fd0f8Sjeremylt 7840f7fd0f8Sjeremylt /** 785d99fa3c5SJeremy L Thompson @brief Take the reciprocal of a CeedVector. 786d99fa3c5SJeremy L Thompson 787ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to take reciprocal 788d99fa3c5SJeremy L Thompson 789d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 790d99fa3c5SJeremy L Thompson 791d99fa3c5SJeremy L Thompson @ref User 792d99fa3c5SJeremy L Thompson **/ 793d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) { 7949c774eddSJeremy L Thompson bool has_valid_array = true; 7952b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 7962b730f8bSJeremy L Thompson 7972b730f8bSJeremy L Thompson if (!has_valid_array) { 7989c774eddSJeremy L Thompson // LCOV_EXCL_START 7999c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 8002b730f8bSJeremy L Thompson "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8019c774eddSJeremy L Thompson // LCOV_EXCL_STOP 8022b730f8bSJeremy L Thompson } 8039c774eddSJeremy L Thompson 804d99fa3c5SJeremy L Thompson // Check if vector data set 8052b730f8bSJeremy L Thompson if (!vec->state) { 806d99fa3c5SJeremy L Thompson // LCOV_EXCL_START 8072b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); 808d99fa3c5SJeremy L Thompson // LCOV_EXCL_STOP 8092b730f8bSJeremy L Thompson } 810d99fa3c5SJeremy L Thompson 811d99fa3c5SJeremy L Thompson // Backend impl for GPU, if added 812d99fa3c5SJeremy L Thompson if (vec->Reciprocal) { 8132b730f8bSJeremy L Thompson CeedCall(vec->Reciprocal(vec)); 814e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 815d99fa3c5SJeremy L Thompson } 816d99fa3c5SJeremy L Thompson 8171f9221feSJeremy L Thompson CeedSize len; 8182b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &len)); 819d99fa3c5SJeremy L Thompson CeedScalar *array; 8202b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 8212b730f8bSJeremy L Thompson for (CeedInt i = 0; i < len; i++) { 8222b730f8bSJeremy L Thompson if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i]; 8232b730f8bSJeremy L Thompson } 824d99fa3c5SJeremy L Thompson 8252b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 826e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 827d99fa3c5SJeremy L Thompson } 828d99fa3c5SJeremy L Thompson 829d99fa3c5SJeremy L Thompson /** 8307a982d89SJeremy L. Thompson @brief View a CeedVector 8310436c2adSjeremylt 8320a0da059Sjeremylt @param[in] vec CeedVector to view 833d1d35e2fSjeremylt @param[in] fp_fmt Printing format 8340a0da059Sjeremylt @param[in] stream Filestream to write to 8350a0da059Sjeremylt 8360436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8370436c2adSjeremylt 8387a982d89SJeremy L. Thompson @ref User 8390436c2adSjeremylt **/ 840d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { 8417a982d89SJeremy L. Thompson const CeedScalar *x; 8422b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x)); 8437a982d89SJeremy L. Thompson 8447a982d89SJeremy L. Thompson char fmt[1024]; 8457a982d89SJeremy L. Thompson fprintf(stream, "CeedVector length %ld\n", (long)vec->length); 846d1d35e2fSjeremylt snprintf(fmt, sizeof fmt, " %s\n", fp_fmt ? fp_fmt : "%g"); 8472b730f8bSJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) fprintf(stream, fmt, x[i]); 8487a982d89SJeremy L. Thompson 8492b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &x)); 850e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8510436c2adSjeremylt } 8520436c2adSjeremylt 8530436c2adSjeremylt /** 854b7c9bbdaSJeremy L Thompson @brief Get the Ceed associated with a CeedVector 855b7c9bbdaSJeremy L Thompson 856ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 857b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store ceed 858b7c9bbdaSJeremy L Thompson 859b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 860b7c9bbdaSJeremy L Thompson 861b7c9bbdaSJeremy L Thompson @ref Advanced 862b7c9bbdaSJeremy L Thompson **/ 863b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 864b7c9bbdaSJeremy L Thompson *ceed = vec->ceed; 865b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 866b7c9bbdaSJeremy L Thompson } 867b7c9bbdaSJeremy L Thompson 868b7c9bbdaSJeremy L Thompson /** 8697a982d89SJeremy L. Thompson @brief Get the length of a CeedVector 8700436c2adSjeremylt 871ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve length 8727a982d89SJeremy L. Thompson @param[out] length Variable to store length 8730436c2adSjeremylt 8740436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8750436c2adSjeremylt 8767a982d89SJeremy L. Thompson @ref User 8770436c2adSjeremylt **/ 8781f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) { 8797a982d89SJeremy L. Thompson *length = vec->length; 880e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8810436c2adSjeremylt } 8820436c2adSjeremylt 8830436c2adSjeremylt /** 8840436c2adSjeremylt @brief Destroy a CeedVector 8850436c2adSjeremylt 886ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to destroy 8870436c2adSjeremylt 8880436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8890436c2adSjeremylt 8907a982d89SJeremy L. Thompson @ref User 8910436c2adSjeremylt **/ 8920436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) { 893d1d35e2fSjeremylt if (!*vec || --(*vec)->ref_count > 0) return CEED_ERROR_SUCCESS; 8940436c2adSjeremylt 8952b730f8bSJeremy L Thompson if (((*vec)->state % 2) == 1) { 8962b730f8bSJeremy L Thompson return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use"); 8972b730f8bSJeremy L Thompson } 8982b730f8bSJeremy L Thompson if ((*vec)->num_readers > 0) { 899e92b641fSJeremy L Thompson // LCOV_EXCL_START 9002b730f8bSJeremy L Thompson return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access"); 901e92b641fSJeremy L Thompson // LCOV_EXCL_STOP 9020436c2adSjeremylt } 9030436c2adSjeremylt 9042b730f8bSJeremy L Thompson if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec)); 9052b730f8bSJeremy L Thompson 9062b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*vec)->ceed)); 9072b730f8bSJeremy L Thompson CeedCall(CeedFree(vec)); 908e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9090436c2adSjeremylt } 9100436c2adSjeremylt 9110436c2adSjeremylt /// @} 912