xref: /libCEED/interface/ceed-vector.c (revision 393ac2cdd9c85249de1a71b00b977b0338eea6aa)
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 
873380422SJeremy L Thompson #include <assert.h>
92b730f8bSJeremy L Thompson #include <ceed-impl.h>
1049aac155SJeremy L Thompson #include <ceed.h>
112b730f8bSJeremy L Thompson #include <ceed/backend.h>
12547d9b97Sjeremylt #include <math.h>
1349aac155SJeremy L Thompson #include <stdbool.h>
143d576824SJeremy L Thompson #include <stdint.h>
153d576824SJeremy L Thompson #include <stdio.h>
160436c2adSjeremylt 
177a982d89SJeremy L. Thompson /// @file
187a982d89SJeremy L. Thompson /// Implementation of public CeedVector interfaces
197a982d89SJeremy L. Thompson 
200436c2adSjeremylt /// @cond DOXYGEN_SKIP
210436c2adSjeremylt static struct CeedVector_private ceed_vector_active;
220436c2adSjeremylt static struct CeedVector_private ceed_vector_none;
230436c2adSjeremylt /// @endcond
240436c2adSjeremylt 
257a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser
267a982d89SJeremy L. Thompson /// @{
277a982d89SJeremy L. Thompson 
28ea61e9acSJeremy L Thompson /// Indicate that vector will be provided as an explicit argument to CeedOperatorApply().
297a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_ACTIVE = &ceed_vector_active;
307a982d89SJeremy L. Thompson 
3196b902e2Sjeremylt /// Indicate that no vector is applicable (i.e., for @ref CEED_EVAL_WEIGHT).
327a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_NONE = &ceed_vector_none;
337a982d89SJeremy L. Thompson 
347a982d89SJeremy L. Thompson /// @}
357a982d89SJeremy L. Thompson 
367a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
377a982d89SJeremy L. Thompson /// CeedVector Backend API
387a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
397a982d89SJeremy L. Thompson /// @addtogroup CeedVectorBackend
407a982d89SJeremy L. Thompson /// @{
417a982d89SJeremy L. Thompson 
427a982d89SJeremy L. Thompson /**
439c774eddSJeremy L Thompson   @brief Check for valid data in a CeedVector
449c774eddSJeremy L Thompson 
45ea61e9acSJeremy L Thompson   @param[in]  vec             CeedVector to check validity
469c774eddSJeremy L Thompson   @param[out] has_valid_array Variable to store validity
479c774eddSJeremy L Thompson 
489c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
499c774eddSJeremy L Thompson 
509c774eddSJeremy L Thompson   @ref Backend
519c774eddSJeremy L Thompson **/
529c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) {
532b730f8bSJeremy L Thompson   if (!vec->HasValidArray) {
549c774eddSJeremy L Thompson     // LCOV_EXCL_START
552b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasValidArray");
569c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
572b730f8bSJeremy L Thompson   }
589c774eddSJeremy L Thompson 
592b730f8bSJeremy L Thompson   CeedCall(vec->HasValidArray(vec, has_valid_array));
609c774eddSJeremy L Thompson 
619c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
629c774eddSJeremy L Thompson }
639c774eddSJeremy L Thompson 
649c774eddSJeremy L Thompson /**
659c774eddSJeremy L Thompson   @brief Check for borrowed array of a specific CeedMemType in a CeedVector
669c774eddSJeremy L Thompson 
67ea61e9acSJeremy L Thompson   @param[in]  vec                        CeedVector to check
68ea61e9acSJeremy L Thompson   @param[in]  mem_type                   Memory type to check
699c774eddSJeremy L Thompson   @param[out] has_borrowed_array_of_type Variable to store result
709c774eddSJeremy L Thompson 
719c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
729c774eddSJeremy L Thompson 
739c774eddSJeremy L Thompson   @ref Backend
749c774eddSJeremy L Thompson **/
752b730f8bSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) {
762b730f8bSJeremy L Thompson   if (!vec->HasBorrowedArrayOfType) {
779c774eddSJeremy L Thompson     // LCOV_EXCL_START
782b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasBorrowedArrayOfType");
799c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
802b730f8bSJeremy L Thompson   }
819c774eddSJeremy L Thompson 
822b730f8bSJeremy L Thompson   CeedCall(vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type));
839c774eddSJeremy L Thompson 
849c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
859c774eddSJeremy L Thompson }
869c774eddSJeremy L Thompson 
879c774eddSJeremy L Thompson /**
887a982d89SJeremy L. Thompson   @brief Get the state of a CeedVector
897a982d89SJeremy L. Thompson 
90ea61e9acSJeremy L Thompson   @param[in]  vec    CeedVector to retrieve state
917a982d89SJeremy L. Thompson   @param[out] state  Variable to store state
927a982d89SJeremy L. Thompson 
937a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
947a982d89SJeremy L. Thompson 
957a982d89SJeremy L. Thompson   @ref Backend
967a982d89SJeremy L. Thompson **/
977a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) {
987a982d89SJeremy L. Thompson   *state = vec->state;
99e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1007a982d89SJeremy L. Thompson }
1017a982d89SJeremy L. Thompson 
1027a982d89SJeremy L. Thompson /**
10327312b0fSJed Brown   @brief Add a reference to a CeedVector
1047a982d89SJeremy L. Thompson 
105ea61e9acSJeremy L Thompson   @param[in,out] vec CeedVector to increment reference counter
1067a982d89SJeremy L. Thompson 
1077a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson   @ref Backend
1107a982d89SJeremy L. Thompson **/
1117a982d89SJeremy L. Thompson int CeedVectorAddReference(CeedVector vec) {
112d1d35e2fSjeremylt   vec->ref_count++;
113e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1147a982d89SJeremy L. Thompson }
1157a982d89SJeremy L. Thompson 
1167a982d89SJeremy L. Thompson /**
1177a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedVector
1187a982d89SJeremy L. Thompson 
119ea61e9acSJeremy L Thompson   @param[in]  vec  CeedVector to retrieve state
1207a982d89SJeremy L. Thompson   @param[out] data Variable to store data
1217a982d89SJeremy L. Thompson 
1227a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1237a982d89SJeremy L. Thompson 
1247a982d89SJeremy L. Thompson   @ref Backend
1257a982d89SJeremy L. Thompson **/
126777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) {
127777ff853SJeremy L Thompson   *(void **)data = vec->data;
128e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1297a982d89SJeremy L. Thompson }
1307a982d89SJeremy L. Thompson 
1317a982d89SJeremy L. Thompson /**
1327a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedVector
1337a982d89SJeremy L. Thompson 
134ea61e9acSJeremy L Thompson   @param[in,out] vec  CeedVector to retrieve state
135ea61e9acSJeremy L Thompson   @param[in]     data Data to set
1367a982d89SJeremy L. Thompson 
1377a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1387a982d89SJeremy L. Thompson 
1397a982d89SJeremy L. Thompson   @ref Backend
1407a982d89SJeremy L. Thompson **/
141777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) {
142777ff853SJeremy L Thompson   vec->data = data;
143e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1447a982d89SJeremy L. Thompson }
1457a982d89SJeremy L. Thompson 
14634359f16Sjeremylt /**
14734359f16Sjeremylt   @brief Increment the reference counter for a CeedVector
14834359f16Sjeremylt 
149ea61e9acSJeremy L Thompson   @param[in,out] vec CeedVector to increment the reference counter
15034359f16Sjeremylt 
15134359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
15234359f16Sjeremylt 
15334359f16Sjeremylt   @ref Backend
15434359f16Sjeremylt **/
1559560d06aSjeremylt int CeedVectorReference(CeedVector vec) {
15634359f16Sjeremylt   vec->ref_count++;
15734359f16Sjeremylt   return CEED_ERROR_SUCCESS;
15834359f16Sjeremylt }
15934359f16Sjeremylt 
1607a982d89SJeremy L. Thompson /// @}
1617a982d89SJeremy L. Thompson 
1627a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1637a982d89SJeremy L. Thompson /// CeedVector Public API
1647a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1657a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser
1660436c2adSjeremylt /// @{
1670436c2adSjeremylt 
1680436c2adSjeremylt /**
1690436c2adSjeremylt   @brief Create a CeedVector of the specified length (does not allocate memory)
1700436c2adSjeremylt 
171ea61e9acSJeremy L Thompson   @param[in]  ceed   Ceed object where the CeedVector will be created
172ea61e9acSJeremy L Thompson   @param[in]  length Length of vector
173ea61e9acSJeremy L Thompson   @param[out] vec    Address of the variable where the newly created CeedVector will be stored
1740436c2adSjeremylt 
1750436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
1760436c2adSjeremylt 
1777a982d89SJeremy L. Thompson   @ref User
1780436c2adSjeremylt **/
1791f9221feSJeremy L Thompson int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) {
1800436c2adSjeremylt   if (!ceed->VectorCreate) {
1810436c2adSjeremylt     Ceed delegate;
1822b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector"));
1830436c2adSjeremylt 
1842b730f8bSJeremy L Thompson     if (!delegate) {
1850436c2adSjeremylt       // LCOV_EXCL_START
1862b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorCreate");
1870436c2adSjeremylt       // LCOV_EXCL_STOP
1882b730f8bSJeremy L Thompson     }
1890436c2adSjeremylt 
1902b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(delegate, length, vec));
191e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
1920436c2adSjeremylt   }
1930436c2adSjeremylt 
1942b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, vec));
1950436c2adSjeremylt   (*vec)->ceed = ceed;
1962b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
197d1d35e2fSjeremylt   (*vec)->ref_count = 1;
1980436c2adSjeremylt   (*vec)->length    = length;
1990436c2adSjeremylt   (*vec)->state     = 0;
2002b730f8bSJeremy L Thompson   CeedCall(ceed->VectorCreate(length, *vec));
201e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2020436c2adSjeremylt }
2030436c2adSjeremylt 
2040436c2adSjeremylt /**
205ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedVector.
206ea61e9acSJeremy L Thompson            Both pointers should be destroyed with `CeedVectorDestroy()`.
207512bb800SJeremy L Thompson 
208512bb800SJeremy 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.
209512bb800SJeremy L Thompson              This CeedVector will be destroyed if `vec_copy` is the only reference to this CeedVector.
2109560d06aSjeremylt 
211ea61e9acSJeremy L Thompson   @param[in]     vec      CeedVector to copy reference to
212ea61e9acSJeremy L Thompson   @param[in,out] vec_copy Variable to store copied reference
2139560d06aSjeremylt 
2149560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
2159560d06aSjeremylt 
2169560d06aSjeremylt   @ref User
2179560d06aSjeremylt **/
2189560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) {
219*393ac2cdSJeremy L Thompson   if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorReference(vec));
2202b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(vec_copy));
2219560d06aSjeremylt   *vec_copy = vec;
2229560d06aSjeremylt   return CEED_ERROR_SUCCESS;
2239560d06aSjeremylt }
2249560d06aSjeremylt 
2259560d06aSjeremylt /**
2265fb68f37SKaren (Ren) Stengel   @brief Copy a CeedVector into a different CeedVector.
2275fb68f37SKaren (Ren) Stengel           Both pointers should be destroyed with `CeedVectorDestroy()`.
2285fb68f37SKaren (Ren) Stengel           Note: If `*vec_copy` is non-NULL, then it is assumed that `*vec_copy` is a pointer to a CeedVector.
2295fb68f37SKaren (Ren) Stengel           This CeedVector will be destroyed if `*vec_copy` is the only reference to this CeedVector.
2305fb68f37SKaren (Ren) Stengel 
2315fb68f37SKaren (Ren) Stengel   @param[in]     vec      CeedVector to copy
2325fb68f37SKaren (Ren) Stengel   @param[in,out] vec_copy Variable to store copied CeedVector to
2335fb68f37SKaren (Ren) Stengel 
2345fb68f37SKaren (Ren) Stengel   @return An error code: 0 - success, otherwise - failure
2355fb68f37SKaren (Ren) Stengel 
2365fb68f37SKaren (Ren) Stengel   @ref User
2375fb68f37SKaren (Ren) Stengel **/
2385fb68f37SKaren (Ren) Stengel int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) {
2395fb68f37SKaren (Ren) Stengel   Ceed        ceed;
2405fb68f37SKaren (Ren) Stengel   CeedMemType mem_type, mem_type_copy;
2415fb68f37SKaren (Ren) Stengel   CeedScalar *array;
2425fb68f37SKaren (Ren) Stengel 
2435fb68f37SKaren (Ren) Stengel   // Get the preferred memory type
2445fb68f37SKaren (Ren) Stengel   CeedVectorGetCeed(vec, &ceed);
2455fb68f37SKaren (Ren) Stengel   CeedGetPreferredMemType(ceed, &mem_type);
2465fb68f37SKaren (Ren) Stengel 
2475fb68f37SKaren (Ren) Stengel   // Get the preferred memory type
2485fb68f37SKaren (Ren) Stengel   CeedVectorGetCeed(vec_copy, &ceed);
2495fb68f37SKaren (Ren) Stengel   CeedGetPreferredMemType(ceed, &mem_type_copy);
2505fb68f37SKaren (Ren) Stengel 
2515fb68f37SKaren (Ren) Stengel   // Check that both have same memory type
2525fb68f37SKaren (Ren) Stengel   if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST;
2535fb68f37SKaren (Ren) Stengel 
2545fb68f37SKaren (Ren) Stengel   // Copy the values from vec to vec_copy
2555fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArray(vec, mem_type, &array));
2565fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array));
2575fb68f37SKaren (Ren) Stengel 
2585fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArray(vec, &array));
2595fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
2605fb68f37SKaren (Ren) Stengel }
2615fb68f37SKaren (Ren) Stengel 
2625fb68f37SKaren (Ren) Stengel /**
263ea61e9acSJeremy L Thompson   @brief Set the array used by a CeedVector, freeing any previously allocated array if applicable.
264ea61e9acSJeremy L Thompson            The backend may copy values to a different memtype, such as during @ref CeedOperatorApply().
2656a6c615bSJeremy L Thompson            See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray().
2660436c2adSjeremylt 
267ea61e9acSJeremy L Thompson   @param[in,out] vec       CeedVector
268ea61e9acSJeremy L Thompson   @param[in]     mem_type  Memory type of the array being passed
269ea61e9acSJeremy L Thompson   @param[in]     copy_mode Copy mode for the array
270ea61e9acSJeremy L Thompson   @param[in]     array     Array to be used, or NULL with @ref CEED_COPY_VALUES to have the library allocate
2710436c2adSjeremylt 
2720436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2730436c2adSjeremylt 
2747a982d89SJeremy L. Thompson   @ref User
2750436c2adSjeremylt **/
2762b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) {
2772b730f8bSJeremy L Thompson   if (!vec->SetArray) {
2780436c2adSjeremylt     // LCOV_EXCL_START
2792b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray");
2800436c2adSjeremylt     // LCOV_EXCL_STOP
2812b730f8bSJeremy L Thompson   }
2822b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
2832b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
2842b730f8bSJeremy L Thompson   }
2852b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
2862b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
2872b730f8bSJeremy L Thompson   }
2880436c2adSjeremylt 
2892b730f8bSJeremy L Thompson   CeedCall(vec->SetArray(vec, mem_type, copy_mode, array));
2900436c2adSjeremylt   vec->state += 2;
291e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2920436c2adSjeremylt }
2930436c2adSjeremylt 
2940436c2adSjeremylt /**
2950436c2adSjeremylt   @brief Set the CeedVector to a constant value
2960436c2adSjeremylt 
297ea61e9acSJeremy L Thompson   @param[in,out] vec   CeedVector
2980436c2adSjeremylt   @param[in]     value Value to be used
2990436c2adSjeremylt 
3000436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3010436c2adSjeremylt 
3027a982d89SJeremy L. Thompson   @ref User
3030436c2adSjeremylt **/
3040436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) {
3052b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
3069c774eddSJeremy L Thompson     // LCOV_EXCL_START
3072b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
3089c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
3092b730f8bSJeremy L Thompson   }
3102b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
3119c774eddSJeremy L Thompson     // LCOV_EXCL_START
3122b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
3139c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
3142b730f8bSJeremy L Thompson   }
3150436c2adSjeremylt 
3160436c2adSjeremylt   if (vec->SetValue) {
3172b730f8bSJeremy L Thompson     CeedCall(vec->SetValue(vec, value));
3180436c2adSjeremylt   } else {
3190436c2adSjeremylt     CeedScalar *array;
3202b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array));
32192ae7e47SJeremy L Thompson     for (CeedInt i = 0; i < vec->length; i++) array[i] = value;
3222b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(vec, &array));
3230436c2adSjeremylt   }
3240436c2adSjeremylt   vec->state += 2;
325e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3260436c2adSjeremylt }
3270436c2adSjeremylt 
3280436c2adSjeremylt /**
329ea61e9acSJeremy L Thompson   @brief Sync the CeedVector to a specified memtype.
330ea61e9acSJeremy L Thompson            This function is used to force synchronization of arrays set with @ref CeedVectorSetArray().
331ea61e9acSJeremy L Thompson            If the requested memtype is already synchronized, this function results in a no-op.
3320436c2adSjeremylt 
333ea61e9acSJeremy L Thompson   @param[in,out] vec      CeedVector
334ea61e9acSJeremy L Thompson   @param[in]     mem_type Memtype to be synced
3350436c2adSjeremylt 
3360436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3370436c2adSjeremylt 
3387a982d89SJeremy L. Thompson   @ref User
3390436c2adSjeremylt **/
340d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) {
3412b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
3422b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use");
3432b730f8bSJeremy L Thompson   }
3440436c2adSjeremylt 
3450436c2adSjeremylt   if (vec->SyncArray) {
3462b730f8bSJeremy L Thompson     CeedCall(vec->SyncArray(vec, mem_type));
3470436c2adSjeremylt   } else {
3480436c2adSjeremylt     const CeedScalar *array;
3492b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array));
3502b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(vec, &array));
3510436c2adSjeremylt   }
352e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3530436c2adSjeremylt }
3540436c2adSjeremylt 
3550436c2adSjeremylt /**
356ea61e9acSJeremy 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.
3579c774eddSJeremy L Thompson            The caller is responsible for managing and freeing the array.
358ea61e9acSJeremy L Thompson            This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type.
3596a6c615bSJeremy L Thompson 
360ea61e9acSJeremy L Thompson   @param[in,out] vec      CeedVector
361ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to take the array.
362ea61e9acSJeremy L Thompson                             If the backend uses a different memory type, this will perform a copy.
363ea61e9acSJeremy L Thompson   @param[out]    array    Array on memory type mem_type, or NULL if array pointer is not required
3646a6c615bSJeremy L Thompson 
3656a6c615bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3666a6c615bSJeremy L Thompson 
3676a6c615bSJeremy L Thompson   @ref User
3686a6c615bSJeremy L Thompson **/
3692b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
3702b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
3712b730f8bSJeremy L Thompson     // LCOV_EXCL_START
3722b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use");
3732b730f8bSJeremy L Thompson     // LCOV_EXCL_STOP
3742b730f8bSJeremy L Thompson   }
3752b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
3762b730f8bSJeremy L Thompson     // LCOV_EXCL_START
3772b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access");
3782b730f8bSJeremy L Thompson     // LCOV_EXCL_STOP
3792b730f8bSJeremy L Thompson   }
3806a6c615bSJeremy L Thompson 
38150c643e1SJed Brown   CeedScalar *temp_array = NULL;
38250c643e1SJed Brown   if (vec->length > 0) {
3839c774eddSJeremy L Thompson     bool has_borrowed_array_of_type = true;
3842b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type));
3852b730f8bSJeremy L Thompson     if (!has_borrowed_array_of_type) {
3869c774eddSJeremy L Thompson       // LCOV_EXCL_START
3872b730f8bSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND, "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray",
3882b730f8bSJeremy L Thompson                        CeedMemTypes[mem_type]);
3899c774eddSJeremy L Thompson       // LCOV_EXCL_STOP
3902b730f8bSJeremy L Thompson     }
3919c774eddSJeremy L Thompson 
3929c774eddSJeremy L Thompson     bool has_valid_array = true;
3932b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
3942b730f8bSJeremy L Thompson     if (!has_valid_array) {
3959c774eddSJeremy L Thompson       // LCOV_EXCL_START
3969c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
3972b730f8bSJeremy L Thompson                        "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray");
3989c774eddSJeremy L Thompson       // LCOV_EXCL_STOP
3992b730f8bSJeremy L Thompson     }
4009c774eddSJeremy L Thompson 
4012b730f8bSJeremy L Thompson     CeedCall(vec->TakeArray(vec, mem_type, &temp_array));
40250c643e1SJed Brown   }
403d1d35e2fSjeremylt   if (array) (*array) = temp_array;
404e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4056a6c615bSJeremy L Thompson }
4066a6c615bSJeremy L Thompson 
4076a6c615bSJeremy L Thompson /**
408b3cf021fSjeremylt   @brief Get read/write access to a CeedVector via the specified memory type.
409b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArray().
4100436c2adSjeremylt 
411ea61e9acSJeremy L Thompson   @param[in,out] vec      CeedVector to access
412ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to access the array.
413ea61e9acSJeremy L Thompson                             If the backend uses a different memory type, this will perform a copy.
414d1d35e2fSjeremylt   @param[out]    array    Array on memory type mem_type
4150436c2adSjeremylt 
416ea61e9acSJeremy L Thompson   @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide access to array pointers in the desired memory space.
417ea61e9acSJeremy L Thompson     Pairing get/restore allows the Vector to track access, thus knowing if norms or other operations may need to be recomputed.
4180436c2adSjeremylt 
4190436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4200436c2adSjeremylt 
4217a982d89SJeremy L. Thompson   @ref User
4220436c2adSjeremylt **/
4232b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
4242b730f8bSJeremy L Thompson   if (!vec->GetArray) {
4250436c2adSjeremylt     // LCOV_EXCL_START
4262b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray");
4270436c2adSjeremylt     // LCOV_EXCL_STOP
4282b730f8bSJeremy L Thompson   }
4292b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
4302b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
4312b730f8bSJeremy L Thompson   }
4322b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
4332b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
4342b730f8bSJeremy L Thompson   }
4350436c2adSjeremylt 
4369c774eddSJeremy L Thompson   bool has_valid_array = true;
4372b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
4382b730f8bSJeremy L Thompson   if (!has_valid_array) {
4399c774eddSJeremy L Thompson     // LCOV_EXCL_START
4409c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
4412b730f8bSJeremy L Thompson                      "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
4429c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
4432b730f8bSJeremy L Thompson   }
4449c774eddSJeremy L Thompson 
4452b730f8bSJeremy L Thompson   CeedCall(vec->GetArray(vec, mem_type, array));
44628bfd0b7SJeremy L Thompson   vec->state++;
447e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4480436c2adSjeremylt }
4490436c2adSjeremylt 
4500436c2adSjeremylt /**
451b3cf021fSjeremylt   @brief Get read-only access to a CeedVector via the specified memory type.
452b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArrayRead().
4530436c2adSjeremylt 
454ea61e9acSJeremy L Thompson   @param[in]  vec      CeedVector to access
455ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the array.
456ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy (possibly cached).
457d1d35e2fSjeremylt   @param[out] array    Array on memory type mem_type
4580436c2adSjeremylt 
4590436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4600436c2adSjeremylt 
4617a982d89SJeremy L. Thompson   @ref User
4620436c2adSjeremylt **/
4632b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
4642b730f8bSJeremy L Thompson   if (!vec->GetArrayRead) {
4650436c2adSjeremylt     // LCOV_EXCL_START
4662b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead");
4670436c2adSjeremylt     // LCOV_EXCL_STOP
4682b730f8bSJeremy L Thompson   }
4692b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
4702b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use");
4712b730f8bSJeremy L Thompson   }
4720436c2adSjeremylt 
47350c643e1SJed Brown   if (vec->length > 0) {
4749c774eddSJeremy L Thompson     bool has_valid_array = true;
4752b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
4762b730f8bSJeremy L Thompson     if (!has_valid_array) {
4779c774eddSJeremy L Thompson       // LCOV_EXCL_START
4789c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
4792b730f8bSJeremy L Thompson                        "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
4809c774eddSJeremy L Thompson       // LCOV_EXCL_STOP
4812b730f8bSJeremy L Thompson     }
4829c774eddSJeremy L Thompson 
4832b730f8bSJeremy L Thompson     CeedCall(vec->GetArrayRead(vec, mem_type, array));
48450c643e1SJed Brown   } else {
48550c643e1SJed Brown     *array = NULL;
48650c643e1SJed Brown   }
487d1d35e2fSjeremylt   vec->num_readers++;
488e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4890436c2adSjeremylt }
4900436c2adSjeremylt 
4910436c2adSjeremylt /**
4929c774eddSJeremy L Thompson   @brief Get write access to a CeedVector via the specified memory type.
493ea61e9acSJeremy L Thompson            Restore access with @ref CeedVectorRestoreArray().
494ea61e9acSJeremy L Thompson            All old values should be assumed to be invalid.
4959c774eddSJeremy L Thompson 
496ea61e9acSJeremy L Thompson   @param[in,out] vec      CeedVector to access
497ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to access the array.
4989c774eddSJeremy L Thompson   @param[out]    array    Array on memory type mem_type
4999c774eddSJeremy L Thompson 
5009c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5019c774eddSJeremy L Thompson 
5029c774eddSJeremy L Thompson   @ref User
5039c774eddSJeremy L Thompson **/
5042b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
5052b730f8bSJeremy L Thompson   if (!vec->GetArrayWrite) {
5069c774eddSJeremy L Thompson     // LCOV_EXCL_START
5072b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayWrite");
5089c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5092b730f8bSJeremy L Thompson   }
5102b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
5119c774eddSJeremy L Thompson     // LCOV_EXCL_START
5122b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
5139c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5142b730f8bSJeremy L Thompson   }
5152b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
5169c774eddSJeremy L Thompson     // LCOV_EXCL_START
5172b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
5189c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5192b730f8bSJeremy L Thompson   }
5209c774eddSJeremy L Thompson 
5212b730f8bSJeremy L Thompson   CeedCall(vec->GetArrayWrite(vec, mem_type, array));
52228bfd0b7SJeremy L Thompson   vec->state++;
5239c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5249c774eddSJeremy L Thompson }
5259c774eddSJeremy L Thompson 
5269c774eddSJeremy L Thompson /**
527ea61e9acSJeremy L Thompson   @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite()
5280436c2adSjeremylt 
529ea61e9acSJeremy L Thompson   @param[in,out] vec   CeedVector to restore
530ea61e9acSJeremy L Thompson   @param[in,out] array Array of vector data
5310436c2adSjeremylt 
5320436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5330436c2adSjeremylt 
5347a982d89SJeremy L. Thompson   @ref User
5350436c2adSjeremylt **/
5360436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
5372b730f8bSJeremy L Thompson   if (vec->state % 2 != 1) {
5382b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted");
539706efda3SJeremy L Thompson   }
5402b730f8bSJeremy L Thompson   if (vec->RestoreArray) CeedCall(vec->RestoreArray(vec));
5410436c2adSjeremylt   *array = NULL;
54228bfd0b7SJeremy L Thompson   vec->state++;
543e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5440436c2adSjeremylt }
5450436c2adSjeremylt 
5460436c2adSjeremylt /**
547b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArrayRead()
5480436c2adSjeremylt 
549ea61e9acSJeremy L Thompson   @param[in]     vec   CeedVector to restore
550ea61e9acSJeremy L Thompson   @param[in,out] array Array of vector data
5510436c2adSjeremylt 
5520436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5530436c2adSjeremylt 
5547a982d89SJeremy L. Thompson   @ref User
5550436c2adSjeremylt **/
5560436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) {
5572b730f8bSJeremy L Thompson   if (vec->num_readers == 0) {
5589c774eddSJeremy L Thompson     // LCOV_EXCL_START
5592b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted");
5609c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5612b730f8bSJeremy L Thompson   }
5629c774eddSJeremy L Thompson 
56375a19770SJeremy L Thompson   vec->num_readers--;
5642b730f8bSJeremy L Thompson   if (vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec));
5650436c2adSjeremylt   *array = NULL;
56675a19770SJeremy L Thompson 
567e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5680436c2adSjeremylt }
5690436c2adSjeremylt 
5700436c2adSjeremylt /**
571171f8ca9Sjeremylt   @brief Get the norm of a CeedVector.
572171f8ca9Sjeremylt 
573ea61e9acSJeremy L Thompson   Note: This operation is local to the CeedVector.
574ea61e9acSJeremy 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
575ea61e9acSJeremy L Thompson duplicated or hanging nodes.
576547d9b97Sjeremylt 
577ea61e9acSJeremy L Thompson   @param[in]  vec       CeedVector to retrieve maximum value
578ea61e9acSJeremy L Thompson   @param[in]  norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
579547d9b97Sjeremylt   @param[out] norm      Variable to store norm value
580547d9b97Sjeremylt 
581547d9b97Sjeremylt   @return An error code: 0 - success, otherwise - failure
582547d9b97Sjeremylt 
5837a982d89SJeremy L. Thompson   @ref User
584547d9b97Sjeremylt **/
585d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
5869c774eddSJeremy L Thompson   bool has_valid_array = true;
5872b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
5882b730f8bSJeremy L Thompson   if (!has_valid_array) {
5899c774eddSJeremy L Thompson     // LCOV_EXCL_START
5909c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
5912b730f8bSJeremy L Thompson                      "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray");
5929c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5932b730f8bSJeremy L Thompson   }
5949c774eddSJeremy L Thompson 
595547d9b97Sjeremylt   // Backend impl for GPU, if added
596547d9b97Sjeremylt   if (vec->Norm) {
5972b730f8bSJeremy L Thompson     CeedCall(vec->Norm(vec, norm_type, norm));
598e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
599547d9b97Sjeremylt   }
600547d9b97Sjeremylt 
601547d9b97Sjeremylt   const CeedScalar *array;
6022b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array));
603547d9b97Sjeremylt 
604547d9b97Sjeremylt   *norm = 0.;
605d1d35e2fSjeremylt   switch (norm_type) {
606547d9b97Sjeremylt     case CEED_NORM_1:
60792ae7e47SJeremy L Thompson       for (CeedInt i = 0; i < vec->length; i++) {
608547d9b97Sjeremylt         *norm += fabs(array[i]);
609547d9b97Sjeremylt       }
610547d9b97Sjeremylt       break;
611547d9b97Sjeremylt     case CEED_NORM_2:
61292ae7e47SJeremy L Thompson       for (CeedInt i = 0; i < vec->length; i++) {
613547d9b97Sjeremylt         *norm += fabs(array[i]) * fabs(array[i]);
614547d9b97Sjeremylt       }
615547d9b97Sjeremylt       break;
616547d9b97Sjeremylt     case CEED_NORM_MAX:
61792ae7e47SJeremy L Thompson       for (CeedInt i = 0; i < vec->length; i++) {
618d1d35e2fSjeremylt         const CeedScalar abs_v_i = fabs(array[i]);
619d1d35e2fSjeremylt         *norm                    = *norm > abs_v_i ? *norm : abs_v_i;
620547d9b97Sjeremylt       }
621547d9b97Sjeremylt   }
6222b730f8bSJeremy L Thompson   if (norm_type == CEED_NORM_2) *norm = sqrt(*norm);
623547d9b97Sjeremylt 
6242b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &array));
625e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
626547d9b97Sjeremylt }
627547d9b97Sjeremylt 
628547d9b97Sjeremylt /**
629e0dd3b27Sjeremylt   @brief Compute x = alpha x
630e0dd3b27Sjeremylt 
63196b902e2Sjeremylt   @param[in,out] x     vector for scaling
63296b902e2Sjeremylt   @param[in]     alpha scaling factor
633e0dd3b27Sjeremylt 
634e0dd3b27Sjeremylt   @return An error code: 0 - success, otherwise - failure
635e0dd3b27Sjeremylt 
636e0dd3b27Sjeremylt   @ref User
637e0dd3b27Sjeremylt **/
638e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) {
63973380422SJeremy L Thompson   CeedScalar *x_array = NULL;
6401f9221feSJeremy L Thompson   CeedSize    n_x;
641e0dd3b27Sjeremylt 
6429c774eddSJeremy L Thompson   bool has_valid_array = true;
6432b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array));
6442b730f8bSJeremy L Thompson   if (!has_valid_array) {
6459c774eddSJeremy L Thompson     // LCOV_EXCL_START
6469c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
6472b730f8bSJeremy L Thompson                      "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray");
6489c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
6492b730f8bSJeremy L Thompson   }
6509c774eddSJeremy L Thompson 
6512b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
652e0dd3b27Sjeremylt 
653e0dd3b27Sjeremylt   // Backend implementation
6542b730f8bSJeremy L Thompson   if (x->Scale) return x->Scale(x, alpha);
655e0dd3b27Sjeremylt 
656e0dd3b27Sjeremylt   // Default implementation
6572b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array));
6582b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < n_x; i++) x_array[i] *= alpha;
6592b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(x, &x_array));
660e0dd3b27Sjeremylt 
661e0dd3b27Sjeremylt   return CEED_ERROR_SUCCESS;
662e0dd3b27Sjeremylt }
663e0dd3b27Sjeremylt 
664e0dd3b27Sjeremylt /**
6650f7fd0f8Sjeremylt   @brief Compute y = alpha x + y
6660f7fd0f8Sjeremylt 
66796b902e2Sjeremylt   @param[in,out] y     target vector for sum
66896b902e2Sjeremylt   @param[in]     alpha scaling factor
66996b902e2Sjeremylt   @param[in]     x     second vector, must be different than y
6700f7fd0f8Sjeremylt 
6710f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
6720f7fd0f8Sjeremylt 
6730f7fd0f8Sjeremylt   @ref User
6740f7fd0f8Sjeremylt **/
6750f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
67673380422SJeremy L Thompson   CeedScalar       *y_array = NULL;
67773380422SJeremy L Thompson   CeedScalar const *x_array = NULL;
6781f9221feSJeremy L Thompson   CeedSize          n_x, n_y;
6790f7fd0f8Sjeremylt 
6802b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &n_y));
6812b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
6822b730f8bSJeremy L Thompson   if (n_x != n_y) {
6830f7fd0f8Sjeremylt     // LCOV_EXCL_START
6842b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths");
6850f7fd0f8Sjeremylt     // LCOV_EXCL_STOP
6862b730f8bSJeremy L Thompson   }
6872b730f8bSJeremy L Thompson   if (x == y) {
6880f7fd0f8Sjeremylt     // LCOV_EXCL_START
6892b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY");
6900f7fd0f8Sjeremylt     // LCOV_EXCL_STOP
6912b730f8bSJeremy L Thompson   }
6920f7fd0f8Sjeremylt 
6939c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
6942b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
6952b730f8bSJeremy L Thompson   if (!has_valid_array_x) {
6969c774eddSJeremy L Thompson     // LCOV_EXCL_START
6972b730f8bSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6989c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
6992b730f8bSJeremy L Thompson   }
7002b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
7012b730f8bSJeremy L Thompson   if (!has_valid_array_y) {
7029c774eddSJeremy L Thompson     // LCOV_EXCL_START
7032b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7049c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
7052b730f8bSJeremy L Thompson   }
7069c774eddSJeremy L Thompson 
7072d04630dSjeremylt   Ceed ceed_parent_x, ceed_parent_y;
7082b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
7092b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
7102b730f8bSJeremy L Thompson   if (ceed_parent_x != ceed_parent_y) {
7112d04630dSjeremylt     // LCOV_EXCL_START
7122b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context");
7132d04630dSjeremylt     // LCOV_EXCL_STOP
7142b730f8bSJeremy L Thompson   }
7152d04630dSjeremylt 
7160f7fd0f8Sjeremylt   // Backend implementation
717eaf62fffSJeremy L Thompson   if (y->AXPY) {
7182b730f8bSJeremy L Thompson     CeedCall(y->AXPY(y, alpha, x));
719eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
720eaf62fffSJeremy L Thompson   }
7210f7fd0f8Sjeremylt 
7220f7fd0f8Sjeremylt   // Default implementation
7232b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array));
7242b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
7250f7fd0f8Sjeremylt 
7262b730f8bSJeremy L Thompson   assert(x_array);
7272b730f8bSJeremy L Thompson   assert(y_array);
72873380422SJeremy L Thompson 
7292b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i];
7300f7fd0f8Sjeremylt 
7312b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(y, &y_array));
7322b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
7330f7fd0f8Sjeremylt 
7340f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
7350f7fd0f8Sjeremylt }
7360f7fd0f8Sjeremylt 
7370f7fd0f8Sjeremylt /**
7385fb68f37SKaren (Ren) Stengel   @brief Compute y = alpha x + beta y
7395fb68f37SKaren (Ren) Stengel 
7405fb68f37SKaren (Ren) Stengel   @param[in,out] y     target vector for sum
7415fb68f37SKaren (Ren) Stengel   @param[in]     alpha first scaling factor
7425fb68f37SKaren (Ren) Stengel   @param[in]     beta  second scaling factor
7435fb68f37SKaren (Ren) Stengel   @param[in]     x     second vector, must be different than y
7445fb68f37SKaren (Ren) Stengel 
7455fb68f37SKaren (Ren) Stengel   @return An error code: 0 - success, otherwise - failure
7465fb68f37SKaren (Ren) Stengel 
7475fb68f37SKaren (Ren) Stengel   @ref User
7485fb68f37SKaren (Ren) Stengel **/
7495fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
7505fb68f37SKaren (Ren) Stengel   CeedScalar       *y_array = NULL;
7515fb68f37SKaren (Ren) Stengel   CeedScalar const *x_array = NULL;
7525fb68f37SKaren (Ren) Stengel   CeedSize          n_x, n_y;
7535fb68f37SKaren (Ren) Stengel 
7545fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetLength(y, &n_y));
7555fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetLength(x, &n_x));
7565fb68f37SKaren (Ren) Stengel   if (n_x != n_y) {
7575fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_START
7585fb68f37SKaren (Ren) Stengel     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths");
7595fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_STOP
7605fb68f37SKaren (Ren) Stengel   }
7615fb68f37SKaren (Ren) Stengel   if (x == y) {
7625fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_START
7635fb68f37SKaren (Ren) Stengel     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY");
7645fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_STOP
7655fb68f37SKaren (Ren) Stengel   }
7665fb68f37SKaren (Ren) Stengel 
7675fb68f37SKaren (Ren) Stengel   bool has_valid_array_x = true, has_valid_array_y = true;
7685fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
7695fb68f37SKaren (Ren) Stengel   if (!has_valid_array_x) {
7705fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_START
7715fb68f37SKaren (Ren) Stengel     return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7725fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_STOP
7735fb68f37SKaren (Ren) Stengel   }
7745fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
7755fb68f37SKaren (Ren) Stengel   if (!has_valid_array_y) {
7765fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_START
7775fb68f37SKaren (Ren) Stengel     return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7785fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_STOP
7795fb68f37SKaren (Ren) Stengel   }
7805fb68f37SKaren (Ren) Stengel 
7815fb68f37SKaren (Ren) Stengel   Ceed ceed_parent_x, ceed_parent_y;
7825fb68f37SKaren (Ren) Stengel   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
7835fb68f37SKaren (Ren) Stengel   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
7845fb68f37SKaren (Ren) Stengel   if (ceed_parent_x != ceed_parent_y) {
7855fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_START
7865fb68f37SKaren (Ren) Stengel     return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context");
7875fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_STOP
7885fb68f37SKaren (Ren) Stengel   }
7895fb68f37SKaren (Ren) Stengel 
7905fb68f37SKaren (Ren) Stengel   // Backend implementation
7915fb68f37SKaren (Ren) Stengel   if (y->AXPBY) {
7925fb68f37SKaren (Ren) Stengel     CeedCall(y->AXPBY(y, alpha, beta, x));
7935fb68f37SKaren (Ren) Stengel     return CEED_ERROR_SUCCESS;
7945fb68f37SKaren (Ren) Stengel   }
7955fb68f37SKaren (Ren) Stengel 
7965fb68f37SKaren (Ren) Stengel   // Default implementation
7975fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
7985fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
7995fb68f37SKaren (Ren) Stengel 
8005fb68f37SKaren (Ren) Stengel   assert(x_array);
8015fb68f37SKaren (Ren) Stengel   assert(y_array);
8025fb68f37SKaren (Ren) Stengel 
8035fb68f37SKaren (Ren) Stengel   for (CeedInt i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i] + beta * y_array[i];
8045fb68f37SKaren (Ren) Stengel 
8055fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArray(y, &y_array));
8065fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
8075fb68f37SKaren (Ren) Stengel 
8085fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
8095fb68f37SKaren (Ren) Stengel }
8105fb68f37SKaren (Ren) Stengel 
8115fb68f37SKaren (Ren) Stengel /**
812ea61e9acSJeremy L Thompson   @brief Compute the pointwise multiplication w = x .* y.
813ea61e9acSJeremy L Thompson            Any subset of x, y, and w may be the same vector.
8140f7fd0f8Sjeremylt 
81596b902e2Sjeremylt   @param[out] w target vector for the product
81696b902e2Sjeremylt   @param[in]  x first vector for product
81796b902e2Sjeremylt   @param[in]  y second vector for the product
8180f7fd0f8Sjeremylt 
8190f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
8200f7fd0f8Sjeremylt 
8210f7fd0f8Sjeremylt   @ref User
8220f7fd0f8Sjeremylt **/
8230f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
82473380422SJeremy L Thompson   CeedScalar       *w_array = NULL;
82573380422SJeremy L Thompson   CeedScalar const *x_array = NULL, *y_array = NULL;
8261f9221feSJeremy L Thompson   CeedSize          n_w, n_x, n_y;
8270f7fd0f8Sjeremylt 
8282b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(w, &n_w));
8292b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
8302b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &n_y));
8312b730f8bSJeremy L Thompson   if (n_w != n_x || n_w != n_y) {
8320f7fd0f8Sjeremylt     // LCOV_EXCL_START
8332b730f8bSJeremy L Thompson     return CeedError(w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths");
8340f7fd0f8Sjeremylt     // LCOV_EXCL_STOP
8352b730f8bSJeremy L Thompson   }
8360f7fd0f8Sjeremylt 
8372d04630dSjeremylt   Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y;
8382b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(w->ceed, &ceed_parent_w));
8392b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
8402b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
8412b730f8bSJeremy L Thompson   if ((ceed_parent_w != ceed_parent_x) || (ceed_parent_w != ceed_parent_y)) {
8422d04630dSjeremylt     // LCOV_EXCL_START
8432b730f8bSJeremy L Thompson     return CeedError(w->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors w, x, and y must be created by the same Ceed context");
8442d04630dSjeremylt     // LCOV_EXCL_STOP
8452b730f8bSJeremy L Thompson   }
8462d04630dSjeremylt 
8479c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
8482b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
8492b730f8bSJeremy L Thompson   if (!has_valid_array_x) {
8509c774eddSJeremy L Thompson     // LCOV_EXCL_START
8512b730f8bSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
8529c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
8532b730f8bSJeremy L Thompson   }
8542b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
8552b730f8bSJeremy L Thompson   if (!has_valid_array_y) {
8569c774eddSJeremy L Thompson     // LCOV_EXCL_START
8572b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
8589c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
8592b730f8bSJeremy L Thompson   }
8609c774eddSJeremy L Thompson 
8610f7fd0f8Sjeremylt   // Backend implementation
862eaf62fffSJeremy L Thompson   if (w->PointwiseMult) {
8632b730f8bSJeremy L Thompson     CeedCall(w->PointwiseMult(w, x, y));
864eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
865eaf62fffSJeremy L Thompson   }
8660f7fd0f8Sjeremylt 
8670f7fd0f8Sjeremylt   // Default implementation
8680f7fd0f8Sjeremylt   if (x != w) {
8692b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
8700f7fd0f8Sjeremylt   } else {
871c6d796c6SJeremy L Thompson     CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array));
8720f7fd0f8Sjeremylt     x_array = w_array;
8730f7fd0f8Sjeremylt   }
8740f7fd0f8Sjeremylt   if (y != w && y != x) {
8752b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array));
876c6d796c6SJeremy L Thompson   } else if (y == x) {
8770f7fd0f8Sjeremylt     y_array = x_array;
878c6d796c6SJeremy L Thompson   } else {
879c6d796c6SJeremy L Thompson     CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array));
880c6d796c6SJeremy L Thompson     y_array = w_array;
8810f7fd0f8Sjeremylt   }
882c6d796c6SJeremy L Thompson   if (!w_array) CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array));
8830f7fd0f8Sjeremylt 
8842b730f8bSJeremy L Thompson   assert(w_array);
8852b730f8bSJeremy L Thompson   assert(x_array);
8862b730f8bSJeremy L Thompson   assert(y_array);
88773380422SJeremy L Thompson 
8882b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i];
8890f7fd0f8Sjeremylt 
8902b730f8bSJeremy L Thompson   if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array));
8912b730f8bSJeremy L Thompson   if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
8922b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(w, &w_array));
8930f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
8940f7fd0f8Sjeremylt }
8950f7fd0f8Sjeremylt 
8960f7fd0f8Sjeremylt /**
897d99fa3c5SJeremy L Thompson   @brief Take the reciprocal of a CeedVector.
898d99fa3c5SJeremy L Thompson 
899ea61e9acSJeremy L Thompson   @param[in,out] vec CeedVector to take reciprocal
900d99fa3c5SJeremy L Thompson 
901d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
902d99fa3c5SJeremy L Thompson 
903d99fa3c5SJeremy L Thompson   @ref User
904d99fa3c5SJeremy L Thompson **/
905d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) {
9069c774eddSJeremy L Thompson   bool has_valid_array = true;
9072b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
9082b730f8bSJeremy L Thompson 
9092b730f8bSJeremy L Thompson   if (!has_valid_array) {
9109c774eddSJeremy L Thompson     // LCOV_EXCL_START
9119c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
9122b730f8bSJeremy L Thompson                      "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray");
9139c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
9142b730f8bSJeremy L Thompson   }
9159c774eddSJeremy L Thompson 
916d99fa3c5SJeremy L Thompson   // Check if vector data set
9172b730f8bSJeremy L Thompson   if (!vec->state) {
918d99fa3c5SJeremy L Thompson     // LCOV_EXCL_START
9192b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal");
920d99fa3c5SJeremy L Thompson     // LCOV_EXCL_STOP
9212b730f8bSJeremy L Thompson   }
922d99fa3c5SJeremy L Thompson 
923d99fa3c5SJeremy L Thompson   // Backend impl for GPU, if added
924d99fa3c5SJeremy L Thompson   if (vec->Reciprocal) {
9252b730f8bSJeremy L Thompson     CeedCall(vec->Reciprocal(vec));
926e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
927d99fa3c5SJeremy L Thompson   }
928d99fa3c5SJeremy L Thompson 
9291f9221feSJeremy L Thompson   CeedSize len;
9302b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &len));
931d99fa3c5SJeremy L Thompson   CeedScalar *array;
9322b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array));
9332b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < len; i++) {
9342b730f8bSJeremy L Thompson     if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i];
9352b730f8bSJeremy L Thompson   }
936d99fa3c5SJeremy L Thompson 
9372b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(vec, &array));
938e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
939d99fa3c5SJeremy L Thompson }
940d99fa3c5SJeremy L Thompson 
941d99fa3c5SJeremy L Thompson /**
9427a982d89SJeremy L. Thompson   @brief View a CeedVector
9430436c2adSjeremylt 
944acb2c48cSJeremy L Thompson           Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`.
945acb2c48cSJeremy L Thompson                 Any portion of the provided range that is outside the range of valid indices for the CeedVector will be ignored.
946acb2c48cSJeremy L Thompson 
947acb2c48cSJeremy L Thompson   @param[in] vec    CeedVector to view
948acb2c48cSJeremy L Thompson   @param[in] start  Index of first CeedVector entry to view
949acb2c48cSJeremy L Thompson   @param[in] stop   Index of last CeedVector entry to view
950acb2c48cSJeremy L Thompson   @param[in] step   Step between CeedVector entries to view
951acb2c48cSJeremy L Thompson   @param[in] fp_fmt Printing format
952acb2c48cSJeremy L Thompson   @param[in] stream Filestream to write to
953acb2c48cSJeremy L Thompson 
954acb2c48cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
955acb2c48cSJeremy L Thompson 
956acb2c48cSJeremy L Thompson   @ref User
957acb2c48cSJeremy L Thompson **/
958acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) {
959acb2c48cSJeremy L Thompson   const CeedScalar *x;
960acb2c48cSJeremy L Thompson   char              fmt[1024];
961acb2c48cSJeremy L Thompson 
962acb2c48cSJeremy L Thompson   if (step == 0) return CeedError(vec->ceed, CEED_ERROR_MINOR, "View range 'step' must be nonzero");
963acb2c48cSJeremy L Thompson 
964acb2c48cSJeremy L Thompson   fprintf(stream, "CeedVector length %ld\n", (long)vec->length);
965acb2c48cSJeremy L Thompson   if (start != 0 || stop != vec->length || step != 1) {
966acb2c48cSJeremy L Thompson     fprintf(stream, "  start: %ld\n  stop:  %ld\n  step:  %" CeedInt_FMT "\n", (long)start, (long)stop, step);
967acb2c48cSJeremy L Thompson   }
968acb2c48cSJeremy L Thompson   if (start > vec->length) start = vec->length;
969acb2c48cSJeremy L Thompson   if (stop > vec->length) stop = vec->length;
970acb2c48cSJeremy L Thompson 
971acb2c48cSJeremy L Thompson   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
972acb2c48cSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x));
973acb2c48cSJeremy L Thompson   for (CeedInt i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]);
974acb2c48cSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &x));
975acb2c48cSJeremy L Thompson   if (stop != vec->length) fprintf(stream, "  ...\n");
976acb2c48cSJeremy L Thompson 
977acb2c48cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
978acb2c48cSJeremy L Thompson }
979acb2c48cSJeremy L Thompson 
980acb2c48cSJeremy L Thompson /**
981acb2c48cSJeremy L Thompson   @brief View a CeedVector
982acb2c48cSJeremy L Thompson 
9830a0da059Sjeremylt   @param[in] vec    CeedVector to view
984d1d35e2fSjeremylt   @param[in] fp_fmt Printing format
9850a0da059Sjeremylt   @param[in] stream Filestream to write to
9860a0da059Sjeremylt 
9870436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9880436c2adSjeremylt 
9897a982d89SJeremy L. Thompson   @ref User
9900436c2adSjeremylt **/
991d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
992acb2c48cSJeremy L Thompson   CeedCall(CeedVectorViewRange(vec, 0, vec->length, 1, fp_fmt, stream));
993e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9940436c2adSjeremylt }
9950436c2adSjeremylt 
9960436c2adSjeremylt /**
997b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedVector
998b7c9bbdaSJeremy L Thompson 
999ea61e9acSJeremy L Thompson   @param[in]  vec  CeedVector to retrieve state
1000b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store ceed
1001b7c9bbdaSJeremy L Thompson 
1002b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1003b7c9bbdaSJeremy L Thompson 
1004b7c9bbdaSJeremy L Thompson   @ref Advanced
1005b7c9bbdaSJeremy L Thompson **/
1006b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
1007b7c9bbdaSJeremy L Thompson   *ceed = vec->ceed;
1008b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1009b7c9bbdaSJeremy L Thompson }
1010b7c9bbdaSJeremy L Thompson 
1011b7c9bbdaSJeremy L Thompson /**
10127a982d89SJeremy L. Thompson   @brief Get the length of a CeedVector
10130436c2adSjeremylt 
1014ea61e9acSJeremy L Thompson   @param[in]  vec    CeedVector to retrieve length
10157a982d89SJeremy L. Thompson   @param[out] length Variable to store length
10160436c2adSjeremylt 
10170436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
10180436c2adSjeremylt 
10197a982d89SJeremy L. Thompson   @ref User
10200436c2adSjeremylt **/
10211f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) {
10227a982d89SJeremy L. Thompson   *length = vec->length;
1023e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10240436c2adSjeremylt }
10250436c2adSjeremylt 
10260436c2adSjeremylt /**
10270436c2adSjeremylt   @brief Destroy a CeedVector
10280436c2adSjeremylt 
1029ea61e9acSJeremy L Thompson   @param[in,out] vec CeedVector to destroy
10300436c2adSjeremylt 
10310436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
10320436c2adSjeremylt 
10337a982d89SJeremy L. Thompson   @ref User
10340436c2adSjeremylt **/
10350436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) {
1036*393ac2cdSJeremy L Thompson   if (!*vec || *vec == CEED_VECTOR_ACTIVE && *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) {
1037ad6481ceSJeremy L Thompson     *vec = NULL;
1038ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1039ad6481ceSJeremy L Thompson   }
10402b730f8bSJeremy L Thompson   if (((*vec)->state % 2) == 1) {
10412b730f8bSJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use");
10422b730f8bSJeremy L Thompson   }
10432b730f8bSJeremy L Thompson   if ((*vec)->num_readers > 0) {
1044e92b641fSJeremy L Thompson     // LCOV_EXCL_START
10452b730f8bSJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access");
1046e92b641fSJeremy L Thompson     // LCOV_EXCL_STOP
10470436c2adSjeremylt   }
10480436c2adSjeremylt 
10492b730f8bSJeremy L Thompson   if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec));
10502b730f8bSJeremy L Thompson 
10512b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*vec)->ceed));
10522b730f8bSJeremy L Thompson   CeedCall(CeedFree(vec));
1053e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10540436c2adSjeremylt }
10550436c2adSjeremylt 
10560436c2adSjeremylt /// @}
1057