xref: /libCEED/interface/ceed-vector.c (revision 1203703b5dc87b4acbe66c9a27384ca8ad07798d)
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 
28ca94c3ddSJeremy 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 /**
43ca94c3ddSJeremy L Thompson   @brief Check for valid data in a `CeedVector`
449c774eddSJeremy L Thompson 
45ca94c3ddSJeremy 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*1203703bSJeremy L Thompson   CeedSize length;
54*1203703bSJeremy L Thompson 
55ca94c3ddSJeremy L Thompson   CeedCheck(vec->HasValidArray, vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorHasValidArray");
56*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
57*1203703bSJeremy L Thompson   if (length == 0) {
58b0976d5aSZach Atkins     *has_valid_array = true;
59b0976d5aSZach Atkins     return CEED_ERROR_SUCCESS;
60b0976d5aSZach Atkins   }
612b730f8bSJeremy L Thompson   CeedCall(vec->HasValidArray(vec, has_valid_array));
629c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
639c774eddSJeremy L Thompson }
649c774eddSJeremy L Thompson 
659c774eddSJeremy L Thompson /**
66ca94c3ddSJeremy L Thompson   @brief Check for borrowed array of a specific @ref CeedMemType in a `CeedVector`
679c774eddSJeremy L Thompson 
68ca94c3ddSJeremy L Thompson   @param[in]  vec                        `CeedVector` to check
69ea61e9acSJeremy L Thompson   @param[in]  mem_type                   Memory type to check
709c774eddSJeremy L Thompson   @param[out] has_borrowed_array_of_type Variable to store result
719c774eddSJeremy L Thompson 
729c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
739c774eddSJeremy L Thompson 
749c774eddSJeremy L Thompson   @ref Backend
759c774eddSJeremy L Thompson **/
762b730f8bSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) {
77*1203703bSJeremy L Thompson   Ceed ceed;
78*1203703bSJeremy L Thompson 
79*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
80*1203703bSJeremy L Thompson   CeedCheck(vec->HasBorrowedArrayOfType, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorHasBorrowedArrayOfType");
812b730f8bSJeremy L Thompson   CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type));
829c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
839c774eddSJeremy L Thompson }
849c774eddSJeremy L Thompson 
859c774eddSJeremy L Thompson /**
86ca94c3ddSJeremy L Thompson   @brief Get the state of a `CeedVector`
877a982d89SJeremy L. Thompson 
88ca94c3ddSJeremy L Thompson   @param[in]  vec    `CeedVector` to retrieve state
897a982d89SJeremy L. Thompson   @param[out] state  Variable to store state
907a982d89SJeremy L. Thompson 
917a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
927a982d89SJeremy L. Thompson 
937a982d89SJeremy L. Thompson   @ref Backend
947a982d89SJeremy L. Thompson **/
957a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) {
967a982d89SJeremy L. Thompson   *state = vec->state;
97e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
987a982d89SJeremy L. Thompson }
997a982d89SJeremy L. Thompson 
1007a982d89SJeremy L. Thompson /**
101ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedVector`
1027a982d89SJeremy L. Thompson 
103ca94c3ddSJeremy L Thompson   @param[in]  vec  `CeedVector` to retrieve state
1047a982d89SJeremy L. Thompson   @param[out] data Variable to store data
1057a982d89SJeremy L. Thompson 
1067a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1077a982d89SJeremy L. Thompson 
1087a982d89SJeremy L. Thompson   @ref Backend
1097a982d89SJeremy L. Thompson **/
110777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) {
111777ff853SJeremy L Thompson   *(void **)data = vec->data;
112e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1137a982d89SJeremy L. Thompson }
1147a982d89SJeremy L. Thompson 
1157a982d89SJeremy L. Thompson /**
116ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedVector`
1177a982d89SJeremy L. Thompson 
118ca94c3ddSJeremy L Thompson   @param[in,out] vec  `CeedVector` to retrieve state
119ea61e9acSJeremy L Thompson   @param[in]     data Data to set
1207a982d89SJeremy L. Thompson 
1217a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1227a982d89SJeremy L. Thompson 
1237a982d89SJeremy L. Thompson   @ref Backend
1247a982d89SJeremy L. Thompson **/
125777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) {
126777ff853SJeremy L Thompson   vec->data = data;
127e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1287a982d89SJeremy L. Thompson }
1297a982d89SJeremy L. Thompson 
13034359f16Sjeremylt /**
131ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedVector`
13234359f16Sjeremylt 
133ca94c3ddSJeremy L Thompson   @param[in,out] vec `CeedVector` to increment the reference counter
13434359f16Sjeremylt 
13534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
13634359f16Sjeremylt 
13734359f16Sjeremylt   @ref Backend
13834359f16Sjeremylt **/
1399560d06aSjeremylt int CeedVectorReference(CeedVector vec) {
14034359f16Sjeremylt   vec->ref_count++;
14134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
14234359f16Sjeremylt }
14334359f16Sjeremylt 
1447a982d89SJeremy L. Thompson /// @}
1457a982d89SJeremy L. Thompson 
1467a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1477a982d89SJeremy L. Thompson /// CeedVector Public API
1487a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1497a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser
1500436c2adSjeremylt /// @{
1510436c2adSjeremylt 
1520436c2adSjeremylt /**
153ca94c3ddSJeremy L Thompson   @brief Create a `CeedVector` of the specified length (does not allocate memory)
1540436c2adSjeremylt 
155ca94c3ddSJeremy L Thompson   @param[in]  ceed   `Ceed` object used to create the `CeedVector`
156ea61e9acSJeremy L Thompson   @param[in]  length Length of vector
157ca94c3ddSJeremy L Thompson   @param[out] vec    Address of the variable where the newly created `CeedVector` will be stored
1580436c2adSjeremylt 
1590436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
1600436c2adSjeremylt 
1617a982d89SJeremy L. Thompson   @ref User
1620436c2adSjeremylt **/
1631f9221feSJeremy L Thompson int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) {
1640436c2adSjeremylt   if (!ceed->VectorCreate) {
1650436c2adSjeremylt     Ceed delegate;
1666574a04fSJeremy L Thompson 
1672b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector"));
1686574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorCreate");
1692b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(delegate, length, vec));
170e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
1710436c2adSjeremylt   }
1720436c2adSjeremylt 
1732b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, vec));
174db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*vec)->ceed));
175d1d35e2fSjeremylt   (*vec)->ref_count = 1;
1760436c2adSjeremylt   (*vec)->length    = length;
1770436c2adSjeremylt   (*vec)->state     = 0;
1782b730f8bSJeremy L Thompson   CeedCall(ceed->VectorCreate(length, *vec));
179e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1800436c2adSjeremylt }
1810436c2adSjeremylt 
1820436c2adSjeremylt /**
183ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedVector`.
1844385fb7fSSebastian Grimberg 
185ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedVectorDestroy().
186512bb800SJeremy L Thompson 
187ca94c3ddSJeremy 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`.
188ca94c3ddSJeremy L Thompson         This `CeedVector` will be destroyed if `*vec_copy` is the only reference to this `CeedVector`.
1899560d06aSjeremylt 
190ca94c3ddSJeremy L Thompson   @param[in]     vec      `CeedVector` to copy reference to
191ea61e9acSJeremy L Thompson   @param[in,out] vec_copy Variable to store copied reference
1929560d06aSjeremylt 
1939560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
1949560d06aSjeremylt 
1959560d06aSjeremylt   @ref User
1969560d06aSjeremylt **/
1979560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) {
198393ac2cdSJeremy L Thompson   if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorReference(vec));
1992b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(vec_copy));
2009560d06aSjeremylt   *vec_copy = vec;
2019560d06aSjeremylt   return CEED_ERROR_SUCCESS;
2029560d06aSjeremylt }
2039560d06aSjeremylt 
2049560d06aSjeremylt /**
205ca94c3ddSJeremy L Thompson   @brief Copy a `CeedVector` into a different `CeedVector`.
2064385fb7fSSebastian Grimberg 
207ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedVectorDestroy().
2084385fb7fSSebastian Grimberg 
209ca94c3ddSJeremy L Thompson   Note: If `*vec_copy` is non-`NULL`, then it is assumed that `*vec_copy` is a pointer to a `CeedVector`.
210ca94c3ddSJeremy L Thompson         This `CeedVector` will be destroyed if `*vec_copy` is the only reference to this `CeedVector`.
2115fb68f37SKaren (Ren) Stengel 
212ca94c3ddSJeremy L Thompson   @param[in]     vec      `CeedVector` to copy
213ca94c3ddSJeremy L Thompson   @param[in,out] vec_copy Variable to store copied `CeedVector` to
2145fb68f37SKaren (Ren) Stengel 
2155fb68f37SKaren (Ren) Stengel   @return An error code: 0 - success, otherwise - failure
2165fb68f37SKaren (Ren) Stengel 
2175fb68f37SKaren (Ren) Stengel   @ref User
2185fb68f37SKaren (Ren) Stengel **/
2195fb68f37SKaren (Ren) Stengel int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) {
2205fb68f37SKaren (Ren) Stengel   Ceed        ceed;
2215fb68f37SKaren (Ren) Stengel   CeedMemType mem_type, mem_type_copy;
2225fb68f37SKaren (Ren) Stengel   CeedScalar *array;
2235fb68f37SKaren (Ren) Stengel 
2245fb68f37SKaren (Ren) Stengel   // Get the preferred memory type
2255fb68f37SKaren (Ren) Stengel   CeedVectorGetCeed(vec, &ceed);
2265fb68f37SKaren (Ren) Stengel   CeedGetPreferredMemType(ceed, &mem_type);
2275fb68f37SKaren (Ren) Stengel 
2285fb68f37SKaren (Ren) Stengel   // Get the preferred memory type
2295fb68f37SKaren (Ren) Stengel   CeedVectorGetCeed(vec_copy, &ceed);
2305fb68f37SKaren (Ren) Stengel   CeedGetPreferredMemType(ceed, &mem_type_copy);
2315fb68f37SKaren (Ren) Stengel 
2325fb68f37SKaren (Ren) Stengel   // Check that both have same memory type
2335fb68f37SKaren (Ren) Stengel   if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST;
2345fb68f37SKaren (Ren) Stengel 
2355fb68f37SKaren (Ren) Stengel   // Copy the values from vec to vec_copy
2365fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArray(vec, mem_type, &array));
2375fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array));
2385fb68f37SKaren (Ren) Stengel 
2395fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArray(vec, &array));
2405fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
2415fb68f37SKaren (Ren) Stengel }
2425fb68f37SKaren (Ren) Stengel 
2435fb68f37SKaren (Ren) Stengel /**
244ca94c3ddSJeremy L Thompson   @brief Set the array used by a `CeedVector`, freeing any previously allocated array if applicable.
2454385fb7fSSebastian Grimberg 
246ca94c3ddSJeremy L Thompson   The backend may copy values to a different @ref CeedMemType, such as during @ref CeedOperatorApply().
2476a6c615bSJeremy L Thompson   See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray().
2480436c2adSjeremylt 
249ca94c3ddSJeremy L Thompson   @param[in,out] vec       `CeedVector`
250ea61e9acSJeremy L Thompson   @param[in]     mem_type  Memory type of the array being passed
251ea61e9acSJeremy L Thompson   @param[in]     copy_mode Copy mode for the array
252ca94c3ddSJeremy L Thompson   @param[in]     array     Array to be used, or `NULL` with @ref CEED_COPY_VALUES to have the library allocate
2530436c2adSjeremylt 
2540436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2550436c2adSjeremylt 
2567a982d89SJeremy L. Thompson   @ref User
2570436c2adSjeremylt **/
2582b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) {
259*1203703bSJeremy L Thompson   CeedSize length;
260*1203703bSJeremy L Thompson   Ceed     ceed;
2610436c2adSjeremylt 
262*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
263*1203703bSJeremy L Thompson 
264*1203703bSJeremy L Thompson   CeedCheck(vec->SetArray, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray");
265*1203703bSJeremy L Thompson   CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
266*1203703bSJeremy L Thompson   CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
267*1203703bSJeremy L Thompson 
268*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
269*1203703bSJeremy L Thompson   if (length > 0) CeedCall(vec->SetArray(vec, mem_type, copy_mode, array));
2700436c2adSjeremylt   vec->state += 2;
271e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2720436c2adSjeremylt }
2730436c2adSjeremylt 
2740436c2adSjeremylt /**
275ca94c3ddSJeremy L Thompson   @brief Set the `CeedVector` to a constant value
2760436c2adSjeremylt 
277ca94c3ddSJeremy L Thompson   @param[in,out] vec   `CeedVector`
2780436c2adSjeremylt   @param[in]     value Value to be used
2790436c2adSjeremylt 
2800436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2810436c2adSjeremylt 
2827a982d89SJeremy L. Thompson   @ref User
2830436c2adSjeremylt **/
2840436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) {
285*1203703bSJeremy L Thompson   Ceed ceed;
286*1203703bSJeremy L Thompson 
287*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
288*1203703bSJeremy L Thompson   CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
289*1203703bSJeremy L Thompson   CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
2900436c2adSjeremylt 
2910436c2adSjeremylt   if (vec->SetValue) {
2922b730f8bSJeremy L Thompson     CeedCall(vec->SetValue(vec, value));
2930436c2adSjeremylt   } else {
294*1203703bSJeremy L Thompson     CeedSize    length;
2950436c2adSjeremylt     CeedScalar *array;
296*1203703bSJeremy L Thompson 
2972b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array));
298*1203703bSJeremy L Thompson     CeedCall(CeedVectorGetLength(vec, &length));
299*1203703bSJeremy L Thompson     for (CeedSize i = 0; i < length; i++) array[i] = value;
3002b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(vec, &array));
3010436c2adSjeremylt   }
3020436c2adSjeremylt   vec->state += 2;
303e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3040436c2adSjeremylt }
3050436c2adSjeremylt 
3060436c2adSjeremylt /**
307ca94c3ddSJeremy L Thompson   @brief Sync the `CeedVector` to a specified `mem_type`.
3084385fb7fSSebastian Grimberg 
309ea61e9acSJeremy L Thompson   This function is used to force synchronization of arrays set with @ref CeedVectorSetArray().
310ca94c3ddSJeremy L Thompson   If the requested `mem_type` is already synchronized, this function results in a no-op.
3110436c2adSjeremylt 
312ca94c3ddSJeremy L Thompson   @param[in,out] vec      `CeedVector`
313ca94c3ddSJeremy L Thompson   @param[in]     mem_type @ref CeedMemType to be synced
3140436c2adSjeremylt 
3150436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3160436c2adSjeremylt 
3177a982d89SJeremy L. Thompson   @ref User
3180436c2adSjeremylt **/
319d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) {
320*1203703bSJeremy L Thompson   CeedSize length;
321*1203703bSJeremy L Thompson 
3226574a04fSJeremy L Thompson   CeedCheck(vec->state % 2 == 0, vec->ceed, CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use");
3230436c2adSjeremylt 
324b0976d5aSZach Atkins   // Don't sync empty array
325*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
326*1203703bSJeremy L Thompson   if (length == 0) return CEED_ERROR_SUCCESS;
327b0976d5aSZach Atkins 
3280436c2adSjeremylt   if (vec->SyncArray) {
3292b730f8bSJeremy L Thompson     CeedCall(vec->SyncArray(vec, mem_type));
3300436c2adSjeremylt   } else {
3310436c2adSjeremylt     const CeedScalar *array;
332*1203703bSJeremy L Thompson 
3332b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array));
3342b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(vec, &array));
3350436c2adSjeremylt   }
336e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3370436c2adSjeremylt }
3380436c2adSjeremylt 
3390436c2adSjeremylt /**
340ca94c3ddSJeremy 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`.
3414385fb7fSSebastian Grimberg 
3429c774eddSJeremy L Thompson   The caller is responsible for managing and freeing the array.
343ea61e9acSJeremy L Thompson   This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type.
3446a6c615bSJeremy L Thompson 
345ca94c3ddSJeremy L Thompson   @param[in,out] vec      `CeedVector`
346ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to take the array.
347ea61e9acSJeremy L Thompson                             If the backend uses a different memory type, this will perform a copy.
348ca94c3ddSJeremy L Thompson   @param[out]    array    Array on memory type `mem_type`, or `NULL` if array pointer is not required
3496a6c615bSJeremy L Thompson 
3506a6c615bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3516a6c615bSJeremy L Thompson 
3526a6c615bSJeremy L Thompson   @ref User
3536a6c615bSJeremy L Thompson **/
3542b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
355*1203703bSJeremy L Thompson   CeedSize    length;
3561c66c397SJeremy L Thompson   CeedScalar *temp_array = NULL;
357*1203703bSJeremy L Thompson   Ceed        ceed;
3581c66c397SJeremy L Thompson 
359*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
360*1203703bSJeremy L Thompson   CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use");
361*1203703bSJeremy L Thompson   CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access");
3626a6c615bSJeremy L Thompson 
363*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
364*1203703bSJeremy L Thompson   if (length > 0) {
365*1203703bSJeremy L Thompson     bool has_borrowed_array_of_type = true, has_valid_array = true;
366*1203703bSJeremy L Thompson 
3672b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type));
368*1203703bSJeremy L Thompson     CeedCheck(has_borrowed_array_of_type, ceed, CEED_ERROR_BACKEND, "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray",
369*1203703bSJeremy L Thompson               CeedMemTypes[mem_type]);
3709c774eddSJeremy L Thompson 
3712b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
372*1203703bSJeremy L Thompson     CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND,
3732b730f8bSJeremy L Thompson               "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray");
3749c774eddSJeremy L Thompson 
3752b730f8bSJeremy L Thompson     CeedCall(vec->TakeArray(vec, mem_type, &temp_array));
37650c643e1SJed Brown   }
377d1d35e2fSjeremylt   if (array) (*array) = temp_array;
378e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3796a6c615bSJeremy L Thompson }
3806a6c615bSJeremy L Thompson 
3816a6c615bSJeremy L Thompson /**
382ca94c3ddSJeremy L Thompson   @brief Get read/write access to a `CeedVector` via the specified memory type.
3834385fb7fSSebastian Grimberg 
384b3cf021fSjeremylt   Restore access with @ref CeedVectorRestoreArray().
3850436c2adSjeremylt 
386ca94c3ddSJeremy L Thompson   @param[in,out] vec      `CeedVector` to access
387ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to access the array.
388ea61e9acSJeremy L Thompson                             If the backend uses a different memory type, this will perform a copy.
389ca94c3ddSJeremy L Thompson   @param[out]    array    Array on memory type `mem_type`
3900436c2adSjeremylt 
391ca94c3ddSJeremy L Thompson   @note The @ref CeedVectorGetArray() and @ref CeedVectorRestoreArray() functions provide access to array pointers in the desired memory space.
392ca94c3ddSJeremy L Thompson         Pairing get/restore allows the `CeedVector` to track access, thus knowing if norms or other operations may need to be recomputed.
3930436c2adSjeremylt 
3940436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3950436c2adSjeremylt 
3967a982d89SJeremy L. Thompson   @ref User
3970436c2adSjeremylt **/
3982b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
399*1203703bSJeremy L Thompson   CeedSize length;
400*1203703bSJeremy L Thompson   Ceed     ceed;
4010436c2adSjeremylt 
402*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
403*1203703bSJeremy L Thompson   CeedCheck(vec->GetArray, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray");
404*1203703bSJeremy L Thompson   CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
405*1203703bSJeremy L Thompson   CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
406*1203703bSJeremy L Thompson 
407*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
408*1203703bSJeremy L Thompson   if (length > 0) {
4099c774eddSJeremy L Thompson     bool has_valid_array = true;
410b0976d5aSZach Atkins 
4112b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
412*1203703bSJeremy L Thompson     CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND,
4132b730f8bSJeremy L Thompson               "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
4149c774eddSJeremy L Thompson 
4152b730f8bSJeremy L Thompson     CeedCall(vec->GetArray(vec, mem_type, array));
416b0976d5aSZach Atkins   } else {
417b0976d5aSZach Atkins     *array = NULL;
418b0976d5aSZach Atkins   }
41928bfd0b7SJeremy L Thompson   vec->state++;
420e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4210436c2adSjeremylt }
4220436c2adSjeremylt 
4230436c2adSjeremylt /**
424ca94c3ddSJeremy L Thompson   @brief Get read-only access to a `CeedVector` via the specified memory type.
4254385fb7fSSebastian Grimberg 
426b3cf021fSjeremylt   Restore access with @ref CeedVectorRestoreArrayRead().
4270436c2adSjeremylt 
428ca94c3ddSJeremy L Thompson   @param[in]  vec      `CeedVector` to access
429ca94c3ddSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the array.
430ca94c3ddSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy (possibly cached).
431ca94c3ddSJeremy L Thompson   @param[out] array    Array on memory type `mem_type`
4320436c2adSjeremylt 
4330436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4340436c2adSjeremylt 
4357a982d89SJeremy L. Thompson   @ref User
4360436c2adSjeremylt **/
4372b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
438*1203703bSJeremy L Thompson   CeedSize length;
439*1203703bSJeremy L Thompson   Ceed     ceed;
4400436c2adSjeremylt 
441*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
442*1203703bSJeremy L Thompson   CeedCheck(vec->GetArrayRead, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead");
443*1203703bSJeremy L Thompson   CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use");
444*1203703bSJeremy L Thompson 
445*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
446*1203703bSJeremy L Thompson   if (length > 0) {
4479c774eddSJeremy L Thompson     bool has_valid_array = true;
448b0976d5aSZach Atkins 
4492b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
450*1203703bSJeremy L Thompson     CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND,
4512b730f8bSJeremy L Thompson               "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
4529c774eddSJeremy L Thompson 
4532b730f8bSJeremy L Thompson     CeedCall(vec->GetArrayRead(vec, mem_type, array));
45450c643e1SJed Brown   } else {
45550c643e1SJed Brown     *array = NULL;
45650c643e1SJed Brown   }
457d1d35e2fSjeremylt   vec->num_readers++;
458e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4590436c2adSjeremylt }
4600436c2adSjeremylt 
4610436c2adSjeremylt /**
462ca94c3ddSJeremy L Thompson   @brief Get write access to a `CeedVector` via the specified memory type.
4634385fb7fSSebastian Grimberg 
464ea61e9acSJeremy L Thompson   Restore access with @ref CeedVectorRestoreArray().
465ea61e9acSJeremy L Thompson   All old values should be assumed to be invalid.
4669c774eddSJeremy L Thompson 
467ca94c3ddSJeremy L Thompson   @param[in,out] vec      `CeedVector` to access
468ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to access the array.
469ca94c3ddSJeremy L Thompson   @param[out]    array    Array on memory type `mem_type`
4709c774eddSJeremy L Thompson 
4719c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4729c774eddSJeremy L Thompson 
4739c774eddSJeremy L Thompson   @ref User
4749c774eddSJeremy L Thompson **/
4752b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
476*1203703bSJeremy L Thompson   CeedSize length;
477*1203703bSJeremy L Thompson   Ceed     ceed;
4789c774eddSJeremy L Thompson 
479*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
480*1203703bSJeremy L Thompson   CeedCheck(vec->GetArrayWrite, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorGetArrayWrite");
481*1203703bSJeremy L Thompson   CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
482*1203703bSJeremy L Thompson   CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
483*1203703bSJeremy L Thompson 
484*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
485*1203703bSJeremy L Thompson   if (length > 0) {
4862b730f8bSJeremy L Thompson     CeedCall(vec->GetArrayWrite(vec, mem_type, array));
487b0976d5aSZach Atkins   } else {
488b0976d5aSZach Atkins     *array = NULL;
489b0976d5aSZach Atkins   }
49028bfd0b7SJeremy L Thompson   vec->state++;
4919c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
4929c774eddSJeremy L Thompson }
4939c774eddSJeremy L Thompson 
4949c774eddSJeremy L Thompson /**
495ea61e9acSJeremy L Thompson   @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite()
4960436c2adSjeremylt 
497ca94c3ddSJeremy L Thompson   @param[in,out] vec   `CeedVector` to restore
498ea61e9acSJeremy L Thompson   @param[in,out] array Array of vector data
4990436c2adSjeremylt 
5000436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5010436c2adSjeremylt 
5027a982d89SJeremy L. Thompson   @ref User
5030436c2adSjeremylt **/
5040436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
505*1203703bSJeremy L Thompson   CeedSize length;
506*1203703bSJeremy L Thompson   Ceed     ceed;
507*1203703bSJeremy L Thompson 
508*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
509*1203703bSJeremy L Thompson   CeedCheck(vec->state % 2 == 1, ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted");
510*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
511*1203703bSJeremy L Thompson   if (length > 0 && vec->RestoreArray) CeedCall(vec->RestoreArray(vec));
5120436c2adSjeremylt   *array = NULL;
51328bfd0b7SJeremy L Thompson   vec->state++;
514e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5150436c2adSjeremylt }
5160436c2adSjeremylt 
5170436c2adSjeremylt /**
518b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArrayRead()
5190436c2adSjeremylt 
520ca94c3ddSJeremy L Thompson   @param[in]     vec   `CeedVector` to restore
521ea61e9acSJeremy L Thompson   @param[in,out] array Array of vector data
5220436c2adSjeremylt 
5230436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5240436c2adSjeremylt 
5257a982d89SJeremy L. Thompson   @ref User
5260436c2adSjeremylt **/
5270436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) {
528*1203703bSJeremy L Thompson   CeedSize length;
529*1203703bSJeremy L Thompson   Ceed     ceed;
530*1203703bSJeremy L Thompson 
531*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
532*1203703bSJeremy L Thompson   CeedCheck(vec->num_readers > 0, ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted");
5339c774eddSJeremy L Thompson 
53475a19770SJeremy L Thompson   vec->num_readers--;
535*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
536*1203703bSJeremy L Thompson   if (length > 0 && vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec));
5370436c2adSjeremylt   *array = NULL;
538e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5390436c2adSjeremylt }
5400436c2adSjeremylt 
5410436c2adSjeremylt /**
542ca94c3ddSJeremy L Thompson   @brief Get the norm of a `CeedVector`.
543171f8ca9Sjeremylt 
544ca94c3ddSJeremy L Thompson   Note: This operation is local to the `CeedVector`.
545ca94c3ddSJeremy 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.
546547d9b97Sjeremylt 
547ca94c3ddSJeremy L Thompson   @param[in]  vec       `CeedVector` to retrieve maximum value
548ea61e9acSJeremy L Thompson   @param[in]  norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
549547d9b97Sjeremylt   @param[out] norm      Variable to store norm value
550547d9b97Sjeremylt 
551547d9b97Sjeremylt   @return An error code: 0 - success, otherwise - failure
552547d9b97Sjeremylt 
5537a982d89SJeremy L. Thompson   @ref User
554547d9b97Sjeremylt **/
555d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
5569c774eddSJeremy L Thompson   bool     has_valid_array = true;
557*1203703bSJeremy L Thompson   CeedSize length;
558*1203703bSJeremy L Thompson   Ceed     ceed;
559*1203703bSJeremy L Thompson 
560*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
5612b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
562*1203703bSJeremy L Thompson   CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND,
5632b730f8bSJeremy L Thompson             "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray");
5649c774eddSJeremy L Thompson 
565*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
566*1203703bSJeremy L Thompson   if (length == 0) {
567b0976d5aSZach Atkins     *norm = 0;
568b0976d5aSZach Atkins     return CEED_ERROR_SUCCESS;
569b0976d5aSZach Atkins   }
570b0976d5aSZach Atkins 
571547d9b97Sjeremylt   // Backend impl for GPU, if added
572547d9b97Sjeremylt   if (vec->Norm) {
5732b730f8bSJeremy L Thompson     CeedCall(vec->Norm(vec, norm_type, norm));
574e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
575547d9b97Sjeremylt   }
576547d9b97Sjeremylt 
577547d9b97Sjeremylt   const CeedScalar *array;
5782b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array));
579b0976d5aSZach Atkins   assert(array);
580547d9b97Sjeremylt 
581547d9b97Sjeremylt   *norm = 0.;
582d1d35e2fSjeremylt   switch (norm_type) {
583547d9b97Sjeremylt     case CEED_NORM_1:
584*1203703bSJeremy L Thompson       for (CeedSize i = 0; i < length; i++) {
585547d9b97Sjeremylt         *norm += fabs(array[i]);
586547d9b97Sjeremylt       }
587547d9b97Sjeremylt       break;
588547d9b97Sjeremylt     case CEED_NORM_2:
589*1203703bSJeremy L Thompson       for (CeedSize i = 0; i < length; i++) {
590547d9b97Sjeremylt         *norm += fabs(array[i]) * fabs(array[i]);
591547d9b97Sjeremylt       }
592547d9b97Sjeremylt       break;
593547d9b97Sjeremylt     case CEED_NORM_MAX:
594*1203703bSJeremy L Thompson       for (CeedSize i = 0; i < length; i++) {
595d1d35e2fSjeremylt         const CeedScalar abs_v_i = fabs(array[i]);
596d1d35e2fSjeremylt         *norm                    = *norm > abs_v_i ? *norm : abs_v_i;
597547d9b97Sjeremylt       }
598547d9b97Sjeremylt   }
5992b730f8bSJeremy L Thompson   if (norm_type == CEED_NORM_2) *norm = sqrt(*norm);
600547d9b97Sjeremylt 
6012b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &array));
602e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
603547d9b97Sjeremylt }
604547d9b97Sjeremylt 
605547d9b97Sjeremylt /**
606ca94c3ddSJeremy L Thompson   @brief Compute `x = alpha x`
607e0dd3b27Sjeremylt 
608ca94c3ddSJeremy L Thompson   @param[in,out] x     `CeedVector` for scaling
60996b902e2Sjeremylt   @param[in]     alpha scaling factor
610e0dd3b27Sjeremylt 
611e0dd3b27Sjeremylt   @return An error code: 0 - success, otherwise - failure
612e0dd3b27Sjeremylt 
613e0dd3b27Sjeremylt   @ref User
614e0dd3b27Sjeremylt **/
615e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) {
6169c774eddSJeremy L Thompson   bool        has_valid_array = true;
617*1203703bSJeremy L Thompson   CeedSize    length;
618*1203703bSJeremy L Thompson   CeedScalar *x_array = NULL;
619*1203703bSJeremy L Thompson   Ceed        ceed;
6201c66c397SJeremy L Thompson 
621*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(x, &ceed));
6222b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array));
623*1203703bSJeremy L Thompson   CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND,
6242b730f8bSJeremy L Thompson             "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray");
6259c774eddSJeremy L Thompson 
626b0976d5aSZach Atkins   // Return early for empty vector
627*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &length));
628*1203703bSJeremy L Thompson   if (length == 0) return CEED_ERROR_SUCCESS;
629b0976d5aSZach Atkins 
630e0dd3b27Sjeremylt   // Backend implementation
6312b730f8bSJeremy L Thompson   if (x->Scale) return x->Scale(x, alpha);
632e0dd3b27Sjeremylt 
633e0dd3b27Sjeremylt   // Default implementation
634567d69a2SJeremy L Thompson   CeedCall(CeedVectorGetArray(x, CEED_MEM_HOST, &x_array));
635b0976d5aSZach Atkins   assert(x_array);
636*1203703bSJeremy L Thompson   for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha;
6372b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(x, &x_array));
638e0dd3b27Sjeremylt   return CEED_ERROR_SUCCESS;
639e0dd3b27Sjeremylt }
640e0dd3b27Sjeremylt 
641e0dd3b27Sjeremylt /**
642ca94c3ddSJeremy L Thompson   @brief Compute `y = alpha x + y`
6430f7fd0f8Sjeremylt 
644ca94c3ddSJeremy L Thompson   @param[in,out] y     target `CeedVector` for sum
64596b902e2Sjeremylt   @param[in]     alpha scaling factor
646ca94c3ddSJeremy L Thompson   @param[in]     x     second `CeedVector`, must be different than ``y`
6470f7fd0f8Sjeremylt 
6480f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
6490f7fd0f8Sjeremylt 
6500f7fd0f8Sjeremylt   @ref User
6510f7fd0f8Sjeremylt **/
6520f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
653*1203703bSJeremy L Thompson   bool              has_valid_array_x = true, has_valid_array_y = true;
654*1203703bSJeremy L Thompson   CeedSize          length_x, length_y;
65573380422SJeremy L Thompson   CeedScalar       *y_array = NULL;
65673380422SJeremy L Thompson   CeedScalar const *x_array = NULL;
657*1203703bSJeremy L Thompson   Ceed              ceed, ceed_parent_x, ceed_parent_y;
6580f7fd0f8Sjeremylt 
659*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(y, &ceed));
660*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &length_y));
661*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &length_x));
662*1203703bSJeremy L Thompson   CeedCheck(length_x == length_y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths");
663*1203703bSJeremy L Thompson   CeedCheck(x != y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY");
6640f7fd0f8Sjeremylt 
6652b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
666*1203703bSJeremy L Thompson   CeedCheck(has_valid_array_x, ceed, CEED_ERROR_BACKEND,
6676574a04fSJeremy L Thompson             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6682b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
669*1203703bSJeremy L Thompson   CeedCheck(has_valid_array_y, ceed, CEED_ERROR_BACKEND,
6706574a04fSJeremy L Thompson             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6719c774eddSJeremy L Thompson 
6722b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
6732b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
674*1203703bSJeremy L Thompson   CeedCheck(ceed_parent_x == ceed_parent_y, ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context");
6752d04630dSjeremylt 
676b0976d5aSZach Atkins   // Return early for empty vectors
677*1203703bSJeremy L Thompson   if (length_y == 0) return CEED_ERROR_SUCCESS;
678b0976d5aSZach Atkins 
6790f7fd0f8Sjeremylt   // Backend implementation
680eaf62fffSJeremy L Thompson   if (y->AXPY) {
6812b730f8bSJeremy L Thompson     CeedCall(y->AXPY(y, alpha, x));
682eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
683eaf62fffSJeremy L Thompson   }
6840f7fd0f8Sjeremylt 
6850f7fd0f8Sjeremylt   // Default implementation
686567d69a2SJeremy L Thompson   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
6872b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
6880f7fd0f8Sjeremylt 
6892b730f8bSJeremy L Thompson   assert(x_array);
6902b730f8bSJeremy L Thompson   assert(y_array);
69173380422SJeremy L Thompson 
692*1203703bSJeremy L Thompson   for (CeedSize i = 0; i < length_y; i++) y_array[i] += alpha * x_array[i];
6930f7fd0f8Sjeremylt 
6942b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(y, &y_array));
6952b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
6960f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
6970f7fd0f8Sjeremylt }
6980f7fd0f8Sjeremylt 
6990f7fd0f8Sjeremylt /**
700ca94c3ddSJeremy L Thompson   @brief Compute `y = alpha x + beta y`
7015fb68f37SKaren (Ren) Stengel 
702ca94c3ddSJeremy L Thompson   @param[in,out] y     target `CeedVector` for sum
7035fb68f37SKaren (Ren) Stengel   @param[in]     alpha first scaling factor
7045fb68f37SKaren (Ren) Stengel   @param[in]     beta  second scaling factor
705ca94c3ddSJeremy L Thompson   @param[in]     x     second `CeedVector`, must be different than `y`
7065fb68f37SKaren (Ren) Stengel 
7075fb68f37SKaren (Ren) Stengel   @return An error code: 0 - success, otherwise - failure
7085fb68f37SKaren (Ren) Stengel 
7095fb68f37SKaren (Ren) Stengel   @ref User
7105fb68f37SKaren (Ren) Stengel **/
7115fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
712*1203703bSJeremy L Thompson   bool              has_valid_array_x = true, has_valid_array_y = true;
713*1203703bSJeremy L Thompson   CeedSize          length_x, length_y;
7145fb68f37SKaren (Ren) Stengel   CeedScalar       *y_array = NULL;
7155fb68f37SKaren (Ren) Stengel   CeedScalar const *x_array = NULL;
716*1203703bSJeremy L Thompson   Ceed              ceed, ceed_parent_x, ceed_parent_y;
7175fb68f37SKaren (Ren) Stengel 
718*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(y, &ceed));
719*1203703bSJeremy L Thompson 
720*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &length_y));
721*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &length_x));
722*1203703bSJeremy L Thompson   CeedCheck(length_x == length_y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths");
723*1203703bSJeremy L Thompson   CeedCheck(x != y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY");
7245fb68f37SKaren (Ren) Stengel 
7255fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
726*1203703bSJeremy L Thompson   CeedCheck(has_valid_array_x, ceed, CEED_ERROR_BACKEND,
7276574a04fSJeremy L Thompson             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7285fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
729*1203703bSJeremy L Thompson   CeedCheck(has_valid_array_y, ceed, CEED_ERROR_BACKEND,
7306574a04fSJeremy L Thompson             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7315fb68f37SKaren (Ren) Stengel 
7325fb68f37SKaren (Ren) Stengel   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
7335fb68f37SKaren (Ren) Stengel   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
734*1203703bSJeremy L Thompson   CeedCheck(ceed_parent_x == ceed_parent_y, ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context");
7355fb68f37SKaren (Ren) Stengel 
736b0976d5aSZach Atkins   // Return early for empty vectors
737*1203703bSJeremy L Thompson   if (length_y == 0) return CEED_ERROR_SUCCESS;
738b0976d5aSZach Atkins 
7395fb68f37SKaren (Ren) Stengel   // Backend implementation
7405fb68f37SKaren (Ren) Stengel   if (y->AXPBY) {
7415fb68f37SKaren (Ren) Stengel     CeedCall(y->AXPBY(y, alpha, beta, x));
7425fb68f37SKaren (Ren) Stengel     return CEED_ERROR_SUCCESS;
7435fb68f37SKaren (Ren) Stengel   }
7445fb68f37SKaren (Ren) Stengel 
7455fb68f37SKaren (Ren) Stengel   // Default implementation
7465fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
7475fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
7485fb68f37SKaren (Ren) Stengel 
7495fb68f37SKaren (Ren) Stengel   assert(x_array);
7505fb68f37SKaren (Ren) Stengel   assert(y_array);
7515fb68f37SKaren (Ren) Stengel 
752*1203703bSJeremy L Thompson   for (CeedSize i = 0; i < length_y; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i];
7535fb68f37SKaren (Ren) Stengel 
7545fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArray(y, &y_array));
7555fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
7565fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
7575fb68f37SKaren (Ren) Stengel }
7585fb68f37SKaren (Ren) Stengel 
7595fb68f37SKaren (Ren) Stengel /**
760ca94c3ddSJeremy L Thompson   @brief Compute the pointwise multiplication \f$w = x .* y\f$.
7614385fb7fSSebastian Grimberg 
762ca94c3ddSJeremy L Thompson   Any subset of `x`, `y`, and `w` may be the same `CeedVector`.
7630f7fd0f8Sjeremylt 
764ca94c3ddSJeremy L Thompson   @param[out] w target `CeedVector` for the product
765ca94c3ddSJeremy L Thompson   @param[in]  x first `CeedVector` for product
766ca94c3ddSJeremy L Thompson   @param[in]  y second `CeedVector` for the product
7670f7fd0f8Sjeremylt 
7680f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
7690f7fd0f8Sjeremylt 
7700f7fd0f8Sjeremylt   @ref User
7710f7fd0f8Sjeremylt **/
7720f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
773*1203703bSJeremy L Thompson   bool              has_valid_array_x = true, has_valid_array_y = true;
77473380422SJeremy L Thompson   CeedScalar       *w_array = NULL;
77573380422SJeremy L Thompson   CeedScalar const *x_array = NULL, *y_array = NULL;
776*1203703bSJeremy L Thompson   CeedSize          length_w, length_x, length_y;
777*1203703bSJeremy L Thompson   Ceed              ceed, ceed_parent_w, ceed_parent_x, ceed_parent_y;
7780f7fd0f8Sjeremylt 
779*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(w, &ceed));
780*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(w, &length_w));
781*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &length_x));
782*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &length_y));
783*1203703bSJeremy L Thompson   CeedCheck(length_w == length_x && length_w == length_y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths");
7840f7fd0f8Sjeremylt 
7852b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(w->ceed, &ceed_parent_w));
7862b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
7872b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
788*1203703bSJeremy L Thompson   CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, ceed, CEED_ERROR_INCOMPATIBLE,
7896574a04fSJeremy L Thompson             "Vectors w, x, and y must be created by the same Ceed context");
7902d04630dSjeremylt 
7912b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
792*1203703bSJeremy L Thompson   CeedCheck(has_valid_array_x, ceed, CEED_ERROR_BACKEND,
7936574a04fSJeremy L Thompson             "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7942b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
795*1203703bSJeremy L Thompson   CeedCheck(has_valid_array_y, ceed, CEED_ERROR_BACKEND,
7966574a04fSJeremy L Thompson             "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7979c774eddSJeremy L Thompson 
798b0976d5aSZach Atkins   // Return early for empty vectors
799*1203703bSJeremy L Thompson   if (length_w == 0) return CEED_ERROR_SUCCESS;
800b0976d5aSZach Atkins 
8010f7fd0f8Sjeremylt   // Backend implementation
802eaf62fffSJeremy L Thompson   if (w->PointwiseMult) {
8032b730f8bSJeremy L Thompson     CeedCall(w->PointwiseMult(w, x, y));
804eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
805eaf62fffSJeremy L Thompson   }
8060f7fd0f8Sjeremylt 
8070f7fd0f8Sjeremylt   // Default implementation
808b0976d5aSZach Atkins   if (x == w || y == w) {
809b0976d5aSZach Atkins     CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array));
810b0976d5aSZach Atkins   } else {
811b0976d5aSZach Atkins     CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array));
812b0976d5aSZach Atkins   }
8130f7fd0f8Sjeremylt   if (x != w) {
8142b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
8150f7fd0f8Sjeremylt   } else {
8160f7fd0f8Sjeremylt     x_array = w_array;
8170f7fd0f8Sjeremylt   }
8180f7fd0f8Sjeremylt   if (y != w && y != x) {
8192b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array));
820c6d796c6SJeremy L Thompson   } else if (y == x) {
8210f7fd0f8Sjeremylt     y_array = x_array;
822b0976d5aSZach Atkins   } else if (y == w) {
823c6d796c6SJeremy L Thompson     y_array = w_array;
8240f7fd0f8Sjeremylt   }
8250f7fd0f8Sjeremylt 
8262b730f8bSJeremy L Thompson   assert(w_array);
8272b730f8bSJeremy L Thompson   assert(x_array);
8282b730f8bSJeremy L Thompson   assert(y_array);
82973380422SJeremy L Thompson 
830*1203703bSJeremy L Thompson   for (CeedSize i = 0; i < length_w; i++) w_array[i] = x_array[i] * y_array[i];
8310f7fd0f8Sjeremylt 
8322b730f8bSJeremy L Thompson   if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array));
8332b730f8bSJeremy L Thompson   if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
8342b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(w, &w_array));
8350f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
8360f7fd0f8Sjeremylt }
8370f7fd0f8Sjeremylt 
8380f7fd0f8Sjeremylt /**
839ca94c3ddSJeremy L Thompson   @brief Take the reciprocal of a `CeedVector`.
840d99fa3c5SJeremy L Thompson 
841ca94c3ddSJeremy L Thompson   @param[in,out] vec `CeedVector` to take reciprocal
842d99fa3c5SJeremy L Thompson 
843d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
844d99fa3c5SJeremy L Thompson 
845d99fa3c5SJeremy L Thompson   @ref User
846d99fa3c5SJeremy L Thompson **/
847d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) {
8489c774eddSJeremy L Thompson   bool        has_valid_array = true;
849*1203703bSJeremy L Thompson   CeedSize    length;
850*1203703bSJeremy L Thompson   CeedScalar *array;
851*1203703bSJeremy L Thompson   Ceed        ceed;
8522b730f8bSJeremy L Thompson 
853*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
8541c66c397SJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
855*1203703bSJeremy L Thompson   CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND,
8562b730f8bSJeremy L Thompson             "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray");
8579c774eddSJeremy L Thompson 
858d99fa3c5SJeremy L Thompson   // Check if vector data set
859*1203703bSJeremy L Thompson   CeedCheck(vec->state > 0, ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal");
860d99fa3c5SJeremy L Thompson 
861b0976d5aSZach Atkins   // Return early for empty vector
862*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
863*1203703bSJeremy L Thompson   if (length == 0) return CEED_ERROR_SUCCESS;
864b0976d5aSZach Atkins 
865d99fa3c5SJeremy L Thompson   // Backend impl for GPU, if added
866d99fa3c5SJeremy L Thompson   if (vec->Reciprocal) {
8672b730f8bSJeremy L Thompson     CeedCall(vec->Reciprocal(vec));
868e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
869d99fa3c5SJeremy L Thompson   }
870d99fa3c5SJeremy L Thompson 
871567d69a2SJeremy L Thompson   CeedCall(CeedVectorGetArray(vec, CEED_MEM_HOST, &array));
872*1203703bSJeremy L Thompson   for (CeedSize i = 0; i < length; i++) {
8732b730f8bSJeremy L Thompson     if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i];
8742b730f8bSJeremy L Thompson   }
875d99fa3c5SJeremy L Thompson 
8762b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(vec, &array));
877e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
878d99fa3c5SJeremy L Thompson }
879d99fa3c5SJeremy L Thompson 
880d99fa3c5SJeremy L Thompson /**
881ca94c3ddSJeremy L Thompson   @brief View a `CeedVector`
8820436c2adSjeremylt 
883acb2c48cSJeremy L Thompson   Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`.
884ca94c3ddSJeremy L Thompson         Any portion of the provided range that is outside the range of valid indices for the `CeedVector` will be ignored.
885acb2c48cSJeremy L Thompson 
886ca94c3ddSJeremy L Thompson   @param[in] vec    `CeedVector` to view
887ca94c3ddSJeremy L Thompson   @param[in] start  Index of first `CeedVector` entry to view
888ca94c3ddSJeremy L Thompson   @param[in] stop   Index of last `CeedVector` entry to view
889ca94c3ddSJeremy L Thompson   @param[in] step   Step between `CeedVector` entries to view
890acb2c48cSJeremy L Thompson   @param[in] fp_fmt Printing format
891acb2c48cSJeremy L Thompson   @param[in] stream Filestream to write to
892acb2c48cSJeremy L Thompson 
893acb2c48cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
894acb2c48cSJeremy L Thompson 
895acb2c48cSJeremy L Thompson   @ref User
896acb2c48cSJeremy L Thompson **/
897acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) {
898acb2c48cSJeremy L Thompson   char              fmt[1024];
899*1203703bSJeremy L Thompson   CeedSize          length;
900*1203703bSJeremy L Thompson   const CeedScalar *x;
901*1203703bSJeremy L Thompson   Ceed              ceed;
902acb2c48cSJeremy L Thompson 
903*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetCeed(vec, &ceed));
904*1203703bSJeremy L Thompson   CeedCheck(step != 0, ceed, CEED_ERROR_MINOR, "View range 'step' must be nonzero");
905acb2c48cSJeremy L Thompson 
906*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
907*1203703bSJeremy L Thompson   fprintf(stream, "CeedVector length %" CeedSize_FMT "\n", length);
908*1203703bSJeremy L Thompson   if (start != 0 || stop != length || step != 1) {
909*1203703bSJeremy L Thompson     fprintf(stream, "  start: %" CeedSize_FMT "\n  stop:  %" CeedSize_FMT "\n  step:  %" CeedInt_FMT "\n", start, stop, step);
910acb2c48cSJeremy L Thompson   }
911*1203703bSJeremy L Thompson   if (start > length) start = length;
912*1203703bSJeremy L Thompson   if (stop > length) stop = length;
913acb2c48cSJeremy L Thompson 
914acb2c48cSJeremy L Thompson   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
915acb2c48cSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x));
916b94338b9SJed Brown   for (CeedSize i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]);
917acb2c48cSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &x));
918*1203703bSJeremy L Thompson   if (stop != length) fprintf(stream, "  ...\n");
919acb2c48cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
920acb2c48cSJeremy L Thompson }
921acb2c48cSJeremy L Thompson 
922acb2c48cSJeremy L Thompson /**
923ca94c3ddSJeremy L Thompson   @brief View a `CeedVector`
924acb2c48cSJeremy L Thompson 
925ca94c3ddSJeremy L Thompson   @param[in] vec    `CeedVector` to view
926d1d35e2fSjeremylt   @param[in] fp_fmt Printing format
9270a0da059Sjeremylt   @param[in] stream Filestream to write to
9280a0da059Sjeremylt 
9290436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9300436c2adSjeremylt 
9317a982d89SJeremy L. Thompson   @ref User
9320436c2adSjeremylt **/
933d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
934*1203703bSJeremy L Thompson   CeedSize length;
935*1203703bSJeremy L Thompson 
936*1203703bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &length));
937*1203703bSJeremy L Thompson   CeedCall(CeedVectorViewRange(vec, 0, length, 1, fp_fmt, stream));
938e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9390436c2adSjeremylt }
9400436c2adSjeremylt 
9410436c2adSjeremylt /**
942ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedVector`
943b7c9bbdaSJeremy L Thompson 
944ca94c3ddSJeremy L Thompson   @param[in]  vec  `CeedVector` to retrieve state
945ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
946b7c9bbdaSJeremy L Thompson 
947b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
948b7c9bbdaSJeremy L Thompson 
949b7c9bbdaSJeremy L Thompson   @ref Advanced
950b7c9bbdaSJeremy L Thompson **/
951b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
952b7c9bbdaSJeremy L Thompson   *ceed = vec->ceed;
953b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
954b7c9bbdaSJeremy L Thompson }
955b7c9bbdaSJeremy L Thompson 
956b7c9bbdaSJeremy L Thompson /**
957ca94c3ddSJeremy L Thompson   @brief Get the length of a `CeedVector`
9580436c2adSjeremylt 
959ca94c3ddSJeremy L Thompson   @param[in]  vec    `CeedVector` to retrieve length
9607a982d89SJeremy L. Thompson   @param[out] length Variable to store length
9610436c2adSjeremylt 
9620436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9630436c2adSjeremylt 
9647a982d89SJeremy L. Thompson   @ref User
9650436c2adSjeremylt **/
9661f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) {
9677a982d89SJeremy L. Thompson   *length = vec->length;
968e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9690436c2adSjeremylt }
9700436c2adSjeremylt 
9710436c2adSjeremylt /**
972ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedVector`
9730436c2adSjeremylt 
974ca94c3ddSJeremy L Thompson   @param[in,out] vec `CeedVector` to destroy
9750436c2adSjeremylt 
9760436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9770436c2adSjeremylt 
9787a982d89SJeremy L. Thompson   @ref User
9790436c2adSjeremylt **/
9800436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) {
9817425e127SJeremy L Thompson   if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) {
982ad6481ceSJeremy L Thompson     *vec = NULL;
983ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
984ad6481ceSJeremy L Thompson   }
9856574a04fSJeremy L Thompson   CeedCheck((*vec)->state % 2 == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use");
9866574a04fSJeremy L Thompson   CeedCheck((*vec)->num_readers == 0, (*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access");
9870436c2adSjeremylt 
9882b730f8bSJeremy L Thompson   if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec));
9892b730f8bSJeremy L Thompson 
9902b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*vec)->ceed));
9912b730f8bSJeremy L Thompson   CeedCall(CeedFree(vec));
992e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9930436c2adSjeremylt }
9940436c2adSjeremylt 
9950436c2adSjeremylt /// @}
996