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 27*ea61e9acSJeremy 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 44*ea61e9acSJeremy 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 66*ea61e9acSJeremy L Thompson @param[in] vec CeedVector to check 67*ea61e9acSJeremy 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 89*ea61e9acSJeremy 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 104*ea61e9acSJeremy 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 118*ea61e9acSJeremy 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 133*ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to retrieve state 134*ea61e9acSJeremy 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 148*ea61e9acSJeremy 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 170*ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedVector will be created 171*ea61e9acSJeremy L Thompson @param[in] length Length of vector 172*ea61e9acSJeremy 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 /** 204*ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedVector. 205*ea61e9acSJeremy L Thompson Both pointers should be destroyed with `CeedVectorDestroy()`. 206*ea61e9acSJeremy L Thompson Note: If `*vec_copy` is non-NULL, then it is assumed that `*vec_copy` is a pointer to a CeedVector. 207*ea61e9acSJeremy L Thompson This CeedVector will be destroyed if `*vec_copy` is the only reference to this CeedVector. 2089560d06aSjeremylt 209*ea61e9acSJeremy L Thompson @param[in] vec CeedVector to copy reference to 210*ea61e9acSJeremy L Thompson @param[in,out] vec_copy Variable to store copied reference 2119560d06aSjeremylt 2129560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 2139560d06aSjeremylt 2149560d06aSjeremylt @ref User 2159560d06aSjeremylt **/ 2169560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) { 2172b730f8bSJeremy L Thompson CeedCall(CeedVectorReference(vec)); 2182b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(vec_copy)); 2199560d06aSjeremylt *vec_copy = vec; 2209560d06aSjeremylt return CEED_ERROR_SUCCESS; 2219560d06aSjeremylt } 2229560d06aSjeremylt 2239560d06aSjeremylt /** 224*ea61e9acSJeremy L Thompson @brief Set the array used by a CeedVector, freeing any previously allocated array if applicable. 225*ea61e9acSJeremy L Thompson The backend may copy values to a different memtype, such as during @ref CeedOperatorApply(). 2266a6c615bSJeremy L Thompson See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 2270436c2adSjeremylt 228*ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 229*ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the array being passed 230*ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the array 231*ea61e9acSJeremy L Thompson @param[in] array Array to be used, or NULL with @ref CEED_COPY_VALUES to have the library allocate 2320436c2adSjeremylt 2330436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2340436c2adSjeremylt 2357a982d89SJeremy L. Thompson @ref User 2360436c2adSjeremylt **/ 2372b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 2382b730f8bSJeremy L Thompson if (!vec->SetArray) { 2390436c2adSjeremylt // LCOV_EXCL_START 2402b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); 2410436c2adSjeremylt // LCOV_EXCL_STOP 2422b730f8bSJeremy L Thompson } 2432b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 2442b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 2452b730f8bSJeremy L Thompson } 2462b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 2472b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 2482b730f8bSJeremy L Thompson } 2490436c2adSjeremylt 2502b730f8bSJeremy L Thompson CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); 2510436c2adSjeremylt vec->state += 2; 252e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2530436c2adSjeremylt } 2540436c2adSjeremylt 2550436c2adSjeremylt /** 2560436c2adSjeremylt @brief Set the CeedVector to a constant value 2570436c2adSjeremylt 258*ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 2590436c2adSjeremylt @param[in] value Value to be used 2600436c2adSjeremylt 2610436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2620436c2adSjeremylt 2637a982d89SJeremy L. Thompson @ref User 2640436c2adSjeremylt **/ 2650436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 2662b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 2679c774eddSJeremy L Thompson // LCOV_EXCL_START 2682b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 2699c774eddSJeremy L Thompson // LCOV_EXCL_STOP 2702b730f8bSJeremy L Thompson } 2712b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 2729c774eddSJeremy L Thompson // LCOV_EXCL_START 2732b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 2749c774eddSJeremy L Thompson // LCOV_EXCL_STOP 2752b730f8bSJeremy L Thompson } 2760436c2adSjeremylt 2770436c2adSjeremylt if (vec->SetValue) { 2782b730f8bSJeremy L Thompson CeedCall(vec->SetValue(vec, value)); 2790436c2adSjeremylt } else { 2800436c2adSjeremylt CeedScalar *array; 2812b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 28292ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) array[i] = value; 2832b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 2840436c2adSjeremylt } 2850436c2adSjeremylt vec->state += 2; 286e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2870436c2adSjeremylt } 2880436c2adSjeremylt 2890436c2adSjeremylt /** 290*ea61e9acSJeremy L Thompson @brief Sync the CeedVector to a specified memtype. 291*ea61e9acSJeremy L Thompson This function is used to force synchronization of arrays set with @ref CeedVectorSetArray(). 292*ea61e9acSJeremy L Thompson If the requested memtype is already synchronized, this function results in a no-op. 2930436c2adSjeremylt 294*ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 295*ea61e9acSJeremy L Thompson @param[in] mem_type Memtype to be synced 2960436c2adSjeremylt 2970436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2980436c2adSjeremylt 2997a982d89SJeremy L. Thompson @ref User 3000436c2adSjeremylt **/ 301d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { 3022b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 3032b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use"); 3042b730f8bSJeremy L Thompson } 3050436c2adSjeremylt 3060436c2adSjeremylt if (vec->SyncArray) { 3072b730f8bSJeremy L Thompson CeedCall(vec->SyncArray(vec, mem_type)); 3080436c2adSjeremylt } else { 3090436c2adSjeremylt const CeedScalar *array; 3102b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array)); 3112b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 3120436c2adSjeremylt } 313e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3140436c2adSjeremylt } 3150436c2adSjeremylt 3160436c2adSjeremylt /** 317*ea61e9acSJeremy 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. 3189c774eddSJeremy L Thompson The caller is responsible for managing and freeing the array. 319*ea61e9acSJeremy L Thompson This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type. 3206a6c615bSJeremy L Thompson 321*ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 322*ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to take the array. 323*ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 324*ea61e9acSJeremy L Thompson @param[out] array Array on memory type mem_type, or NULL if array pointer is not required 3256a6c615bSJeremy L Thompson 3266a6c615bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3276a6c615bSJeremy L Thompson 3286a6c615bSJeremy L Thompson @ref User 3296a6c615bSJeremy L Thompson **/ 3302b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 3312b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 3322b730f8bSJeremy L Thompson // LCOV_EXCL_START 3332b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); 3342b730f8bSJeremy L Thompson // LCOV_EXCL_STOP 3352b730f8bSJeremy L Thompson } 3362b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 3372b730f8bSJeremy L Thompson // LCOV_EXCL_START 3382b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); 3392b730f8bSJeremy L Thompson // LCOV_EXCL_STOP 3402b730f8bSJeremy L Thompson } 3416a6c615bSJeremy L Thompson 34250c643e1SJed Brown CeedScalar *temp_array = NULL; 34350c643e1SJed Brown if (vec->length > 0) { 3449c774eddSJeremy L Thompson bool has_borrowed_array_of_type = true; 3452b730f8bSJeremy L Thompson CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); 3462b730f8bSJeremy L Thompson if (!has_borrowed_array_of_type) { 3479c774eddSJeremy L Thompson // LCOV_EXCL_START 3482b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", 3492b730f8bSJeremy L Thompson CeedMemTypes[mem_type]); 3509c774eddSJeremy L Thompson // LCOV_EXCL_STOP 3512b730f8bSJeremy L Thompson } 3529c774eddSJeremy L Thompson 3539c774eddSJeremy L Thompson bool has_valid_array = true; 3542b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 3552b730f8bSJeremy L Thompson if (!has_valid_array) { 3569c774eddSJeremy L Thompson // LCOV_EXCL_START 3579c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 3582b730f8bSJeremy L Thompson "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); 3599c774eddSJeremy L Thompson // LCOV_EXCL_STOP 3602b730f8bSJeremy L Thompson } 3619c774eddSJeremy L Thompson 3622b730f8bSJeremy L Thompson CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); 36350c643e1SJed Brown } 364d1d35e2fSjeremylt if (array) (*array) = temp_array; 365e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3666a6c615bSJeremy L Thompson } 3676a6c615bSJeremy L Thompson 3686a6c615bSJeremy L Thompson /** 369b3cf021fSjeremylt @brief Get read/write access to a CeedVector via the specified memory type. 370b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArray(). 3710436c2adSjeremylt 372*ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to access 373*ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 374*ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 375d1d35e2fSjeremylt @param[out] array Array on memory type mem_type 3760436c2adSjeremylt 377*ea61e9acSJeremy L Thompson @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide access to array pointers in the desired memory space. 378*ea61e9acSJeremy L Thompson Pairing get/restore allows the Vector to track access, thus knowing if norms or other operations may need to be recomputed. 3790436c2adSjeremylt 3800436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3810436c2adSjeremylt 3827a982d89SJeremy L. Thompson @ref User 3830436c2adSjeremylt **/ 3842b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 3852b730f8bSJeremy L Thompson if (!vec->GetArray) { 3860436c2adSjeremylt // LCOV_EXCL_START 3872b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); 3880436c2adSjeremylt // LCOV_EXCL_STOP 3892b730f8bSJeremy L Thompson } 3902b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 3912b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 3922b730f8bSJeremy L Thompson } 3932b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 3942b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3952b730f8bSJeremy L Thompson } 3960436c2adSjeremylt 3979c774eddSJeremy L Thompson bool has_valid_array = true; 3982b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 3992b730f8bSJeremy L Thompson if (!has_valid_array) { 4009c774eddSJeremy L Thompson // LCOV_EXCL_START 4019c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 4022b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4039c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4042b730f8bSJeremy L Thompson } 4059c774eddSJeremy L Thompson 4062b730f8bSJeremy L Thompson CeedCall(vec->GetArray(vec, mem_type, array)); 40728bfd0b7SJeremy L Thompson vec->state++; 408e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4090436c2adSjeremylt } 4100436c2adSjeremylt 4110436c2adSjeremylt /** 412b3cf021fSjeremylt @brief Get read-only access to a CeedVector via the specified memory type. 413b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArrayRead(). 4140436c2adSjeremylt 415*ea61e9acSJeremy L Thompson @param[in] vec CeedVector to access 416*ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 417*ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy (possibly cached). 418d1d35e2fSjeremylt @param[out] array Array on memory type mem_type 4190436c2adSjeremylt 4200436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4210436c2adSjeremylt 4227a982d89SJeremy L. Thompson @ref User 4230436c2adSjeremylt **/ 4242b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 4252b730f8bSJeremy L Thompson if (!vec->GetArrayRead) { 4260436c2adSjeremylt // LCOV_EXCL_START 4272b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); 4280436c2adSjeremylt // LCOV_EXCL_STOP 4292b730f8bSJeremy L Thompson } 4302b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 4312b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use"); 4322b730f8bSJeremy L Thompson } 4330436c2adSjeremylt 43450c643e1SJed Brown if (vec->length > 0) { 4359c774eddSJeremy L Thompson bool has_valid_array = true; 4362b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4372b730f8bSJeremy L Thompson if (!has_valid_array) { 4389c774eddSJeremy L Thompson // LCOV_EXCL_START 4399c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 4402b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4419c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4422b730f8bSJeremy L Thompson } 4439c774eddSJeremy L Thompson 4442b730f8bSJeremy L Thompson CeedCall(vec->GetArrayRead(vec, mem_type, array)); 44550c643e1SJed Brown } else { 44650c643e1SJed Brown *array = NULL; 44750c643e1SJed Brown } 448d1d35e2fSjeremylt vec->num_readers++; 449e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4500436c2adSjeremylt } 4510436c2adSjeremylt 4520436c2adSjeremylt /** 4539c774eddSJeremy L Thompson @brief Get write access to a CeedVector via the specified memory type. 454*ea61e9acSJeremy L Thompson Restore access with @ref CeedVectorRestoreArray(). 455*ea61e9acSJeremy L Thompson All old values should be assumed to be invalid. 4569c774eddSJeremy L Thompson 457*ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to access 458*ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 4599c774eddSJeremy L Thompson @param[out] array Array on memory type mem_type 4609c774eddSJeremy L Thompson 4619c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4629c774eddSJeremy L Thompson 4639c774eddSJeremy L Thompson @ref User 4649c774eddSJeremy L Thompson **/ 4652b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4662b730f8bSJeremy L Thompson if (!vec->GetArrayWrite) { 4679c774eddSJeremy L Thompson // LCOV_EXCL_START 4682b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayWrite"); 4699c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4702b730f8bSJeremy L Thompson } 4712b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 4729c774eddSJeremy L Thompson // LCOV_EXCL_START 4732b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 4749c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4752b730f8bSJeremy L Thompson } 4762b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 4779c774eddSJeremy L Thompson // LCOV_EXCL_START 4782b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 4799c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4802b730f8bSJeremy L Thompson } 4819c774eddSJeremy L Thompson 4822b730f8bSJeremy L Thompson CeedCall(vec->GetArrayWrite(vec, mem_type, array)); 48328bfd0b7SJeremy L Thompson vec->state++; 4849c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 4859c774eddSJeremy L Thompson } 4869c774eddSJeremy L Thompson 4879c774eddSJeremy L Thompson /** 488*ea61e9acSJeremy L Thompson @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite() 4890436c2adSjeremylt 490*ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to restore 491*ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 4920436c2adSjeremylt 4930436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4940436c2adSjeremylt 4957a982d89SJeremy L. Thompson @ref User 4960436c2adSjeremylt **/ 4970436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 4982b730f8bSJeremy L Thompson if (vec->state % 2 != 1) { 4992b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted"); 500706efda3SJeremy L Thompson } 5012b730f8bSJeremy L Thompson if (vec->RestoreArray) CeedCall(vec->RestoreArray(vec)); 5020436c2adSjeremylt *array = NULL; 50328bfd0b7SJeremy L Thompson vec->state++; 504e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5050436c2adSjeremylt } 5060436c2adSjeremylt 5070436c2adSjeremylt /** 508b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 5090436c2adSjeremylt 510*ea61e9acSJeremy L Thompson @param[in] vec CeedVector to restore 511*ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 5120436c2adSjeremylt 5130436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5140436c2adSjeremylt 5157a982d89SJeremy L. Thompson @ref User 5160436c2adSjeremylt **/ 5170436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 5182b730f8bSJeremy L Thompson if (vec->num_readers == 0) { 5199c774eddSJeremy L Thompson // LCOV_EXCL_START 5202b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted"); 5219c774eddSJeremy L Thompson // LCOV_EXCL_STOP 5222b730f8bSJeremy L Thompson } 5239c774eddSJeremy L Thompson 52475a19770SJeremy L Thompson vec->num_readers--; 5252b730f8bSJeremy L Thompson if (vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec)); 5260436c2adSjeremylt *array = NULL; 52775a19770SJeremy L Thompson 528e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5290436c2adSjeremylt } 5300436c2adSjeremylt 5310436c2adSjeremylt /** 532171f8ca9Sjeremylt @brief Get the norm of a CeedVector. 533171f8ca9Sjeremylt 534*ea61e9acSJeremy L Thompson Note: This operation is local to the CeedVector. 535*ea61e9acSJeremy 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 536*ea61e9acSJeremy L Thompson duplicated or hanging nodes. 537547d9b97Sjeremylt 538*ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve maximum value 539*ea61e9acSJeremy L Thompson @param[in] norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 540547d9b97Sjeremylt @param[out] norm Variable to store norm value 541547d9b97Sjeremylt 542547d9b97Sjeremylt @return An error code: 0 - success, otherwise - failure 543547d9b97Sjeremylt 5447a982d89SJeremy L. Thompson @ref User 545547d9b97Sjeremylt **/ 546d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) { 5479c774eddSJeremy L Thompson bool has_valid_array = true; 5482b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5492b730f8bSJeremy L Thompson if (!has_valid_array) { 5509c774eddSJeremy L Thompson // LCOV_EXCL_START 5519c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 5522b730f8bSJeremy L Thompson "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5539c774eddSJeremy L Thompson // LCOV_EXCL_STOP 5542b730f8bSJeremy L Thompson } 5559c774eddSJeremy L Thompson 556547d9b97Sjeremylt // Backend impl for GPU, if added 557547d9b97Sjeremylt if (vec->Norm) { 5582b730f8bSJeremy L Thompson CeedCall(vec->Norm(vec, norm_type, norm)); 559e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 560547d9b97Sjeremylt } 561547d9b97Sjeremylt 562547d9b97Sjeremylt const CeedScalar *array; 5632b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 564547d9b97Sjeremylt 565547d9b97Sjeremylt *norm = 0.; 566d1d35e2fSjeremylt switch (norm_type) { 567547d9b97Sjeremylt case CEED_NORM_1: 56892ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) { 569547d9b97Sjeremylt *norm += fabs(array[i]); 570547d9b97Sjeremylt } 571547d9b97Sjeremylt break; 572547d9b97Sjeremylt case CEED_NORM_2: 57392ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) { 574547d9b97Sjeremylt *norm += fabs(array[i]) * fabs(array[i]); 575547d9b97Sjeremylt } 576547d9b97Sjeremylt break; 577547d9b97Sjeremylt case CEED_NORM_MAX: 57892ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) { 579d1d35e2fSjeremylt const CeedScalar abs_v_i = fabs(array[i]); 580d1d35e2fSjeremylt *norm = *norm > abs_v_i ? *norm : abs_v_i; 581547d9b97Sjeremylt } 582547d9b97Sjeremylt } 5832b730f8bSJeremy L Thompson if (norm_type == CEED_NORM_2) *norm = sqrt(*norm); 584547d9b97Sjeremylt 5852b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 586e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 587547d9b97Sjeremylt } 588547d9b97Sjeremylt 589547d9b97Sjeremylt /** 590e0dd3b27Sjeremylt @brief Compute x = alpha x 591e0dd3b27Sjeremylt 59296b902e2Sjeremylt @param[in,out] x vector for scaling 59396b902e2Sjeremylt @param[in] alpha scaling factor 594e0dd3b27Sjeremylt 595e0dd3b27Sjeremylt @return An error code: 0 - success, otherwise - failure 596e0dd3b27Sjeremylt 597e0dd3b27Sjeremylt @ref User 598e0dd3b27Sjeremylt **/ 599e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) { 60073380422SJeremy L Thompson CeedScalar *x_array = NULL; 6011f9221feSJeremy L Thompson CeedSize n_x; 602e0dd3b27Sjeremylt 6039c774eddSJeremy L Thompson bool has_valid_array = true; 6042b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array)); 6052b730f8bSJeremy L Thompson if (!has_valid_array) { 6069c774eddSJeremy L Thompson // LCOV_EXCL_START 6079c774eddSJeremy L Thompson return CeedError(x->ceed, CEED_ERROR_BACKEND, 6082b730f8bSJeremy L Thompson "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6099c774eddSJeremy L Thompson // LCOV_EXCL_STOP 6102b730f8bSJeremy L Thompson } 6119c774eddSJeremy L Thompson 6122b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 613e0dd3b27Sjeremylt 614e0dd3b27Sjeremylt // Backend implementation 6152b730f8bSJeremy L Thompson if (x->Scale) return x->Scale(x, alpha); 616e0dd3b27Sjeremylt 617e0dd3b27Sjeremylt // Default implementation 6182b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array)); 6192b730f8bSJeremy L Thompson for (CeedInt i = 0; i < n_x; i++) x_array[i] *= alpha; 6202b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(x, &x_array)); 621e0dd3b27Sjeremylt 622e0dd3b27Sjeremylt return CEED_ERROR_SUCCESS; 623e0dd3b27Sjeremylt } 624e0dd3b27Sjeremylt 625e0dd3b27Sjeremylt /** 6260f7fd0f8Sjeremylt @brief Compute y = alpha x + y 6270f7fd0f8Sjeremylt 62896b902e2Sjeremylt @param[in,out] y target vector for sum 62996b902e2Sjeremylt @param[in] alpha scaling factor 63096b902e2Sjeremylt @param[in] x second vector, must be different than y 6310f7fd0f8Sjeremylt 6320f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 6330f7fd0f8Sjeremylt 6340f7fd0f8Sjeremylt @ref User 6350f7fd0f8Sjeremylt **/ 6360f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { 63773380422SJeremy L Thompson CeedScalar *y_array = NULL; 63873380422SJeremy L Thompson CeedScalar const *x_array = NULL; 6391f9221feSJeremy L Thompson CeedSize n_x, n_y; 6400f7fd0f8Sjeremylt 6412b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &n_y)); 6422b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 6432b730f8bSJeremy L Thompson if (n_x != n_y) { 6440f7fd0f8Sjeremylt // LCOV_EXCL_START 6452b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 6460f7fd0f8Sjeremylt // LCOV_EXCL_STOP 6472b730f8bSJeremy L Thompson } 6482b730f8bSJeremy L Thompson if (x == y) { 6490f7fd0f8Sjeremylt // LCOV_EXCL_START 6502b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); 6510f7fd0f8Sjeremylt // LCOV_EXCL_STOP 6522b730f8bSJeremy L Thompson } 6530f7fd0f8Sjeremylt 6549c774eddSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 6552b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 6562b730f8bSJeremy L Thompson if (!has_valid_array_x) { 6579c774eddSJeremy L Thompson // LCOV_EXCL_START 6582b730f8bSJeremy L Thompson return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6599c774eddSJeremy L Thompson // LCOV_EXCL_STOP 6602b730f8bSJeremy L Thompson } 6612b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 6622b730f8bSJeremy L Thompson if (!has_valid_array_y) { 6639c774eddSJeremy L Thompson // LCOV_EXCL_START 6642b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6659c774eddSJeremy L Thompson // LCOV_EXCL_STOP 6662b730f8bSJeremy L Thompson } 6679c774eddSJeremy L Thompson 6682d04630dSjeremylt Ceed ceed_parent_x, ceed_parent_y; 6692b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 6702b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 6712b730f8bSJeremy L Thompson if (ceed_parent_x != ceed_parent_y) { 6722d04630dSjeremylt // LCOV_EXCL_START 6732b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); 6742d04630dSjeremylt // LCOV_EXCL_STOP 6752b730f8bSJeremy L Thompson } 6762d04630dSjeremylt 6770f7fd0f8Sjeremylt // Backend implementation 678eaf62fffSJeremy L Thompson if (y->AXPY) { 6792b730f8bSJeremy L Thompson CeedCall(y->AXPY(y, alpha, x)); 680eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 681eaf62fffSJeremy L Thompson } 6820f7fd0f8Sjeremylt 6830f7fd0f8Sjeremylt // Default implementation 6842b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array)); 6852b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 6860f7fd0f8Sjeremylt 6872b730f8bSJeremy L Thompson assert(x_array); 6882b730f8bSJeremy L Thompson assert(y_array); 68973380422SJeremy L Thompson 6902b730f8bSJeremy L Thompson for (CeedInt i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i]; 6910f7fd0f8Sjeremylt 6922b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(y, &y_array)); 6932b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 6940f7fd0f8Sjeremylt 6950f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 6960f7fd0f8Sjeremylt } 6970f7fd0f8Sjeremylt 6980f7fd0f8Sjeremylt /** 699*ea61e9acSJeremy L Thompson @brief Compute the pointwise multiplication w = x .* y. 700*ea61e9acSJeremy L Thompson Any subset of x, y, and w may be the same vector. 7010f7fd0f8Sjeremylt 70296b902e2Sjeremylt @param[out] w target vector for the product 70396b902e2Sjeremylt @param[in] x first vector for product 70496b902e2Sjeremylt @param[in] y second vector for the product 7050f7fd0f8Sjeremylt 7060f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 7070f7fd0f8Sjeremylt 7080f7fd0f8Sjeremylt @ ref User 7090f7fd0f8Sjeremylt **/ 7100f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { 71173380422SJeremy L Thompson CeedScalar *w_array = NULL; 71273380422SJeremy L Thompson CeedScalar const *x_array = NULL, *y_array = NULL; 7131f9221feSJeremy L Thompson CeedSize n_w, n_x, n_y; 7140f7fd0f8Sjeremylt 7152b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(w, &n_w)); 7162b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 7172b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &n_y)); 7182b730f8bSJeremy L Thompson if (n_w != n_x || n_w != n_y) { 7190f7fd0f8Sjeremylt // LCOV_EXCL_START 7202b730f8bSJeremy L Thompson return CeedError(w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths"); 7210f7fd0f8Sjeremylt // LCOV_EXCL_STOP 7222b730f8bSJeremy L Thompson } 7230f7fd0f8Sjeremylt 7242d04630dSjeremylt Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y; 7252b730f8bSJeremy L Thompson CeedCall(CeedGetParent(w->ceed, &ceed_parent_w)); 7262b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 7272b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 7282b730f8bSJeremy L Thompson if ((ceed_parent_w != ceed_parent_x) || (ceed_parent_w != ceed_parent_y)) { 7292d04630dSjeremylt // LCOV_EXCL_START 7302b730f8bSJeremy L Thompson return CeedError(w->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors w, x, and y must be created by the same Ceed context"); 7312d04630dSjeremylt // LCOV_EXCL_STOP 7322b730f8bSJeremy L Thompson } 7332d04630dSjeremylt 7349c774eddSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 7352b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 7362b730f8bSJeremy L Thompson if (!has_valid_array_x) { 7379c774eddSJeremy L Thompson // LCOV_EXCL_START 7382b730f8bSJeremy L Thompson return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7399c774eddSJeremy L Thompson // LCOV_EXCL_STOP 7402b730f8bSJeremy L Thompson } 7412b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 7422b730f8bSJeremy L Thompson if (!has_valid_array_y) { 7439c774eddSJeremy L Thompson // LCOV_EXCL_START 7442b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7459c774eddSJeremy L Thompson // LCOV_EXCL_STOP 7462b730f8bSJeremy L Thompson } 7479c774eddSJeremy L Thompson 7480f7fd0f8Sjeremylt // Backend implementation 749eaf62fffSJeremy L Thompson if (w->PointwiseMult) { 7502b730f8bSJeremy L Thompson CeedCall(w->PointwiseMult(w, x, y)); 751eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 752eaf62fffSJeremy L Thompson } 7530f7fd0f8Sjeremylt 7540f7fd0f8Sjeremylt // Default implementation 7552b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array)); 7560f7fd0f8Sjeremylt if (x != w) { 7572b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 7580f7fd0f8Sjeremylt } else { 7590f7fd0f8Sjeremylt x_array = w_array; 7600f7fd0f8Sjeremylt } 7612b730f8bSJeremy L Thompson 7620f7fd0f8Sjeremylt if (y != w && y != x) { 7632b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array)); 7640f7fd0f8Sjeremylt } else if (y != x) { 7650f7fd0f8Sjeremylt y_array = w_array; 7660f7fd0f8Sjeremylt } else { 7670f7fd0f8Sjeremylt y_array = x_array; 7680f7fd0f8Sjeremylt } 7690f7fd0f8Sjeremylt 7702b730f8bSJeremy L Thompson assert(w_array); 7712b730f8bSJeremy L Thompson assert(x_array); 7722b730f8bSJeremy L Thompson assert(y_array); 77373380422SJeremy L Thompson 7742b730f8bSJeremy L Thompson for (CeedInt i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i]; 7750f7fd0f8Sjeremylt 7762b730f8bSJeremy L Thompson if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array)); 7772b730f8bSJeremy L Thompson if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 7782b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(w, &w_array)); 7790f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 7800f7fd0f8Sjeremylt } 7810f7fd0f8Sjeremylt 7820f7fd0f8Sjeremylt /** 783d99fa3c5SJeremy L Thompson @brief Take the reciprocal of a CeedVector. 784d99fa3c5SJeremy L Thompson 785*ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to take reciprocal 786d99fa3c5SJeremy L Thompson 787d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 788d99fa3c5SJeremy L Thompson 789d99fa3c5SJeremy L Thompson @ref User 790d99fa3c5SJeremy L Thompson **/ 791d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) { 7929c774eddSJeremy L Thompson bool has_valid_array = true; 7932b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 7942b730f8bSJeremy L Thompson 7952b730f8bSJeremy L Thompson if (!has_valid_array) { 7969c774eddSJeremy L Thompson // LCOV_EXCL_START 7979c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 7982b730f8bSJeremy L Thompson "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7999c774eddSJeremy L Thompson // LCOV_EXCL_STOP 8002b730f8bSJeremy L Thompson } 8019c774eddSJeremy L Thompson 802d99fa3c5SJeremy L Thompson // Check if vector data set 8032b730f8bSJeremy L Thompson if (!vec->state) { 804d99fa3c5SJeremy L Thompson // LCOV_EXCL_START 8052b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); 806d99fa3c5SJeremy L Thompson // LCOV_EXCL_STOP 8072b730f8bSJeremy L Thompson } 808d99fa3c5SJeremy L Thompson 809d99fa3c5SJeremy L Thompson // Backend impl for GPU, if added 810d99fa3c5SJeremy L Thompson if (vec->Reciprocal) { 8112b730f8bSJeremy L Thompson CeedCall(vec->Reciprocal(vec)); 812e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 813d99fa3c5SJeremy L Thompson } 814d99fa3c5SJeremy L Thompson 8151f9221feSJeremy L Thompson CeedSize len; 8162b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &len)); 817d99fa3c5SJeremy L Thompson CeedScalar *array; 8182b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 8192b730f8bSJeremy L Thompson for (CeedInt i = 0; i < len; i++) { 8202b730f8bSJeremy L Thompson if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i]; 8212b730f8bSJeremy L Thompson } 822d99fa3c5SJeremy L Thompson 8232b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 824e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 825d99fa3c5SJeremy L Thompson } 826d99fa3c5SJeremy L Thompson 827d99fa3c5SJeremy L Thompson /** 8287a982d89SJeremy L. Thompson @brief View a CeedVector 8290436c2adSjeremylt 8300a0da059Sjeremylt @param[in] vec CeedVector to view 831d1d35e2fSjeremylt @param[in] fp_fmt Printing format 8320a0da059Sjeremylt @param[in] stream Filestream to write to 8330a0da059Sjeremylt 8340436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8350436c2adSjeremylt 8367a982d89SJeremy L. Thompson @ref User 8370436c2adSjeremylt **/ 838d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { 8397a982d89SJeremy L. Thompson const CeedScalar *x; 8402b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x)); 8417a982d89SJeremy L. Thompson 8427a982d89SJeremy L. Thompson char fmt[1024]; 8437a982d89SJeremy L. Thompson fprintf(stream, "CeedVector length %ld\n", (long)vec->length); 844d1d35e2fSjeremylt snprintf(fmt, sizeof fmt, " %s\n", fp_fmt ? fp_fmt : "%g"); 8452b730f8bSJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) fprintf(stream, fmt, x[i]); 8467a982d89SJeremy L. Thompson 8472b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &x)); 848e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8490436c2adSjeremylt } 8500436c2adSjeremylt 8510436c2adSjeremylt /** 852b7c9bbdaSJeremy L Thompson @brief Get the Ceed associated with a CeedVector 853b7c9bbdaSJeremy L Thompson 854*ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 855b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store ceed 856b7c9bbdaSJeremy L Thompson 857b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 858b7c9bbdaSJeremy L Thompson 859b7c9bbdaSJeremy L Thompson @ref Advanced 860b7c9bbdaSJeremy L Thompson **/ 861b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 862b7c9bbdaSJeremy L Thompson *ceed = vec->ceed; 863b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 864b7c9bbdaSJeremy L Thompson } 865b7c9bbdaSJeremy L Thompson 866b7c9bbdaSJeremy L Thompson /** 8677a982d89SJeremy L. Thompson @brief Get the length of a CeedVector 8680436c2adSjeremylt 869*ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve length 8707a982d89SJeremy L. Thompson @param[out] length Variable to store length 8710436c2adSjeremylt 8720436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8730436c2adSjeremylt 8747a982d89SJeremy L. Thompson @ref User 8750436c2adSjeremylt **/ 8761f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) { 8777a982d89SJeremy L. Thompson *length = vec->length; 878e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8790436c2adSjeremylt } 8800436c2adSjeremylt 8810436c2adSjeremylt /** 8820436c2adSjeremylt @brief Destroy a CeedVector 8830436c2adSjeremylt 884*ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to destroy 8850436c2adSjeremylt 8860436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8870436c2adSjeremylt 8887a982d89SJeremy L. Thompson @ref User 8890436c2adSjeremylt **/ 8900436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) { 891d1d35e2fSjeremylt if (!*vec || --(*vec)->ref_count > 0) return CEED_ERROR_SUCCESS; 8920436c2adSjeremylt 8932b730f8bSJeremy L Thompson if (((*vec)->state % 2) == 1) { 8942b730f8bSJeremy L Thompson return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use"); 8952b730f8bSJeremy L Thompson } 8962b730f8bSJeremy L Thompson if ((*vec)->num_readers > 0) { 897e92b641fSJeremy L Thompson // LCOV_EXCL_START 8982b730f8bSJeremy L Thompson return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access"); 899e92b641fSJeremy L Thompson // LCOV_EXCL_STOP 9000436c2adSjeremylt } 9010436c2adSjeremylt 9022b730f8bSJeremy L Thompson if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec)); 9032b730f8bSJeremy L Thompson 9042b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*vec)->ceed)); 9052b730f8bSJeremy L Thompson CeedCall(CeedFree(vec)); 906e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9070436c2adSjeremylt } 9080436c2adSjeremylt 9090436c2adSjeremylt /// @} 910