xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-vector.c (revision 4385fb7f4421da9c7ce289d92dc27dadf16843ac)
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.
206*4385fb7fSSebastian Grimberg 
207ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedVectorDestroy()`.
208512bb800SJeremy L Thompson 
209512bb800SJeremy 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.
210512bb800SJeremy L Thompson         This CeedVector will be destroyed if `vec_copy` is the only reference to this CeedVector.
2119560d06aSjeremylt 
212ea61e9acSJeremy L Thompson   @param[in]     vec      CeedVector to copy reference to
213ea61e9acSJeremy L Thompson   @param[in,out] vec_copy Variable to store copied reference
2149560d06aSjeremylt 
2159560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
2169560d06aSjeremylt 
2179560d06aSjeremylt   @ref User
2189560d06aSjeremylt **/
2199560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) {
220393ac2cdSJeremy L Thompson   if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorReference(vec));
2212b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(vec_copy));
2229560d06aSjeremylt   *vec_copy = vec;
2239560d06aSjeremylt   return CEED_ERROR_SUCCESS;
2249560d06aSjeremylt }
2259560d06aSjeremylt 
2269560d06aSjeremylt /**
2275fb68f37SKaren (Ren) Stengel   @brief Copy a CeedVector into a different CeedVector.
228*4385fb7fSSebastian Grimberg 
2295fb68f37SKaren (Ren) Stengel   Both pointers should be destroyed with `CeedVectorDestroy()`.
230*4385fb7fSSebastian Grimberg 
2315fb68f37SKaren (Ren) Stengel   Note: If `*vec_copy` is non-NULL, then it is assumed that `*vec_copy` is a pointer to a CeedVector.
2325fb68f37SKaren (Ren) Stengel         This CeedVector will be destroyed if `*vec_copy` is the only reference to this CeedVector.
2335fb68f37SKaren (Ren) Stengel 
2345fb68f37SKaren (Ren) Stengel   @param[in]     vec      CeedVector to copy
2355fb68f37SKaren (Ren) Stengel   @param[in,out] vec_copy Variable to store copied CeedVector to
2365fb68f37SKaren (Ren) Stengel 
2375fb68f37SKaren (Ren) Stengel   @return An error code: 0 - success, otherwise - failure
2385fb68f37SKaren (Ren) Stengel 
2395fb68f37SKaren (Ren) Stengel   @ref User
2405fb68f37SKaren (Ren) Stengel **/
2415fb68f37SKaren (Ren) Stengel int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) {
2425fb68f37SKaren (Ren) Stengel   Ceed        ceed;
2435fb68f37SKaren (Ren) Stengel   CeedMemType mem_type, mem_type_copy;
2445fb68f37SKaren (Ren) Stengel   CeedScalar *array;
2455fb68f37SKaren (Ren) Stengel 
2465fb68f37SKaren (Ren) Stengel   // Get the preferred memory type
2475fb68f37SKaren (Ren) Stengel   CeedVectorGetCeed(vec, &ceed);
2485fb68f37SKaren (Ren) Stengel   CeedGetPreferredMemType(ceed, &mem_type);
2495fb68f37SKaren (Ren) Stengel 
2505fb68f37SKaren (Ren) Stengel   // Get the preferred memory type
2515fb68f37SKaren (Ren) Stengel   CeedVectorGetCeed(vec_copy, &ceed);
2525fb68f37SKaren (Ren) Stengel   CeedGetPreferredMemType(ceed, &mem_type_copy);
2535fb68f37SKaren (Ren) Stengel 
2545fb68f37SKaren (Ren) Stengel   // Check that both have same memory type
2555fb68f37SKaren (Ren) Stengel   if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST;
2565fb68f37SKaren (Ren) Stengel 
2575fb68f37SKaren (Ren) Stengel   // Copy the values from vec to vec_copy
2585fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArray(vec, mem_type, &array));
2595fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorSetArray(vec_copy, mem_type, CEED_COPY_VALUES, array));
2605fb68f37SKaren (Ren) Stengel 
2615fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArray(vec, &array));
2625fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
2635fb68f37SKaren (Ren) Stengel }
2645fb68f37SKaren (Ren) Stengel 
2655fb68f37SKaren (Ren) Stengel /**
266ea61e9acSJeremy L Thompson   @brief Set the array used by a CeedVector, freeing any previously allocated array if applicable.
267*4385fb7fSSebastian Grimberg 
268ea61e9acSJeremy L Thompson   The backend may copy values to a different memtype, such as during @ref CeedOperatorApply().
2696a6c615bSJeremy L Thompson   See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray().
2700436c2adSjeremylt 
271ea61e9acSJeremy L Thompson   @param[in,out] vec       CeedVector
272ea61e9acSJeremy L Thompson   @param[in]     mem_type  Memory type of the array being passed
273ea61e9acSJeremy L Thompson   @param[in]     copy_mode Copy mode for the array
274ea61e9acSJeremy L Thompson   @param[in]     array     Array to be used, or NULL with @ref CEED_COPY_VALUES to have the library allocate
2750436c2adSjeremylt 
2760436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2770436c2adSjeremylt 
2787a982d89SJeremy L. Thompson   @ref User
2790436c2adSjeremylt **/
2802b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) {
2812b730f8bSJeremy L Thompson   if (!vec->SetArray) {
2820436c2adSjeremylt     // LCOV_EXCL_START
2832b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray");
2840436c2adSjeremylt     // LCOV_EXCL_STOP
2852b730f8bSJeremy L Thompson   }
2862b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
2872b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
2882b730f8bSJeremy L Thompson   }
2892b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
2902b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
2912b730f8bSJeremy L Thompson   }
2920436c2adSjeremylt 
2932b730f8bSJeremy L Thompson   CeedCall(vec->SetArray(vec, mem_type, copy_mode, array));
2940436c2adSjeremylt   vec->state += 2;
295e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2960436c2adSjeremylt }
2970436c2adSjeremylt 
2980436c2adSjeremylt /**
2990436c2adSjeremylt   @brief Set the CeedVector to a constant value
3000436c2adSjeremylt 
301ea61e9acSJeremy L Thompson   @param[in,out] vec   CeedVector
3020436c2adSjeremylt   @param[in]     value Value to be used
3030436c2adSjeremylt 
3040436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3050436c2adSjeremylt 
3067a982d89SJeremy L. Thompson   @ref User
3070436c2adSjeremylt **/
3080436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) {
3092b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
3109c774eddSJeremy L Thompson     // LCOV_EXCL_START
3112b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
3129c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
3132b730f8bSJeremy L Thompson   }
3142b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
3159c774eddSJeremy L Thompson     // LCOV_EXCL_START
3162b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
3179c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
3182b730f8bSJeremy L Thompson   }
3190436c2adSjeremylt 
3200436c2adSjeremylt   if (vec->SetValue) {
3212b730f8bSJeremy L Thompson     CeedCall(vec->SetValue(vec, value));
3220436c2adSjeremylt   } else {
3230436c2adSjeremylt     CeedScalar *array;
3242b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array));
32592ae7e47SJeremy L Thompson     for (CeedInt i = 0; i < vec->length; i++) array[i] = value;
3262b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(vec, &array));
3270436c2adSjeremylt   }
3280436c2adSjeremylt   vec->state += 2;
329e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3300436c2adSjeremylt }
3310436c2adSjeremylt 
3320436c2adSjeremylt /**
333ea61e9acSJeremy L Thompson   @brief Sync the CeedVector to a specified memtype.
334*4385fb7fSSebastian Grimberg 
335ea61e9acSJeremy L Thompson   This function is used to force synchronization of arrays set with @ref CeedVectorSetArray().
336ea61e9acSJeremy L Thompson   If the requested memtype is already synchronized, this function results in a no-op.
3370436c2adSjeremylt 
338ea61e9acSJeremy L Thompson   @param[in,out] vec      CeedVector
339ea61e9acSJeremy L Thompson   @param[in]     mem_type Memtype to be synced
3400436c2adSjeremylt 
3410436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3420436c2adSjeremylt 
3437a982d89SJeremy L. Thompson   @ref User
3440436c2adSjeremylt **/
345d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) {
3462b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
3472b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use");
3482b730f8bSJeremy L Thompson   }
3490436c2adSjeremylt 
3500436c2adSjeremylt   if (vec->SyncArray) {
3512b730f8bSJeremy L Thompson     CeedCall(vec->SyncArray(vec, mem_type));
3520436c2adSjeremylt   } else {
3530436c2adSjeremylt     const CeedScalar *array;
3542b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array));
3552b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(vec, &array));
3560436c2adSjeremylt   }
357e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3580436c2adSjeremylt }
3590436c2adSjeremylt 
3600436c2adSjeremylt /**
361ea61e9acSJeremy 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.
362*4385fb7fSSebastian Grimberg 
3639c774eddSJeremy L Thompson   The caller is responsible for managing and freeing the array.
364ea61e9acSJeremy L Thompson   This function will error if @ref CeedVectorSetArray() was not previously called with @ref CEED_USE_POINTER for the corresponding mem_type.
3656a6c615bSJeremy L Thompson 
366ea61e9acSJeremy L Thompson   @param[in,out] vec      CeedVector
367ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to take the array.
368ea61e9acSJeremy L Thompson                             If the backend uses a different memory type, this will perform a copy.
369ea61e9acSJeremy L Thompson   @param[out]    array    Array on memory type mem_type, or NULL if array pointer is not required
3706a6c615bSJeremy L Thompson 
3716a6c615bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3726a6c615bSJeremy L Thompson 
3736a6c615bSJeremy L Thompson   @ref User
3746a6c615bSJeremy L Thompson **/
3752b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
3762b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
3772b730f8bSJeremy L Thompson     // LCOV_EXCL_START
3782b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use");
3792b730f8bSJeremy L Thompson     // LCOV_EXCL_STOP
3802b730f8bSJeremy L Thompson   }
3812b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
3822b730f8bSJeremy L Thompson     // LCOV_EXCL_START
3832b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access");
3842b730f8bSJeremy L Thompson     // LCOV_EXCL_STOP
3852b730f8bSJeremy L Thompson   }
3866a6c615bSJeremy L Thompson 
38750c643e1SJed Brown   CeedScalar *temp_array = NULL;
38850c643e1SJed Brown   if (vec->length > 0) {
3899c774eddSJeremy L Thompson     bool has_borrowed_array_of_type = true;
3902b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type));
3912b730f8bSJeremy L Thompson     if (!has_borrowed_array_of_type) {
3929c774eddSJeremy L Thompson       // LCOV_EXCL_START
3932b730f8bSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND, "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray",
3942b730f8bSJeremy L Thompson                        CeedMemTypes[mem_type]);
3959c774eddSJeremy L Thompson       // LCOV_EXCL_STOP
3962b730f8bSJeremy L Thompson     }
3979c774eddSJeremy L Thompson 
3989c774eddSJeremy L Thompson     bool has_valid_array = true;
3992b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
4002b730f8bSJeremy L Thompson     if (!has_valid_array) {
4019c774eddSJeremy L Thompson       // LCOV_EXCL_START
4029c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
4032b730f8bSJeremy L Thompson                        "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray");
4049c774eddSJeremy L Thompson       // LCOV_EXCL_STOP
4052b730f8bSJeremy L Thompson     }
4069c774eddSJeremy L Thompson 
4072b730f8bSJeremy L Thompson     CeedCall(vec->TakeArray(vec, mem_type, &temp_array));
40850c643e1SJed Brown   }
409d1d35e2fSjeremylt   if (array) (*array) = temp_array;
410e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4116a6c615bSJeremy L Thompson }
4126a6c615bSJeremy L Thompson 
4136a6c615bSJeremy L Thompson /**
414b3cf021fSjeremylt   @brief Get read/write access to a CeedVector via the specified memory type.
415*4385fb7fSSebastian Grimberg 
416b3cf021fSjeremylt   Restore access with @ref CeedVectorRestoreArray().
4170436c2adSjeremylt 
418ea61e9acSJeremy L Thompson   @param[in,out] vec      CeedVector to access
419ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to access the array.
420ea61e9acSJeremy L Thompson                             If the backend uses a different memory type, this will perform a copy.
421d1d35e2fSjeremylt   @param[out]    array    Array on memory type mem_type
4220436c2adSjeremylt 
423ea61e9acSJeremy L Thompson   @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide access to array pointers in the desired memory space.
424ea61e9acSJeremy L Thompson     Pairing get/restore allows the Vector to track access, thus knowing if norms or other operations may need to be recomputed.
4250436c2adSjeremylt 
4260436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4270436c2adSjeremylt 
4287a982d89SJeremy L. Thompson   @ref User
4290436c2adSjeremylt **/
4302b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
4312b730f8bSJeremy L Thompson   if (!vec->GetArray) {
4320436c2adSjeremylt     // LCOV_EXCL_START
4332b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray");
4340436c2adSjeremylt     // LCOV_EXCL_STOP
4352b730f8bSJeremy L Thompson   }
4362b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
4372b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
4382b730f8bSJeremy L Thompson   }
4392b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
4402b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
4412b730f8bSJeremy L Thompson   }
4420436c2adSjeremylt 
4439c774eddSJeremy L Thompson   bool has_valid_array = true;
4442b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
4452b730f8bSJeremy L Thompson   if (!has_valid_array) {
4469c774eddSJeremy L Thompson     // LCOV_EXCL_START
4479c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
4482b730f8bSJeremy L Thompson                      "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
4499c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
4502b730f8bSJeremy L Thompson   }
4519c774eddSJeremy L Thompson 
4522b730f8bSJeremy L Thompson   CeedCall(vec->GetArray(vec, mem_type, array));
45328bfd0b7SJeremy L Thompson   vec->state++;
454e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4550436c2adSjeremylt }
4560436c2adSjeremylt 
4570436c2adSjeremylt /**
458b3cf021fSjeremylt   @brief Get read-only access to a CeedVector via the specified memory type.
459*4385fb7fSSebastian Grimberg 
460b3cf021fSjeremylt   Restore access with @ref CeedVectorRestoreArrayRead().
4610436c2adSjeremylt 
462ea61e9acSJeremy L Thompson   @param[in]  vec      CeedVector to access
463*4385fb7fSSebastian Grimberg   @param[in]  mem_type Memory type on which to access the array. If the backend uses a different memory type, this will perform a copy (possibly
464*4385fb7fSSebastian Grimberg cached).
465d1d35e2fSjeremylt   @param[out] array    Array on memory type mem_type
4660436c2adSjeremylt 
4670436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4680436c2adSjeremylt 
4697a982d89SJeremy L. Thompson   @ref User
4700436c2adSjeremylt **/
4712b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
4722b730f8bSJeremy L Thompson   if (!vec->GetArrayRead) {
4730436c2adSjeremylt     // LCOV_EXCL_START
4742b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead");
4750436c2adSjeremylt     // LCOV_EXCL_STOP
4762b730f8bSJeremy L Thompson   }
4772b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
4782b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use");
4792b730f8bSJeremy L Thompson   }
4800436c2adSjeremylt 
48150c643e1SJed Brown   if (vec->length > 0) {
4829c774eddSJeremy L Thompson     bool has_valid_array = true;
4832b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
4842b730f8bSJeremy L Thompson     if (!has_valid_array) {
4859c774eddSJeremy L Thompson       // LCOV_EXCL_START
4869c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
4872b730f8bSJeremy L Thompson                        "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
4889c774eddSJeremy L Thompson       // LCOV_EXCL_STOP
4892b730f8bSJeremy L Thompson     }
4909c774eddSJeremy L Thompson 
4912b730f8bSJeremy L Thompson     CeedCall(vec->GetArrayRead(vec, mem_type, array));
49250c643e1SJed Brown   } else {
49350c643e1SJed Brown     *array = NULL;
49450c643e1SJed Brown   }
495d1d35e2fSjeremylt   vec->num_readers++;
496e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4970436c2adSjeremylt }
4980436c2adSjeremylt 
4990436c2adSjeremylt /**
5009c774eddSJeremy L Thompson   @brief Get write access to a CeedVector via the specified memory type.
501*4385fb7fSSebastian Grimberg 
502ea61e9acSJeremy L Thompson   Restore access with @ref CeedVectorRestoreArray().
503ea61e9acSJeremy L Thompson   All old values should be assumed to be invalid.
5049c774eddSJeremy L Thompson 
505ea61e9acSJeremy L Thompson   @param[in,out] vec      CeedVector to access
506ea61e9acSJeremy L Thompson   @param[in]     mem_type Memory type on which to access the array.
5079c774eddSJeremy L Thompson   @param[out]    array    Array on memory type mem_type
5089c774eddSJeremy L Thompson 
5099c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5109c774eddSJeremy L Thompson 
5119c774eddSJeremy L Thompson   @ref User
5129c774eddSJeremy L Thompson **/
5132b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
5142b730f8bSJeremy L Thompson   if (!vec->GetArrayWrite) {
5159c774eddSJeremy L Thompson     // LCOV_EXCL_START
5162b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayWrite");
5179c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5182b730f8bSJeremy L Thompson   }
5192b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
5209c774eddSJeremy L Thompson     // LCOV_EXCL_START
5212b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
5229c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5232b730f8bSJeremy L Thompson   }
5242b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
5259c774eddSJeremy L Thompson     // LCOV_EXCL_START
5262b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
5279c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5282b730f8bSJeremy L Thompson   }
5299c774eddSJeremy L Thompson 
5302b730f8bSJeremy L Thompson   CeedCall(vec->GetArrayWrite(vec, mem_type, array));
53128bfd0b7SJeremy L Thompson   vec->state++;
5329c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5339c774eddSJeremy L Thompson }
5349c774eddSJeremy L Thompson 
5359c774eddSJeremy L Thompson /**
536ea61e9acSJeremy L Thompson   @brief Restore an array obtained using @ref CeedVectorGetArray() or @ref CeedVectorGetArrayWrite()
5370436c2adSjeremylt 
538ea61e9acSJeremy L Thompson   @param[in,out] vec   CeedVector to restore
539ea61e9acSJeremy L Thompson   @param[in,out] array Array of vector data
5400436c2adSjeremylt 
5410436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5420436c2adSjeremylt 
5437a982d89SJeremy L. Thompson   @ref User
5440436c2adSjeremylt **/
5450436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
5462b730f8bSJeremy L Thompson   if (vec->state % 2 != 1) {
5472b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted");
548706efda3SJeremy L Thompson   }
5492b730f8bSJeremy L Thompson   if (vec->RestoreArray) CeedCall(vec->RestoreArray(vec));
5500436c2adSjeremylt   *array = NULL;
55128bfd0b7SJeremy L Thompson   vec->state++;
552e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5530436c2adSjeremylt }
5540436c2adSjeremylt 
5550436c2adSjeremylt /**
556b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArrayRead()
5570436c2adSjeremylt 
558ea61e9acSJeremy L Thompson   @param[in]     vec   CeedVector to restore
559ea61e9acSJeremy L Thompson   @param[in,out] array Array of vector data
5600436c2adSjeremylt 
5610436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5620436c2adSjeremylt 
5637a982d89SJeremy L. Thompson   @ref User
5640436c2adSjeremylt **/
5650436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) {
5662b730f8bSJeremy L Thompson   if (vec->num_readers == 0) {
5679c774eddSJeremy L Thompson     // LCOV_EXCL_START
5682b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted");
5699c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5702b730f8bSJeremy L Thompson   }
5719c774eddSJeremy L Thompson 
57275a19770SJeremy L Thompson   vec->num_readers--;
5732b730f8bSJeremy L Thompson   if (vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec));
5740436c2adSjeremylt   *array = NULL;
57575a19770SJeremy L Thompson 
576e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5770436c2adSjeremylt }
5780436c2adSjeremylt 
5790436c2adSjeremylt /**
580171f8ca9Sjeremylt   @brief Get the norm of a CeedVector.
581171f8ca9Sjeremylt 
582ea61e9acSJeremy L Thompson   Note: This operation is local to the CeedVector.
583ea61e9acSJeremy 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
584ea61e9acSJeremy L Thompson duplicated or hanging nodes.
585547d9b97Sjeremylt 
586ea61e9acSJeremy L Thompson   @param[in]  vec       CeedVector to retrieve maximum value
587ea61e9acSJeremy L Thompson   @param[in]  norm_type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
588547d9b97Sjeremylt   @param[out] norm      Variable to store norm value
589547d9b97Sjeremylt 
590547d9b97Sjeremylt   @return An error code: 0 - success, otherwise - failure
591547d9b97Sjeremylt 
5927a982d89SJeremy L. Thompson   @ref User
593547d9b97Sjeremylt **/
594d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
5959c774eddSJeremy L Thompson   bool has_valid_array = true;
5962b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
5972b730f8bSJeremy L Thompson   if (!has_valid_array) {
5989c774eddSJeremy L Thompson     // LCOV_EXCL_START
5999c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
6002b730f8bSJeremy L Thompson                      "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray");
6019c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
6022b730f8bSJeremy L Thompson   }
6039c774eddSJeremy L Thompson 
604547d9b97Sjeremylt   // Backend impl for GPU, if added
605547d9b97Sjeremylt   if (vec->Norm) {
6062b730f8bSJeremy L Thompson     CeedCall(vec->Norm(vec, norm_type, norm));
607e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
608547d9b97Sjeremylt   }
609547d9b97Sjeremylt 
610547d9b97Sjeremylt   const CeedScalar *array;
6112b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array));
612547d9b97Sjeremylt 
613547d9b97Sjeremylt   *norm = 0.;
614d1d35e2fSjeremylt   switch (norm_type) {
615547d9b97Sjeremylt     case CEED_NORM_1:
61692ae7e47SJeremy L Thompson       for (CeedInt i = 0; i < vec->length; i++) {
617547d9b97Sjeremylt         *norm += fabs(array[i]);
618547d9b97Sjeremylt       }
619547d9b97Sjeremylt       break;
620547d9b97Sjeremylt     case CEED_NORM_2:
62192ae7e47SJeremy L Thompson       for (CeedInt i = 0; i < vec->length; i++) {
622547d9b97Sjeremylt         *norm += fabs(array[i]) * fabs(array[i]);
623547d9b97Sjeremylt       }
624547d9b97Sjeremylt       break;
625547d9b97Sjeremylt     case CEED_NORM_MAX:
62692ae7e47SJeremy L Thompson       for (CeedInt i = 0; i < vec->length; i++) {
627d1d35e2fSjeremylt         const CeedScalar abs_v_i = fabs(array[i]);
628d1d35e2fSjeremylt         *norm                    = *norm > abs_v_i ? *norm : abs_v_i;
629547d9b97Sjeremylt       }
630547d9b97Sjeremylt   }
6312b730f8bSJeremy L Thompson   if (norm_type == CEED_NORM_2) *norm = sqrt(*norm);
632547d9b97Sjeremylt 
6332b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &array));
634e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
635547d9b97Sjeremylt }
636547d9b97Sjeremylt 
637547d9b97Sjeremylt /**
638e0dd3b27Sjeremylt   @brief Compute x = alpha x
639e0dd3b27Sjeremylt 
64096b902e2Sjeremylt   @param[in,out] x     vector for scaling
64196b902e2Sjeremylt   @param[in]     alpha scaling factor
642e0dd3b27Sjeremylt 
643e0dd3b27Sjeremylt   @return An error code: 0 - success, otherwise - failure
644e0dd3b27Sjeremylt 
645e0dd3b27Sjeremylt   @ref User
646e0dd3b27Sjeremylt **/
647e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) {
64873380422SJeremy L Thompson   CeedScalar *x_array = NULL;
6491f9221feSJeremy L Thompson   CeedSize    n_x;
650e0dd3b27Sjeremylt 
6519c774eddSJeremy L Thompson   bool has_valid_array = true;
6522b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array));
6532b730f8bSJeremy L Thompson   if (!has_valid_array) {
6549c774eddSJeremy L Thompson     // LCOV_EXCL_START
6559c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
6562b730f8bSJeremy L Thompson                      "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray");
6579c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
6582b730f8bSJeremy L Thompson   }
6599c774eddSJeremy L Thompson 
6602b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
661e0dd3b27Sjeremylt 
662e0dd3b27Sjeremylt   // Backend implementation
6632b730f8bSJeremy L Thompson   if (x->Scale) return x->Scale(x, alpha);
664e0dd3b27Sjeremylt 
665e0dd3b27Sjeremylt   // Default implementation
6662b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array));
6672b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < n_x; i++) x_array[i] *= alpha;
6682b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(x, &x_array));
669e0dd3b27Sjeremylt 
670e0dd3b27Sjeremylt   return CEED_ERROR_SUCCESS;
671e0dd3b27Sjeremylt }
672e0dd3b27Sjeremylt 
673e0dd3b27Sjeremylt /**
6740f7fd0f8Sjeremylt   @brief Compute y = alpha x + y
6750f7fd0f8Sjeremylt 
67696b902e2Sjeremylt   @param[in,out] y     target vector for sum
67796b902e2Sjeremylt   @param[in]     alpha scaling factor
67896b902e2Sjeremylt   @param[in]     x     second vector, must be different than y
6790f7fd0f8Sjeremylt 
6800f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
6810f7fd0f8Sjeremylt 
6820f7fd0f8Sjeremylt   @ref User
6830f7fd0f8Sjeremylt **/
6840f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
68573380422SJeremy L Thompson   CeedScalar       *y_array = NULL;
68673380422SJeremy L Thompson   CeedScalar const *x_array = NULL;
6871f9221feSJeremy L Thompson   CeedSize          n_x, n_y;
6880f7fd0f8Sjeremylt 
6892b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &n_y));
6902b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
6912b730f8bSJeremy L Thompson   if (n_x != n_y) {
6920f7fd0f8Sjeremylt     // LCOV_EXCL_START
6932b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths");
6940f7fd0f8Sjeremylt     // LCOV_EXCL_STOP
6952b730f8bSJeremy L Thompson   }
6962b730f8bSJeremy L Thompson   if (x == y) {
6970f7fd0f8Sjeremylt     // LCOV_EXCL_START
6982b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY");
6990f7fd0f8Sjeremylt     // LCOV_EXCL_STOP
7002b730f8bSJeremy L Thompson   }
7010f7fd0f8Sjeremylt 
7029c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
7032b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
7042b730f8bSJeremy L Thompson   if (!has_valid_array_x) {
7059c774eddSJeremy L Thompson     // LCOV_EXCL_START
7062b730f8bSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7079c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
7082b730f8bSJeremy L Thompson   }
7092b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
7102b730f8bSJeremy L Thompson   if (!has_valid_array_y) {
7119c774eddSJeremy L Thompson     // LCOV_EXCL_START
7122b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7139c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
7142b730f8bSJeremy L Thompson   }
7159c774eddSJeremy L Thompson 
7162d04630dSjeremylt   Ceed ceed_parent_x, ceed_parent_y;
7172b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
7182b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
7192b730f8bSJeremy L Thompson   if (ceed_parent_x != ceed_parent_y) {
7202d04630dSjeremylt     // LCOV_EXCL_START
7212b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context");
7222d04630dSjeremylt     // LCOV_EXCL_STOP
7232b730f8bSJeremy L Thompson   }
7242d04630dSjeremylt 
7250f7fd0f8Sjeremylt   // Backend implementation
726eaf62fffSJeremy L Thompson   if (y->AXPY) {
7272b730f8bSJeremy L Thompson     CeedCall(y->AXPY(y, alpha, x));
728eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
729eaf62fffSJeremy L Thompson   }
7300f7fd0f8Sjeremylt 
7310f7fd0f8Sjeremylt   // Default implementation
7322b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array));
7332b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
7340f7fd0f8Sjeremylt 
7352b730f8bSJeremy L Thompson   assert(x_array);
7362b730f8bSJeremy L Thompson   assert(y_array);
73773380422SJeremy L Thompson 
7382b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i];
7390f7fd0f8Sjeremylt 
7402b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(y, &y_array));
7412b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
7420f7fd0f8Sjeremylt 
7430f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
7440f7fd0f8Sjeremylt }
7450f7fd0f8Sjeremylt 
7460f7fd0f8Sjeremylt /**
7475fb68f37SKaren (Ren) Stengel   @brief Compute y = alpha x + beta y
7485fb68f37SKaren (Ren) Stengel 
7495fb68f37SKaren (Ren) Stengel   @param[in,out] y     target vector for sum
7505fb68f37SKaren (Ren) Stengel   @param[in]     alpha first scaling factor
7515fb68f37SKaren (Ren) Stengel   @param[in]     beta  second scaling factor
7525fb68f37SKaren (Ren) Stengel   @param[in]     x     second vector, must be different than y
7535fb68f37SKaren (Ren) Stengel 
7545fb68f37SKaren (Ren) Stengel   @return An error code: 0 - success, otherwise - failure
7555fb68f37SKaren (Ren) Stengel 
7565fb68f37SKaren (Ren) Stengel   @ref User
7575fb68f37SKaren (Ren) Stengel **/
7585fb68f37SKaren (Ren) Stengel int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
7595fb68f37SKaren (Ren) Stengel   CeedScalar       *y_array = NULL;
7605fb68f37SKaren (Ren) Stengel   CeedScalar const *x_array = NULL;
7615fb68f37SKaren (Ren) Stengel   CeedSize          n_x, n_y;
7625fb68f37SKaren (Ren) Stengel 
7635fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetLength(y, &n_y));
7645fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetLength(x, &n_x));
7655fb68f37SKaren (Ren) Stengel   if (n_x != n_y) {
7665fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_START
7675fb68f37SKaren (Ren) Stengel     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths");
7685fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_STOP
7695fb68f37SKaren (Ren) Stengel   }
7705fb68f37SKaren (Ren) Stengel   if (x == y) {
7715fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_START
7725fb68f37SKaren (Ren) Stengel     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY");
7735fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_STOP
7745fb68f37SKaren (Ren) Stengel   }
7755fb68f37SKaren (Ren) Stengel 
7765fb68f37SKaren (Ren) Stengel   bool has_valid_array_x = true, has_valid_array_y = true;
7775fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
7785fb68f37SKaren (Ren) Stengel   if (!has_valid_array_x) {
7795fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_START
7805fb68f37SKaren (Ren) Stengel     return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7815fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_STOP
7825fb68f37SKaren (Ren) Stengel   }
7835fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
7845fb68f37SKaren (Ren) Stengel   if (!has_valid_array_y) {
7855fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_START
7865fb68f37SKaren (Ren) Stengel     return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7875fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_STOP
7885fb68f37SKaren (Ren) Stengel   }
7895fb68f37SKaren (Ren) Stengel 
7905fb68f37SKaren (Ren) Stengel   Ceed ceed_parent_x, ceed_parent_y;
7915fb68f37SKaren (Ren) Stengel   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
7925fb68f37SKaren (Ren) Stengel   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
7935fb68f37SKaren (Ren) Stengel   if (ceed_parent_x != ceed_parent_y) {
7945fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_START
7955fb68f37SKaren (Ren) Stengel     return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context");
7965fb68f37SKaren (Ren) Stengel     // LCOV_EXCL_STOP
7975fb68f37SKaren (Ren) Stengel   }
7985fb68f37SKaren (Ren) Stengel 
7995fb68f37SKaren (Ren) Stengel   // Backend implementation
8005fb68f37SKaren (Ren) Stengel   if (y->AXPBY) {
8015fb68f37SKaren (Ren) Stengel     CeedCall(y->AXPBY(y, alpha, beta, x));
8025fb68f37SKaren (Ren) Stengel     return CEED_ERROR_SUCCESS;
8035fb68f37SKaren (Ren) Stengel   }
8045fb68f37SKaren (Ren) Stengel 
8055fb68f37SKaren (Ren) Stengel   // Default implementation
8065fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArray(y, CEED_MEM_HOST, &y_array));
8075fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
8085fb68f37SKaren (Ren) Stengel 
8095fb68f37SKaren (Ren) Stengel   assert(x_array);
8105fb68f37SKaren (Ren) Stengel   assert(y_array);
8115fb68f37SKaren (Ren) Stengel 
8125fb68f37SKaren (Ren) Stengel   for (CeedInt i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i] + beta * y_array[i];
8135fb68f37SKaren (Ren) Stengel 
8145fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArray(y, &y_array));
8155fb68f37SKaren (Ren) Stengel   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
8165fb68f37SKaren (Ren) Stengel 
8175fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
8185fb68f37SKaren (Ren) Stengel }
8195fb68f37SKaren (Ren) Stengel 
8205fb68f37SKaren (Ren) Stengel /**
821ea61e9acSJeremy L Thompson   @brief Compute the pointwise multiplication w = x .* y.
822*4385fb7fSSebastian Grimberg 
823ea61e9acSJeremy L Thompson   Any subset of x, y, and w may be the same vector.
8240f7fd0f8Sjeremylt 
82596b902e2Sjeremylt   @param[out] w target vector for the product
82696b902e2Sjeremylt   @param[in]  x first vector for product
82796b902e2Sjeremylt   @param[in]  y second vector for the product
8280f7fd0f8Sjeremylt 
8290f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
8300f7fd0f8Sjeremylt 
8310f7fd0f8Sjeremylt   @ref User
8320f7fd0f8Sjeremylt **/
8330f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
83473380422SJeremy L Thompson   CeedScalar       *w_array = NULL;
83573380422SJeremy L Thompson   CeedScalar const *x_array = NULL, *y_array = NULL;
8361f9221feSJeremy L Thompson   CeedSize          n_w, n_x, n_y;
8370f7fd0f8Sjeremylt 
8382b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(w, &n_w));
8392b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
8402b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &n_y));
8412b730f8bSJeremy L Thompson   if (n_w != n_x || n_w != n_y) {
8420f7fd0f8Sjeremylt     // LCOV_EXCL_START
8432b730f8bSJeremy L Thompson     return CeedError(w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths");
8440f7fd0f8Sjeremylt     // LCOV_EXCL_STOP
8452b730f8bSJeremy L Thompson   }
8460f7fd0f8Sjeremylt 
8472d04630dSjeremylt   Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y;
8482b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(w->ceed, &ceed_parent_w));
8492b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
8502b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
8512b730f8bSJeremy L Thompson   if ((ceed_parent_w != ceed_parent_x) || (ceed_parent_w != ceed_parent_y)) {
8522d04630dSjeremylt     // LCOV_EXCL_START
8532b730f8bSJeremy L Thompson     return CeedError(w->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors w, x, and y must be created by the same Ceed context");
8542d04630dSjeremylt     // LCOV_EXCL_STOP
8552b730f8bSJeremy L Thompson   }
8562d04630dSjeremylt 
8579c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
8582b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
8592b730f8bSJeremy L Thompson   if (!has_valid_array_x) {
8609c774eddSJeremy L Thompson     // LCOV_EXCL_START
8612b730f8bSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
8629c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
8632b730f8bSJeremy L Thompson   }
8642b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
8652b730f8bSJeremy L Thompson   if (!has_valid_array_y) {
8669c774eddSJeremy L Thompson     // LCOV_EXCL_START
8672b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
8689c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
8692b730f8bSJeremy L Thompson   }
8709c774eddSJeremy L Thompson 
8710f7fd0f8Sjeremylt   // Backend implementation
872eaf62fffSJeremy L Thompson   if (w->PointwiseMult) {
8732b730f8bSJeremy L Thompson     CeedCall(w->PointwiseMult(w, x, y));
874eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
875eaf62fffSJeremy L Thompson   }
8760f7fd0f8Sjeremylt 
8770f7fd0f8Sjeremylt   // Default implementation
8780f7fd0f8Sjeremylt   if (x != w) {
8792b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
8800f7fd0f8Sjeremylt   } else {
881c6d796c6SJeremy L Thompson     CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array));
8820f7fd0f8Sjeremylt     x_array = w_array;
8830f7fd0f8Sjeremylt   }
8840f7fd0f8Sjeremylt   if (y != w && y != x) {
8852b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array));
886c6d796c6SJeremy L Thompson   } else if (y == x) {
8870f7fd0f8Sjeremylt     y_array = x_array;
888c6d796c6SJeremy L Thompson   } else {
889c6d796c6SJeremy L Thompson     CeedCall(CeedVectorGetArray(w, CEED_MEM_HOST, &w_array));
890c6d796c6SJeremy L Thompson     y_array = w_array;
8910f7fd0f8Sjeremylt   }
892c6d796c6SJeremy L Thompson   if (!w_array) CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array));
8930f7fd0f8Sjeremylt 
8942b730f8bSJeremy L Thompson   assert(w_array);
8952b730f8bSJeremy L Thompson   assert(x_array);
8962b730f8bSJeremy L Thompson   assert(y_array);
89773380422SJeremy L Thompson 
8982b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i];
8990f7fd0f8Sjeremylt 
9002b730f8bSJeremy L Thompson   if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array));
9012b730f8bSJeremy L Thompson   if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
9022b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(w, &w_array));
9030f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
9040f7fd0f8Sjeremylt }
9050f7fd0f8Sjeremylt 
9060f7fd0f8Sjeremylt /**
907d99fa3c5SJeremy L Thompson   @brief Take the reciprocal of a CeedVector.
908d99fa3c5SJeremy L Thompson 
909ea61e9acSJeremy L Thompson   @param[in,out] vec CeedVector to take reciprocal
910d99fa3c5SJeremy L Thompson 
911d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
912d99fa3c5SJeremy L Thompson 
913d99fa3c5SJeremy L Thompson   @ref User
914d99fa3c5SJeremy L Thompson **/
915d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) {
9169c774eddSJeremy L Thompson   bool has_valid_array = true;
9172b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
9182b730f8bSJeremy L Thompson 
9192b730f8bSJeremy L Thompson   if (!has_valid_array) {
9209c774eddSJeremy L Thompson     // LCOV_EXCL_START
9219c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
9222b730f8bSJeremy L Thompson                      "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray");
9239c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
9242b730f8bSJeremy L Thompson   }
9259c774eddSJeremy L Thompson 
926d99fa3c5SJeremy L Thompson   // Check if vector data set
9272b730f8bSJeremy L Thompson   if (!vec->state) {
928d99fa3c5SJeremy L Thompson     // LCOV_EXCL_START
9292b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal");
930d99fa3c5SJeremy L Thompson     // LCOV_EXCL_STOP
9312b730f8bSJeremy L Thompson   }
932d99fa3c5SJeremy L Thompson 
933d99fa3c5SJeremy L Thompson   // Backend impl for GPU, if added
934d99fa3c5SJeremy L Thompson   if (vec->Reciprocal) {
9352b730f8bSJeremy L Thompson     CeedCall(vec->Reciprocal(vec));
936e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
937d99fa3c5SJeremy L Thompson   }
938d99fa3c5SJeremy L Thompson 
9391f9221feSJeremy L Thompson   CeedSize len;
9402b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &len));
941d99fa3c5SJeremy L Thompson   CeedScalar *array;
9422b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array));
9432b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < len; i++) {
9442b730f8bSJeremy L Thompson     if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i];
9452b730f8bSJeremy L Thompson   }
946d99fa3c5SJeremy L Thompson 
9472b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(vec, &array));
948e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
949d99fa3c5SJeremy L Thompson }
950d99fa3c5SJeremy L Thompson 
951d99fa3c5SJeremy L Thompson /**
9527a982d89SJeremy L. Thompson   @brief View a CeedVector
9530436c2adSjeremylt 
954acb2c48cSJeremy L Thompson   Note: It is safe to use any unsigned values for `start` or `stop` and any nonzero integer for `step`.
955acb2c48cSJeremy L Thompson         Any portion of the provided range that is outside the range of valid indices for the CeedVector will be ignored.
956acb2c48cSJeremy L Thompson 
957acb2c48cSJeremy L Thompson   @param[in] vec    CeedVector to view
958acb2c48cSJeremy L Thompson   @param[in] start  Index of first CeedVector entry to view
959acb2c48cSJeremy L Thompson   @param[in] stop   Index of last CeedVector entry to view
960acb2c48cSJeremy L Thompson   @param[in] step   Step between CeedVector entries to view
961acb2c48cSJeremy L Thompson   @param[in] fp_fmt Printing format
962acb2c48cSJeremy L Thompson   @param[in] stream Filestream to write to
963acb2c48cSJeremy L Thompson 
964acb2c48cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
965acb2c48cSJeremy L Thompson 
966acb2c48cSJeremy L Thompson   @ref User
967acb2c48cSJeremy L Thompson **/
968acb2c48cSJeremy L Thompson int CeedVectorViewRange(CeedVector vec, CeedSize start, CeedSize stop, CeedInt step, const char *fp_fmt, FILE *stream) {
969acb2c48cSJeremy L Thompson   const CeedScalar *x;
970acb2c48cSJeremy L Thompson   char              fmt[1024];
971acb2c48cSJeremy L Thompson 
972acb2c48cSJeremy L Thompson   if (step == 0) return CeedError(vec->ceed, CEED_ERROR_MINOR, "View range 'step' must be nonzero");
973acb2c48cSJeremy L Thompson 
974acb2c48cSJeremy L Thompson   fprintf(stream, "CeedVector length %ld\n", (long)vec->length);
975acb2c48cSJeremy L Thompson   if (start != 0 || stop != vec->length || step != 1) {
976acb2c48cSJeremy L Thompson     fprintf(stream, "  start: %ld\n  stop:  %ld\n  step:  %" CeedInt_FMT "\n", (long)start, (long)stop, step);
977acb2c48cSJeremy L Thompson   }
978acb2c48cSJeremy L Thompson   if (start > vec->length) start = vec->length;
979acb2c48cSJeremy L Thompson   if (stop > vec->length) stop = vec->length;
980acb2c48cSJeremy L Thompson 
981acb2c48cSJeremy L Thompson   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
982acb2c48cSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x));
983acb2c48cSJeremy L Thompson   for (CeedInt i = start; step > 0 ? (i < stop) : (i > stop); i += step) fprintf(stream, fmt, x[i]);
984acb2c48cSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &x));
985acb2c48cSJeremy L Thompson   if (stop != vec->length) fprintf(stream, "  ...\n");
986acb2c48cSJeremy L Thompson 
987acb2c48cSJeremy L Thompson   return CEED_ERROR_SUCCESS;
988acb2c48cSJeremy L Thompson }
989acb2c48cSJeremy L Thompson 
990acb2c48cSJeremy L Thompson /**
991acb2c48cSJeremy L Thompson   @brief View a CeedVector
992acb2c48cSJeremy L Thompson 
9930a0da059Sjeremylt   @param[in] vec    CeedVector to view
994d1d35e2fSjeremylt   @param[in] fp_fmt Printing format
9950a0da059Sjeremylt   @param[in] stream Filestream to write to
9960a0da059Sjeremylt 
9970436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9980436c2adSjeremylt 
9997a982d89SJeremy L. Thompson   @ref User
10000436c2adSjeremylt **/
1001d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
1002acb2c48cSJeremy L Thompson   CeedCall(CeedVectorViewRange(vec, 0, vec->length, 1, fp_fmt, stream));
1003e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10040436c2adSjeremylt }
10050436c2adSjeremylt 
10060436c2adSjeremylt /**
1007b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedVector
1008b7c9bbdaSJeremy L Thompson 
1009ea61e9acSJeremy L Thompson   @param[in]  vec  CeedVector to retrieve state
1010b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store ceed
1011b7c9bbdaSJeremy L Thompson 
1012b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1013b7c9bbdaSJeremy L Thompson 
1014b7c9bbdaSJeremy L Thompson   @ref Advanced
1015b7c9bbdaSJeremy L Thompson **/
1016b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
1017b7c9bbdaSJeremy L Thompson   *ceed = vec->ceed;
1018b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1019b7c9bbdaSJeremy L Thompson }
1020b7c9bbdaSJeremy L Thompson 
1021b7c9bbdaSJeremy L Thompson /**
10227a982d89SJeremy L. Thompson   @brief Get the length of a CeedVector
10230436c2adSjeremylt 
1024ea61e9acSJeremy L Thompson   @param[in]  vec    CeedVector to retrieve length
10257a982d89SJeremy L. Thompson   @param[out] length Variable to store length
10260436c2adSjeremylt 
10270436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
10280436c2adSjeremylt 
10297a982d89SJeremy L. Thompson   @ref User
10300436c2adSjeremylt **/
10311f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) {
10327a982d89SJeremy L. Thompson   *length = vec->length;
1033e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10340436c2adSjeremylt }
10350436c2adSjeremylt 
10360436c2adSjeremylt /**
10370436c2adSjeremylt   @brief Destroy a CeedVector
10380436c2adSjeremylt 
1039ea61e9acSJeremy L Thompson   @param[in,out] vec CeedVector to destroy
10400436c2adSjeremylt 
10410436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
10420436c2adSjeremylt 
10437a982d89SJeremy L. Thompson   @ref User
10440436c2adSjeremylt **/
10450436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) {
10467425e127SJeremy L Thompson   if (!*vec || *vec == CEED_VECTOR_ACTIVE || *vec == CEED_VECTOR_NONE || --(*vec)->ref_count > 0) {
1047ad6481ceSJeremy L Thompson     *vec = NULL;
1048ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1049ad6481ceSJeremy L Thompson   }
10502b730f8bSJeremy L Thompson   if (((*vec)->state % 2) == 1) {
10512b730f8bSJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use");
10522b730f8bSJeremy L Thompson   }
10532b730f8bSJeremy L Thompson   if ((*vec)->num_readers > 0) {
1054e92b641fSJeremy L Thompson     // LCOV_EXCL_START
10552b730f8bSJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access");
1056e92b641fSJeremy L Thompson     // LCOV_EXCL_STOP
10570436c2adSjeremylt   }
10580436c2adSjeremylt 
10592b730f8bSJeremy L Thompson   if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec));
10602b730f8bSJeremy L Thompson 
10612b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*vec)->ceed));
10622b730f8bSJeremy L Thompson   CeedCall(CeedFree(vec));
1063e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10640436c2adSjeremylt }
10650436c2adSjeremylt 
10660436c2adSjeremylt /// @}
1067