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 82b730f8bSJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 11c85e8640SSebastian Grimberg #include <assert.h> 12547d9b97Sjeremylt #include <math.h> 1349aac155SJeremy L Thompson #include <stdbool.h> 143d576824SJeremy L Thompson #include <stdint.h> 153d576824SJeremy L Thompson #include <stdio.h> 160436c2adSjeremylt 177a982d89SJeremy L. Thompson /// @file 187a982d89SJeremy L. Thompson /// Implementation of public CeedVector interfaces 197a982d89SJeremy L. Thompson 200436c2adSjeremylt /// @cond DOXYGEN_SKIP 210436c2adSjeremylt static struct CeedVector_private ceed_vector_active; 220436c2adSjeremylt static struct CeedVector_private ceed_vector_none; 230436c2adSjeremylt /// @endcond 240436c2adSjeremylt 257a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 267a982d89SJeremy L. Thompson /// @{ 277a982d89SJeremy L. Thompson 28ea61e9acSJeremy L Thompson /// Indicate that vector will be provided as an explicit argument to CeedOperatorApply(). 297a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_ACTIVE = &ceed_vector_active; 307a982d89SJeremy L. Thompson 3196b902e2Sjeremylt /// Indicate that no vector is applicable (i.e., for @ref CEED_EVAL_WEIGHT). 327a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_NONE = &ceed_vector_none; 337a982d89SJeremy L. Thompson 347a982d89SJeremy L. Thompson /// @} 357a982d89SJeremy L. Thompson 367a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 377a982d89SJeremy L. Thompson /// CeedVector Backend API 387a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 397a982d89SJeremy L. Thompson /// @addtogroup CeedVectorBackend 407a982d89SJeremy L. Thompson /// @{ 417a982d89SJeremy L. Thompson 427a982d89SJeremy L. Thompson /** 439c774eddSJeremy L Thompson @brief Check for valid data in a CeedVector 449c774eddSJeremy L Thompson 45ea61e9acSJeremy L Thompson @param[in] vec CeedVector to check validity 469c774eddSJeremy L Thompson @param[out] has_valid_array Variable to store validity 479c774eddSJeremy L Thompson 489c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 499c774eddSJeremy L Thompson 509c774eddSJeremy L Thompson @ref Backend 519c774eddSJeremy L Thompson **/ 529c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) { 536574a04fSJeremy L Thompson CeedCheck(vec->HasValidArray, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasValidArray"); 542b730f8bSJeremy L Thompson CeedCall(vec->HasValidArray(vec, has_valid_array)); 559c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 569c774eddSJeremy L Thompson } 579c774eddSJeremy L Thompson 589c774eddSJeremy L Thompson /** 599c774eddSJeremy L Thompson @brief Check for borrowed array of a specific CeedMemType in a CeedVector 609c774eddSJeremy L Thompson 61ea61e9acSJeremy L Thompson @param[in] vec CeedVector to check 62ea61e9acSJeremy L Thompson @param[in] mem_type Memory type to check 639c774eddSJeremy L Thompson @param[out] has_borrowed_array_of_type Variable to store result 649c774eddSJeremy L Thompson 659c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 669c774eddSJeremy L Thompson 679c774eddSJeremy L Thompson @ref Backend 689c774eddSJeremy L Thompson **/ 692b730f8bSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 706574a04fSJeremy L Thompson CeedCheck(vec->HasBorrowedArrayOfType, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasBorrowedArrayOfType"); 712b730f8bSJeremy L Thompson CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type)); 729c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 739c774eddSJeremy L Thompson } 749c774eddSJeremy L Thompson 759c774eddSJeremy L Thompson /** 767a982d89SJeremy L. Thompson @brief Get the state of a CeedVector 777a982d89SJeremy L. Thompson 78ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 797a982d89SJeremy L. Thompson @param[out] state Variable to store state 807a982d89SJeremy L. Thompson 817a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 827a982d89SJeremy L. Thompson 837a982d89SJeremy L. Thompson @ref Backend 847a982d89SJeremy L. Thompson **/ 857a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) { 867a982d89SJeremy L. Thompson *state = vec->state; 87e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 887a982d89SJeremy L. Thompson } 897a982d89SJeremy L. Thompson 907a982d89SJeremy L. Thompson /** 917a982d89SJeremy L. Thompson @brief Get the backend data of a CeedVector 927a982d89SJeremy L. Thompson 93ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 947a982d89SJeremy L. Thompson @param[out] data Variable to store data 957a982d89SJeremy L. Thompson 967a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 977a982d89SJeremy L. Thompson 987a982d89SJeremy L. Thompson @ref Backend 997a982d89SJeremy L. Thompson **/ 100777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) { 101777ff853SJeremy L Thompson *(void **)data = vec->data; 102e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1037a982d89SJeremy L. Thompson } 1047a982d89SJeremy L. Thompson 1057a982d89SJeremy L. Thompson /** 1067a982d89SJeremy L. Thompson @brief Set the backend data of a CeedVector 1077a982d89SJeremy L. Thompson 108ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to retrieve state 109ea61e9acSJeremy L Thompson @param[in] data Data to set 1107a982d89SJeremy L. Thompson 1117a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1127a982d89SJeremy L. Thompson 1137a982d89SJeremy L. Thompson @ref Backend 1147a982d89SJeremy L. Thompson **/ 115777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) { 116777ff853SJeremy L Thompson vec->data = data; 117e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1187a982d89SJeremy L. Thompson } 1197a982d89SJeremy L. Thompson 12034359f16Sjeremylt /** 12134359f16Sjeremylt @brief Increment the reference counter for a CeedVector 12234359f16Sjeremylt 123ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to increment the reference counter 12434359f16Sjeremylt 12534359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 12634359f16Sjeremylt 12734359f16Sjeremylt @ref Backend 12834359f16Sjeremylt **/ 1299560d06aSjeremylt int CeedVectorReference(CeedVector vec) { 13034359f16Sjeremylt vec->ref_count++; 13134359f16Sjeremylt return CEED_ERROR_SUCCESS; 13234359f16Sjeremylt } 13334359f16Sjeremylt 1347a982d89SJeremy L. Thompson /// @} 1357a982d89SJeremy L. Thompson 1367a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1377a982d89SJeremy L. Thompson /// CeedVector Public API 1387a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1397a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 1400436c2adSjeremylt /// @{ 1410436c2adSjeremylt 1420436c2adSjeremylt /** 1430436c2adSjeremylt @brief Create a CeedVector of the specified length (does not allocate memory) 1440436c2adSjeremylt 145ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedVector will be created 146ea61e9acSJeremy L Thompson @param[in] length Length of vector 147ea61e9acSJeremy L Thompson @param[out] vec Address of the variable where the newly created CeedVector will be stored 1480436c2adSjeremylt 1490436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 1500436c2adSjeremylt 1517a982d89SJeremy L. Thompson @ref User 1520436c2adSjeremylt **/ 1531f9221feSJeremy L Thompson int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) { 1540436c2adSjeremylt if (!ceed->VectorCreate) { 1550436c2adSjeremylt Ceed delegate; 1566574a04fSJeremy L Thompson 1572b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector")); 1586574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorCreate"); 1592b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(delegate, length, vec)); 160e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1610436c2adSjeremylt } 1620436c2adSjeremylt 1632b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, vec)); 164db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*vec)->ceed)); 165d1d35e2fSjeremylt (*vec)->ref_count = 1; 1660436c2adSjeremylt (*vec)->length = length; 1670436c2adSjeremylt (*vec)->state = 0; 1682b730f8bSJeremy L Thompson CeedCall(ceed->VectorCreate(length, *vec)); 169e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1700436c2adSjeremylt } 1710436c2adSjeremylt 1720436c2adSjeremylt /** 173ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedVector. 1744385fb7fSSebastian Grimberg 175ea61e9acSJeremy L Thompson Both pointers should be destroyed with `CeedVectorDestroy()`. 176512bb800SJeremy L Thompson 177512bb800SJeremy 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. 178512bb800SJeremy L Thompson This CeedVector will be destroyed if `vec_copy` is the only reference to this CeedVector. 1799560d06aSjeremylt 180ea61e9acSJeremy L Thompson @param[in] vec CeedVector to copy reference to 181ea61e9acSJeremy L Thompson @param[in,out] vec_copy Variable to store copied reference 1829560d06aSjeremylt 1839560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 1849560d06aSjeremylt 1859560d06aSjeremylt @ref User 1869560d06aSjeremylt **/ 1879560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) { 188393ac2cdSJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorReference(vec)); 1892b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(vec_copy)); 1909560d06aSjeremylt *vec_copy = vec; 1919560d06aSjeremylt return CEED_ERROR_SUCCESS; 1929560d06aSjeremylt } 1939560d06aSjeremylt 1949560d06aSjeremylt /** 1955fb68f37SKaren (Ren) Stengel @brief Copy a CeedVector into a different CeedVector. 1964385fb7fSSebastian Grimberg 1975fb68f37SKaren (Ren) Stengel Both pointers should be destroyed with `CeedVectorDestroy()`. 1984385fb7fSSebastian Grimberg 1995fb68f37SKaren (Ren) Stengel Note: If `*vec_copy` is non-NULL, then it is assumed that `*vec_copy` is a pointer to a CeedVector. 2005fb68f37SKaren (Ren) Stengel This CeedVector will be destroyed if `*vec_copy` is the only reference to this CeedVector. 2015fb68f37SKaren (Ren) Stengel 2025fb68f37SKaren (Ren) Stengel @param[in] vec CeedVector to copy 2035fb68f37SKaren (Ren) Stengel @param[in,out] vec_copy Variable to store copied CeedVector to 2045fb68f37SKaren (Ren) Stengel 2055fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 2065fb68f37SKaren (Ren) Stengel 2075fb68f37SKaren (Ren) Stengel @ref User 2085fb68f37SKaren (Ren) Stengel **/ 2095fb68f37SKaren (Ren) Stengel int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) { 2105fb68f37SKaren (Ren) Stengel Ceed ceed; 2115fb68f37SKaren (Ren) Stengel CeedMemType mem_type, mem_type_copy; 2125fb68f37SKaren (Ren) Stengel CeedScalar *array; 2135fb68f37SKaren (Ren) Stengel 2145fb68f37SKaren (Ren) Stengel // Get the preferred memory type 2155fb68f37SKaren (Ren) Stengel CeedVectorGetCeed(vec, &ceed); 2165fb68f37SKaren (Ren) Stengel CeedGetPreferredMemType(ceed, &mem_type); 2175fb68f37SKaren (Ren) Stengel 2185fb68f37SKaren (Ren) Stengel // Get the preferred memory type 2195fb68f37SKaren (Ren) Stengel CeedVectorGetCeed(vec_copy, &ceed); 2205fb68f37SKaren (Ren) Stengel CeedGetPreferredMemType(ceed, &mem_type_copy); 2215fb68f37SKaren (Ren) Stengel 2225fb68f37SKaren (Ren) Stengel // Check that both have same memory type 2235fb68f37SKaren (Ren) Stengel if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST; 2245fb68f37SKaren (Ren) Stengel 2255fb68f37SKaren (Ren) Stengel // Copy the values from vec to vec_copy 2265fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(vec, mem_type, &array)); 2275fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array)); 2285fb68f37SKaren (Ren) Stengel 2295fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(vec, &array)); 2305fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 2315fb68f37SKaren (Ren) Stengel } 2325fb68f37SKaren (Ren) Stengel 2335fb68f37SKaren (Ren) Stengel /** 234ea61e9acSJeremy L Thompson @brief Set the array used by a CeedVector, freeing any previously allocated array if applicable. 2354385fb7fSSebastian Grimberg 236ea61e9acSJeremy L Thompson The backend may copy values to a different memtype, such as during @ref CeedOperatorApply(). 2376a6c615bSJeremy L Thompson See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 2380436c2adSjeremylt 239ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 240ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the array being passed 241ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the array 242ea61e9acSJeremy L Thompson @param[in] array Array to be used, or NULL with @ref CEED_COPY_VALUES to have the library allocate 2430436c2adSjeremylt 2440436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2450436c2adSjeremylt 2467a982d89SJeremy L. Thompson @ref User 2470436c2adSjeremylt **/ 2482b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 2496574a04fSJeremy L Thompson CeedCheck(vec->SetArray, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); 2506574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 2516574a04fSJeremy L Thompson CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 2520436c2adSjeremylt 2532b730f8bSJeremy L Thompson CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); 2540436c2adSjeremylt vec->state += 2; 255e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2560436c2adSjeremylt } 2570436c2adSjeremylt 2580436c2adSjeremylt /** 2590436c2adSjeremylt @brief Set the CeedVector to a constant value 2600436c2adSjeremylt 261ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 2620436c2adSjeremylt @param[in] value Value to be used 2630436c2adSjeremylt 2640436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2650436c2adSjeremylt 2667a982d89SJeremy L. Thompson @ref User 2670436c2adSjeremylt **/ 2680436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 2696574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 2706574a04fSJeremy L Thompson CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 2710436c2adSjeremylt 2720436c2adSjeremylt if (vec->SetValue) { 2732b730f8bSJeremy L Thompson CeedCall(vec->SetValue(vec, value)); 2740436c2adSjeremylt } else { 2750436c2adSjeremylt CeedScalar *array; 2762b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 277*b94338b9SJed Brown for (CeedSize i = 0; i < vec->length; i++) array[i] = value; 2782b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 2790436c2adSjeremylt } 2800436c2adSjeremylt vec->state += 2; 281e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2820436c2adSjeremylt } 2830436c2adSjeremylt 2840436c2adSjeremylt /** 285ea61e9acSJeremy L Thompson @brief Sync the CeedVector to a specified memtype. 2864385fb7fSSebastian Grimberg 287ea61e9acSJeremy L Thompson This function is used to force synchronization of arrays set with @ref CeedVectorSetArray(). 288ea61e9acSJeremy L Thompson If the requested memtype is already synchronized, this function results in a no-op. 2890436c2adSjeremylt 290ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 291ea61e9acSJeremy L Thompson @param[in] mem_type Memtype to be synced 2920436c2adSjeremylt 2930436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2940436c2adSjeremylt 2957a982d89SJeremy L. Thompson @ref User 2960436c2adSjeremylt **/ 297d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { 2986574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use"); 2990436c2adSjeremylt 3000436c2adSjeremylt if (vec->SyncArray) { 3012b730f8bSJeremy L Thompson CeedCall(vec->SyncArray(vec, mem_type)); 3020436c2adSjeremylt } else { 3030436c2adSjeremylt const CeedScalar *array; 3042b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array)); 3052b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 3060436c2adSjeremylt } 307e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3080436c2adSjeremylt } 3090436c2adSjeremylt 3100436c2adSjeremylt /** 311ea61e9acSJeremy 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. 3124385fb7fSSebastian Grimberg 3139c774eddSJeremy L Thompson The caller is responsible for managing and freeing the array. 314ea61e9acSJeremy L Thompson This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type. 3156a6c615bSJeremy L Thompson 316ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 317ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to take the array. 318ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 319ea61e9acSJeremy L Thompson @param[out] array Array on memory type mem_type, or NULL if array pointer is not required 3206a6c615bSJeremy L Thompson 3216a6c615bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3226a6c615bSJeremy L Thompson 3236a6c615bSJeremy L Thompson @ref User 3246a6c615bSJeremy L Thompson **/ 3252b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 3266574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); 3276574a04fSJeremy L Thompson CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); 3286a6c615bSJeremy L Thompson 32950c643e1SJed Brown CeedScalar *temp_array = NULL; 33050c643e1SJed Brown if (vec->length > 0) { 3319c774eddSJeremy L Thompson bool has_borrowed_array_of_type = true; 3322b730f8bSJeremy L Thompson CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); 3336574a04fSJeremy L Thompson CeedCheck(has_borrowed_array_of_type, vec->ceed, CEED_ERROR_BACKEND, 3346574a04fSJeremy L Thompson "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", CeedMemTypes[mem_type]); 3359c774eddSJeremy L Thompson 3369c774eddSJeremy L Thompson bool has_valid_array = true; 3372b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 3386574a04fSJeremy L Thompson CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND, 3392b730f8bSJeremy L Thompson "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); 3409c774eddSJeremy L Thompson 3412b730f8bSJeremy L Thompson CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); 34250c643e1SJed Brown } 343d1d35e2fSjeremylt if (array) (*array) = temp_array; 344e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3456a6c615bSJeremy L Thompson } 3466a6c615bSJeremy L Thompson 3476a6c615bSJeremy L Thompson /** 348b3cf021fSjeremylt @brief Get read/write access to a CeedVector via the specified memory type. 3494385fb7fSSebastian Grimberg 350b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArray(). 3510436c2adSjeremylt 352ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to access 353ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 354ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 355d1d35e2fSjeremylt @param[out] array Array on memory type mem_type 3560436c2adSjeremylt 357ea61e9acSJeremy L Thompson @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide access to array pointers in the desired memory space. 358ea61e9acSJeremy L Thompson Pairing get/restore allows the Vector to track access, thus knowing if norms or other operations may need to be recomputed. 3590436c2adSjeremylt 3600436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3610436c2adSjeremylt 3627a982d89SJeremy L. Thompson @ref User 3630436c2adSjeremylt **/ 3642b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 3656574a04fSJeremy L Thompson CeedCheck(vec->GetArray, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); 3666574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 3676574a04fSJeremy L Thompson CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3680436c2adSjeremylt 3699c774eddSJeremy L Thompson bool has_valid_array = true; 3702b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 3716574a04fSJeremy L Thompson CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND, 3722b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 3739c774eddSJeremy L Thompson 3742b730f8bSJeremy L Thompson CeedCall(vec->GetArray(vec, mem_type, array)); 37528bfd0b7SJeremy L Thompson vec->state++; 376e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3770436c2adSjeremylt } 3780436c2adSjeremylt 3790436c2adSjeremylt /** 380b3cf021fSjeremylt @brief Get read-only access to a CeedVector via the specified memory type. 3814385fb7fSSebastian Grimberg 382b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArrayRead(). 3830436c2adSjeremylt 384ea61e9acSJeremy L Thompson @param[in] vec CeedVector to access 3854385fb7fSSebastian Grimberg @param[in] mem_type Memory type on which to access the array. If the backend uses a different memory type, this will perform a copy (possibly 3864385fb7fSSebastian Grimberg cached). 387d1d35e2fSjeremylt @param[out] array Array on memory type mem_type 3880436c2adSjeremylt 3890436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3900436c2adSjeremylt 3917a982d89SJeremy L. Thompson @ref User 3920436c2adSjeremylt **/ 3932b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 3946574a04fSJeremy L Thompson CeedCheck(vec->GetArrayRead, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); 3956574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use"); 3960436c2adSjeremylt 39750c643e1SJed Brown if (vec->length > 0) { 3989c774eddSJeremy L Thompson bool has_valid_array = true; 3992b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4006574a04fSJeremy L Thompson CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND, 4012b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4029c774eddSJeremy L Thompson 4032b730f8bSJeremy L Thompson CeedCall(vec->GetArrayRead(vec, mem_type, array)); 40450c643e1SJed Brown } else { 40550c643e1SJed Brown *array = NULL; 40650c643e1SJed Brown } 407d1d35e2fSjeremylt vec->num_readers++; 408e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4090436c2adSjeremylt } 4100436c2adSjeremylt 4110436c2adSjeremylt /** 4129c774eddSJeremy L Thompson @brief Get write access to a CeedVector via the specified memory type. 4134385fb7fSSebastian Grimberg 414ea61e9acSJeremy L Thompson Restore access with @ref CeedVectorRestoreArray(). 415ea61e9acSJeremy L Thompson All old values should be assumed to be invalid. 4169c774eddSJeremy L Thompson 417ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to access 418ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 4199c774eddSJeremy L Thompson @param[out] array Array on memory type mem_type 4209c774eddSJeremy L Thompson 4219c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4229c774eddSJeremy L Thompson 4239c774eddSJeremy L Thompson @ref User 4249c774eddSJeremy L Thompson **/ 4252b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4266574a04fSJeremy L Thompson CeedCheck(vec->GetArrayWrite, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayWrite"); 4276574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 4286574a04fSJeremy L Thompson CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 4299c774eddSJeremy L Thompson 4302b730f8bSJeremy L Thompson CeedCall(vec->GetArrayWrite(vec, mem_type, array)); 43128bfd0b7SJeremy L Thompson vec->state++; 4329c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 4339c774eddSJeremy L Thompson } 4349c774eddSJeremy L Thompson 4359c774eddSJeremy L Thompson /** 436ea61e9acSJeremy L Thompson @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite() 4370436c2adSjeremylt 438ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to restore 439ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 4400436c2adSjeremylt 4410436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4420436c2adSjeremylt 4437a982d89SJeremy L. Thompson @ref User 4440436c2adSjeremylt **/ 4450436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 4466574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 1, vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted"); 4472b730f8bSJeremy L Thompson if (vec->RestoreArray) CeedCall(vec->RestoreArray(vec)); 4480436c2adSjeremylt *array = NULL; 44928bfd0b7SJeremy L Thompson vec->state++; 450e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4510436c2adSjeremylt } 4520436c2adSjeremylt 4530436c2adSjeremylt /** 454b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 4550436c2adSjeremylt 456ea61e9acSJeremy L Thompson @param[in] vec CeedVector to restore 457ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 4580436c2adSjeremylt 4590436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4600436c2adSjeremylt 4617a982d89SJeremy L. Thompson @ref User 4620436c2adSjeremylt **/ 4630436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 4646574a04fSJeremy L Thompson CeedCheck(vec->num_readers > 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted"); 4659c774eddSJeremy L Thompson 46675a19770SJeremy L Thompson vec->num_readers--; 4672b730f8bSJeremy L Thompson if (vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec)); 4680436c2adSjeremylt *array = NULL; 46975a19770SJeremy L Thompson 470e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4710436c2adSjeremylt } 4720436c2adSjeremylt 4730436c2adSjeremylt /** 474171f8ca9Sjeremylt @brief Get the norm of a CeedVector. 475171f8ca9Sjeremylt 476ea61e9acSJeremy L Thompson Note: This operation is local to the CeedVector. 477ea61e9acSJeremy 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 478ea61e9acSJeremy L Thompson duplicated or hanging nodes. 479547d9b97Sjeremylt 480ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve maximum value 481ea61e9acSJeremy L Thompson @param[in] norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 482547d9b97Sjeremylt @param[out] norm Variable to store norm value 483547d9b97Sjeremylt 484547d9b97Sjeremylt @return An error code: 0 - success, otherwise - failure 485547d9b97Sjeremylt 4867a982d89SJeremy L. Thompson @ref User 487547d9b97Sjeremylt **/ 488d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) { 4899c774eddSJeremy L Thompson bool has_valid_array = true; 4902b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4916574a04fSJeremy L Thompson CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND, 4922b730f8bSJeremy L Thompson "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4939c774eddSJeremy L Thompson 494547d9b97Sjeremylt // Backend impl for GPU, if added 495547d9b97Sjeremylt if (vec->Norm) { 4962b730f8bSJeremy L Thompson CeedCall(vec->Norm(vec, norm_type, norm)); 497e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 498547d9b97Sjeremylt } 499547d9b97Sjeremylt 500547d9b97Sjeremylt const CeedScalar *array; 5012b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 502547d9b97Sjeremylt 503547d9b97Sjeremylt *norm = 0.; 504d1d35e2fSjeremylt switch (norm_type) { 505547d9b97Sjeremylt case CEED_NORM_1: 506*b94338b9SJed Brown for (CeedSize i = 0; i < vec->length; i++) { 507547d9b97Sjeremylt *norm += fabs(array[i]); 508547d9b97Sjeremylt } 509547d9b97Sjeremylt break; 510547d9b97Sjeremylt case CEED_NORM_2: 511*b94338b9SJed Brown for (CeedSize i = 0; i < vec->length; i++) { 512547d9b97Sjeremylt *norm += fabs(array[i]) * fabs(array[i]); 513547d9b97Sjeremylt } 514547d9b97Sjeremylt break; 515547d9b97Sjeremylt case CEED_NORM_MAX: 516*b94338b9SJed Brown for (CeedSize i = 0; i < vec->length; i++) { 517d1d35e2fSjeremylt const CeedScalar abs_v_i = fabs(array[i]); 518d1d35e2fSjeremylt *norm = *norm > abs_v_i ? *norm : abs_v_i; 519547d9b97Sjeremylt } 520547d9b97Sjeremylt } 5212b730f8bSJeremy L Thompson if (norm_type == CEED_NORM_2) *norm = sqrt(*norm); 522547d9b97Sjeremylt 5232b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 524e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 525547d9b97Sjeremylt } 526547d9b97Sjeremylt 527547d9b97Sjeremylt /** 528e0dd3b27Sjeremylt @brief Compute x = alpha x 529e0dd3b27Sjeremylt 53096b902e2Sjeremylt @param[in,out] x vector for scaling 53196b902e2Sjeremylt @param[in] alpha scaling factor 532e0dd3b27Sjeremylt 533e0dd3b27Sjeremylt @return An error code: 0 - success, otherwise - failure 534e0dd3b27Sjeremylt 535e0dd3b27Sjeremylt @ref User 536e0dd3b27Sjeremylt **/ 537e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) { 53873380422SJeremy L Thompson CeedScalar *x_array = NULL; 5391f9221feSJeremy L Thompson CeedSize n_x; 540e0dd3b27Sjeremylt 5419c774eddSJeremy L Thompson bool has_valid_array = true; 5422b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array)); 5436574a04fSJeremy L Thompson CeedCheck(has_valid_array, x->ceed, CEED_ERROR_BACKEND, 5442b730f8bSJeremy L Thompson "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5459c774eddSJeremy L Thompson 5462b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 547e0dd3b27Sjeremylt 548e0dd3b27Sjeremylt // Backend implementation 5492b730f8bSJeremy L Thompson if (x->Scale) return x->Scale(x, alpha); 550e0dd3b27Sjeremylt 551e0dd3b27Sjeremylt // Default implementation 5522b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array)); 553*b94338b9SJed Brown for (CeedSize i = 0; i < n_x; i++) x_array[i] *= alpha; 5542b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(x, &x_array)); 555e0dd3b27Sjeremylt 556e0dd3b27Sjeremylt return CEED_ERROR_SUCCESS; 557e0dd3b27Sjeremylt } 558e0dd3b27Sjeremylt 559e0dd3b27Sjeremylt /** 5600f7fd0f8Sjeremylt @brief Compute y = alpha x + y 5610f7fd0f8Sjeremylt 56296b902e2Sjeremylt @param[in,out] y target vector for sum 56396b902e2Sjeremylt @param[in] alpha scaling factor 56496b902e2Sjeremylt @param[in] x second vector, must be different than y 5650f7fd0f8Sjeremylt 5660f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 5670f7fd0f8Sjeremylt 5680f7fd0f8Sjeremylt @ref User 5690f7fd0f8Sjeremylt **/ 5700f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { 57173380422SJeremy L Thompson CeedScalar *y_array = NULL; 57273380422SJeremy L Thompson CeedScalar const *x_array = NULL; 5731f9221feSJeremy L Thompson CeedSize n_x, n_y; 5740f7fd0f8Sjeremylt 5752b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &n_y)); 5762b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 5776574a04fSJeremy L Thompson CeedCheck(n_x == n_y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 5786574a04fSJeremy L Thompson CeedCheck(x != y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); 5790f7fd0f8Sjeremylt 5809c774eddSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 5812b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 5826574a04fSJeremy L Thompson CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND, 5836574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5842b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 5856574a04fSJeremy L Thompson CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND, 5866574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5879c774eddSJeremy L Thompson 5882d04630dSjeremylt Ceed ceed_parent_x, ceed_parent_y; 5892b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 5902b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 5916574a04fSJeremy L Thompson CeedCheck(ceed_parent_x == ceed_parent_y, y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); 5922d04630dSjeremylt 5930f7fd0f8Sjeremylt // Backend implementation 594eaf62fffSJeremy L Thompson if (y->AXPY) { 5952b730f8bSJeremy L Thompson CeedCall(y->AXPY(y, alpha, x)); 596eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 597eaf62fffSJeremy L Thompson } 5980f7fd0f8Sjeremylt 5990f7fd0f8Sjeremylt // Default implementation 6002b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array)); 6012b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 6020f7fd0f8Sjeremylt 6032b730f8bSJeremy L Thompson assert(x_array); 6042b730f8bSJeremy L Thompson assert(y_array); 60573380422SJeremy L Thompson 606*b94338b9SJed Brown for (CeedSize i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i]; 6070f7fd0f8Sjeremylt 6082b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(y, &y_array)); 6092b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 6100f7fd0f8Sjeremylt 6110f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 6120f7fd0f8Sjeremylt } 6130f7fd0f8Sjeremylt 6140f7fd0f8Sjeremylt /** 6155fb68f37SKaren (Ren) Stengel @brief Compute y = alpha x + beta y 6165fb68f37SKaren (Ren) Stengel 6175fb68f37SKaren (Ren) Stengel @param[in,out] y target vector for sum 6185fb68f37SKaren (Ren) Stengel @param[in] alpha first scaling factor 6195fb68f37SKaren (Ren) Stengel @param[in] beta second scaling factor 6205fb68f37SKaren (Ren) Stengel @param[in] x second vector, must be different than y 6215fb68f37SKaren (Ren) Stengel 6225fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 6235fb68f37SKaren (Ren) Stengel 6245fb68f37SKaren (Ren) Stengel @ref User 6255fb68f37SKaren (Ren) Stengel **/ 6265fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 6275fb68f37SKaren (Ren) Stengel CeedScalar *y_array = NULL; 6285fb68f37SKaren (Ren) Stengel CeedScalar const *x_array = NULL; 6295fb68f37SKaren (Ren) Stengel CeedSize n_x, n_y; 6305fb68f37SKaren (Ren) Stengel 6315fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetLength(y, &n_y)); 6325fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetLength(x, &n_x)); 6336574a04fSJeremy L Thompson CeedCheck(n_x == n_y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 6346574a04fSJeremy L Thompson CeedCheck(x != y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY"); 6355fb68f37SKaren (Ren) Stengel 6365fb68f37SKaren (Ren) Stengel bool has_valid_array_x = true, has_valid_array_y = true; 6375fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 6386574a04fSJeremy L Thompson CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND, 6396574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6405fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 6416574a04fSJeremy L Thompson CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND, 6426574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6435fb68f37SKaren (Ren) Stengel 6445fb68f37SKaren (Ren) Stengel Ceed ceed_parent_x, ceed_parent_y; 6455fb68f37SKaren (Ren) Stengel CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 6465fb68f37SKaren (Ren) Stengel CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 6476574a04fSJeremy L Thompson CeedCheck(ceed_parent_x == ceed_parent_y, y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); 6485fb68f37SKaren (Ren) Stengel 6495fb68f37SKaren (Ren) Stengel // Backend implementation 6505fb68f37SKaren (Ren) Stengel if (y->AXPBY) { 6515fb68f37SKaren (Ren) Stengel CeedCall(y->AXPBY(y, alpha, beta, x)); 6525fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 6535fb68f37SKaren (Ren) Stengel } 6545fb68f37SKaren (Ren) Stengel 6555fb68f37SKaren (Ren) Stengel // Default implementation 6565fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 6575fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 6585fb68f37SKaren (Ren) Stengel 6595fb68f37SKaren (Ren) Stengel assert(x_array); 6605fb68f37SKaren (Ren) Stengel assert(y_array); 6615fb68f37SKaren (Ren) Stengel 662*b94338b9SJed Brown for (CeedSize i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i] + beta * y_array[i]; 6635fb68f37SKaren (Ren) Stengel 6645fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(y, &y_array)); 6655fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 6665fb68f37SKaren (Ren) Stengel 6675fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 6685fb68f37SKaren (Ren) Stengel } 6695fb68f37SKaren (Ren) Stengel 6705fb68f37SKaren (Ren) Stengel /** 671ea61e9acSJeremy L Thompson @brief Compute the pointwise multiplication w = x .* y. 6724385fb7fSSebastian Grimberg 673ea61e9acSJeremy L Thompson Any subset of x, y, and w may be the same vector. 6740f7fd0f8Sjeremylt 67596b902e2Sjeremylt @param[out] w target vector for the product 67696b902e2Sjeremylt @param[in] x first vector for product 67796b902e2Sjeremylt @param[in] y second vector for the product 6780f7fd0f8Sjeremylt 6790f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 6800f7fd0f8Sjeremylt 6810f7fd0f8Sjeremylt @ref User 6820f7fd0f8Sjeremylt **/ 6830f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { 68473380422SJeremy L Thompson CeedScalar *w_array = NULL; 68573380422SJeremy L Thompson CeedScalar const *x_array = NULL, *y_array = NULL; 6861f9221feSJeremy L Thompson CeedSize n_w, n_x, n_y; 6870f7fd0f8Sjeremylt 6882b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(w, &n_w)); 6892b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 6902b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &n_y)); 6916574a04fSJeremy L Thompson CeedCheck(n_w == n_x && n_w == n_y, w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths"); 6920f7fd0f8Sjeremylt 6932d04630dSjeremylt Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y; 6942b730f8bSJeremy L Thompson CeedCall(CeedGetParent(w->ceed, &ceed_parent_w)); 6952b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 6962b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 6976574a04fSJeremy L Thompson CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, w->ceed, CEED_ERROR_INCOMPATIBLE, 6986574a04fSJeremy L Thompson "Vectors w, x, and y must be created by the same Ceed context"); 6992d04630dSjeremylt 7009c774eddSJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 7012b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 7026574a04fSJeremy L Thompson CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND, 7036574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7042b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 7056574a04fSJeremy L Thompson CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND, 7066574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7079c774eddSJeremy L Thompson 7080f7fd0f8Sjeremylt // Backend implementation 709eaf62fffSJeremy L Thompson if (w->PointwiseMult) { 7102b730f8bSJeremy L Thompson CeedCall(w->PointwiseMult(w, x, y)); 711eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 712eaf62fffSJeremy L Thompson } 7130f7fd0f8Sjeremylt 7140f7fd0f8Sjeremylt // Default implementation 7150f7fd0f8Sjeremylt if (x != w) { 7162b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 7170f7fd0f8Sjeremylt } else { 718c6d796c6SJeremy L Thompson CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 7190f7fd0f8Sjeremylt x_array = w_array; 7200f7fd0f8Sjeremylt } 7210f7fd0f8Sjeremylt if (y != w && y != x) { 7222b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array)); 723c6d796c6SJeremy L Thompson } else if (y == x) { 7240f7fd0f8Sjeremylt y_array = x_array; 725c6d796c6SJeremy L Thompson } else { 726c6d796c6SJeremy L Thompson CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 727c6d796c6SJeremy L Thompson y_array = w_array; 7280f7fd0f8Sjeremylt } 729c6d796c6SJeremy L Thompson if (!w_array) CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array)); 7300f7fd0f8Sjeremylt 7312b730f8bSJeremy L Thompson assert(w_array); 7322b730f8bSJeremy L Thompson assert(x_array); 7332b730f8bSJeremy L Thompson assert(y_array); 73473380422SJeremy L Thompson 735*b94338b9SJed Brown for (CeedSize i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i]; 7360f7fd0f8Sjeremylt 7372b730f8bSJeremy L Thompson if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array)); 7382b730f8bSJeremy L Thompson if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 7392b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(w, &w_array)); 7400f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 7410f7fd0f8Sjeremylt } 7420f7fd0f8Sjeremylt 7430f7fd0f8Sjeremylt /** 744d99fa3c5SJeremy L Thompson @brief Take the reciprocal of a CeedVector. 745d99fa3c5SJeremy L Thompson 746ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to take reciprocal 747d99fa3c5SJeremy L Thompson 748d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 749d99fa3c5SJeremy L Thompson 750d99fa3c5SJeremy L Thompson @ref User 751d99fa3c5SJeremy L Thompson **/ 752d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) { 7539c774eddSJeremy L Thompson bool has_valid_array = true; 7542b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 7552b730f8bSJeremy L Thompson 7566574a04fSJeremy L Thompson CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND, 7572b730f8bSJeremy L Thompson "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7589c774eddSJeremy L Thompson 759d99fa3c5SJeremy L Thompson // Check if vector data set 7606574a04fSJeremy L Thompson CeedCheck(vec->state > 0, vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); 761d99fa3c5SJeremy L Thompson 762d99fa3c5SJeremy L Thompson // Backend impl for GPU, if added 763d99fa3c5SJeremy L Thompson if (vec->Reciprocal) { 7642b730f8bSJeremy L Thompson CeedCall(vec->Reciprocal(vec)); 765e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 766d99fa3c5SJeremy L Thompson } 767d99fa3c5SJeremy L Thompson 7681f9221feSJeremy L Thompson CeedSize len; 7692b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &len)); 770d99fa3c5SJeremy L Thompson CeedScalar *array; 7712b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 772*b94338b9SJed Brown for (CeedSize i = 0; i < len; i++) { 7732b730f8bSJeremy L Thompson if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i]; 7742b730f8bSJeremy L Thompson } 775d99fa3c5SJeremy L Thompson 7762b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 777e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 778d99fa3c5SJeremy L Thompson } 779d99fa3c5SJeremy L Thompson 780d99fa3c5SJeremy L Thompson /** 7817a982d89SJeremy L. Thompson @brief View a CeedVector 7820436c2adSjeremylt 783acb2c48cSJeremy L Thompson Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`. 784acb2c48cSJeremy L Thompson Any portion of the provided range that is outside the range of valid indices for the CeedVector will be ignored. 785acb2c48cSJeremy L Thompson 786acb2c48cSJeremy L Thompson @param[in] vec CeedVector to view 787acb2c48cSJeremy L Thompson @param[in] start Index of first CeedVector entry to view 788acb2c48cSJeremy L Thompson @param[in] stop Index of last CeedVector entry to view 789acb2c48cSJeremy L Thompson @param[in] step Step between CeedVector entries to view 790acb2c48cSJeremy L Thompson @param[in] fp_fmt Printing format 791acb2c48cSJeremy L Thompson @param[in] stream Filestream to write to 792acb2c48cSJeremy L Thompson 793acb2c48cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 794acb2c48cSJeremy L Thompson 795acb2c48cSJeremy L Thompson @ref User 796acb2c48cSJeremy L Thompson **/ 797acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) { 798acb2c48cSJeremy L Thompson const CeedScalar *x; 799acb2c48cSJeremy L Thompson char fmt[1024]; 800acb2c48cSJeremy L Thompson 8016574a04fSJeremy L Thompson CeedCheck(step != 0, vec->ceed, CEED_ERROR_MINOR, "View range 'step' must be nonzero"); 802acb2c48cSJeremy L Thompson 803acb2c48cSJeremy L Thompson fprintf(stream, "CeedVector length %ld\n", (long)vec->length); 804acb2c48cSJeremy L Thompson if (start != 0 || stop != vec->length || step != 1) { 805acb2c48cSJeremy L Thompson fprintf(stream, " start: %ld\n stop: %ld\n step: %" CeedInt_FMT "\n", (long)start, (long)stop, step); 806acb2c48cSJeremy L Thompson } 807acb2c48cSJeremy L Thompson if (start > vec->length) start = vec->length; 808acb2c48cSJeremy L Thompson if (stop > vec->length) stop = vec->length; 809acb2c48cSJeremy L Thompson 810acb2c48cSJeremy L Thompson snprintf(fmt, sizeof fmt, " %s\n", fp_fmt ? fp_fmt : "%g"); 811acb2c48cSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x)); 812*b94338b9SJed Brown for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]); 813acb2c48cSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &x)); 814acb2c48cSJeremy L Thompson if (stop != vec->length) fprintf(stream, " ...\n"); 815acb2c48cSJeremy L Thompson 816acb2c48cSJeremy L Thompson return CEED_ERROR_SUCCESS; 817acb2c48cSJeremy L Thompson } 818acb2c48cSJeremy L Thompson 819acb2c48cSJeremy L Thompson /** 820acb2c48cSJeremy L Thompson @brief View a CeedVector 821acb2c48cSJeremy L Thompson 8220a0da059Sjeremylt @param[in] vec CeedVector to view 823d1d35e2fSjeremylt @param[in] fp_fmt Printing format 8240a0da059Sjeremylt @param[in] stream Filestream to write to 8250a0da059Sjeremylt 8260436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8270436c2adSjeremylt 8287a982d89SJeremy L. Thompson @ref User 8290436c2adSjeremylt **/ 830d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { 831acb2c48cSJeremy L Thompson CeedCall(CeedVectorViewRange(vec, 0, vec->length, 1, fp_fmt, stream)); 832e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8330436c2adSjeremylt } 8340436c2adSjeremylt 8350436c2adSjeremylt /** 836b7c9bbdaSJeremy L Thompson @brief Get the Ceed associated with a CeedVector 837b7c9bbdaSJeremy L Thompson 838ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 839b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store ceed 840b7c9bbdaSJeremy L Thompson 841b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 842b7c9bbdaSJeremy L Thompson 843b7c9bbdaSJeremy L Thompson @ref Advanced 844b7c9bbdaSJeremy L Thompson **/ 845b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 846b7c9bbdaSJeremy L Thompson *ceed = vec->ceed; 847b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 848b7c9bbdaSJeremy L Thompson } 849b7c9bbdaSJeremy L Thompson 850b7c9bbdaSJeremy L Thompson /** 8517a982d89SJeremy L. Thompson @brief Get the length of a CeedVector 8520436c2adSjeremylt 853ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve length 8547a982d89SJeremy L. Thompson @param[out] length Variable to store length 8550436c2adSjeremylt 8560436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8570436c2adSjeremylt 8587a982d89SJeremy L. Thompson @ref User 8590436c2adSjeremylt **/ 8601f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) { 8617a982d89SJeremy L. Thompson *length = vec->length; 862e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8630436c2adSjeremylt } 8640436c2adSjeremylt 8650436c2adSjeremylt /** 8660436c2adSjeremylt @brief Destroy a CeedVector 8670436c2adSjeremylt 868ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to destroy 8690436c2adSjeremylt 8700436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8710436c2adSjeremylt 8727a982d89SJeremy L. Thompson @ref User 8730436c2adSjeremylt **/ 8740436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) { 8757425e127SJeremy L Thompson if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) { 876ad6481ceSJeremy L Thompson *vec = NULL; 877ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 878ad6481ceSJeremy L Thompson } 8796574a04fSJeremy L Thompson CeedCheck((*vec)->state % 2 == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use"); 8806574a04fSJeremy L Thompson CeedCheck((*vec)->num_readers == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access"); 8810436c2adSjeremylt 8822b730f8bSJeremy L Thompson if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec)); 8832b730f8bSJeremy L Thompson 8842b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*vec)->ceed)); 8852b730f8bSJeremy L Thompson CeedCall(CeedFree(vec)); 886e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8870436c2adSjeremylt } 8880436c2adSjeremylt 8890436c2adSjeremylt /// @} 890