xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-vector.c (revision ca94c3ddc8f82b7d93a79f9e4812e99b8be840ff)
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 
28*ca94c3ddSJeremy L Thompson /// Indicate that vector will be provided as an explicit argument to @ref 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 /**
43*ca94c3ddSJeremy L Thompson   @brief Check for valid data in a `CeedVector`
449c774eddSJeremy L Thompson 
45*ca94c3ddSJeremy 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) {
53*ca94c3ddSJeremy L Thompson   CeedCheck(vec->HasValidArray, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorHasValidArray");
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 /**
63*ca94c3ddSJeremy L Thompson   @brief Check for borrowed array of a specific @ref CeedMemType in a `CeedVector`
649c774eddSJeremy L Thompson 
65*ca94c3ddSJeremy 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) {
74*ca94c3ddSJeremy L Thompson   CeedCheck(vec->HasBorrowedArrayOfType, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorHasBorrowedArrayOfType");
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 /**
80*ca94c3ddSJeremy L Thompson   @brief Get the state of a `CeedVector`
817a982d89SJeremy L. Thompson 
82*ca94c3ddSJeremy 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 /**
95*ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedVector`
967a982d89SJeremy L. Thompson 
97*ca94c3ddSJeremy 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 /**
110*ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedVector`
1117a982d89SJeremy L. Thompson 
112*ca94c3ddSJeremy 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 /**
125*ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedVector`
12634359f16Sjeremylt 
127*ca94c3ddSJeremy 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 /**
147*ca94c3ddSJeremy L Thompson   @brief Create a `CeedVector` of the specified length (does not allocate memory)
1480436c2adSjeremylt 
149*ca94c3ddSJeremy L Thompson   @param[in]  ceed   `Ceed` object used to create the `CeedVector`
150ea61e9acSJeremy L Thompson   @param[in]  length Length of vector
151*ca94c3ddSJeremy 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 /**
177*ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedVector`.
1784385fb7fSSebastian Grimberg 
179*ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedVectorDestroy().
180512bb800SJeremy L Thompson 
181*ca94c3ddSJeremy 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`.
182*ca94c3ddSJeremy L Thompson         This `CeedVector` will be destroyed if `*vec_copy` is the only reference to this `CeedVector`.
1839560d06aSjeremylt 
184*ca94c3ddSJeremy 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 /**
199*ca94c3ddSJeremy L Thompson   @brief Copy a `CeedVector` into a different `CeedVector`.
2004385fb7fSSebastian Grimberg 
201*ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedVectorDestroy().
2024385fb7fSSebastian Grimberg 
203*ca94c3ddSJeremy L Thompson   Note: If `*vec_copy` is non-`NULL`, then it is assumed that `*vec_copy` is a pointer to a `CeedVector`.
204*ca94c3ddSJeremy L Thompson         This `CeedVector` will be destroyed if `*vec_copy` is the only reference to this `CeedVector`.
2055fb68f37SKaren (Ren) Stengel 
206*ca94c3ddSJeremy L Thompson   @param[in]     vec      `CeedVector` to copy
207*ca94c3ddSJeremy L Thompson   @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 /**
238*ca94c3ddSJeremy L Thompson   @brief Set the array used by a `CeedVector`, freeing any previously allocated array if applicable.
2394385fb7fSSebastian Grimberg 
240*ca94c3ddSJeremy L Thompson   The backend may copy values to a different @ref CeedMemType, such as during @ref CeedOperatorApply().
2416a6c615bSJeremy L Thompson   See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray().
2420436c2adSjeremylt 
243*ca94c3ddSJeremy 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
246*ca94c3ddSJeremy 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 /**
263*ca94c3ddSJeremy L Thompson   @brief Set the `CeedVector` to a constant value
2640436c2adSjeremylt 
265*ca94c3ddSJeremy 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 /**
289*ca94c3ddSJeremy L Thompson   @brief Sync the `CeedVector` to a specified `mem_type`.
2904385fb7fSSebastian Grimberg 
291ea61e9acSJeremy L Thompson   This function is used to force synchronization of arrays set with @ref CeedVectorSetArray().
292*ca94c3ddSJeremy L Thompson   If the requested `mem_type` is already synchronized, this function results in a no-op.
2930436c2adSjeremylt 
294*ca94c3ddSJeremy L Thompson   @param[in,out] vec      `CeedVector`
295*ca94c3ddSJeremy L Thompson   @param[in]     mem_type @ref CeedMemType 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 /**
318*ca94c3ddSJeremy 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 
323*ca94c3ddSJeremy 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.
326*ca94c3ddSJeremy 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) {
3331c66c397SJeremy L Thompson   CeedScalar *temp_array = NULL;
3341c66c397SJeremy 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 /**
356*ca94c3ddSJeremy L Thompson   @brief Get read/write access to a `CeedVector` via the specified memory type.
3574385fb7fSSebastian Grimberg 
358b3cf021fSjeremylt   Restore access with @ref CeedVectorRestoreArray().
3590436c2adSjeremylt 
360*ca94c3ddSJeremy 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.
363*ca94c3ddSJeremy L Thompson   @param[out]    array    Array on memory type `mem_type`
3640436c2adSjeremylt 
365*ca94c3ddSJeremy L Thompson   @note The @ref CeedVectorGetArray() and @ref CeedVectorRestoreArray() functions provide access to array pointers in the desired memory space.
366*ca94c3ddSJeremy L Thompson         Pairing get/restore allows the `CeedVector` 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 /**
393*ca94c3ddSJeremy L Thompson   @brief Get read-only access to a `CeedVector` via the specified memory type.
3944385fb7fSSebastian Grimberg 
395b3cf021fSjeremylt   Restore access with @ref CeedVectorRestoreArrayRead().
3960436c2adSjeremylt 
397*ca94c3ddSJeremy L Thompson   @param[in]  vec      `CeedVector` to access
398*ca94c3ddSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the array.
399*ca94c3ddSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy (possibly cached).
400*ca94c3ddSJeremy L Thompson   @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 /**
426*ca94c3ddSJeremy 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 
431*ca94c3ddSJeremy L Thompson   @param[in,out] vec      `CeedVector` to access
432ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to access the array.
433*ca94c3ddSJeremy 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) {
440*ca94c3ddSJeremy L Thompson   CeedCheck(vec->GetArrayWrite, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorGetArrayWrite");
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 
456*ca94c3ddSJeremy 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 
474*ca94c3ddSJeremy 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 /**
491*ca94c3ddSJeremy L Thompson   @brief Get the norm of a `CeedVector`.
492171f8ca9Sjeremylt 
493*ca94c3ddSJeremy L Thompson   Note: This operation is local to the `CeedVector`.
494*ca94c3ddSJeremy 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 duplicated or hanging nodes.
495547d9b97Sjeremylt 
496*ca94c3ddSJeremy L Thompson   @param[in]  vec       `CeedVector` to retrieve maximum value
497ea61e9acSJeremy L Thompson   @param[in]  norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
498547d9b97Sjeremylt   @param[out] norm      Variable to store norm value
499547d9b97Sjeremylt 
500547d9b97Sjeremylt   @return An error code: 0 - success, otherwise - failure
501547d9b97Sjeremylt 
5027a982d89SJeremy L. Thompson   @ref User
503547d9b97Sjeremylt **/
504d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
5059c774eddSJeremy L Thompson   bool has_valid_array = true;
5062b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
5076574a04fSJeremy L Thompson   CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND,
5082b730f8bSJeremy L Thompson             "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray");
5099c774eddSJeremy L Thompson 
510b0976d5aSZach Atkins   if (vec->length == 0) {
511b0976d5aSZach Atkins     *norm = 0;
512b0976d5aSZach Atkins     return CEED_ERROR_SUCCESS;
513b0976d5aSZach Atkins   }
514b0976d5aSZach Atkins 
515547d9b97Sjeremylt   // Backend impl for GPU, if added
516547d9b97Sjeremylt   if (vec->Norm) {
5172b730f8bSJeremy L Thompson     CeedCall(vec->Norm(vec, norm_type, norm));
518e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
519547d9b97Sjeremylt   }
520547d9b97Sjeremylt 
521547d9b97Sjeremylt   const CeedScalar *array;
5222b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array));
523b0976d5aSZach Atkins   assert(array);
524547d9b97Sjeremylt 
525547d9b97Sjeremylt   *norm = 0.;
526d1d35e2fSjeremylt   switch (norm_type) {
527547d9b97Sjeremylt     case CEED_NORM_1:
528b94338b9SJed Brown       for (CeedSize i = 0; i < vec->length; i++) {
529547d9b97Sjeremylt         *norm += fabs(array[i]);
530547d9b97Sjeremylt       }
531547d9b97Sjeremylt       break;
532547d9b97Sjeremylt     case CEED_NORM_2:
533b94338b9SJed Brown       for (CeedSize i = 0; i < vec->length; i++) {
534547d9b97Sjeremylt         *norm += fabs(array[i]) * fabs(array[i]);
535547d9b97Sjeremylt       }
536547d9b97Sjeremylt       break;
537547d9b97Sjeremylt     case CEED_NORM_MAX:
538b94338b9SJed Brown       for (CeedSize i = 0; i < vec->length; i++) {
539d1d35e2fSjeremylt         const CeedScalar abs_v_i = fabs(array[i]);
540d1d35e2fSjeremylt         *norm                    = *norm > abs_v_i ? *norm : abs_v_i;
541547d9b97Sjeremylt       }
542547d9b97Sjeremylt   }
5432b730f8bSJeremy L Thompson   if (norm_type == CEED_NORM_2) *norm = sqrt(*norm);
544547d9b97Sjeremylt 
5452b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &array));
546e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
547547d9b97Sjeremylt }
548547d9b97Sjeremylt 
549547d9b97Sjeremylt /**
550*ca94c3ddSJeremy L Thompson   @brief Compute `x = alpha x`
551e0dd3b27Sjeremylt 
552*ca94c3ddSJeremy L Thompson   @param[in,out] x     `CeedVector` for scaling
55396b902e2Sjeremylt   @param[in]     alpha scaling factor
554e0dd3b27Sjeremylt 
555e0dd3b27Sjeremylt   @return An error code: 0 - success, otherwise - failure
556e0dd3b27Sjeremylt 
557e0dd3b27Sjeremylt   @ref User
558e0dd3b27Sjeremylt **/
559e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) {
56073380422SJeremy L Thompson   CeedScalar *x_array = NULL;
5611f9221feSJeremy L Thompson   CeedSize    n_x;
5629c774eddSJeremy L Thompson   bool        has_valid_array = true;
5631c66c397SJeremy L Thompson 
5642b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array));
5656574a04fSJeremy L Thompson   CeedCheck(has_valid_array, x->ceed, CEED_ERROR_BACKEND,
5662b730f8bSJeremy L Thompson             "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray");
5679c774eddSJeremy L Thompson 
5682b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
569e0dd3b27Sjeremylt 
570b0976d5aSZach Atkins   // Return early for empty vector
571b0976d5aSZach Atkins   if (n_x == 0) return CEED_ERROR_SUCCESS;
572b0976d5aSZach Atkins 
573e0dd3b27Sjeremylt   // Backend implementation
5742b730f8bSJeremy L Thompson   if (x->Scale) return x->Scale(x, alpha);
575e0dd3b27Sjeremylt 
576e0dd3b27Sjeremylt   // Default implementation
577567d69a2SJeremy L Thompson   CeedCall(CeedVectorGetArray(x, CEED_MEM_HOST, &x_array));
578b0976d5aSZach Atkins   assert(x_array);
579b94338b9SJed Brown   for (CeedSize i = 0; i < n_x; i++) x_array[i] *= alpha;
5802b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(x, &x_array));
581e0dd3b27Sjeremylt   return CEED_ERROR_SUCCESS;
582e0dd3b27Sjeremylt }
583e0dd3b27Sjeremylt 
584e0dd3b27Sjeremylt /**
585*ca94c3ddSJeremy L Thompson   @brief Compute `y = alpha x + y`
5860f7fd0f8Sjeremylt 
587*ca94c3ddSJeremy L Thompson   @param[in,out] y     target `CeedVector` for sum
58896b902e2Sjeremylt   @param[in]     alpha scaling factor
589*ca94c3ddSJeremy L Thompson   @param[in]     x     second `CeedVector`, must be different than ``y`
5900f7fd0f8Sjeremylt 
5910f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
5920f7fd0f8Sjeremylt 
5930f7fd0f8Sjeremylt   @ref User
5940f7fd0f8Sjeremylt **/
5950f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
5961c66c397SJeremy L Thompson   Ceed              ceed_parent_x, ceed_parent_y;
59773380422SJeremy L Thompson   CeedScalar       *y_array = NULL;
59873380422SJeremy L Thompson   CeedScalar const *x_array = NULL;
5991f9221feSJeremy L Thompson   CeedSize          n_x, n_y;
6001c66c397SJeremy L Thompson   bool              has_valid_array_x = true, has_valid_array_y = true;
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 
6072b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
6086574a04fSJeremy L Thompson   CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND,
6096574a04fSJeremy L Thompson             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6102b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
6116574a04fSJeremy L Thompson   CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND,
6126574a04fSJeremy L Thompson             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6139c774eddSJeremy L Thompson 
6142b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
6152b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
6166574a04fSJeremy 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");
6172d04630dSjeremylt 
618b0976d5aSZach Atkins   // Return early for empty vectors
619b0976d5aSZach Atkins   if (n_y == 0) return CEED_ERROR_SUCCESS;
620b0976d5aSZach Atkins 
6210f7fd0f8Sjeremylt   // Backend implementation
622eaf62fffSJeremy L Thompson   if (y->AXPY) {
6232b730f8bSJeremy L Thompson     CeedCall(y->AXPY(y, alpha, x));
624eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
625eaf62fffSJeremy L Thompson   }
6260f7fd0f8Sjeremylt 
6270f7fd0f8Sjeremylt   // Default implementation
628567d69a2SJeremy L Thompson   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
6292b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
6300f7fd0f8Sjeremylt 
6312b730f8bSJeremy L Thompson   assert(x_array);
6322b730f8bSJeremy L Thompson   assert(y_array);
63373380422SJeremy L Thompson 
634b94338b9SJed Brown   for (CeedSize i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i];
6350f7fd0f8Sjeremylt 
6362b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(y, &y_array));
6372b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
6380f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
6390f7fd0f8Sjeremylt }
6400f7fd0f8Sjeremylt 
6410f7fd0f8Sjeremylt /**
642*ca94c3ddSJeremy L Thompson   @brief Compute `y = alpha x + beta y`
6435fb68f37SKaren (Ren) Stengel 
644*ca94c3ddSJeremy L Thompson   @param[in,out] y     target `CeedVector` for sum
6455fb68f37SKaren (Ren) Stengel   @param[in]     alpha first scaling factor
6465fb68f37SKaren (Ren) Stengel   @param[in]     beta  second scaling factor
647*ca94c3ddSJeremy L Thompson   @param[in]     x     second `CeedVector`, must be different than `y`
6485fb68f37SKaren (Ren) Stengel 
6495fb68f37SKaren (Ren) Stengel   @return An error code: 0 - success, otherwise - failure
6505fb68f37SKaren (Ren) Stengel 
6515fb68f37SKaren (Ren) Stengel   @ref User
6525fb68f37SKaren (Ren) Stengel **/
6535fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
6541c66c397SJeremy L Thompson   Ceed              ceed_parent_x, ceed_parent_y;
6555fb68f37SKaren (Ren) Stengel   CeedScalar       *y_array = NULL;
6565fb68f37SKaren (Ren) Stengel   CeedScalar const *x_array = NULL;
6575fb68f37SKaren (Ren) Stengel   CeedSize          n_x, n_y;
6581c66c397SJeremy L Thompson   bool              has_valid_array_x = true, has_valid_array_y = true;
6595fb68f37SKaren (Ren) Stengel 
6605fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetLength(y, &n_y));
6615fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetLength(x, &n_x));
6626574a04fSJeremy L Thompson   CeedCheck(n_x == n_y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths");
6636574a04fSJeremy L Thompson   CeedCheck(x != y, y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY");
6645fb68f37SKaren (Ren) Stengel 
6655fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
6666574a04fSJeremy L Thompson   CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND,
6676574a04fSJeremy L Thompson             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6685fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
6696574a04fSJeremy L Thompson   CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND,
6706574a04fSJeremy L Thompson             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6715fb68f37SKaren (Ren) Stengel 
6725fb68f37SKaren (Ren) Stengel   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
6735fb68f37SKaren (Ren) Stengel   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
6746574a04fSJeremy 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");
6755fb68f37SKaren (Ren) Stengel 
676b0976d5aSZach Atkins   // Return early for empty vectors
677b0976d5aSZach Atkins   if (n_y == 0) return CEED_ERROR_SUCCESS;
678b0976d5aSZach Atkins 
6795fb68f37SKaren (Ren) Stengel   // Backend implementation
6805fb68f37SKaren (Ren) Stengel   if (y->AXPBY) {
6815fb68f37SKaren (Ren) Stengel     CeedCall(y->AXPBY(y, alpha, beta, x));
6825fb68f37SKaren (Ren) Stengel     return CEED_ERROR_SUCCESS;
6835fb68f37SKaren (Ren) Stengel   }
6845fb68f37SKaren (Ren) Stengel 
6855fb68f37SKaren (Ren) Stengel   // Default implementation
6865fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
6875fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
6885fb68f37SKaren (Ren) Stengel 
6895fb68f37SKaren (Ren) Stengel   assert(x_array);
6905fb68f37SKaren (Ren) Stengel   assert(y_array);
6915fb68f37SKaren (Ren) Stengel 
692b94338b9SJed Brown   for (CeedSize i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i] + beta * y_array[i];
6935fb68f37SKaren (Ren) Stengel 
6945fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArray(y, &y_array));
6955fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
6965fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
6975fb68f37SKaren (Ren) Stengel }
6985fb68f37SKaren (Ren) Stengel 
6995fb68f37SKaren (Ren) Stengel /**
700*ca94c3ddSJeremy L Thompson   @brief Compute the pointwise multiplication \f$w = x .* y\f$.
7014385fb7fSSebastian Grimberg 
702*ca94c3ddSJeremy L Thompson   Any subset of `x`, `y`, and `w` may be the same `CeedVector`.
7030f7fd0f8Sjeremylt 
704*ca94c3ddSJeremy L Thompson   @param[out] w target `CeedVector` for the product
705*ca94c3ddSJeremy L Thompson   @param[in]  x first `CeedVector` for product
706*ca94c3ddSJeremy L Thompson   @param[in]  y second `CeedVector` for the product
7070f7fd0f8Sjeremylt 
7080f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
7090f7fd0f8Sjeremylt 
7100f7fd0f8Sjeremylt   @ref User
7110f7fd0f8Sjeremylt **/
7120f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
7131c66c397SJeremy L Thompson   Ceed              ceed_parent_w, ceed_parent_x, ceed_parent_y;
71473380422SJeremy L Thompson   CeedScalar       *w_array = NULL;
71573380422SJeremy L Thompson   CeedScalar const *x_array = NULL, *y_array = NULL;
7161f9221feSJeremy L Thompson   CeedSize          n_w, n_x, n_y;
7171c66c397SJeremy L Thompson   bool              has_valid_array_x = true, has_valid_array_y = true;
7180f7fd0f8Sjeremylt 
7192b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(w, &n_w));
7202b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
7212b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &n_y));
7226574a04fSJeremy L Thompson   CeedCheck(n_w == n_x && n_w == n_y, w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths");
7230f7fd0f8Sjeremylt 
7242b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(w->ceed, &ceed_parent_w));
7252b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
7262b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
7276574a04fSJeremy L Thompson   CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, w->ceed, CEED_ERROR_INCOMPATIBLE,
7286574a04fSJeremy L Thompson             "Vectors w, x, and y must be created by the same Ceed context");
7292d04630dSjeremylt 
7302b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
7316574a04fSJeremy L Thompson   CeedCheck(has_valid_array_x, x->ceed, CEED_ERROR_BACKEND,
7326574a04fSJeremy L Thompson             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7332b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
7346574a04fSJeremy L Thompson   CeedCheck(has_valid_array_y, y->ceed, CEED_ERROR_BACKEND,
7356574a04fSJeremy L Thompson             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7369c774eddSJeremy L Thompson 
737b0976d5aSZach Atkins   // Return early for empty vectors
738b0976d5aSZach Atkins   if (n_w == 0) return CEED_ERROR_SUCCESS;
739b0976d5aSZach Atkins 
7400f7fd0f8Sjeremylt   // Backend implementation
741eaf62fffSJeremy L Thompson   if (w->PointwiseMult) {
7422b730f8bSJeremy L Thompson     CeedCall(w->PointwiseMult(w, x, y));
743eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
744eaf62fffSJeremy L Thompson   }
7450f7fd0f8Sjeremylt 
7460f7fd0f8Sjeremylt   // Default implementation
747b0976d5aSZach Atkins   if (x == w || y == w) {
748b0976d5aSZach Atkins     CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array));
749b0976d5aSZach Atkins   } else {
750b0976d5aSZach Atkins     CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array));
751b0976d5aSZach Atkins   }
7520f7fd0f8Sjeremylt   if (x != w) {
7532b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
7540f7fd0f8Sjeremylt   } else {
7550f7fd0f8Sjeremylt     x_array = w_array;
7560f7fd0f8Sjeremylt   }
7570f7fd0f8Sjeremylt   if (y != w && y != x) {
7582b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array));
759c6d796c6SJeremy L Thompson   } else if (y == x) {
7600f7fd0f8Sjeremylt     y_array = x_array;
761b0976d5aSZach Atkins   } else if (y == w) {
762c6d796c6SJeremy L Thompson     y_array = w_array;
7630f7fd0f8Sjeremylt   }
7640f7fd0f8Sjeremylt 
7652b730f8bSJeremy L Thompson   assert(w_array);
7662b730f8bSJeremy L Thompson   assert(x_array);
7672b730f8bSJeremy L Thompson   assert(y_array);
76873380422SJeremy L Thompson 
769b94338b9SJed Brown   for (CeedSize i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i];
7700f7fd0f8Sjeremylt 
7712b730f8bSJeremy L Thompson   if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array));
7722b730f8bSJeremy L Thompson   if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
7732b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(w, &w_array));
7740f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
7750f7fd0f8Sjeremylt }
7760f7fd0f8Sjeremylt 
7770f7fd0f8Sjeremylt /**
778*ca94c3ddSJeremy L Thompson   @brief Take the reciprocal of a `CeedVector`.
779d99fa3c5SJeremy L Thompson 
780*ca94c3ddSJeremy L Thompson   @param[in,out] vec `CeedVector` to take reciprocal
781d99fa3c5SJeremy L Thompson 
782d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
783d99fa3c5SJeremy L Thompson 
784d99fa3c5SJeremy L Thompson   @ref User
785d99fa3c5SJeremy L Thompson **/
786d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) {
7871c66c397SJeremy L Thompson   CeedSize    len;
7881c66c397SJeremy L Thompson   CeedScalar *array;
7899c774eddSJeremy L Thompson   bool        has_valid_array = true;
7902b730f8bSJeremy L Thompson 
7911c66c397SJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
7926574a04fSJeremy L Thompson   CeedCheck(has_valid_array, vec->ceed, CEED_ERROR_BACKEND,
7932b730f8bSJeremy L Thompson             "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray");
7949c774eddSJeremy L Thompson 
795d99fa3c5SJeremy L Thompson   // Check if vector data set
7966574a04fSJeremy L Thompson   CeedCheck(vec->state > 0, vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal");
797d99fa3c5SJeremy L Thompson 
798b0976d5aSZach Atkins   // Return early for empty vector
799b0976d5aSZach Atkins   if (vec->length == 0) return CEED_ERROR_SUCCESS;
800b0976d5aSZach Atkins 
801d99fa3c5SJeremy L Thompson   // Backend impl for GPU, if added
802d99fa3c5SJeremy L Thompson   if (vec->Reciprocal) {
8032b730f8bSJeremy L Thompson     CeedCall(vec->Reciprocal(vec));
804e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
805d99fa3c5SJeremy L Thompson   }
806d99fa3c5SJeremy L Thompson 
8072b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &len));
808567d69a2SJeremy L Thompson   CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array));
809b94338b9SJed Brown   for (CeedSize i = 0; i < len; i++) {
8102b730f8bSJeremy L Thompson     if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i];
8112b730f8bSJeremy L Thompson   }
812d99fa3c5SJeremy L Thompson 
8132b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(vec, &array));
814e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
815d99fa3c5SJeremy L Thompson }
816d99fa3c5SJeremy L Thompson 
817d99fa3c5SJeremy L Thompson /**
818*ca94c3ddSJeremy L Thompson   @brief View a `CeedVector`
8190436c2adSjeremylt 
820acb2c48cSJeremy L Thompson   Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`.
821*ca94c3ddSJeremy L Thompson         Any portion of the provided range that is outside the range of valid indices for the `CeedVector` will be ignored.
822acb2c48cSJeremy L Thompson 
823*ca94c3ddSJeremy L Thompson   @param[in] vec    `CeedVector` to view
824*ca94c3ddSJeremy L Thompson   @param[in] start  Index of first `CeedVector` entry to view
825*ca94c3ddSJeremy L Thompson   @param[in] stop   Index of last `CeedVector` entry to view
826*ca94c3ddSJeremy L Thompson   @param[in] step   Step between `CeedVector` entries to view
827acb2c48cSJeremy L Thompson   @param[in] fp_fmt Printing format
828acb2c48cSJeremy L Thompson   @param[in] stream Filestream to write to
829acb2c48cSJeremy L Thompson 
830acb2c48cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
831acb2c48cSJeremy L Thompson 
832acb2c48cSJeremy L Thompson   @ref User
833acb2c48cSJeremy L Thompson **/
834acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) {
835acb2c48cSJeremy L Thompson   const CeedScalar *x;
836acb2c48cSJeremy L Thompson   char              fmt[1024];
837acb2c48cSJeremy L Thompson 
8386574a04fSJeremy L Thompson   CeedCheck(step != 0, vec->ceed, CEED_ERROR_MINOR, "View range 'step' must be nonzero");
839acb2c48cSJeremy L Thompson 
840acb2c48cSJeremy L Thompson   fprintf(stream, "CeedVector length %ld\n", (long)vec->length);
841acb2c48cSJeremy L Thompson   if (start != 0 || stop != vec->length || step != 1) {
842acb2c48cSJeremy L Thompson     fprintf(stream, "  start: %ld\n  stop:  %ld\n  step:  %" CeedInt_FMT "\n", (long)start, (long)stop, step);
843acb2c48cSJeremy L Thompson   }
844acb2c48cSJeremy L Thompson   if (start > vec->length) start = vec->length;
845acb2c48cSJeremy L Thompson   if (stop > vec->length) stop = vec->length;
846acb2c48cSJeremy L Thompson 
847acb2c48cSJeremy L Thompson   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
848acb2c48cSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x));
849b94338b9SJed Brown   for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]);
850acb2c48cSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &x));
851acb2c48cSJeremy L Thompson   if (stop != vec->length) fprintf(stream, "  ...\n");
852acb2c48cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
853acb2c48cSJeremy L Thompson }
854acb2c48cSJeremy L Thompson 
855acb2c48cSJeremy L Thompson /**
856*ca94c3ddSJeremy L Thompson   @brief View a `CeedVector`
857acb2c48cSJeremy L Thompson 
858*ca94c3ddSJeremy L Thompson   @param[in] vec    `CeedVector` to view
859d1d35e2fSjeremylt   @param[in] fp_fmt Printing format
8600a0da059Sjeremylt   @param[in] stream Filestream to write to
8610a0da059Sjeremylt 
8620436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
8630436c2adSjeremylt 
8647a982d89SJeremy L. Thompson   @ref User
8650436c2adSjeremylt **/
866d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
867acb2c48cSJeremy L Thompson   CeedCall(CeedVectorViewRange(vec, 0, vec->length, 1, fp_fmt, stream));
868e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8690436c2adSjeremylt }
8700436c2adSjeremylt 
8710436c2adSjeremylt /**
872*ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedVector`
873b7c9bbdaSJeremy L Thompson 
874*ca94c3ddSJeremy L Thompson   @param[in]  vec  `CeedVector` to retrieve state
875*ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
876b7c9bbdaSJeremy L Thompson 
877b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
878b7c9bbdaSJeremy L Thompson 
879b7c9bbdaSJeremy L Thompson   @ref Advanced
880b7c9bbdaSJeremy L Thompson **/
881b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
882b7c9bbdaSJeremy L Thompson   *ceed = vec->ceed;
883b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
884b7c9bbdaSJeremy L Thompson }
885b7c9bbdaSJeremy L Thompson 
886b7c9bbdaSJeremy L Thompson /**
887*ca94c3ddSJeremy L Thompson   @brief Get the length of a `CeedVector`
8880436c2adSjeremylt 
889*ca94c3ddSJeremy L Thompson   @param[in]  vec    `CeedVector` to retrieve length
8907a982d89SJeremy L. Thompson   @param[out] length Variable to store length
8910436c2adSjeremylt 
8920436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
8930436c2adSjeremylt 
8947a982d89SJeremy L. Thompson   @ref User
8950436c2adSjeremylt **/
8961f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) {
8977a982d89SJeremy L. Thompson   *length = vec->length;
898e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8990436c2adSjeremylt }
9000436c2adSjeremylt 
9010436c2adSjeremylt /**
902*ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedVector`
9030436c2adSjeremylt 
904*ca94c3ddSJeremy L Thompson   @param[in,out] vec `CeedVector` to destroy
9050436c2adSjeremylt 
9060436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9070436c2adSjeremylt 
9087a982d89SJeremy L. Thompson   @ref User
9090436c2adSjeremylt **/
9100436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) {
9117425e127SJeremy L Thompson   if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) {
912ad6481ceSJeremy L Thompson     *vec = NULL;
913ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
914ad6481ceSJeremy L Thompson   }
9156574a04fSJeremy L Thompson   CeedCheck((*vec)->state % 2 == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use");
9166574a04fSJeremy L Thompson   CeedCheck((*vec)->num_readers == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access");
9170436c2adSjeremylt 
9182b730f8bSJeremy L Thompson   if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec));
9192b730f8bSJeremy L Thompson 
9202b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*vec)->ceed));
9212b730f8bSJeremy L Thompson   CeedCall(CeedFree(vec));
922e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9230436c2adSjeremylt }
9240436c2adSjeremylt 
9250436c2adSjeremylt /// @}
926