xref: /libCEED/interface/ceed-vector.c (revision 567d69a22da0f47f75e26def20768fbc471a4d2c)
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) {
3336574a04fSJeremy L Thompson   CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use");
3346574a04fSJeremy L Thompson   CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access");
3356a6c615bSJeremy L Thompson 
33650c643e1SJed Brown   CeedScalar *temp_array = NULL;
33750c643e1SJed Brown   if (vec->length > 0) {
3389c774eddSJeremy L Thompson     bool has_borrowed_array_of_type = true;
3392b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type));
3406574a04fSJeremy L Thompson     CeedCheck(has_borrowed_array_of_type, vec->ceed, CEED_ERROR_BACKEND,
3416574a04fSJeremy L Thompson               "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", CeedMemTypes[mem_type]);
3429c774eddSJeremy L Thompson 
3439c774eddSJeremy L Thompson     bool has_valid_array = true;
3442b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
3456574a04fSJeremy L Thompson     CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND,
3462b730f8bSJeremy L Thompson               "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray");
3479c774eddSJeremy L Thompson 
3482b730f8bSJeremy L Thompson     CeedCall(vec->TakeArray(vec, mem_type, &temp_array));
34950c643e1SJed Brown   }
350d1d35e2fSjeremylt   if (array) (*array) = temp_array;
351e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3526a6c615bSJeremy L Thompson }
3536a6c615bSJeremy L Thompson 
3546a6c615bSJeremy L Thompson /**
355b3cf021fSjeremylt   @brief Get read/write access to a CeedVector via the specified memory type.
3564385fb7fSSebastian Grimberg 
357b3cf021fSjeremylt   Restore access with @ref CeedVectorRestoreArray().
3580436c2adSjeremylt 
359ea61e9acSJeremy L Thompson   @param[in,out] vec      CeedVector to access
360ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to access the array.
361ea61e9acSJeremy L Thompson                             If the backend uses a different memory type, this will perform a copy.
362d1d35e2fSjeremylt   @param[out]    array    Array on memory type mem_type
3630436c2adSjeremylt 
364ea61e9acSJeremy L Thompson   @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide access to array pointers in the desired memory space.
365ea61e9acSJeremy L Thompson         Pairing get/restore allows the Vector to track access, thus knowing if norms or other operations may need to be recomputed.
3660436c2adSjeremylt 
3670436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3680436c2adSjeremylt 
3697a982d89SJeremy L. Thompson   @ref User
3700436c2adSjeremylt **/
3712b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
3726574a04fSJeremy L Thompson   CeedCheck(vec->GetArray, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray");
3736574a04fSJeremy L Thompson   CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
3746574a04fSJeremy L Thompson   CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
3750436c2adSjeremylt 
376b0976d5aSZach Atkins   if (vec->length > 0) {
3779c774eddSJeremy L Thompson     bool has_valid_array = true;
378b0976d5aSZach Atkins 
3792b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
3806574a04fSJeremy L Thompson     CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND,
3812b730f8bSJeremy L Thompson               "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
3829c774eddSJeremy L Thompson 
3832b730f8bSJeremy L Thompson     CeedCall(vec->GetArray(vec, mem_type, array));
384b0976d5aSZach Atkins   } else {
385b0976d5aSZach Atkins     *array = NULL;
386b0976d5aSZach Atkins   }
38728bfd0b7SJeremy L Thompson   vec->state++;
388e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3890436c2adSjeremylt }
3900436c2adSjeremylt 
3910436c2adSjeremylt /**
392b3cf021fSjeremylt   @brief Get read-only access to a CeedVector via the specified memory type.
3934385fb7fSSebastian Grimberg 
394b3cf021fSjeremylt   Restore access with @ref CeedVectorRestoreArrayRead().
3950436c2adSjeremylt 
396ea61e9acSJeremy L Thompson   @param[in]  vec      CeedVector to access
3974385fb7fSSebastian 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
3984385fb7fSSebastian Grimberg cached).
399d1d35e2fSjeremylt   @param[out] array    Array on memory type mem_type
4000436c2adSjeremylt 
4010436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4020436c2adSjeremylt 
4037a982d89SJeremy L. Thompson   @ref User
4040436c2adSjeremylt **/
4052b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
4066574a04fSJeremy L Thompson   CeedCheck(vec->GetArrayRead, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead");
4076574a04fSJeremy 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");
4080436c2adSjeremylt 
40950c643e1SJed Brown   if (vec->length > 0) {
4109c774eddSJeremy L Thompson     bool has_valid_array = true;
411b0976d5aSZach Atkins 
4122b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
4136574a04fSJeremy L Thompson     CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND,
4142b730f8bSJeremy L Thompson               "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
4159c774eddSJeremy L Thompson 
4162b730f8bSJeremy L Thompson     CeedCall(vec->GetArrayRead(vec, mem_type, array));
41750c643e1SJed Brown   } else {
41850c643e1SJed Brown     *array = NULL;
41950c643e1SJed Brown   }
420d1d35e2fSjeremylt   vec->num_readers++;
421e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4220436c2adSjeremylt }
4230436c2adSjeremylt 
4240436c2adSjeremylt /**
4259c774eddSJeremy L Thompson   @brief Get write access to a CeedVector via the specified memory type.
4264385fb7fSSebastian Grimberg 
427ea61e9acSJeremy L Thompson   Restore access with @ref CeedVectorRestoreArray().
428ea61e9acSJeremy L Thompson   All old values should be assumed to be invalid.
4299c774eddSJeremy L Thompson 
430ea61e9acSJeremy L Thompson   @param[in,out] vec      CeedVector to access
431ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to access the array.
4329c774eddSJeremy L Thompson   @param[out]    array    Array on memory type mem_type
4339c774eddSJeremy L Thompson 
4349c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4359c774eddSJeremy L Thompson 
4369c774eddSJeremy L Thompson   @ref User
4379c774eddSJeremy L Thompson **/
4382b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
4396574a04fSJeremy L Thompson   CeedCheck(vec->GetArrayWrite, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayWrite");
4406574a04fSJeremy L Thompson   CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
4416574a04fSJeremy L Thompson   CeedCheck(vec->num_readers == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
4429c774eddSJeremy L Thompson 
443b0976d5aSZach Atkins   if (vec->length > 0) {
4442b730f8bSJeremy L Thompson     CeedCall(vec->GetArrayWrite(vec, mem_type, array));
445b0976d5aSZach Atkins   } else {
446b0976d5aSZach Atkins     *array = NULL;
447b0976d5aSZach Atkins   }
44828bfd0b7SJeremy L Thompson   vec->state++;
4499c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
4509c774eddSJeremy L Thompson }
4519c774eddSJeremy L Thompson 
4529c774eddSJeremy L Thompson /**
453ea61e9acSJeremy L Thompson   @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite()
4540436c2adSjeremylt 
455ea61e9acSJeremy L Thompson   @param[in,out] vec   CeedVector to restore
456ea61e9acSJeremy L Thompson   @param[in,out] array Array of vector data
4570436c2adSjeremylt 
4580436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4590436c2adSjeremylt 
4607a982d89SJeremy L. Thompson   @ref User
4610436c2adSjeremylt **/
4620436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
4636574a04fSJeremy L Thompson   CeedCheck(vec->state % 2 == 1, vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted");
464b0976d5aSZach Atkins   if (vec->length > 0 && vec->RestoreArray) CeedCall(vec->RestoreArray(vec));
4650436c2adSjeremylt   *array = NULL;
46628bfd0b7SJeremy L Thompson   vec->state++;
467e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4680436c2adSjeremylt }
4690436c2adSjeremylt 
4700436c2adSjeremylt /**
471b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArrayRead()
4720436c2adSjeremylt 
473ea61e9acSJeremy L Thompson   @param[in]     vec   CeedVector to restore
474ea61e9acSJeremy L Thompson   @param[in,out] array Array of vector data
4750436c2adSjeremylt 
4760436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4770436c2adSjeremylt 
4787a982d89SJeremy L. Thompson   @ref User
4790436c2adSjeremylt **/
4800436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) {
4816574a04fSJeremy L Thompson   CeedCheck(vec->num_readers > 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted");
4829c774eddSJeremy L Thompson 
48375a19770SJeremy L Thompson   vec->num_readers--;
484b0976d5aSZach Atkins   if (vec->length > 0 && vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec));
4850436c2adSjeremylt   *array = NULL;
48675a19770SJeremy L Thompson 
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;
563e0dd3b27Sjeremylt 
5649c774eddSJeremy L Thompson   bool has_valid_array = true;
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
578*567d69a2SJeremy 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 
583e0dd3b27Sjeremylt   return CEED_ERROR_SUCCESS;
584e0dd3b27Sjeremylt }
585e0dd3b27Sjeremylt 
586e0dd3b27Sjeremylt /**
5870f7fd0f8Sjeremylt   @brief Compute y = alpha x + y
5880f7fd0f8Sjeremylt 
58996b902e2Sjeremylt   @param[in,out] y     target vector for sum
59096b902e2Sjeremylt   @param[in]     alpha scaling factor
59196b902e2Sjeremylt   @param[in]     x     second vector, must be different than y
5920f7fd0f8Sjeremylt 
5930f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
5940f7fd0f8Sjeremylt 
5950f7fd0f8Sjeremylt   @ref User
5960f7fd0f8Sjeremylt **/
5970f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
59873380422SJeremy L Thompson   CeedScalar       *y_array = NULL;
59973380422SJeremy L Thompson   CeedScalar const *x_array = NULL;
6001f9221feSJeremy L Thompson   CeedSize          n_x, n_y;
6010f7fd0f8Sjeremylt 
6022b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &n_y));
6032b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
6046574a04fSJeremy L Thompson   CeedCheck(n_x == n_y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths");
6056574a04fSJeremy L Thompson   CeedCheck(x != y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY");
6060f7fd0f8Sjeremylt 
6079c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
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 
6152d04630dSjeremylt   Ceed ceed_parent_x, ceed_parent_y;
6162b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
6172b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
6186574a04fSJeremy 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");
6192d04630dSjeremylt 
620b0976d5aSZach Atkins   // Return early for empty vectors
621b0976d5aSZach Atkins   if (n_y == 0) return CEED_ERROR_SUCCESS;
622b0976d5aSZach Atkins 
6230f7fd0f8Sjeremylt   // Backend implementation
624eaf62fffSJeremy L Thompson   if (y->AXPY) {
6252b730f8bSJeremy L Thompson     CeedCall(y->AXPY(y, alpha, x));
626eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
627eaf62fffSJeremy L Thompson   }
6280f7fd0f8Sjeremylt 
6290f7fd0f8Sjeremylt   // Default implementation
630*567d69a2SJeremy L Thompson   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
6312b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
6320f7fd0f8Sjeremylt 
6332b730f8bSJeremy L Thompson   assert(x_array);
6342b730f8bSJeremy L Thompson   assert(y_array);
63573380422SJeremy L Thompson 
636b94338b9SJed Brown   for (CeedSize i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i];
6370f7fd0f8Sjeremylt 
6382b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(y, &y_array));
6392b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
6400f7fd0f8Sjeremylt 
6410f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
6420f7fd0f8Sjeremylt }
6430f7fd0f8Sjeremylt 
6440f7fd0f8Sjeremylt /**
6455fb68f37SKaren (Ren) Stengel   @brief Compute y = alpha x + beta y
6465fb68f37SKaren (Ren) Stengel 
6475fb68f37SKaren (Ren) Stengel   @param[in,out] y     target vector for sum
6485fb68f37SKaren (Ren) Stengel   @param[in]     alpha first scaling factor
6495fb68f37SKaren (Ren) Stengel   @param[in]     beta  second scaling factor
6505fb68f37SKaren (Ren) Stengel   @param[in]     x     second vector, must be different than y
6515fb68f37SKaren (Ren) Stengel 
6525fb68f37SKaren (Ren) Stengel   @return An error code: 0 - success, otherwise - failure
6535fb68f37SKaren (Ren) Stengel 
6545fb68f37SKaren (Ren) Stengel   @ref User
6555fb68f37SKaren (Ren) Stengel **/
6565fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
6575fb68f37SKaren (Ren) Stengel   CeedScalar       *y_array = NULL;
6585fb68f37SKaren (Ren) Stengel   CeedScalar const *x_array = NULL;
6595fb68f37SKaren (Ren) Stengel   CeedSize          n_x, n_y;
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   bool has_valid_array_x = true, has_valid_array_y = true;
6675fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
6686574a04fSJeremy L Thompson   CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND,
6696574a04fSJeremy L Thompson             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6705fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
6716574a04fSJeremy L Thompson   CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND,
6726574a04fSJeremy L Thompson             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6735fb68f37SKaren (Ren) Stengel 
6745fb68f37SKaren (Ren) Stengel   Ceed ceed_parent_x, ceed_parent_y;
6755fb68f37SKaren (Ren) Stengel   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
6765fb68f37SKaren (Ren) Stengel   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
6776574a04fSJeremy 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");
6785fb68f37SKaren (Ren) Stengel 
679b0976d5aSZach Atkins   // Return early for empty vectors
680b0976d5aSZach Atkins   if (n_y == 0) return CEED_ERROR_SUCCESS;
681b0976d5aSZach Atkins 
6825fb68f37SKaren (Ren) Stengel   // Backend implementation
6835fb68f37SKaren (Ren) Stengel   if (y->AXPBY) {
6845fb68f37SKaren (Ren) Stengel     CeedCall(y->AXPBY(y, alpha, beta, x));
6855fb68f37SKaren (Ren) Stengel     return CEED_ERROR_SUCCESS;
6865fb68f37SKaren (Ren) Stengel   }
6875fb68f37SKaren (Ren) Stengel 
6885fb68f37SKaren (Ren) Stengel   // Default implementation
6895fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
6905fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
6915fb68f37SKaren (Ren) Stengel 
6925fb68f37SKaren (Ren) Stengel   assert(x_array);
6935fb68f37SKaren (Ren) Stengel   assert(y_array);
6945fb68f37SKaren (Ren) Stengel 
695b94338b9SJed Brown   for (CeedSize i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i] + beta * y_array[i];
6965fb68f37SKaren (Ren) Stengel 
6975fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArray(y, &y_array));
6985fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
6995fb68f37SKaren (Ren) Stengel 
7005fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
7015fb68f37SKaren (Ren) Stengel }
7025fb68f37SKaren (Ren) Stengel 
7035fb68f37SKaren (Ren) Stengel /**
704ea61e9acSJeremy L Thompson   @brief Compute the pointwise multiplication w = x .* y.
7054385fb7fSSebastian Grimberg 
706ea61e9acSJeremy L Thompson   Any subset of x, y, and w may be the same vector.
7070f7fd0f8Sjeremylt 
70896b902e2Sjeremylt   @param[out] w target vector for the product
70996b902e2Sjeremylt   @param[in]  x first vector for product
71096b902e2Sjeremylt   @param[in]  y second vector for the product
7110f7fd0f8Sjeremylt 
7120f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
7130f7fd0f8Sjeremylt 
7140f7fd0f8Sjeremylt   @ref User
7150f7fd0f8Sjeremylt **/
7160f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
71773380422SJeremy L Thompson   CeedScalar       *w_array = NULL;
71873380422SJeremy L Thompson   CeedScalar const *x_array = NULL, *y_array = NULL;
7191f9221feSJeremy L Thompson   CeedSize          n_w, n_x, n_y;
7200f7fd0f8Sjeremylt 
7212b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(w, &n_w));
7222b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
7232b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &n_y));
7246574a04fSJeremy L Thompson   CeedCheck(n_w == n_x && n_w == n_y, w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths");
7250f7fd0f8Sjeremylt 
7262d04630dSjeremylt   Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y;
7272b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(w->ceed, &ceed_parent_w));
7282b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
7292b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
7306574a04fSJeremy L Thompson   CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, w->ceed, CEED_ERROR_INCOMPATIBLE,
7316574a04fSJeremy L Thompson             "Vectors w, x, and y must be created by the same Ceed context");
7322d04630dSjeremylt 
7339c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
7342b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
7356574a04fSJeremy L Thompson   CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND,
7366574a04fSJeremy L Thompson             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7372b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
7386574a04fSJeremy L Thompson   CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND,
7396574a04fSJeremy L Thompson             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7409c774eddSJeremy L Thompson 
741b0976d5aSZach Atkins   // Return early for empty vectors
742b0976d5aSZach Atkins   if (n_w == 0) return CEED_ERROR_SUCCESS;
743b0976d5aSZach Atkins 
7440f7fd0f8Sjeremylt   // Backend implementation
745eaf62fffSJeremy L Thompson   if (w->PointwiseMult) {
7462b730f8bSJeremy L Thompson     CeedCall(w->PointwiseMult(w, x, y));
747eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
748eaf62fffSJeremy L Thompson   }
7490f7fd0f8Sjeremylt 
7500f7fd0f8Sjeremylt   // Default implementation
751b0976d5aSZach Atkins   if (x == w || y == w) {
752b0976d5aSZach Atkins     CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array));
753b0976d5aSZach Atkins   } else {
754b0976d5aSZach Atkins     CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array));
755b0976d5aSZach Atkins   }
7560f7fd0f8Sjeremylt   if (x != w) {
7572b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
7580f7fd0f8Sjeremylt   } else {
7590f7fd0f8Sjeremylt     x_array = w_array;
7600f7fd0f8Sjeremylt   }
7610f7fd0f8Sjeremylt   if (y != w && y != x) {
7622b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array));
763c6d796c6SJeremy L Thompson   } else if (y == x) {
7640f7fd0f8Sjeremylt     y_array = x_array;
765b0976d5aSZach Atkins   } else if (y == w) {
766c6d796c6SJeremy L Thompson     y_array = w_array;
7670f7fd0f8Sjeremylt   }
7680f7fd0f8Sjeremylt 
7692b730f8bSJeremy L Thompson   assert(w_array);
7702b730f8bSJeremy L Thompson   assert(x_array);
7712b730f8bSJeremy L Thompson   assert(y_array);
77273380422SJeremy L Thompson 
773b94338b9SJed Brown   for (CeedSize i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i];
7740f7fd0f8Sjeremylt 
7752b730f8bSJeremy L Thompson   if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array));
7762b730f8bSJeremy L Thompson   if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
7772b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(w, &w_array));
7780f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
7790f7fd0f8Sjeremylt }
7800f7fd0f8Sjeremylt 
7810f7fd0f8Sjeremylt /**
782d99fa3c5SJeremy L Thompson   @brief Take the reciprocal of a CeedVector.
783d99fa3c5SJeremy L Thompson 
784ea61e9acSJeremy L Thompson   @param[in,out] vec CeedVector to take reciprocal
785d99fa3c5SJeremy L Thompson 
786d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
787d99fa3c5SJeremy L Thompson 
788d99fa3c5SJeremy L Thompson   @ref User
789d99fa3c5SJeremy L Thompson **/
790d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) {
7919c774eddSJeremy L Thompson   bool has_valid_array = true;
7922b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
7932b730f8bSJeremy L Thompson 
7946574a04fSJeremy L Thompson   CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND,
7952b730f8bSJeremy L Thompson             "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray");
7969c774eddSJeremy L Thompson 
797d99fa3c5SJeremy L Thompson   // Check if vector data set
7986574a04fSJeremy L Thompson   CeedCheck(vec->state > 0, vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal");
799d99fa3c5SJeremy L Thompson 
800b0976d5aSZach Atkins   // Return early for empty vector
801b0976d5aSZach Atkins   if (vec->length == 0) return CEED_ERROR_SUCCESS;
802b0976d5aSZach Atkins 
803d99fa3c5SJeremy L Thompson   // Backend impl for GPU, if added
804d99fa3c5SJeremy L Thompson   if (vec->Reciprocal) {
8052b730f8bSJeremy L Thompson     CeedCall(vec->Reciprocal(vec));
806e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
807d99fa3c5SJeremy L Thompson   }
808d99fa3c5SJeremy L Thompson 
8091f9221feSJeremy L Thompson   CeedSize len;
8102b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &len));
811d99fa3c5SJeremy L Thompson   CeedScalar *array;
812*567d69a2SJeremy L Thompson   CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array));
813b94338b9SJed Brown   for (CeedSize i = 0; i < len; i++) {
8142b730f8bSJeremy L Thompson     if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i];
8152b730f8bSJeremy L Thompson   }
816d99fa3c5SJeremy L Thompson 
8172b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(vec, &array));
818e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
819d99fa3c5SJeremy L Thompson }
820d99fa3c5SJeremy L Thompson 
821d99fa3c5SJeremy L Thompson /**
8227a982d89SJeremy L. Thompson   @brief View a CeedVector
8230436c2adSjeremylt 
824acb2c48cSJeremy L Thompson   Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`.
825acb2c48cSJeremy L Thompson         Any portion of the provided range that is outside the range of valid indices for the CeedVector will be ignored.
826acb2c48cSJeremy L Thompson 
827acb2c48cSJeremy L Thompson   @param[in] vec    CeedVector to view
828acb2c48cSJeremy L Thompson   @param[in] start  Index of first CeedVector entry to view
829acb2c48cSJeremy L Thompson   @param[in] stop   Index of last CeedVector entry to view
830acb2c48cSJeremy L Thompson   @param[in] step   Step between CeedVector entries to view
831acb2c48cSJeremy L Thompson   @param[in] fp_fmt Printing format
832acb2c48cSJeremy L Thompson   @param[in] stream Filestream to write to
833acb2c48cSJeremy L Thompson 
834acb2c48cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
835acb2c48cSJeremy L Thompson 
836acb2c48cSJeremy L Thompson   @ref User
837acb2c48cSJeremy L Thompson **/
838acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) {
839acb2c48cSJeremy L Thompson   const CeedScalar *x;
840acb2c48cSJeremy L Thompson   char              fmt[1024];
841acb2c48cSJeremy L Thompson 
8426574a04fSJeremy L Thompson   CeedCheck(step != 0, vec->ceed, CEED_ERROR_MINOR, "View range 'step' must be nonzero");
843acb2c48cSJeremy L Thompson 
844acb2c48cSJeremy L Thompson   fprintf(stream, "CeedVector length %ld\n", (long)vec->length);
845acb2c48cSJeremy L Thompson   if (start != 0 || stop != vec->length || step != 1) {
846acb2c48cSJeremy L Thompson     fprintf(stream, "  start: %ld\n  stop:  %ld\n  step:  %" CeedInt_FMT "\n", (long)start, (long)stop, step);
847acb2c48cSJeremy L Thompson   }
848acb2c48cSJeremy L Thompson   if (start > vec->length) start = vec->length;
849acb2c48cSJeremy L Thompson   if (stop > vec->length) stop = vec->length;
850acb2c48cSJeremy L Thompson 
851acb2c48cSJeremy L Thompson   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
852acb2c48cSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x));
853b94338b9SJed Brown   for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]);
854acb2c48cSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &x));
855acb2c48cSJeremy L Thompson   if (stop != vec->length) fprintf(stream, "  ...\n");
856acb2c48cSJeremy L Thompson 
857acb2c48cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
858acb2c48cSJeremy L Thompson }
859acb2c48cSJeremy L Thompson 
860acb2c48cSJeremy L Thompson /**
861acb2c48cSJeremy L Thompson   @brief View a CeedVector
862acb2c48cSJeremy L Thompson 
8630a0da059Sjeremylt   @param[in] vec    CeedVector to view
864d1d35e2fSjeremylt   @param[in] fp_fmt Printing format
8650a0da059Sjeremylt   @param[in] stream Filestream to write to
8660a0da059Sjeremylt 
8670436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
8680436c2adSjeremylt 
8697a982d89SJeremy L. Thompson   @ref User
8700436c2adSjeremylt **/
871d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
872acb2c48cSJeremy L Thompson   CeedCall(CeedVectorViewRange(vec, 0, vec->length, 1, fp_fmt, stream));
873e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8740436c2adSjeremylt }
8750436c2adSjeremylt 
8760436c2adSjeremylt /**
877b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedVector
878b7c9bbdaSJeremy L Thompson 
879ea61e9acSJeremy L Thompson   @param[in]  vec  CeedVector to retrieve state
880b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store ceed
881b7c9bbdaSJeremy L Thompson 
882b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
883b7c9bbdaSJeremy L Thompson 
884b7c9bbdaSJeremy L Thompson   @ref Advanced
885b7c9bbdaSJeremy L Thompson **/
886b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
887b7c9bbdaSJeremy L Thompson   *ceed = vec->ceed;
888b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
889b7c9bbdaSJeremy L Thompson }
890b7c9bbdaSJeremy L Thompson 
891b7c9bbdaSJeremy L Thompson /**
8927a982d89SJeremy L. Thompson   @brief Get the length of a CeedVector
8930436c2adSjeremylt 
894ea61e9acSJeremy L Thompson   @param[in]  vec    CeedVector to retrieve length
8957a982d89SJeremy L. Thompson   @param[out] length Variable to store length
8960436c2adSjeremylt 
8970436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
8980436c2adSjeremylt 
8997a982d89SJeremy L. Thompson   @ref User
9000436c2adSjeremylt **/
9011f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) {
9027a982d89SJeremy L. Thompson   *length = vec->length;
903e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9040436c2adSjeremylt }
9050436c2adSjeremylt 
9060436c2adSjeremylt /**
9070436c2adSjeremylt   @brief Destroy a CeedVector
9080436c2adSjeremylt 
909ea61e9acSJeremy L Thompson   @param[in,out] vec CeedVector to destroy
9100436c2adSjeremylt 
9110436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9120436c2adSjeremylt 
9137a982d89SJeremy L. Thompson   @ref User
9140436c2adSjeremylt **/
9150436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) {
9167425e127SJeremy L Thompson   if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) {
917ad6481ceSJeremy L Thompson     *vec = NULL;
918ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
919ad6481ceSJeremy L Thompson   }
9206574a04fSJeremy L Thompson   CeedCheck((*vec)->state % 2 == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use");
9216574a04fSJeremy L Thompson   CeedCheck((*vec)->num_readers == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access");
9220436c2adSjeremylt 
9232b730f8bSJeremy L Thompson   if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec));
9242b730f8bSJeremy L Thompson 
9252b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*vec)->ceed));
9262b730f8bSJeremy L Thompson   CeedCall(CeedFree(vec));
927e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9280436c2adSjeremylt }
9290436c2adSjeremylt 
9300436c2adSjeremylt /// @}
931