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"); 54b0976d5aSZach Atkins if (vec->length == 0) { 55b0976d5aSZach Atkins *has_valid_array = true; 56b0976d5aSZach Atkins return CEED_ERROR_SUCCESS; 57b0976d5aSZach Atkins } 582b730f8bSJeremy L Thompson CeedCall(vec->HasValidArray(vec, has_valid_array)); 599c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 609c774eddSJeremy L Thompson } 619c774eddSJeremy L Thompson 629c774eddSJeremy L Thompson /** 639c774eddSJeremy L Thompson @brief Check for borrowed array of a specific CeedMemType in a CeedVector 649c774eddSJeremy L Thompson 65ea61e9acSJeremy L Thompson @param[in] vec CeedVector to check 66ea61e9acSJeremy L Thompson @param[in] mem_type Memory type to check 679c774eddSJeremy L Thompson @param[out] has_borrowed_array_of_type Variable to store result 689c774eddSJeremy L Thompson 699c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 709c774eddSJeremy L Thompson 719c774eddSJeremy L Thompson @ref Backend 729c774eddSJeremy L Thompson **/ 732b730f8bSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 746574a04fSJeremy L Thompson CeedCheck(vec->HasBorrowedArrayOfType, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasBorrowedArrayOfType"); 752b730f8bSJeremy L Thompson CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type)); 769c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 779c774eddSJeremy L Thompson } 789c774eddSJeremy L Thompson 799c774eddSJeremy L Thompson /** 807a982d89SJeremy L. Thompson @brief Get the state of a CeedVector 817a982d89SJeremy L. Thompson 82ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 837a982d89SJeremy L. Thompson @param[out] state Variable to store state 847a982d89SJeremy L. Thompson 857a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 867a982d89SJeremy L. Thompson 877a982d89SJeremy L. Thompson @ref Backend 887a982d89SJeremy L. Thompson **/ 897a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) { 907a982d89SJeremy L. Thompson *state = vec->state; 91e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 927a982d89SJeremy L. Thompson } 937a982d89SJeremy L. Thompson 947a982d89SJeremy L. Thompson /** 957a982d89SJeremy L. Thompson @brief Get the backend data of a CeedVector 967a982d89SJeremy L. Thompson 97ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 987a982d89SJeremy L. Thompson @param[out] data Variable to store data 997a982d89SJeremy L. Thompson 1007a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1017a982d89SJeremy L. Thompson 1027a982d89SJeremy L. Thompson @ref Backend 1037a982d89SJeremy L. Thompson **/ 104777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) { 105777ff853SJeremy L Thompson *(void **)data = vec->data; 106e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1077a982d89SJeremy L. Thompson } 1087a982d89SJeremy L. Thompson 1097a982d89SJeremy L. Thompson /** 1107a982d89SJeremy L. Thompson @brief Set the backend data of a CeedVector 1117a982d89SJeremy L. Thompson 112ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to retrieve state 113ea61e9acSJeremy L Thompson @param[in] data Data to set 1147a982d89SJeremy L. Thompson 1157a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1167a982d89SJeremy L. Thompson 1177a982d89SJeremy L. Thompson @ref Backend 1187a982d89SJeremy L. Thompson **/ 119777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) { 120777ff853SJeremy L Thompson vec->data = data; 121e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1227a982d89SJeremy L. Thompson } 1237a982d89SJeremy L. Thompson 12434359f16Sjeremylt /** 12534359f16Sjeremylt @brief Increment the reference counter for a CeedVector 12634359f16Sjeremylt 127ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to increment the reference counter 12834359f16Sjeremylt 12934359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 13034359f16Sjeremylt 13134359f16Sjeremylt @ref Backend 13234359f16Sjeremylt **/ 1339560d06aSjeremylt int CeedVectorReference(CeedVector vec) { 13434359f16Sjeremylt vec->ref_count++; 13534359f16Sjeremylt return CEED_ERROR_SUCCESS; 13634359f16Sjeremylt } 13734359f16Sjeremylt 1387a982d89SJeremy L. Thompson /// @} 1397a982d89SJeremy L. Thompson 1407a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1417a982d89SJeremy L. Thompson /// CeedVector Public API 1427a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1437a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 1440436c2adSjeremylt /// @{ 1450436c2adSjeremylt 1460436c2adSjeremylt /** 1470436c2adSjeremylt @brief Create a CeedVector of the specified length (does not allocate memory) 1480436c2adSjeremylt 149ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedVector will be created 150ea61e9acSJeremy L Thompson @param[in] length Length of vector 151ea61e9acSJeremy L Thompson @param[out] vec Address of the variable where the newly created CeedVector will be stored 1520436c2adSjeremylt 1530436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 1540436c2adSjeremylt 1557a982d89SJeremy L. Thompson @ref User 1560436c2adSjeremylt **/ 1571f9221feSJeremy L Thompson int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) { 1580436c2adSjeremylt if (!ceed->VectorCreate) { 1590436c2adSjeremylt Ceed delegate; 1606574a04fSJeremy L Thompson 1612b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector")); 1626574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorCreate"); 1632b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(delegate, length, vec)); 164e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1650436c2adSjeremylt } 1660436c2adSjeremylt 1672b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, vec)); 168db002c03SJeremy L Thompson CeedCall(CeedReferenceCopy(ceed, &(*vec)->ceed)); 169d1d35e2fSjeremylt (*vec)->ref_count = 1; 1700436c2adSjeremylt (*vec)->length = length; 1710436c2adSjeremylt (*vec)->state = 0; 1722b730f8bSJeremy L Thompson CeedCall(ceed->VectorCreate(length, *vec)); 173e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1740436c2adSjeremylt } 1750436c2adSjeremylt 1760436c2adSjeremylt /** 177ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedVector. 1784385fb7fSSebastian Grimberg 179ea61e9acSJeremy L Thompson Both pointers should be destroyed with `CeedVectorDestroy()`. 180512bb800SJeremy L Thompson 181512bb800SJeremy 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. 182512bb800SJeremy L Thompson This CeedVector will be destroyed if `vec_copy` is the only reference to this CeedVector. 1839560d06aSjeremylt 184ea61e9acSJeremy L Thompson @param[in] vec CeedVector to copy reference to 185ea61e9acSJeremy L Thompson @param[in,out] vec_copy Variable to store copied reference 1869560d06aSjeremylt 1879560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 1889560d06aSjeremylt 1899560d06aSjeremylt @ref User 1909560d06aSjeremylt **/ 1919560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) { 192393ac2cdSJeremy L Thompson if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorReference(vec)); 1932b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(vec_copy)); 1949560d06aSjeremylt *vec_copy = vec; 1959560d06aSjeremylt return CEED_ERROR_SUCCESS; 1969560d06aSjeremylt } 1979560d06aSjeremylt 1989560d06aSjeremylt /** 1995fb68f37SKaren (Ren) Stengel @brief Copy a CeedVector into a different CeedVector. 2004385fb7fSSebastian Grimberg 2015fb68f37SKaren (Ren) Stengel Both pointers should be destroyed with `CeedVectorDestroy()`. 2024385fb7fSSebastian Grimberg 2035fb68f37SKaren (Ren) Stengel Note: If `*vec_copy` is non-NULL, then it is assumed that `*vec_copy` is a pointer to a CeedVector. 2045fb68f37SKaren (Ren) Stengel This CeedVector will be destroyed if `*vec_copy` is the only reference to this CeedVector. 2055fb68f37SKaren (Ren) Stengel 2065fb68f37SKaren (Ren) Stengel @param[in] vec CeedVector to copy 2075fb68f37SKaren (Ren) Stengel @param[in,out] vec_copy Variable to store copied CeedVector to 2085fb68f37SKaren (Ren) Stengel 2095fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 2105fb68f37SKaren (Ren) Stengel 2115fb68f37SKaren (Ren) Stengel @ref User 2125fb68f37SKaren (Ren) Stengel **/ 2135fb68f37SKaren (Ren) Stengel int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) { 2145fb68f37SKaren (Ren) Stengel Ceed ceed; 2155fb68f37SKaren (Ren) Stengel CeedMemType mem_type, mem_type_copy; 2165fb68f37SKaren (Ren) Stengel CeedScalar *array; 2175fb68f37SKaren (Ren) Stengel 2185fb68f37SKaren (Ren) Stengel // Get the preferred memory type 2195fb68f37SKaren (Ren) Stengel CeedVectorGetCeed(vec, &ceed); 2205fb68f37SKaren (Ren) Stengel CeedGetPreferredMemType(ceed, &mem_type); 2215fb68f37SKaren (Ren) Stengel 2225fb68f37SKaren (Ren) Stengel // Get the preferred memory type 2235fb68f37SKaren (Ren) Stengel CeedVectorGetCeed(vec_copy, &ceed); 2245fb68f37SKaren (Ren) Stengel CeedGetPreferredMemType(ceed, &mem_type_copy); 2255fb68f37SKaren (Ren) Stengel 2265fb68f37SKaren (Ren) Stengel // Check that both have same memory type 2275fb68f37SKaren (Ren) Stengel if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST; 2285fb68f37SKaren (Ren) Stengel 2295fb68f37SKaren (Ren) Stengel // Copy the values from vec to vec_copy 2305fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(vec, mem_type, &array)); 2315fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array)); 2325fb68f37SKaren (Ren) Stengel 2335fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(vec, &array)); 2345fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 2355fb68f37SKaren (Ren) Stengel } 2365fb68f37SKaren (Ren) Stengel 2375fb68f37SKaren (Ren) Stengel /** 238ea61e9acSJeremy L Thompson @brief Set the array used by a CeedVector, freeing any previously allocated array if applicable. 2394385fb7fSSebastian Grimberg 240ea61e9acSJeremy L Thompson The backend may copy values to a different memtype, such as during @ref CeedOperatorApply(). 2416a6c615bSJeremy L Thompson See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 2420436c2adSjeremylt 243ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 244ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the array being passed 245ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the array 246ea61e9acSJeremy L Thompson @param[in] array Array to be used, or NULL with @ref CEED_COPY_VALUES to have the library allocate 2470436c2adSjeremylt 2480436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2490436c2adSjeremylt 2507a982d89SJeremy L. Thompson @ref User 2510436c2adSjeremylt **/ 2522b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { 2536574a04fSJeremy L Thompson CeedCheck(vec->SetArray, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); 2546574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 2556574a04fSJeremy L Thompson CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 2560436c2adSjeremylt 257b0976d5aSZach Atkins if (vec->length > 0) CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); 2580436c2adSjeremylt vec->state += 2; 259e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2600436c2adSjeremylt } 2610436c2adSjeremylt 2620436c2adSjeremylt /** 2630436c2adSjeremylt @brief Set the CeedVector to a constant value 2640436c2adSjeremylt 265ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 2660436c2adSjeremylt @param[in] value Value to be used 2670436c2adSjeremylt 2680436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2690436c2adSjeremylt 2707a982d89SJeremy L. Thompson @ref User 2710436c2adSjeremylt **/ 2720436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 2736574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 2746574a04fSJeremy L Thompson CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 2750436c2adSjeremylt 2760436c2adSjeremylt if (vec->SetValue) { 2772b730f8bSJeremy L Thompson CeedCall(vec->SetValue(vec, value)); 2780436c2adSjeremylt } else { 2790436c2adSjeremylt CeedScalar *array; 2802b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array)); 281b94338b9SJed Brown for (CeedSize i = 0; i < vec->length; i++) array[i] = value; 2822b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 2830436c2adSjeremylt } 2840436c2adSjeremylt vec->state += 2; 285e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2860436c2adSjeremylt } 2870436c2adSjeremylt 2880436c2adSjeremylt /** 289ea61e9acSJeremy L Thompson @brief Sync the CeedVector to a specified memtype. 2904385fb7fSSebastian Grimberg 291ea61e9acSJeremy L Thompson This function is used to force synchronization of arrays set with @ref CeedVectorSetArray(). 292ea61e9acSJeremy L Thompson If the requested memtype is already synchronized, this function results in a no-op. 2930436c2adSjeremylt 294ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 295ea61e9acSJeremy L Thompson @param[in] mem_type Memtype to be synced 2960436c2adSjeremylt 2970436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2980436c2adSjeremylt 2997a982d89SJeremy L. Thompson @ref User 3000436c2adSjeremylt **/ 301d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { 3026574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use"); 3030436c2adSjeremylt 304b0976d5aSZach Atkins // Don't sync empty array 305b0976d5aSZach Atkins if (vec->length == 0) return CEED_ERROR_SUCCESS; 306b0976d5aSZach Atkins 3070436c2adSjeremylt if (vec->SyncArray) { 3082b730f8bSJeremy L Thompson CeedCall(vec->SyncArray(vec, mem_type)); 3090436c2adSjeremylt } else { 3100436c2adSjeremylt const CeedScalar *array; 3112b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array)); 3122b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 3130436c2adSjeremylt } 314e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3150436c2adSjeremylt } 3160436c2adSjeremylt 3170436c2adSjeremylt /** 318ea61e9acSJeremy L Thompson @brief Take ownership of the CeedVector array set by @ref CeedVectorSetArray() with @ref CEED_USE_POINTER and remove the array from the CeedVector. 3194385fb7fSSebastian Grimberg 3209c774eddSJeremy L Thompson The caller is responsible for managing and freeing the array. 321ea61e9acSJeremy L Thompson This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type. 3226a6c615bSJeremy L Thompson 323ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector 324ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to take the array. 325ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 326ea61e9acSJeremy L Thompson @param[out] array Array on memory type mem_type, or NULL if array pointer is not required 3276a6c615bSJeremy L Thompson 3286a6c615bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3296a6c615bSJeremy L Thompson 3306a6c615bSJeremy L Thompson @ref User 3316a6c615bSJeremy L Thompson **/ 3322b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 333*1c66c397SJeremy L Thompson CeedScalar *temp_array = NULL; 334*1c66c397SJeremy L Thompson 3356574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); 3366574a04fSJeremy L Thompson CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); 3376a6c615bSJeremy L Thompson 33850c643e1SJed Brown if (vec->length > 0) { 3399c774eddSJeremy L Thompson bool has_borrowed_array_of_type = true; 3402b730f8bSJeremy L Thompson CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); 3416574a04fSJeremy L Thompson CeedCheck(has_borrowed_array_of_type, vec->ceed, CEED_ERROR_BACKEND, 3426574a04fSJeremy L Thompson "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", CeedMemTypes[mem_type]); 3439c774eddSJeremy L Thompson 3449c774eddSJeremy L Thompson bool has_valid_array = true; 3452b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 3466574a04fSJeremy L Thompson CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND, 3472b730f8bSJeremy L Thompson "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); 3489c774eddSJeremy L Thompson 3492b730f8bSJeremy L Thompson CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); 35050c643e1SJed Brown } 351d1d35e2fSjeremylt if (array) (*array) = temp_array; 352e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3536a6c615bSJeremy L Thompson } 3546a6c615bSJeremy L Thompson 3556a6c615bSJeremy L Thompson /** 356b3cf021fSjeremylt @brief Get read/write access to a CeedVector via the specified memory type. 3574385fb7fSSebastian Grimberg 358b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArray(). 3590436c2adSjeremylt 360ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to access 361ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 362ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 363d1d35e2fSjeremylt @param[out] array Array on memory type mem_type 3640436c2adSjeremylt 365ea61e9acSJeremy L Thompson @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide access to array pointers in the desired memory space. 366ea61e9acSJeremy L Thompson Pairing get/restore allows the Vector to track access, thus knowing if norms or other operations may need to be recomputed. 3670436c2adSjeremylt 3680436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3690436c2adSjeremylt 3707a982d89SJeremy L. Thompson @ref User 3710436c2adSjeremylt **/ 3722b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 3736574a04fSJeremy L Thompson CeedCheck(vec->GetArray, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); 3746574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 3756574a04fSJeremy L Thompson CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 3760436c2adSjeremylt 377b0976d5aSZach Atkins if (vec->length > 0) { 3789c774eddSJeremy L Thompson bool has_valid_array = true; 379b0976d5aSZach Atkins 3802b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 3816574a04fSJeremy L Thompson CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND, 3822b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 3839c774eddSJeremy L Thompson 3842b730f8bSJeremy L Thompson CeedCall(vec->GetArray(vec, mem_type, array)); 385b0976d5aSZach Atkins } else { 386b0976d5aSZach Atkins *array = NULL; 387b0976d5aSZach Atkins } 38828bfd0b7SJeremy L Thompson vec->state++; 389e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3900436c2adSjeremylt } 3910436c2adSjeremylt 3920436c2adSjeremylt /** 393b3cf021fSjeremylt @brief Get read-only access to a CeedVector via the specified memory type. 3944385fb7fSSebastian Grimberg 395b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArrayRead(). 3960436c2adSjeremylt 397ea61e9acSJeremy L Thompson @param[in] vec CeedVector to access 3984385fb7fSSebastian 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 3994385fb7fSSebastian Grimberg cached). 400d1d35e2fSjeremylt @param[out] array Array on memory type mem_type 4010436c2adSjeremylt 4020436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4030436c2adSjeremylt 4047a982d89SJeremy L. Thompson @ref User 4050436c2adSjeremylt **/ 4062b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { 4076574a04fSJeremy L Thompson CeedCheck(vec->GetArrayRead, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); 4086574a04fSJeremy 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"); 4090436c2adSjeremylt 41050c643e1SJed Brown if (vec->length > 0) { 4119c774eddSJeremy L Thompson bool has_valid_array = true; 412b0976d5aSZach Atkins 4132b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 4146574a04fSJeremy L Thompson CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND, 4152b730f8bSJeremy L Thompson "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); 4169c774eddSJeremy L Thompson 4172b730f8bSJeremy L Thompson CeedCall(vec->GetArrayRead(vec, mem_type, array)); 41850c643e1SJed Brown } else { 41950c643e1SJed Brown *array = NULL; 42050c643e1SJed Brown } 421d1d35e2fSjeremylt vec->num_readers++; 422e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4230436c2adSjeremylt } 4240436c2adSjeremylt 4250436c2adSjeremylt /** 4269c774eddSJeremy L Thompson @brief Get write access to a CeedVector via the specified memory type. 4274385fb7fSSebastian Grimberg 428ea61e9acSJeremy L Thompson Restore access with @ref CeedVectorRestoreArray(). 429ea61e9acSJeremy L Thompson All old values should be assumed to be invalid. 4309c774eddSJeremy L Thompson 431ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to access 432ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the array. 4339c774eddSJeremy L Thompson @param[out] array Array on memory type mem_type 4349c774eddSJeremy L Thompson 4359c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4369c774eddSJeremy L Thompson 4379c774eddSJeremy L Thompson @ref User 4389c774eddSJeremy L Thompson **/ 4392b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 4406574a04fSJeremy L Thompson CeedCheck(vec->GetArrayWrite, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayWrite"); 4416574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); 4426574a04fSJeremy L Thompson CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); 4439c774eddSJeremy L Thompson 444b0976d5aSZach Atkins if (vec->length > 0) { 4452b730f8bSJeremy L Thompson CeedCall(vec->GetArrayWrite(vec, mem_type, array)); 446b0976d5aSZach Atkins } else { 447b0976d5aSZach Atkins *array = NULL; 448b0976d5aSZach Atkins } 44928bfd0b7SJeremy L Thompson vec->state++; 4509c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 4519c774eddSJeremy L Thompson } 4529c774eddSJeremy L Thompson 4539c774eddSJeremy L Thompson /** 454ea61e9acSJeremy L Thompson @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite() 4550436c2adSjeremylt 456ea61e9acSJeremy L Thompson @param[in,out] 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 CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 4646574a04fSJeremy L Thompson CeedCheck(vec->state % 2 == 1, vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted"); 465b0976d5aSZach Atkins if (vec->length > 0 && vec->RestoreArray) CeedCall(vec->RestoreArray(vec)); 4660436c2adSjeremylt *array = NULL; 46728bfd0b7SJeremy L Thompson vec->state++; 468e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4690436c2adSjeremylt } 4700436c2adSjeremylt 4710436c2adSjeremylt /** 472b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 4730436c2adSjeremylt 474ea61e9acSJeremy L Thompson @param[in] vec CeedVector to restore 475ea61e9acSJeremy L Thompson @param[in,out] array Array of vector data 4760436c2adSjeremylt 4770436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4780436c2adSjeremylt 4797a982d89SJeremy L. Thompson @ref User 4800436c2adSjeremylt **/ 4810436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 4826574a04fSJeremy L Thompson CeedCheck(vec->num_readers > 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted"); 4839c774eddSJeremy L Thompson 48475a19770SJeremy L Thompson vec->num_readers--; 485b0976d5aSZach Atkins if (vec->length > 0 && vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec)); 4860436c2adSjeremylt *array = NULL; 487e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4880436c2adSjeremylt } 4890436c2adSjeremylt 4900436c2adSjeremylt /** 491171f8ca9Sjeremylt @brief Get the norm of a CeedVector. 492171f8ca9Sjeremylt 493ea61e9acSJeremy L Thompson Note: This operation is local to the CeedVector. 494ea61e9acSJeremy 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 495ea61e9acSJeremy L Thompson duplicated or hanging nodes. 496547d9b97Sjeremylt 497ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve maximum value 498ea61e9acSJeremy L Thompson @param[in] norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 499547d9b97Sjeremylt @param[out] norm Variable to store norm value 500547d9b97Sjeremylt 501547d9b97Sjeremylt @return An error code: 0 - success, otherwise - failure 502547d9b97Sjeremylt 5037a982d89SJeremy L. Thompson @ref User 504547d9b97Sjeremylt **/ 505d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) { 5069c774eddSJeremy L Thompson bool has_valid_array = true; 5072b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 5086574a04fSJeremy L Thompson CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND, 5092b730f8bSJeremy L Thompson "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5109c774eddSJeremy L Thompson 511b0976d5aSZach Atkins if (vec->length == 0) { 512b0976d5aSZach Atkins *norm = 0; 513b0976d5aSZach Atkins return CEED_ERROR_SUCCESS; 514b0976d5aSZach Atkins } 515b0976d5aSZach Atkins 516547d9b97Sjeremylt // Backend impl for GPU, if added 517547d9b97Sjeremylt if (vec->Norm) { 5182b730f8bSJeremy L Thompson CeedCall(vec->Norm(vec, norm_type, norm)); 519e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 520547d9b97Sjeremylt } 521547d9b97Sjeremylt 522547d9b97Sjeremylt const CeedScalar *array; 5232b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array)); 524b0976d5aSZach Atkins assert(array); 525547d9b97Sjeremylt 526547d9b97Sjeremylt *norm = 0.; 527d1d35e2fSjeremylt switch (norm_type) { 528547d9b97Sjeremylt case CEED_NORM_1: 529b94338b9SJed Brown for (CeedSize i = 0; i < vec->length; i++) { 530547d9b97Sjeremylt *norm += fabs(array[i]); 531547d9b97Sjeremylt } 532547d9b97Sjeremylt break; 533547d9b97Sjeremylt case CEED_NORM_2: 534b94338b9SJed Brown for (CeedSize i = 0; i < vec->length; i++) { 535547d9b97Sjeremylt *norm += fabs(array[i]) * fabs(array[i]); 536547d9b97Sjeremylt } 537547d9b97Sjeremylt break; 538547d9b97Sjeremylt case CEED_NORM_MAX: 539b94338b9SJed Brown for (CeedSize i = 0; i < vec->length; i++) { 540d1d35e2fSjeremylt const CeedScalar abs_v_i = fabs(array[i]); 541d1d35e2fSjeremylt *norm = *norm > abs_v_i ? *norm : abs_v_i; 542547d9b97Sjeremylt } 543547d9b97Sjeremylt } 5442b730f8bSJeremy L Thompson if (norm_type == CEED_NORM_2) *norm = sqrt(*norm); 545547d9b97Sjeremylt 5462b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &array)); 547e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 548547d9b97Sjeremylt } 549547d9b97Sjeremylt 550547d9b97Sjeremylt /** 551e0dd3b27Sjeremylt @brief Compute x = alpha x 552e0dd3b27Sjeremylt 55396b902e2Sjeremylt @param[in,out] x vector for scaling 55496b902e2Sjeremylt @param[in] alpha scaling factor 555e0dd3b27Sjeremylt 556e0dd3b27Sjeremylt @return An error code: 0 - success, otherwise - failure 557e0dd3b27Sjeremylt 558e0dd3b27Sjeremylt @ref User 559e0dd3b27Sjeremylt **/ 560e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) { 56173380422SJeremy L Thompson CeedScalar *x_array = NULL; 5621f9221feSJeremy L Thompson CeedSize n_x; 5639c774eddSJeremy L Thompson bool has_valid_array = true; 564*1c66c397SJeremy L Thompson 5652b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array)); 5666574a04fSJeremy L Thompson CeedCheck(has_valid_array, x->ceed, CEED_ERROR_BACKEND, 5672b730f8bSJeremy L Thompson "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray"); 5689c774eddSJeremy L Thompson 5692b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 570e0dd3b27Sjeremylt 571b0976d5aSZach Atkins // Return early for empty vector 572b0976d5aSZach Atkins if (n_x == 0) return CEED_ERROR_SUCCESS; 573b0976d5aSZach Atkins 574e0dd3b27Sjeremylt // Backend implementation 5752b730f8bSJeremy L Thompson if (x->Scale) return x->Scale(x, alpha); 576e0dd3b27Sjeremylt 577e0dd3b27Sjeremylt // Default implementation 578567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(x, CEED_MEM_HOST, &x_array)); 579b0976d5aSZach Atkins assert(x_array); 580b94338b9SJed Brown for (CeedSize i = 0; i < n_x; i++) x_array[i] *= alpha; 5812b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(x, &x_array)); 582e0dd3b27Sjeremylt return CEED_ERROR_SUCCESS; 583e0dd3b27Sjeremylt } 584e0dd3b27Sjeremylt 585e0dd3b27Sjeremylt /** 5860f7fd0f8Sjeremylt @brief Compute y = alpha x + y 5870f7fd0f8Sjeremylt 58896b902e2Sjeremylt @param[in,out] y target vector for sum 58996b902e2Sjeremylt @param[in] alpha scaling factor 59096b902e2Sjeremylt @param[in] x second vector, must be different than y 5910f7fd0f8Sjeremylt 5920f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 5930f7fd0f8Sjeremylt 5940f7fd0f8Sjeremylt @ref User 5950f7fd0f8Sjeremylt **/ 5960f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { 597*1c66c397SJeremy L Thompson Ceed ceed_parent_x, ceed_parent_y; 59873380422SJeremy L Thompson CeedScalar *y_array = NULL; 59973380422SJeremy L Thompson CeedScalar const *x_array = NULL; 6001f9221feSJeremy L Thompson CeedSize n_x, n_y; 601*1c66c397SJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 6020f7fd0f8Sjeremylt 6032b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &n_y)); 6042b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 6056574a04fSJeremy L Thompson CeedCheck(n_x == n_y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 6066574a04fSJeremy L Thompson CeedCheck(x != y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); 6070f7fd0f8Sjeremylt 6082b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 6096574a04fSJeremy L Thompson CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND, 6106574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6112b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 6126574a04fSJeremy L Thompson CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND, 6136574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6149c774eddSJeremy L Thompson 6152b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 6162b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 6176574a04fSJeremy 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"); 6182d04630dSjeremylt 619b0976d5aSZach Atkins // Return early for empty vectors 620b0976d5aSZach Atkins if (n_y == 0) return CEED_ERROR_SUCCESS; 621b0976d5aSZach Atkins 6220f7fd0f8Sjeremylt // Backend implementation 623eaf62fffSJeremy L Thompson if (y->AXPY) { 6242b730f8bSJeremy L Thompson CeedCall(y->AXPY(y, alpha, x)); 625eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 626eaf62fffSJeremy L Thompson } 6270f7fd0f8Sjeremylt 6280f7fd0f8Sjeremylt // Default implementation 629567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 6302b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 6310f7fd0f8Sjeremylt 6322b730f8bSJeremy L Thompson assert(x_array); 6332b730f8bSJeremy L Thompson assert(y_array); 63473380422SJeremy L Thompson 635b94338b9SJed Brown for (CeedSize i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i]; 6360f7fd0f8Sjeremylt 6372b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(y, &y_array)); 6382b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 6390f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 6400f7fd0f8Sjeremylt } 6410f7fd0f8Sjeremylt 6420f7fd0f8Sjeremylt /** 6435fb68f37SKaren (Ren) Stengel @brief Compute y = alpha x + beta y 6445fb68f37SKaren (Ren) Stengel 6455fb68f37SKaren (Ren) Stengel @param[in,out] y target vector for sum 6465fb68f37SKaren (Ren) Stengel @param[in] alpha first scaling factor 6475fb68f37SKaren (Ren) Stengel @param[in] beta second scaling factor 6485fb68f37SKaren (Ren) Stengel @param[in] x second vector, must be different than y 6495fb68f37SKaren (Ren) Stengel 6505fb68f37SKaren (Ren) Stengel @return An error code: 0 - success, otherwise - failure 6515fb68f37SKaren (Ren) Stengel 6525fb68f37SKaren (Ren) Stengel @ref User 6535fb68f37SKaren (Ren) Stengel **/ 6545fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 655*1c66c397SJeremy L Thompson Ceed ceed_parent_x, ceed_parent_y; 6565fb68f37SKaren (Ren) Stengel CeedScalar *y_array = NULL; 6575fb68f37SKaren (Ren) Stengel CeedScalar const *x_array = NULL; 6585fb68f37SKaren (Ren) Stengel CeedSize n_x, n_y; 659*1c66c397SJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 6605fb68f37SKaren (Ren) Stengel 6615fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetLength(y, &n_y)); 6625fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetLength(x, &n_x)); 6636574a04fSJeremy L Thompson CeedCheck(n_x == n_y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths"); 6646574a04fSJeremy L Thompson CeedCheck(x != y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY"); 6655fb68f37SKaren (Ren) Stengel 6665fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 6676574a04fSJeremy L Thompson CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND, 6686574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6695fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 6706574a04fSJeremy L Thompson CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND, 6716574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 6725fb68f37SKaren (Ren) Stengel 6735fb68f37SKaren (Ren) Stengel CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 6745fb68f37SKaren (Ren) Stengel CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 6756574a04fSJeremy 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"); 6765fb68f37SKaren (Ren) Stengel 677b0976d5aSZach Atkins // Return early for empty vectors 678b0976d5aSZach Atkins if (n_y == 0) return CEED_ERROR_SUCCESS; 679b0976d5aSZach Atkins 6805fb68f37SKaren (Ren) Stengel // Backend implementation 6815fb68f37SKaren (Ren) Stengel if (y->AXPBY) { 6825fb68f37SKaren (Ren) Stengel CeedCall(y->AXPBY(y, alpha, beta, x)); 6835fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 6845fb68f37SKaren (Ren) Stengel } 6855fb68f37SKaren (Ren) Stengel 6865fb68f37SKaren (Ren) Stengel // Default implementation 6875fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array)); 6885fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 6895fb68f37SKaren (Ren) Stengel 6905fb68f37SKaren (Ren) Stengel assert(x_array); 6915fb68f37SKaren (Ren) Stengel assert(y_array); 6925fb68f37SKaren (Ren) Stengel 693b94338b9SJed Brown for (CeedSize i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i] + beta * y_array[i]; 6945fb68f37SKaren (Ren) Stengel 6955fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArray(y, &y_array)); 6965fb68f37SKaren (Ren) Stengel CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 6975fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 6985fb68f37SKaren (Ren) Stengel } 6995fb68f37SKaren (Ren) Stengel 7005fb68f37SKaren (Ren) Stengel /** 701ea61e9acSJeremy L Thompson @brief Compute the pointwise multiplication w = x .* y. 7024385fb7fSSebastian Grimberg 703ea61e9acSJeremy L Thompson Any subset of x, y, and w may be the same vector. 7040f7fd0f8Sjeremylt 70596b902e2Sjeremylt @param[out] w target vector for the product 70696b902e2Sjeremylt @param[in] x first vector for product 70796b902e2Sjeremylt @param[in] y second vector for the product 7080f7fd0f8Sjeremylt 7090f7fd0f8Sjeremylt @return An error code: 0 - success, otherwise - failure 7100f7fd0f8Sjeremylt 7110f7fd0f8Sjeremylt @ref User 7120f7fd0f8Sjeremylt **/ 7130f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { 714*1c66c397SJeremy L Thompson Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y; 71573380422SJeremy L Thompson CeedScalar *w_array = NULL; 71673380422SJeremy L Thompson CeedScalar const *x_array = NULL, *y_array = NULL; 7171f9221feSJeremy L Thompson CeedSize n_w, n_x, n_y; 718*1c66c397SJeremy L Thompson bool has_valid_array_x = true, has_valid_array_y = true; 7190f7fd0f8Sjeremylt 7202b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(w, &n_w)); 7212b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(x, &n_x)); 7222b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(y, &n_y)); 7236574a04fSJeremy L Thompson CeedCheck(n_w == n_x && n_w == n_y, w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths"); 7240f7fd0f8Sjeremylt 7252b730f8bSJeremy L Thompson CeedCall(CeedGetParent(w->ceed, &ceed_parent_w)); 7262b730f8bSJeremy L Thompson CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); 7272b730f8bSJeremy L Thompson CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); 7286574a04fSJeremy L Thompson CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, w->ceed, CEED_ERROR_INCOMPATIBLE, 7296574a04fSJeremy L Thompson "Vectors w, x, and y must be created by the same Ceed context"); 7302d04630dSjeremylt 7312b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); 7326574a04fSJeremy L Thompson CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND, 7336574a04fSJeremy L Thompson "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7342b730f8bSJeremy L Thompson CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); 7356574a04fSJeremy L Thompson CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND, 7366574a04fSJeremy L Thompson "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7379c774eddSJeremy L Thompson 738b0976d5aSZach Atkins // Return early for empty vectors 739b0976d5aSZach Atkins if (n_w == 0) return CEED_ERROR_SUCCESS; 740b0976d5aSZach Atkins 7410f7fd0f8Sjeremylt // Backend implementation 742eaf62fffSJeremy L Thompson if (w->PointwiseMult) { 7432b730f8bSJeremy L Thompson CeedCall(w->PointwiseMult(w, x, y)); 744eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 745eaf62fffSJeremy L Thompson } 7460f7fd0f8Sjeremylt 7470f7fd0f8Sjeremylt // Default implementation 748b0976d5aSZach Atkins if (x == w || y == w) { 749b0976d5aSZach Atkins CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array)); 750b0976d5aSZach Atkins } else { 751b0976d5aSZach Atkins CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array)); 752b0976d5aSZach Atkins } 7530f7fd0f8Sjeremylt if (x != w) { 7542b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array)); 7550f7fd0f8Sjeremylt } else { 7560f7fd0f8Sjeremylt x_array = w_array; 7570f7fd0f8Sjeremylt } 7580f7fd0f8Sjeremylt if (y != w && y != x) { 7592b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array)); 760c6d796c6SJeremy L Thompson } else if (y == x) { 7610f7fd0f8Sjeremylt y_array = x_array; 762b0976d5aSZach Atkins } else if (y == w) { 763c6d796c6SJeremy L Thompson y_array = w_array; 7640f7fd0f8Sjeremylt } 7650f7fd0f8Sjeremylt 7662b730f8bSJeremy L Thompson assert(w_array); 7672b730f8bSJeremy L Thompson assert(x_array); 7682b730f8bSJeremy L Thompson assert(y_array); 76973380422SJeremy L Thompson 770b94338b9SJed Brown for (CeedSize i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i]; 7710f7fd0f8Sjeremylt 7722b730f8bSJeremy L Thompson if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array)); 7732b730f8bSJeremy L Thompson if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array)); 7742b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(w, &w_array)); 7750f7fd0f8Sjeremylt return CEED_ERROR_SUCCESS; 7760f7fd0f8Sjeremylt } 7770f7fd0f8Sjeremylt 7780f7fd0f8Sjeremylt /** 779d99fa3c5SJeremy L Thompson @brief Take the reciprocal of a CeedVector. 780d99fa3c5SJeremy L Thompson 781ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to take reciprocal 782d99fa3c5SJeremy L Thompson 783d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 784d99fa3c5SJeremy L Thompson 785d99fa3c5SJeremy L Thompson @ref User 786d99fa3c5SJeremy L Thompson **/ 787d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) { 788*1c66c397SJeremy L Thompson CeedSize len; 789*1c66c397SJeremy L Thompson CeedScalar *array; 7909c774eddSJeremy L Thompson bool has_valid_array = true; 7912b730f8bSJeremy L Thompson 792*1c66c397SJeremy L Thompson CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); 7936574a04fSJeremy L Thompson CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND, 7942b730f8bSJeremy L Thompson "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); 7959c774eddSJeremy L Thompson 796d99fa3c5SJeremy L Thompson // Check if vector data set 7976574a04fSJeremy L Thompson CeedCheck(vec->state > 0, vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); 798d99fa3c5SJeremy L Thompson 799b0976d5aSZach Atkins // Return early for empty vector 800b0976d5aSZach Atkins if (vec->length == 0) return CEED_ERROR_SUCCESS; 801b0976d5aSZach Atkins 802d99fa3c5SJeremy L Thompson // Backend impl for GPU, if added 803d99fa3c5SJeremy L Thompson if (vec->Reciprocal) { 8042b730f8bSJeremy L Thompson CeedCall(vec->Reciprocal(vec)); 805e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 806d99fa3c5SJeremy L Thompson } 807d99fa3c5SJeremy L Thompson 8082b730f8bSJeremy L Thompson CeedCall(CeedVectorGetLength(vec, &len)); 809567d69a2SJeremy L Thompson CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array)); 810b94338b9SJed Brown for (CeedSize i = 0; i < len; i++) { 8112b730f8bSJeremy L Thompson if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i]; 8122b730f8bSJeremy L Thompson } 813d99fa3c5SJeremy L Thompson 8142b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(vec, &array)); 815e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 816d99fa3c5SJeremy L Thompson } 817d99fa3c5SJeremy L Thompson 818d99fa3c5SJeremy L Thompson /** 8197a982d89SJeremy L. Thompson @brief View a CeedVector 8200436c2adSjeremylt 821acb2c48cSJeremy L Thompson Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`. 822acb2c48cSJeremy L Thompson Any portion of the provided range that is outside the range of valid indices for the CeedVector will be ignored. 823acb2c48cSJeremy L Thompson 824acb2c48cSJeremy L Thompson @param[in] vec CeedVector to view 825acb2c48cSJeremy L Thompson @param[in] start Index of first CeedVector entry to view 826acb2c48cSJeremy L Thompson @param[in] stop Index of last CeedVector entry to view 827acb2c48cSJeremy L Thompson @param[in] step Step between CeedVector entries to view 828acb2c48cSJeremy L Thompson @param[in] fp_fmt Printing format 829acb2c48cSJeremy L Thompson @param[in] stream Filestream to write to 830acb2c48cSJeremy L Thompson 831acb2c48cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 832acb2c48cSJeremy L Thompson 833acb2c48cSJeremy L Thompson @ref User 834acb2c48cSJeremy L Thompson **/ 835acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) { 836acb2c48cSJeremy L Thompson const CeedScalar *x; 837acb2c48cSJeremy L Thompson char fmt[1024]; 838acb2c48cSJeremy L Thompson 8396574a04fSJeremy L Thompson CeedCheck(step != 0, vec->ceed, CEED_ERROR_MINOR, "View range 'step' must be nonzero"); 840acb2c48cSJeremy L Thompson 841acb2c48cSJeremy L Thompson fprintf(stream, "CeedVector length %ld\n", (long)vec->length); 842acb2c48cSJeremy L Thompson if (start != 0 || stop != vec->length || step != 1) { 843acb2c48cSJeremy L Thompson fprintf(stream, " start: %ld\n stop: %ld\n step: %" CeedInt_FMT "\n", (long)start, (long)stop, step); 844acb2c48cSJeremy L Thompson } 845acb2c48cSJeremy L Thompson if (start > vec->length) start = vec->length; 846acb2c48cSJeremy L Thompson if (stop > vec->length) stop = vec->length; 847acb2c48cSJeremy L Thompson 848acb2c48cSJeremy L Thompson snprintf(fmt, sizeof fmt, " %s\n", fp_fmt ? fp_fmt : "%g"); 849acb2c48cSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x)); 850b94338b9SJed Brown for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]); 851acb2c48cSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(vec, &x)); 852acb2c48cSJeremy L Thompson if (stop != vec->length) fprintf(stream, " ...\n"); 853acb2c48cSJeremy L Thompson 854acb2c48cSJeremy L Thompson return CEED_ERROR_SUCCESS; 855acb2c48cSJeremy L Thompson } 856acb2c48cSJeremy L Thompson 857acb2c48cSJeremy L Thompson /** 858acb2c48cSJeremy L Thompson @brief View a CeedVector 859acb2c48cSJeremy L Thompson 8600a0da059Sjeremylt @param[in] vec CeedVector to view 861d1d35e2fSjeremylt @param[in] fp_fmt Printing format 8620a0da059Sjeremylt @param[in] stream Filestream to write to 8630a0da059Sjeremylt 8640436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8650436c2adSjeremylt 8667a982d89SJeremy L. Thompson @ref User 8670436c2adSjeremylt **/ 868d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { 869acb2c48cSJeremy L Thompson CeedCall(CeedVectorViewRange(vec, 0, vec->length, 1, fp_fmt, stream)); 870e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8710436c2adSjeremylt } 8720436c2adSjeremylt 8730436c2adSjeremylt /** 874b7c9bbdaSJeremy L Thompson @brief Get the Ceed associated with a CeedVector 875b7c9bbdaSJeremy L Thompson 876ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve state 877b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store ceed 878b7c9bbdaSJeremy L Thompson 879b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 880b7c9bbdaSJeremy L Thompson 881b7c9bbdaSJeremy L Thompson @ref Advanced 882b7c9bbdaSJeremy L Thompson **/ 883b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 884b7c9bbdaSJeremy L Thompson *ceed = vec->ceed; 885b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 886b7c9bbdaSJeremy L Thompson } 887b7c9bbdaSJeremy L Thompson 888b7c9bbdaSJeremy L Thompson /** 8897a982d89SJeremy L. Thompson @brief Get the length of a CeedVector 8900436c2adSjeremylt 891ea61e9acSJeremy L Thompson @param[in] vec CeedVector to retrieve length 8927a982d89SJeremy L. Thompson @param[out] length Variable to store length 8930436c2adSjeremylt 8940436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 8950436c2adSjeremylt 8967a982d89SJeremy L. Thompson @ref User 8970436c2adSjeremylt **/ 8981f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) { 8997a982d89SJeremy L. Thompson *length = vec->length; 900e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9010436c2adSjeremylt } 9020436c2adSjeremylt 9030436c2adSjeremylt /** 9040436c2adSjeremylt @brief Destroy a CeedVector 9050436c2adSjeremylt 906ea61e9acSJeremy L Thompson @param[in,out] vec CeedVector to destroy 9070436c2adSjeremylt 9080436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 9090436c2adSjeremylt 9107a982d89SJeremy L. Thompson @ref User 9110436c2adSjeremylt **/ 9120436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) { 9137425e127SJeremy L Thompson if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) { 914ad6481ceSJeremy L Thompson *vec = NULL; 915ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 916ad6481ceSJeremy L Thompson } 9176574a04fSJeremy L Thompson CeedCheck((*vec)->state % 2 == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use"); 9186574a04fSJeremy L Thompson CeedCheck((*vec)->num_readers == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access"); 9190436c2adSjeremylt 9202b730f8bSJeremy L Thompson if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec)); 9212b730f8bSJeremy L Thompson 9222b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*vec)->ceed)); 9232b730f8bSJeremy L Thompson CeedCall(CeedFree(vec)); 924e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 9250436c2adSjeremylt } 9260436c2adSjeremylt 9270436c2adSjeremylt /// @} 928