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()`. 206512bb800SJeremy L Thompson 207512bb800SJeremy 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. 208512bb800SJeremy 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 /** 2255fb68f37SKaren (Ren) Stengel @brief Copy a CeedVector into a different CeedVector. 2265fb68f37SKaren (Ren) Stengel Both pointers should be destroyed with `CeedVectorDestroy()`. 2275fb68f37SKaren (Ren) Stengel Note: If `*vec_copy` is non-NULL, then it is assumed that `*vec_copy` is a pointer to a CeedVector. 2285fb68f37SKaren (Ren) Stengel This CeedVector will be destroyed if `*vec_copy` is the only reference to this CeedVector. 2295fb68f37SKaren (Ren) Stengel 2305fb68f37SKaren (Ren) Stengel @param[in] vec CeedVector to copy 2315fb68f37SKaren (Ren) Stengel @param[in,out] vec_copy Variable to store copied CeedVector to 2325fb68f37SKaren (Ren) Stengel 2335fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 2345fb68f37SKaren (Ren) Stengel 2355fb68f37SKaren (Ren) Stengel @ref User 2365fb68f37SKaren (Ren) Stengel **/ 2375fb68f37SKaren (Ren) Stengel int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) { 2385fb68f37SKaren (Ren) Stengel Ceed ceed; 2395fb68f37SKaren (Ren) Stengel CeedMemType mem_type, mem_type_copy; 2405fb68f37SKaren (Ren) Stengel CeedScalar *array; 2415fb68f37SKaren (Ren) Stengel 2425fb68f37SKaren (Ren) Stengel // Get the preferred memory type 2435fb68f37SKaren (Ren) Stengel CeedVectorGetCeed(vec, &ceed); 2445fb68f37SKaren (Ren) Stengel CeedGetPreferredMemType(ceed, &mem_type); 2455fb68f37SKaren (Ren) Stengel 2465fb68f37SKaren (Ren) Stengel // Get the preferred memory type 2475fb68f37SKaren (Ren) Stengel CeedVectorGetCeed(vec_copy, &ceed); 2485fb68f37SKaren (Ren) Stengel CeedGetPreferredMemType(ceed, &mem_type_copy); 2495fb68f37SKaren (Ren) Stengel 2505fb68f37SKaren (Ren) Stengel // Check that both have same memory type 2515fb68f37SKaren (Ren) Stengel if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST; 2525fb68f37SKaren (Ren) Stengel 2535fb68f37SKaren (Ren) Stengel // Copy the values from vec to vec_copy 2545fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(vec, mem_type, &array)); 2555fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array)); 2565fb68f37SKaren (Ren) Stengel 2575fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(vec, &array)); 2585fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 2595fb68f37SKaren (Ren) Stengel } 2605fb68f37SKaren (Ren) Stengel 2615fb68f37SKaren (Ren) Stengel /** 262ea61e9acSJeremy L Thompson @brief Set the array used by a CeedVector, freeing any previously allocated array if applicable. 263ea61e9acSJeremy L Thompson The backend may copy values to a different memtype, such as during @ref CeedOperatorApply(). 2646a6c615bSJeremy L Thompson See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 2650436c2adSjeremylt 266ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 267ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the array being passed 268ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the array 269ea61e9acSJeremy L Thompson @param[in] array Array to be used, or NULL with @ref CEED_COPY_VALUES to have the library allocate 2700436c2adSjeremylt 2710436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2720436c2adSjeremylt 2737a982d89SJeremy L. Thompson @ref User 2740436c2adSjeremylt **/ 2752b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 2762b730f8bSJeremy L Thompson if (!vec->SetArray) { 2770436c2adSjeremylt // LCOV_EXCL_START 2782b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); 2790436c2adSjeremylt // LCOV_EXCL_STOP 2802b730f8bSJeremy L Thompson } 2812b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 2822b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 2832b730f8bSJeremy L Thompson } 2842b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 2852b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 2862b730f8bSJeremy L Thompson } 2870436c2adSjeremylt 2882b730f8bSJeremy L Thompson CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); 2890436c2adSjeremylt vec->state += 2; 290e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2910436c2adSjeremylt } 2920436c2adSjeremylt 2930436c2adSjeremylt /** 2940436c2adSjeremylt @brief Set the CeedVector to a constant value 2950436c2adSjeremylt 296ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 2970436c2adSjeremylt @param[in] value Value to be used 2980436c2adSjeremylt 2990436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3000436c2adSjeremylt 3017a982d89SJeremy L. Thompson @ref User 3020436c2adSjeremylt **/ 3030436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 3042b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 3059c774eddSJeremy L Thompson // LCOV_EXCL_START 3062b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 3079c774eddSJeremy L Thompson // LCOV_EXCL_STOP 3082b730f8bSJeremy L Thompson } 3092b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 3109c774eddSJeremy L Thompson // LCOV_EXCL_START 3112b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3129c774eddSJeremy L Thompson // LCOV_EXCL_STOP 3132b730f8bSJeremy L Thompson } 3140436c2adSjeremylt 3150436c2adSjeremylt if (vec->SetValue) { 3162b730f8bSJeremy L Thompson CeedCall(vec->SetValue(vec, value)); 3170436c2adSjeremylt } else { 3180436c2adSjeremylt CeedScalar *array; 3192b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 32092ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) array[i] = value; 3212b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 3220436c2adSjeremylt } 3230436c2adSjeremylt vec->state += 2; 324e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3250436c2adSjeremylt } 3260436c2adSjeremylt 3270436c2adSjeremylt /** 328ea61e9acSJeremy L Thompson @brief Sync the CeedVector to a specified memtype. 329ea61e9acSJeremy L Thompson This function is used to force synchronization of arrays set with @ref CeedVectorSetArray(). 330ea61e9acSJeremy L Thompson If the requested memtype is already synchronized, this function results in a no-op. 3310436c2adSjeremylt 332ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 333ea61e9acSJeremy L Thompson @param[in] mem_type Memtype to be synced 3340436c2adSjeremylt 3350436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3360436c2adSjeremylt 3377a982d89SJeremy L. Thompson @ref User 3380436c2adSjeremylt **/ 339d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { 3402b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 3412b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use"); 3422b730f8bSJeremy L Thompson } 3430436c2adSjeremylt 3440436c2adSjeremylt if (vec->SyncArray) { 3452b730f8bSJeremy L Thompson CeedCall(vec->SyncArray(vec, mem_type)); 3460436c2adSjeremylt } else { 3470436c2adSjeremylt const CeedScalar *array; 3482b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array)); 3492b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 3500436c2adSjeremylt } 351e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3520436c2adSjeremylt } 3530436c2adSjeremylt 3540436c2adSjeremylt /** 355ea61e9acSJeremy 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. 3569c774eddSJeremy L Thompson The caller is responsible for managing and freeing the array. 357ea61e9acSJeremy L Thompson This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type. 3586a6c615bSJeremy L Thompson 359ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 360ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to take the array. 361ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 362ea61e9acSJeremy L Thompson @param[out] array Array on memory type mem_type, or NULL if array pointer is not required 3636a6c615bSJeremy L Thompson 3646a6c615bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3656a6c615bSJeremy L Thompson 3666a6c615bSJeremy L Thompson @ref User 3676a6c615bSJeremy L Thompson **/ 3682b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 3692b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 3702b730f8bSJeremy L Thompson // LCOV_EXCL_START 3712b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); 3722b730f8bSJeremy L Thompson // LCOV_EXCL_STOP 3732b730f8bSJeremy L Thompson } 3742b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 3752b730f8bSJeremy L Thompson // LCOV_EXCL_START 3762b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); 3772b730f8bSJeremy L Thompson // LCOV_EXCL_STOP 3782b730f8bSJeremy L Thompson } 3796a6c615bSJeremy L Thompson 38050c643e1SJed Brown CeedScalar *temp_array = NULL; 38150c643e1SJed Brown if (vec->length > 0) { 3829c774eddSJeremy L Thompson bool has_borrowed_array_of_type = true; 3832b730f8bSJeremy L Thompson CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); 3842b730f8bSJeremy L Thompson if (!has_borrowed_array_of_type) { 3859c774eddSJeremy L Thompson // LCOV_EXCL_START 3862b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", 3872b730f8bSJeremy L Thompson CeedMemTypes[mem_type]); 3889c774eddSJeremy L Thompson // LCOV_EXCL_STOP 3892b730f8bSJeremy L Thompson } 3909c774eddSJeremy L Thompson 3919c774eddSJeremy L Thompson bool has_valid_array = true; 3922b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 3932b730f8bSJeremy L Thompson if (!has_valid_array) { 3949c774eddSJeremy L Thompson // LCOV_EXCL_START 3959c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 3962b730f8bSJeremy L Thompson "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); 3979c774eddSJeremy L Thompson // LCOV_EXCL_STOP 3982b730f8bSJeremy L Thompson } 3999c774eddSJeremy L Thompson 4002b730f8bSJeremy L Thompson CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); 40150c643e1SJed Brown } 402d1d35e2fSjeremylt if (array) (*array) = temp_array; 403e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4046a6c615bSJeremy L Thompson } 4056a6c615bSJeremy L Thompson 4066a6c615bSJeremy L Thompson /** 407b3cf021fSjeremylt @brief Get read/write access to a CeedVector via the specified memory type. 408b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArray(). 4090436c2adSjeremylt 410ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to access 411ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 412ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 413d1d35e2fSjeremylt @param[out] array Array on memory type mem_type 4140436c2adSjeremylt 415ea61e9acSJeremy L Thompson @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide access to array pointers in the desired memory space. 416ea61e9acSJeremy L Thompson Pairing get/restore allows the Vector to track access, thus knowing if norms or other operations may need to be recomputed. 4170436c2adSjeremylt 4180436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4190436c2adSjeremylt 4207a982d89SJeremy L. Thompson @ref User 4210436c2adSjeremylt **/ 4222b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4232b730f8bSJeremy L Thompson if (!vec->GetArray) { 4240436c2adSjeremylt // LCOV_EXCL_START 4252b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); 4260436c2adSjeremylt // LCOV_EXCL_STOP 4272b730f8bSJeremy L Thompson } 4282b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 4292b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 4302b730f8bSJeremy L Thompson } 4312b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 4322b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 4332b730f8bSJeremy L Thompson } 4340436c2adSjeremylt 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->GetArray(vec, mem_type, array)); 44528bfd0b7SJeremy L Thompson vec->state++; 446e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4470436c2adSjeremylt } 4480436c2adSjeremylt 4490436c2adSjeremylt /** 450b3cf021fSjeremylt @brief Get read-only access to a CeedVector via the specified memory type. 451b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArrayRead(). 4520436c2adSjeremylt 453ea61e9acSJeremy L Thompson @param[in] vec CeedVector to access 454ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 455ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy (possibly cached). 456d1d35e2fSjeremylt @param[out] array Array on memory type mem_type 4570436c2adSjeremylt 4580436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4590436c2adSjeremylt 4607a982d89SJeremy L. Thompson @ref User 4610436c2adSjeremylt **/ 4622b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 4632b730f8bSJeremy L Thompson if (!vec->GetArrayRead) { 4640436c2adSjeremylt // LCOV_EXCL_START 4652b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); 4660436c2adSjeremylt // LCOV_EXCL_STOP 4672b730f8bSJeremy L Thompson } 4682b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 4692b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use"); 4702b730f8bSJeremy L Thompson } 4710436c2adSjeremylt 47250c643e1SJed Brown if (vec->length > 0) { 4739c774eddSJeremy L Thompson bool has_valid_array = true; 4742b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4752b730f8bSJeremy L Thompson if (!has_valid_array) { 4769c774eddSJeremy L Thompson // LCOV_EXCL_START 4779c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 4782b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4799c774eddSJeremy L Thompson // LCOV_EXCL_STOP 4802b730f8bSJeremy L Thompson } 4819c774eddSJeremy L Thompson 4822b730f8bSJeremy L Thompson CeedCall(vec->GetArrayRead(vec, mem_type, array)); 48350c643e1SJed Brown } else { 48450c643e1SJed Brown *array = NULL; 48550c643e1SJed Brown } 486d1d35e2fSjeremylt vec->num_readers++; 487e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4880436c2adSjeremylt } 4890436c2adSjeremylt 4900436c2adSjeremylt /** 4919c774eddSJeremy L Thompson @brief Get write access to a CeedVector via the specified memory type. 492ea61e9acSJeremy L Thompson Restore access with @ref CeedVectorRestoreArray(). 493ea61e9acSJeremy L Thompson All old values should be assumed to be invalid. 4949c774eddSJeremy L Thompson 495ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to access 496ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 4979c774eddSJeremy L Thompson @param[out] array Array on memory type mem_type 4989c774eddSJeremy L Thompson 4999c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5009c774eddSJeremy L Thompson 5019c774eddSJeremy L Thompson @ref User 5029c774eddSJeremy L Thompson **/ 5032b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 5042b730f8bSJeremy L Thompson if (!vec->GetArrayWrite) { 5059c774eddSJeremy L Thompson // LCOV_EXCL_START 5062b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayWrite"); 5079c774eddSJeremy L Thompson // LCOV_EXCL_STOP 5082b730f8bSJeremy L Thompson } 5092b730f8bSJeremy L Thompson if (vec->state % 2 == 1) { 5109c774eddSJeremy L Thompson // LCOV_EXCL_START 5112b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 5129c774eddSJeremy L Thompson // LCOV_EXCL_STOP 5132b730f8bSJeremy L Thompson } 5142b730f8bSJeremy L Thompson if (vec->num_readers > 0) { 5159c774eddSJeremy L Thompson // LCOV_EXCL_START 5162b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 5179c774eddSJeremy L Thompson // LCOV_EXCL_STOP 5182b730f8bSJeremy L Thompson } 5199c774eddSJeremy L Thompson 5202b730f8bSJeremy L Thompson CeedCall(vec->GetArrayWrite(vec, mem_type, array)); 52128bfd0b7SJeremy L Thompson vec->state++; 5229c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 5239c774eddSJeremy L Thompson } 5249c774eddSJeremy L Thompson 5259c774eddSJeremy L Thompson /** 526ea61e9acSJeremy L Thompson @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite() 5270436c2adSjeremylt 528ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to restore 529ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 5300436c2adSjeremylt 5310436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5320436c2adSjeremylt 5337a982d89SJeremy L. Thompson @ref User 5340436c2adSjeremylt **/ 5350436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 5362b730f8bSJeremy L Thompson if (vec->state % 2 != 1) { 5372b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted"); 538706efda3SJeremy L Thompson } 5392b730f8bSJeremy L Thompson if (vec->RestoreArray) CeedCall(vec->RestoreArray(vec)); 5400436c2adSjeremylt *array = NULL; 54128bfd0b7SJeremy L Thompson vec->state++; 542e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5430436c2adSjeremylt } 5440436c2adSjeremylt 5450436c2adSjeremylt /** 546b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 5470436c2adSjeremylt 548ea61e9acSJeremy L Thompson @param[in] vec CeedVector to restore 549ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 5500436c2adSjeremylt 5510436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5520436c2adSjeremylt 5537a982d89SJeremy L. Thompson @ref User 5540436c2adSjeremylt **/ 5550436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 5562b730f8bSJeremy L Thompson if (vec->num_readers == 0) { 5579c774eddSJeremy L Thompson // LCOV_EXCL_START 5582b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted"); 5599c774eddSJeremy L Thompson // LCOV_EXCL_STOP 5602b730f8bSJeremy L Thompson } 5619c774eddSJeremy L Thompson 56275a19770SJeremy L Thompson vec->num_readers--; 5632b730f8bSJeremy L Thompson if (vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec)); 5640436c2adSjeremylt *array = NULL; 56575a19770SJeremy L Thompson 566e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5670436c2adSjeremylt } 5680436c2adSjeremylt 5690436c2adSjeremylt /** 570171f8ca9Sjeremylt @brief Get the norm of a CeedVector. 571171f8ca9Sjeremylt 572ea61e9acSJeremy L Thompson Note: This operation is local to the CeedVector. 573ea61e9acSJeremy 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 574ea61e9acSJeremy L Thompson duplicated or hanging nodes. 575547d9b97Sjeremylt 576ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve maximum value 577ea61e9acSJeremy L Thompson @param[in] norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 578547d9b97Sjeremylt @param[out] norm Variable to store norm value 579547d9b97Sjeremylt 580547d9b97Sjeremylt @return An error code: 0 - success, otherwise - failure 581547d9b97Sjeremylt 5827a982d89SJeremy L. Thompson @ref User 583547d9b97Sjeremylt **/ 584d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) { 5859c774eddSJeremy L Thompson bool has_valid_array = true; 5862b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5872b730f8bSJeremy L Thompson if (!has_valid_array) { 5889c774eddSJeremy L Thompson // LCOV_EXCL_START 5899c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 5902b730f8bSJeremy L Thompson "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5919c774eddSJeremy L Thompson // LCOV_EXCL_STOP 5922b730f8bSJeremy L Thompson } 5939c774eddSJeremy L Thompson 594547d9b97Sjeremylt // Backend impl for GPU, if added 595547d9b97Sjeremylt if (vec->Norm) { 5962b730f8bSJeremy L Thompson CeedCall(vec->Norm(vec, norm_type, norm)); 597e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 598547d9b97Sjeremylt } 599547d9b97Sjeremylt 600547d9b97Sjeremylt const CeedScalar *array; 6012b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 602547d9b97Sjeremylt 603547d9b97Sjeremylt *norm = 0.; 604d1d35e2fSjeremylt switch (norm_type) { 605547d9b97Sjeremylt case CEED_NORM_1: 60692ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) { 607547d9b97Sjeremylt *norm += fabs(array[i]); 608547d9b97Sjeremylt } 609547d9b97Sjeremylt break; 610547d9b97Sjeremylt case CEED_NORM_2: 61192ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) { 612547d9b97Sjeremylt *norm += fabs(array[i]) * fabs(array[i]); 613547d9b97Sjeremylt } 614547d9b97Sjeremylt break; 615547d9b97Sjeremylt case CEED_NORM_MAX: 61692ae7e47SJeremy L Thompson for (CeedInt i = 0; i < vec->length; i++) { 617d1d35e2fSjeremylt const CeedScalar abs_v_i = fabs(array[i]); 618d1d35e2fSjeremylt *norm = *norm > abs_v_i ? *norm : abs_v_i; 619547d9b97Sjeremylt } 620547d9b97Sjeremylt } 6212b730f8bSJeremy L Thompson if (norm_type == CEED_NORM_2) *norm = sqrt(*norm); 622547d9b97Sjeremylt 6232b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 624e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 625547d9b97Sjeremylt } 626547d9b97Sjeremylt 627547d9b97Sjeremylt /** 628e0dd3b27Sjeremylt @brief Compute x = alpha x 629e0dd3b27Sjeremylt 63096b902e2Sjeremylt @param[in,out] x vector for scaling 63196b902e2Sjeremylt @param[in] alpha scaling factor 632e0dd3b27Sjeremylt 633e0dd3b27Sjeremylt @return An error code: 0 - success, otherwise - failure 634e0dd3b27Sjeremylt 635e0dd3b27Sjeremylt @ref User 636e0dd3b27Sjeremylt **/ 637e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) { 63873380422SJeremy L Thompson CeedScalar *x_array = NULL; 6391f9221feSJeremy L Thompson CeedSize n_x; 640e0dd3b27Sjeremylt 6419c774eddSJeremy L Thompson bool has_valid_array = true; 6422b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array)); 6432b730f8bSJeremy L Thompson if (!has_valid_array) { 6449c774eddSJeremy L Thompson // LCOV_EXCL_START 6459c774eddSJeremy L Thompson return CeedError(x->ceed, CEED_ERROR_BACKEND, 6462b730f8bSJeremy L Thompson "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6479c774eddSJeremy L Thompson // LCOV_EXCL_STOP 6482b730f8bSJeremy L Thompson } 6499c774eddSJeremy L Thompson 6502b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 651e0dd3b27Sjeremylt 652e0dd3b27Sjeremylt // Backend implementation 6532b730f8bSJeremy L Thompson if (x->Scale) return x->Scale(x, alpha); 654e0dd3b27Sjeremylt 655e0dd3b27Sjeremylt // Default implementation 6562b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array)); 6572b730f8bSJeremy L Thompson for (CeedInt i = 0; i < n_x; i++) x_array[i] *= alpha; 6582b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(x, &x_array)); 659e0dd3b27Sjeremylt 660e0dd3b27Sjeremylt return CEED_ERROR_SUCCESS; 661e0dd3b27Sjeremylt } 662e0dd3b27Sjeremylt 663e0dd3b27Sjeremylt /** 6640f7fd0f8Sjeremylt @brief Compute y = alpha x + y 6650f7fd0f8Sjeremylt 66696b902e2Sjeremylt @param[in,out] y target vector for sum 66796b902e2Sjeremylt @param[in] alpha scaling factor 66896b902e2Sjeremylt @param[in] x second vector, must be different than y 6690f7fd0f8Sjeremylt 6700f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 6710f7fd0f8Sjeremylt 6720f7fd0f8Sjeremylt @ref User 6730f7fd0f8Sjeremylt **/ 6740f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { 67573380422SJeremy L Thompson CeedScalar *y_array = NULL; 67673380422SJeremy L Thompson CeedScalar const *x_array = NULL; 6771f9221feSJeremy L Thompson CeedSize n_x, n_y; 6780f7fd0f8Sjeremylt 6792b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &n_y)); 6802b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 6812b730f8bSJeremy L Thompson if (n_x != n_y) { 6820f7fd0f8Sjeremylt // LCOV_EXCL_START 6832b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 6840f7fd0f8Sjeremylt // LCOV_EXCL_STOP 6852b730f8bSJeremy L Thompson } 6862b730f8bSJeremy L Thompson if (x == y) { 6870f7fd0f8Sjeremylt // LCOV_EXCL_START 6882b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); 6890f7fd0f8Sjeremylt // LCOV_EXCL_STOP 6902b730f8bSJeremy L Thompson } 6910f7fd0f8Sjeremylt 6929c774eddSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 6932b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 6942b730f8bSJeremy L Thompson if (!has_valid_array_x) { 6959c774eddSJeremy L Thompson // LCOV_EXCL_START 6962b730f8bSJeremy L Thompson return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6979c774eddSJeremy L Thompson // LCOV_EXCL_STOP 6982b730f8bSJeremy L Thompson } 6992b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 7002b730f8bSJeremy L Thompson if (!has_valid_array_y) { 7019c774eddSJeremy L Thompson // LCOV_EXCL_START 7022b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7039c774eddSJeremy L Thompson // LCOV_EXCL_STOP 7042b730f8bSJeremy L Thompson } 7059c774eddSJeremy L Thompson 7062d04630dSjeremylt Ceed ceed_parent_x, ceed_parent_y; 7072b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 7082b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 7092b730f8bSJeremy L Thompson if (ceed_parent_x != ceed_parent_y) { 7102d04630dSjeremylt // LCOV_EXCL_START 7112b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); 7122d04630dSjeremylt // LCOV_EXCL_STOP 7132b730f8bSJeremy L Thompson } 7142d04630dSjeremylt 7150f7fd0f8Sjeremylt // Backend implementation 716eaf62fffSJeremy L Thompson if (y->AXPY) { 7172b730f8bSJeremy L Thompson CeedCall(y->AXPY(y, alpha, x)); 718eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 719eaf62fffSJeremy L Thompson } 7200f7fd0f8Sjeremylt 7210f7fd0f8Sjeremylt // Default implementation 7222b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array)); 7232b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 7240f7fd0f8Sjeremylt 7252b730f8bSJeremy L Thompson assert(x_array); 7262b730f8bSJeremy L Thompson assert(y_array); 72773380422SJeremy L Thompson 7282b730f8bSJeremy L Thompson for (CeedInt i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i]; 7290f7fd0f8Sjeremylt 7302b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(y, &y_array)); 7312b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 7320f7fd0f8Sjeremylt 7330f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 7340f7fd0f8Sjeremylt } 7350f7fd0f8Sjeremylt 7360f7fd0f8Sjeremylt /** 7375fb68f37SKaren (Ren) Stengel @brief Compute y = alpha x + beta y 7385fb68f37SKaren (Ren) Stengel 7395fb68f37SKaren (Ren) Stengel @param[in,out] y target vector for sum 7405fb68f37SKaren (Ren) Stengel @param[in] alpha first scaling factor 7415fb68f37SKaren (Ren) Stengel @param[in] beta second scaling factor 7425fb68f37SKaren (Ren) Stengel @param[in] x second vector, must be different than y 7435fb68f37SKaren (Ren) Stengel 7445fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 7455fb68f37SKaren (Ren) Stengel 7465fb68f37SKaren (Ren) Stengel @ref User 7475fb68f37SKaren (Ren) Stengel **/ 7485fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 7495fb68f37SKaren (Ren) Stengel CeedScalar *y_array = NULL; 7505fb68f37SKaren (Ren) Stengel CeedScalar const *x_array = NULL; 7515fb68f37SKaren (Ren) Stengel CeedSize n_x, n_y; 7525fb68f37SKaren (Ren) Stengel 7535fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetLength(y, &n_y)); 7545fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetLength(x, &n_x)); 7555fb68f37SKaren (Ren) Stengel if (n_x != n_y) { 7565fb68f37SKaren (Ren) Stengel // LCOV_EXCL_START 7575fb68f37SKaren (Ren) Stengel return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 7585fb68f37SKaren (Ren) Stengel // LCOV_EXCL_STOP 7595fb68f37SKaren (Ren) Stengel } 7605fb68f37SKaren (Ren) Stengel if (x == y) { 7615fb68f37SKaren (Ren) Stengel // LCOV_EXCL_START 7625fb68f37SKaren (Ren) Stengel return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY"); 7635fb68f37SKaren (Ren) Stengel // LCOV_EXCL_STOP 7645fb68f37SKaren (Ren) Stengel } 7655fb68f37SKaren (Ren) Stengel 7665fb68f37SKaren (Ren) Stengel bool has_valid_array_x = true, has_valid_array_y = true; 7675fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 7685fb68f37SKaren (Ren) Stengel if (!has_valid_array_x) { 7695fb68f37SKaren (Ren) Stengel // LCOV_EXCL_START 7705fb68f37SKaren (Ren) Stengel return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7715fb68f37SKaren (Ren) Stengel // LCOV_EXCL_STOP 7725fb68f37SKaren (Ren) Stengel } 7735fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 7745fb68f37SKaren (Ren) Stengel if (!has_valid_array_y) { 7755fb68f37SKaren (Ren) Stengel // LCOV_EXCL_START 7765fb68f37SKaren (Ren) Stengel return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7775fb68f37SKaren (Ren) Stengel // LCOV_EXCL_STOP 7785fb68f37SKaren (Ren) Stengel } 7795fb68f37SKaren (Ren) Stengel 7805fb68f37SKaren (Ren) Stengel Ceed ceed_parent_x, ceed_parent_y; 7815fb68f37SKaren (Ren) Stengel CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 7825fb68f37SKaren (Ren) Stengel CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 7835fb68f37SKaren (Ren) Stengel if (ceed_parent_x != ceed_parent_y) { 7845fb68f37SKaren (Ren) Stengel // LCOV_EXCL_START 7855fb68f37SKaren (Ren) Stengel return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); 7865fb68f37SKaren (Ren) Stengel // LCOV_EXCL_STOP 7875fb68f37SKaren (Ren) Stengel } 7885fb68f37SKaren (Ren) Stengel 7895fb68f37SKaren (Ren) Stengel // Backend implementation 7905fb68f37SKaren (Ren) Stengel if (y->AXPBY) { 7915fb68f37SKaren (Ren) Stengel CeedCall(y->AXPBY(y, alpha, beta, x)); 7925fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 7935fb68f37SKaren (Ren) Stengel } 7945fb68f37SKaren (Ren) Stengel 7955fb68f37SKaren (Ren) Stengel // Default implementation 7965fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 7975fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 7985fb68f37SKaren (Ren) Stengel 7995fb68f37SKaren (Ren) Stengel assert(x_array); 8005fb68f37SKaren (Ren) Stengel assert(y_array); 8015fb68f37SKaren (Ren) Stengel 8025fb68f37SKaren (Ren) Stengel for (CeedInt i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i] + beta * y_array[i]; 8035fb68f37SKaren (Ren) Stengel 8045fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(y, &y_array)); 8055fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 8065fb68f37SKaren (Ren) Stengel 8075fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 8085fb68f37SKaren (Ren) Stengel } 8095fb68f37SKaren (Ren) Stengel 8105fb68f37SKaren (Ren) Stengel /** 811ea61e9acSJeremy L Thompson @brief Compute the pointwise multiplication w = x .* y. 812ea61e9acSJeremy L Thompson Any subset of x, y, and w may be the same vector. 8130f7fd0f8Sjeremylt 81496b902e2Sjeremylt @param[out] w target vector for the product 81596b902e2Sjeremylt @param[in] x first vector for product 81696b902e2Sjeremylt @param[in] y second vector for the product 8170f7fd0f8Sjeremylt 8180f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 8190f7fd0f8Sjeremylt 8200f7fd0f8Sjeremylt @ ref User 8210f7fd0f8Sjeremylt **/ 8220f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { 82373380422SJeremy L Thompson CeedScalar *w_array = NULL; 82473380422SJeremy L Thompson CeedScalar const *x_array = NULL, *y_array = NULL; 8251f9221feSJeremy L Thompson CeedSize n_w, n_x, n_y; 8260f7fd0f8Sjeremylt 8272b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(w, &n_w)); 8282b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 8292b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &n_y)); 8302b730f8bSJeremy L Thompson if (n_w != n_x || n_w != n_y) { 8310f7fd0f8Sjeremylt // LCOV_EXCL_START 8322b730f8bSJeremy L Thompson return CeedError(w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths"); 8330f7fd0f8Sjeremylt // LCOV_EXCL_STOP 8342b730f8bSJeremy L Thompson } 8350f7fd0f8Sjeremylt 8362d04630dSjeremylt Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y; 8372b730f8bSJeremy L Thompson CeedCall(CeedGetParent(w->ceed, &ceed_parent_w)); 8382b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 8392b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 8402b730f8bSJeremy L Thompson if ((ceed_parent_w != ceed_parent_x) || (ceed_parent_w != ceed_parent_y)) { 8412d04630dSjeremylt // LCOV_EXCL_START 8422b730f8bSJeremy L Thompson return CeedError(w->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors w, x, and y must be created by the same Ceed context"); 8432d04630dSjeremylt // LCOV_EXCL_STOP 8442b730f8bSJeremy L Thompson } 8452d04630dSjeremylt 8469c774eddSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 8472b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 8482b730f8bSJeremy L Thompson if (!has_valid_array_x) { 8499c774eddSJeremy L Thompson // LCOV_EXCL_START 8502b730f8bSJeremy L Thompson return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8519c774eddSJeremy L Thompson // LCOV_EXCL_STOP 8522b730f8bSJeremy L Thompson } 8532b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 8542b730f8bSJeremy L Thompson if (!has_valid_array_y) { 8559c774eddSJeremy L Thompson // LCOV_EXCL_START 8562b730f8bSJeremy L Thompson return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 8579c774eddSJeremy L Thompson // LCOV_EXCL_STOP 8582b730f8bSJeremy L Thompson } 8599c774eddSJeremy L Thompson 8600f7fd0f8Sjeremylt // Backend implementation 861eaf62fffSJeremy L Thompson if (w->PointwiseMult) { 8622b730f8bSJeremy L Thompson CeedCall(w->PointwiseMult(w, x, y)); 863eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 864eaf62fffSJeremy L Thompson } 8650f7fd0f8Sjeremylt 8660f7fd0f8Sjeremylt // Default implementation 8670f7fd0f8Sjeremylt if (x != w) { 8682b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 8690f7fd0f8Sjeremylt } else { 870c6d796c6SJeremy L Thompson CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 8710f7fd0f8Sjeremylt x_array = w_array; 8720f7fd0f8Sjeremylt } 8730f7fd0f8Sjeremylt if (y != w && y != x) { 8742b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array)); 875c6d796c6SJeremy L Thompson } else if (y == x) { 8760f7fd0f8Sjeremylt y_array = x_array; 877c6d796c6SJeremy L Thompson } else { 878c6d796c6SJeremy L Thompson CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 879c6d796c6SJeremy L Thompson y_array = w_array; 8800f7fd0f8Sjeremylt } 881c6d796c6SJeremy L Thompson if (!w_array) CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array)); 8820f7fd0f8Sjeremylt 8832b730f8bSJeremy L Thompson assert(w_array); 8842b730f8bSJeremy L Thompson assert(x_array); 8852b730f8bSJeremy L Thompson assert(y_array); 88673380422SJeremy L Thompson 8872b730f8bSJeremy L Thompson for (CeedInt i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i]; 8880f7fd0f8Sjeremylt 8892b730f8bSJeremy L Thompson if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array)); 8902b730f8bSJeremy L Thompson if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 8912b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(w, &w_array)); 8920f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 8930f7fd0f8Sjeremylt } 8940f7fd0f8Sjeremylt 8950f7fd0f8Sjeremylt /** 896d99fa3c5SJeremy L Thompson @brief Take the reciprocal of a CeedVector. 897d99fa3c5SJeremy L Thompson 898ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to take reciprocal 899d99fa3c5SJeremy L Thompson 900d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 901d99fa3c5SJeremy L Thompson 902d99fa3c5SJeremy L Thompson @ref User 903d99fa3c5SJeremy L Thompson **/ 904d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) { 9059c774eddSJeremy L Thompson bool has_valid_array = true; 9062b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 9072b730f8bSJeremy L Thompson 9082b730f8bSJeremy L Thompson if (!has_valid_array) { 9099c774eddSJeremy L Thompson // LCOV_EXCL_START 9109c774eddSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_BACKEND, 9112b730f8bSJeremy L Thompson "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); 9129c774eddSJeremy L Thompson // LCOV_EXCL_STOP 9132b730f8bSJeremy L Thompson } 9149c774eddSJeremy L Thompson 915d99fa3c5SJeremy L Thompson // Check if vector data set 9162b730f8bSJeremy L Thompson if (!vec->state) { 917d99fa3c5SJeremy L Thompson // LCOV_EXCL_START 9182b730f8bSJeremy L Thompson return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); 919d99fa3c5SJeremy L Thompson // LCOV_EXCL_STOP 9202b730f8bSJeremy L Thompson } 921d99fa3c5SJeremy L Thompson 922d99fa3c5SJeremy L Thompson // Backend impl for GPU, if added 923d99fa3c5SJeremy L Thompson if (vec->Reciprocal) { 9242b730f8bSJeremy L Thompson CeedCall(vec->Reciprocal(vec)); 925e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 926d99fa3c5SJeremy L Thompson } 927d99fa3c5SJeremy L Thompson 9281f9221feSJeremy L Thompson CeedSize len; 9292b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &len)); 930d99fa3c5SJeremy L Thompson CeedScalar *array; 9312b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 9322b730f8bSJeremy L Thompson for (CeedInt i = 0; i < len; i++) { 9332b730f8bSJeremy L Thompson if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i]; 9342b730f8bSJeremy L Thompson } 935d99fa3c5SJeremy L Thompson 9362b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 937e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 938d99fa3c5SJeremy L Thompson } 939d99fa3c5SJeremy L Thompson 940d99fa3c5SJeremy L Thompson /** 9417a982d89SJeremy L. Thompson @brief View a CeedVector 9420436c2adSjeremylt 943*acb2c48cSJeremy L Thompson Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`. 944*acb2c48cSJeremy L Thompson Any portion of the provided range that is outside the range of valid indices for the CeedVector will be ignored. 945*acb2c48cSJeremy L Thompson 946*acb2c48cSJeremy L Thompson @param[in] vec CeedVector to view 947*acb2c48cSJeremy L Thompson @param[in] start Index of first CeedVector entry to view 948*acb2c48cSJeremy L Thompson @param[in] stop Index of last CeedVector entry to view 949*acb2c48cSJeremy L Thompson @param[in] step Step between CeedVector entries to view 950*acb2c48cSJeremy L Thompson @param[in] fp_fmt Printing format 951*acb2c48cSJeremy L Thompson @param[in] stream Filestream to write to 952*acb2c48cSJeremy L Thompson 953*acb2c48cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 954*acb2c48cSJeremy L Thompson 955*acb2c48cSJeremy L Thompson @ref User 956*acb2c48cSJeremy L Thompson **/ 957*acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) { 958*acb2c48cSJeremy L Thompson const CeedScalar *x; 959*acb2c48cSJeremy L Thompson char fmt[1024]; 960*acb2c48cSJeremy L Thompson 961*acb2c48cSJeremy L Thompson if (step == 0) return CeedError(vec->ceed, CEED_ERROR_MINOR, "View range 'step' must be nonzero"); 962*acb2c48cSJeremy L Thompson 963*acb2c48cSJeremy L Thompson fprintf(stream, "CeedVector length %ld\n", (long)vec->length); 964*acb2c48cSJeremy L Thompson if (start != 0 || stop != vec->length || step != 1) { 965*acb2c48cSJeremy L Thompson fprintf(stream, " start: %ld\n stop: %ld\n step: %" CeedInt_FMT "\n", (long)start, (long)stop, step); 966*acb2c48cSJeremy L Thompson } 967*acb2c48cSJeremy L Thompson if (start > vec->length) start = vec->length; 968*acb2c48cSJeremy L Thompson if (stop > vec->length) stop = vec->length; 969*acb2c48cSJeremy L Thompson 970*acb2c48cSJeremy L Thompson snprintf(fmt, sizeof fmt, " %s\n", fp_fmt ? fp_fmt : "%g"); 971*acb2c48cSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x)); 972*acb2c48cSJeremy L Thompson for (CeedInt i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]); 973*acb2c48cSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &x)); 974*acb2c48cSJeremy L Thompson if (stop != vec->length) fprintf(stream, " ...\n"); 975*acb2c48cSJeremy L Thompson 976*acb2c48cSJeremy L Thompson return CEED_ERROR_SUCCESS; 977*acb2c48cSJeremy L Thompson } 978*acb2c48cSJeremy L Thompson 979*acb2c48cSJeremy L Thompson /** 980*acb2c48cSJeremy L Thompson @brief View a CeedVector 981*acb2c48cSJeremy L Thompson 9820a0da059Sjeremylt @param[in] vec CeedVector to view 983d1d35e2fSjeremylt @param[in] fp_fmt Printing format 9840a0da059Sjeremylt @param[in] stream Filestream to write to 9850a0da059Sjeremylt 9860436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 9870436c2adSjeremylt 9887a982d89SJeremy L. Thompson @ref User 9890436c2adSjeremylt **/ 990d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { 991*acb2c48cSJeremy L Thompson CeedCall(CeedVectorViewRange(vec, 0, vec->length, 1, fp_fmt, stream)); 992e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9930436c2adSjeremylt } 9940436c2adSjeremylt 9950436c2adSjeremylt /** 996b7c9bbdaSJeremy L Thompson @brief Get the Ceed associated with a CeedVector 997b7c9bbdaSJeremy L Thompson 998ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 999b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store ceed 1000b7c9bbdaSJeremy L Thompson 1001b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1002b7c9bbdaSJeremy L Thompson 1003b7c9bbdaSJeremy L Thompson @ref Advanced 1004b7c9bbdaSJeremy L Thompson **/ 1005b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 1006b7c9bbdaSJeremy L Thompson *ceed = vec->ceed; 1007b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1008b7c9bbdaSJeremy L Thompson } 1009b7c9bbdaSJeremy L Thompson 1010b7c9bbdaSJeremy L Thompson /** 10117a982d89SJeremy L. Thompson @brief Get the length of a CeedVector 10120436c2adSjeremylt 1013ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve length 10147a982d89SJeremy L. Thompson @param[out] length Variable to store length 10150436c2adSjeremylt 10160436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 10170436c2adSjeremylt 10187a982d89SJeremy L. Thompson @ref User 10190436c2adSjeremylt **/ 10201f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) { 10217a982d89SJeremy L. Thompson *length = vec->length; 1022e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10230436c2adSjeremylt } 10240436c2adSjeremylt 10250436c2adSjeremylt /** 10260436c2adSjeremylt @brief Destroy a CeedVector 10270436c2adSjeremylt 1028ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to destroy 10290436c2adSjeremylt 10300436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 10310436c2adSjeremylt 10327a982d89SJeremy L. Thompson @ref User 10330436c2adSjeremylt **/ 10340436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) { 1035ad6481ceSJeremy L Thompson if (!*vec || --(*vec)->ref_count > 0) { 1036ad6481ceSJeremy L Thompson *vec = NULL; 1037ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1038ad6481ceSJeremy L Thompson } 10392b730f8bSJeremy L Thompson if (((*vec)->state % 2) == 1) { 10402b730f8bSJeremy L Thompson return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use"); 10412b730f8bSJeremy L Thompson } 10422b730f8bSJeremy L Thompson if ((*vec)->num_readers > 0) { 1043e92b641fSJeremy L Thompson // LCOV_EXCL_START 10442b730f8bSJeremy L Thompson return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access"); 1045e92b641fSJeremy L Thompson // LCOV_EXCL_STOP 10460436c2adSjeremylt } 10470436c2adSjeremylt 10482b730f8bSJeremy L Thompson if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec)); 10492b730f8bSJeremy L Thompson 10502b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*vec)->ceed)); 10512b730f8bSJeremy L Thompson CeedCall(CeedFree(vec)); 1052e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 10530436c2adSjeremylt } 10540436c2adSjeremylt 10550436c2adSjeremylt /// @} 1056