xref: /libCEED/interface/ceed-vector.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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>
9*2b730f8bSJeremy L Thompson #include <ceed-impl.h>
10*2b730f8bSJeremy L Thompson #include <ceed/backend.h>
11*2b730f8bSJeremy L Thompson #include <ceed/ceed.h>
12547d9b97Sjeremylt #include <math.h>
133d576824SJeremy L Thompson #include <stdint.h>
143d576824SJeremy L Thompson #include <stdio.h>
150436c2adSjeremylt 
167a982d89SJeremy L. Thompson /// @file
177a982d89SJeremy L. Thompson /// Implementation of public CeedVector interfaces
187a982d89SJeremy L. Thompson 
190436c2adSjeremylt /// @cond DOXYGEN_SKIP
200436c2adSjeremylt static struct CeedVector_private ceed_vector_active;
210436c2adSjeremylt static struct CeedVector_private ceed_vector_none;
220436c2adSjeremylt /// @endcond
230436c2adSjeremylt 
247a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser
257a982d89SJeremy L. Thompson /// @{
267a982d89SJeremy L. Thompson 
277a982d89SJeremy L. Thompson /// Indicate that vector will be provided as an explicit argument to
287a982d89SJeremy L. Thompson ///   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 
459c774eddSJeremy L Thompson   @param vec                   CeedVector to check validity
469c774eddSJeremy L Thompson   @param[out] has_valid_array  Variable to store validity
479c774eddSJeremy L Thompson 
489c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
499c774eddSJeremy L Thompson 
509c774eddSJeremy L Thompson   @ref Backend
519c774eddSJeremy L Thompson **/
529c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) {
53*2b730f8bSJeremy L Thompson   if (!vec->HasValidArray) {
549c774eddSJeremy L Thompson     // LCOV_EXCL_START
55*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasValidArray");
569c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
57*2b730f8bSJeremy L Thompson   }
589c774eddSJeremy L Thompson 
59*2b730f8bSJeremy 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 
679c774eddSJeremy L Thompson   @param vec                              CeedVector to check
689c774eddSJeremy L Thompson   @param 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 **/
75*2b730f8bSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) {
76*2b730f8bSJeremy L Thompson   if (!vec->HasBorrowedArrayOfType) {
779c774eddSJeremy L Thompson     // LCOV_EXCL_START
78*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support HasBorrowedArrayOfType");
799c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
80*2b730f8bSJeremy L Thompson   }
819c774eddSJeremy L Thompson 
82*2b730f8bSJeremy 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 
907a982d89SJeremy L. Thompson   @param 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 
1055f67fadeSJeremy L Thompson   @param[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 
1197a982d89SJeremy L. Thompson   @param 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 
1347a982d89SJeremy L. Thompson   @param[out] vec  CeedVector to retrieve state
1357a982d89SJeremy L. Thompson   @param 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 
14934359f16Sjeremylt   @param 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 
1710436c2adSjeremylt   @param ceed      Ceed object where the CeedVector will be created
1720436c2adSjeremylt   @param length    Length of vector
1730436c2adSjeremylt   @param[out] vec  Address of the variable where the newly created
1740436c2adSjeremylt                      CeedVector will be stored
1750436c2adSjeremylt 
1760436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
1770436c2adSjeremylt 
1787a982d89SJeremy L. Thompson   @ref User
1790436c2adSjeremylt **/
1801f9221feSJeremy L Thompson int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) {
1810436c2adSjeremylt   if (!ceed->VectorCreate) {
1820436c2adSjeremylt     Ceed delegate;
183*2b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector"));
1840436c2adSjeremylt 
185*2b730f8bSJeremy L Thompson     if (!delegate) {
1860436c2adSjeremylt       // LCOV_EXCL_START
187*2b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorCreate");
1880436c2adSjeremylt       // LCOV_EXCL_STOP
189*2b730f8bSJeremy L Thompson     }
1900436c2adSjeremylt 
191*2b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(delegate, length, vec));
192e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
1930436c2adSjeremylt   }
1940436c2adSjeremylt 
195*2b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, vec));
1960436c2adSjeremylt   (*vec)->ceed = ceed;
197*2b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
198d1d35e2fSjeremylt   (*vec)->ref_count = 1;
1990436c2adSjeremylt   (*vec)->length    = length;
2000436c2adSjeremylt   (*vec)->state     = 0;
201*2b730f8bSJeremy L Thompson   CeedCall(ceed->VectorCreate(length, *vec));
202e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2030436c2adSjeremylt }
2040436c2adSjeremylt 
2050436c2adSjeremylt /**
2069560d06aSjeremylt   @brief Copy the pointer to a CeedVector. Both pointers should
2079560d06aSjeremylt            be destroyed with `CeedVectorDestroy()`;
2089560d06aSjeremylt            Note: If `*vec_copy` is non-NULL, then it is assumed that
2099560d06aSjeremylt            `*vec_copy` is a pointer to a CeedVector. This
2109560d06aSjeremylt            CeedVector will be destroyed if `*vec_copy` is the only
2119560d06aSjeremylt            reference to this CeedVector.
2129560d06aSjeremylt 
2139560d06aSjeremylt   @param vec            CeedVector to copy reference to
2149560d06aSjeremylt   @param[out] vec_copy  Variable to store copied reference
2159560d06aSjeremylt 
2169560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
2179560d06aSjeremylt 
2189560d06aSjeremylt   @ref User
2199560d06aSjeremylt **/
2209560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) {
221*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorReference(vec));
222*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(vec_copy));
2239560d06aSjeremylt   *vec_copy = vec;
2249560d06aSjeremylt   return CEED_ERROR_SUCCESS;
2259560d06aSjeremylt }
2269560d06aSjeremylt 
2279560d06aSjeremylt /**
2280436c2adSjeremylt   @brief Set the array used by a CeedVector, freeing any previously allocated
229b3cf021fSjeremylt            array if applicable. The backend may copy values to a different
230b3cf021fSjeremylt            memtype, such as during @ref CeedOperatorApply().
2316a6c615bSJeremy L Thompson            See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray().
2320436c2adSjeremylt 
2330436c2adSjeremylt   @param vec        CeedVector
234d1d35e2fSjeremylt   @param mem_type   Memory type of the array being passed
235d1d35e2fSjeremylt   @param copy_mode  Copy mode for the array
2364cc79fe7SJed Brown   @param array      Array to be used, or NULL with @ref CEED_COPY_VALUES to have the
2370436c2adSjeremylt                       library allocate
2380436c2adSjeremylt 
2390436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2400436c2adSjeremylt 
2417a982d89SJeremy L. Thompson   @ref User
2420436c2adSjeremylt **/
243*2b730f8bSJeremy L Thompson int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) {
244*2b730f8bSJeremy L Thompson   if (!vec->SetArray) {
2450436c2adSjeremylt     // LCOV_EXCL_START
246*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray");
2470436c2adSjeremylt     // LCOV_EXCL_STOP
248*2b730f8bSJeremy L Thompson   }
249*2b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
250*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
251*2b730f8bSJeremy L Thompson   }
252*2b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
253*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
254*2b730f8bSJeremy L Thompson   }
2550436c2adSjeremylt 
256*2b730f8bSJeremy L Thompson   CeedCall(vec->SetArray(vec, mem_type, copy_mode, array));
2570436c2adSjeremylt   vec->state += 2;
258e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2590436c2adSjeremylt }
2600436c2adSjeremylt 
2610436c2adSjeremylt /**
2620436c2adSjeremylt   @brief Set the CeedVector to a constant value
2630436c2adSjeremylt 
2640436c2adSjeremylt   @param vec        CeedVector
2650436c2adSjeremylt   @param[in] value  Value to be used
2660436c2adSjeremylt 
2670436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2680436c2adSjeremylt 
2697a982d89SJeremy L. Thompson   @ref User
2700436c2adSjeremylt **/
2710436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) {
272*2b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
2739c774eddSJeremy L Thompson     // LCOV_EXCL_START
274*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
2759c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
276*2b730f8bSJeremy L Thompson   }
277*2b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
2789c774eddSJeremy L Thompson     // LCOV_EXCL_START
279*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
2809c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
281*2b730f8bSJeremy L Thompson   }
2820436c2adSjeremylt 
2830436c2adSjeremylt   if (vec->SetValue) {
284*2b730f8bSJeremy L Thompson     CeedCall(vec->SetValue(vec, value));
2850436c2adSjeremylt   } else {
2860436c2adSjeremylt     CeedScalar *array;
287*2b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array));
28892ae7e47SJeremy L Thompson     for (CeedInt i = 0; i < vec->length; i++) array[i] = value;
289*2b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(vec, &array));
2900436c2adSjeremylt   }
2910436c2adSjeremylt   vec->state += 2;
292e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2930436c2adSjeremylt }
2940436c2adSjeremylt 
2950436c2adSjeremylt /**
296b3cf021fSjeremylt   @brief Sync the CeedVector to a specified memtype. This function is used to
297b3cf021fSjeremylt            force synchronization of arrays set with @ref CeedVectorSetArray().
298b3cf021fSjeremylt            If the requested memtype is already synchronized, this function
299b3cf021fSjeremylt            results in a no-op.
3000436c2adSjeremylt 
3010436c2adSjeremylt   @param vec       CeedVector
302d1d35e2fSjeremylt   @param mem_type  Memtype to be synced
3030436c2adSjeremylt 
3040436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3050436c2adSjeremylt 
3067a982d89SJeremy L. Thompson   @ref User
3070436c2adSjeremylt **/
308d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) {
309*2b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
310*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot sync CeedVector, the access lock is already in use");
311*2b730f8bSJeremy L Thompson   }
3120436c2adSjeremylt 
3130436c2adSjeremylt   if (vec->SyncArray) {
314*2b730f8bSJeremy L Thompson     CeedCall(vec->SyncArray(vec, mem_type));
3150436c2adSjeremylt   } else {
3160436c2adSjeremylt     const CeedScalar *array;
317*2b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(vec, mem_type, &array));
318*2b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(vec, &array));
3190436c2adSjeremylt   }
320e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3210436c2adSjeremylt }
3220436c2adSjeremylt 
3230436c2adSjeremylt /**
3249c774eddSJeremy L Thompson   @brief Take ownership of the CeedVector array set by @ref CeedVectorSetArray()
3259c774eddSJeremy L Thompson            with @ref CEED_USE_POINTER and remove the array from the CeedVector.
3269c774eddSJeremy L Thompson            The caller is responsible for managing and freeing the array.
3279c774eddSJeremy L Thompson            This function will error if @ref CeedVectorSetArray() was not previously
3289c774eddSJeremy L Thompson            called with @ref CEED_USE_POINTER for the corresponding mem_type.
3296a6c615bSJeremy L Thompson 
3306a6c615bSJeremy L Thompson   @param vec         CeedVector
331d1d35e2fSjeremylt   @param mem_type    Memory type on which to take the array. If the backend
3326a6c615bSJeremy L Thompson                        uses a different memory type, this will perform a copy.
333d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type, or NULL if array pointer is
3346a6c615bSJeremy L Thompson                        not required
3356a6c615bSJeremy L Thompson 
3366a6c615bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3376a6c615bSJeremy L Thompson 
3386a6c615bSJeremy L Thompson   @ref User
3396a6c615bSJeremy L Thompson **/
340*2b730f8bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
341*2b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
342*2b730f8bSJeremy L Thompson     // LCOV_EXCL_START
343*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use");
344*2b730f8bSJeremy L Thompson     // LCOV_EXCL_STOP
345*2b730f8bSJeremy L Thompson   }
346*2b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
347*2b730f8bSJeremy L Thompson     // LCOV_EXCL_START
348*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access");
349*2b730f8bSJeremy L Thompson     // LCOV_EXCL_STOP
350*2b730f8bSJeremy L Thompson   }
3516a6c615bSJeremy L Thompson 
35250c643e1SJed Brown   CeedScalar *temp_array = NULL;
35350c643e1SJed Brown   if (vec->length > 0) {
3549c774eddSJeremy L Thompson     bool has_borrowed_array_of_type = true;
355*2b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type));
356*2b730f8bSJeremy L Thompson     if (!has_borrowed_array_of_type) {
3579c774eddSJeremy L Thompson       // LCOV_EXCL_START
358*2b730f8bSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND, "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray",
359*2b730f8bSJeremy L Thompson                        CeedMemTypes[mem_type]);
3609c774eddSJeremy L Thompson       // LCOV_EXCL_STOP
361*2b730f8bSJeremy L Thompson     }
3629c774eddSJeremy L Thompson 
3639c774eddSJeremy L Thompson     bool has_valid_array = true;
364*2b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
365*2b730f8bSJeremy L Thompson     if (!has_valid_array) {
3669c774eddSJeremy L Thompson       // LCOV_EXCL_START
3679c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
368*2b730f8bSJeremy L Thompson                        "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray");
3699c774eddSJeremy L Thompson       // LCOV_EXCL_STOP
370*2b730f8bSJeremy L Thompson     }
3719c774eddSJeremy L Thompson 
372*2b730f8bSJeremy L Thompson     CeedCall(vec->TakeArray(vec, mem_type, &temp_array));
37350c643e1SJed Brown   }
374d1d35e2fSjeremylt   if (array) (*array) = temp_array;
375e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3766a6c615bSJeremy L Thompson }
3776a6c615bSJeremy L Thompson 
3786a6c615bSJeremy L Thompson /**
379b3cf021fSjeremylt   @brief Get read/write access to a CeedVector via the specified memory type.
380b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArray().
3810436c2adSjeremylt 
3820436c2adSjeremylt   @param vec         CeedVector to access
383d1d35e2fSjeremylt   @param mem_type    Memory type on which to access the array. If the backend
384b3cf021fSjeremylt                        uses a different memory type, this will perform a copy.
385d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type
3860436c2adSjeremylt 
3870436c2adSjeremylt   @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide
3880436c2adSjeremylt     access to array pointers in the desired memory space. Pairing get/restore
3890436c2adSjeremylt     allows the Vector to track access, thus knowing if norms or other
3900436c2adSjeremylt     operations may need to be recomputed.
3910436c2adSjeremylt 
3920436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3930436c2adSjeremylt 
3947a982d89SJeremy L. Thompson   @ref User
3950436c2adSjeremylt **/
396*2b730f8bSJeremy L Thompson int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
397*2b730f8bSJeremy L Thompson   if (!vec->GetArray) {
3980436c2adSjeremylt     // LCOV_EXCL_START
399*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray");
4000436c2adSjeremylt     // LCOV_EXCL_STOP
401*2b730f8bSJeremy L Thompson   }
402*2b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
403*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
404*2b730f8bSJeremy L Thompson   }
405*2b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
406*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
407*2b730f8bSJeremy L Thompson   }
4080436c2adSjeremylt 
4099c774eddSJeremy L Thompson   bool has_valid_array = true;
410*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
411*2b730f8bSJeremy L Thompson   if (!has_valid_array) {
4129c774eddSJeremy L Thompson     // LCOV_EXCL_START
4139c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
414*2b730f8bSJeremy L Thompson                      "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
4159c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
416*2b730f8bSJeremy L Thompson   }
4179c774eddSJeremy L Thompson 
418*2b730f8bSJeremy L Thompson   CeedCall(vec->GetArray(vec, mem_type, array));
41928bfd0b7SJeremy L Thompson   vec->state++;
420e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4210436c2adSjeremylt }
4220436c2adSjeremylt 
4230436c2adSjeremylt /**
424b3cf021fSjeremylt   @brief Get read-only access to a CeedVector via the specified memory type.
425b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArrayRead().
4260436c2adSjeremylt 
4270436c2adSjeremylt   @param vec         CeedVector to access
428d1d35e2fSjeremylt   @param mem_type    Memory type on which to access the array.  If the backend
4290436c2adSjeremylt                        uses a different memory type, this will perform a copy
4300436c2adSjeremylt                        (possibly cached).
431d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type
4320436c2adSjeremylt 
4330436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4340436c2adSjeremylt 
4357a982d89SJeremy L. Thompson   @ref User
4360436c2adSjeremylt **/
437*2b730f8bSJeremy L Thompson int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) {
438*2b730f8bSJeremy L Thompson   if (!vec->GetArrayRead) {
4390436c2adSjeremylt     // LCOV_EXCL_START
440*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead");
4410436c2adSjeremylt     // LCOV_EXCL_STOP
442*2b730f8bSJeremy L Thompson   }
443*2b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
444*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use");
445*2b730f8bSJeremy L Thompson   }
4460436c2adSjeremylt 
44750c643e1SJed Brown   if (vec->length > 0) {
4489c774eddSJeremy L Thompson     bool has_valid_array = true;
449*2b730f8bSJeremy L Thompson     CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
450*2b730f8bSJeremy L Thompson     if (!has_valid_array) {
4519c774eddSJeremy L Thompson       // LCOV_EXCL_START
4529c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
453*2b730f8bSJeremy L Thompson                        "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray");
4549c774eddSJeremy L Thompson       // LCOV_EXCL_STOP
455*2b730f8bSJeremy L Thompson     }
4569c774eddSJeremy L Thompson 
457*2b730f8bSJeremy L Thompson     CeedCall(vec->GetArrayRead(vec, mem_type, array));
45850c643e1SJed Brown   } else {
45950c643e1SJed Brown     *array = NULL;
46050c643e1SJed Brown   }
461d1d35e2fSjeremylt   vec->num_readers++;
462e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4630436c2adSjeremylt }
4640436c2adSjeremylt 
4650436c2adSjeremylt /**
4669c774eddSJeremy L Thompson   @brief Get write access to a CeedVector via the specified memory type.
4679c774eddSJeremy L Thompson            Restore access with @ref CeedVectorRestoreArray(). All old
4689c774eddSJeremy L Thompson            values should be assumed to be invalid.
4699c774eddSJeremy L Thompson 
4709c774eddSJeremy L Thompson   @param vec         CeedVector to access
4719c774eddSJeremy L Thompson   @param mem_type    Memory type on which to access the array.
4729c774eddSJeremy L Thompson   @param[out] array  Array on memory type mem_type
4739c774eddSJeremy L Thompson 
4749c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4759c774eddSJeremy L Thompson 
4769c774eddSJeremy L Thompson   @ref User
4779c774eddSJeremy L Thompson **/
478*2b730f8bSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
479*2b730f8bSJeremy L Thompson   if (!vec->GetArrayWrite) {
4809c774eddSJeremy L Thompson     // LCOV_EXCL_START
481*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayWrite");
4829c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
483*2b730f8bSJeremy L Thompson   }
484*2b730f8bSJeremy L Thompson   if (vec->state % 2 == 1) {
4859c774eddSJeremy L Thompson     // LCOV_EXCL_START
486*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use");
4879c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
488*2b730f8bSJeremy L Thompson   }
489*2b730f8bSJeremy L Thompson   if (vec->num_readers > 0) {
4909c774eddSJeremy L Thompson     // LCOV_EXCL_START
491*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access");
4929c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
493*2b730f8bSJeremy L Thompson   }
4949c774eddSJeremy L Thompson 
495*2b730f8bSJeremy L Thompson   CeedCall(vec->GetArrayWrite(vec, mem_type, array));
49628bfd0b7SJeremy L Thompson   vec->state++;
4979c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
4989c774eddSJeremy L Thompson }
4999c774eddSJeremy L Thompson 
5009c774eddSJeremy L Thompson /**
501b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArray()
5029932a2afSJeremy L Thompson            or @ref CeedVectorGetArrayWrite()
5030436c2adSjeremylt 
5040436c2adSjeremylt   @param vec    CeedVector to restore
5050436c2adSjeremylt   @param array  Array of vector data
5060436c2adSjeremylt 
5070436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5080436c2adSjeremylt 
5097a982d89SJeremy L. Thompson   @ref User
5100436c2adSjeremylt **/
5110436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
512*2b730f8bSJeremy L Thompson   if (vec->state % 2 != 1) {
513*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array access, access was not granted");
514706efda3SJeremy L Thompson   }
515*2b730f8bSJeremy L Thompson   if (vec->RestoreArray) CeedCall(vec->RestoreArray(vec));
5160436c2adSjeremylt   *array = NULL;
51728bfd0b7SJeremy L Thompson   vec->state++;
518e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5190436c2adSjeremylt }
5200436c2adSjeremylt 
5210436c2adSjeremylt /**
522b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArrayRead()
5230436c2adSjeremylt 
5240436c2adSjeremylt   @param vec    CeedVector to restore
5250436c2adSjeremylt   @param array  Array of vector data
5260436c2adSjeremylt 
5270436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5280436c2adSjeremylt 
5297a982d89SJeremy L. Thompson   @ref User
5300436c2adSjeremylt **/
5310436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) {
532*2b730f8bSJeremy L Thompson   if (vec->num_readers == 0) {
5339c774eddSJeremy L Thompson     // LCOV_EXCL_START
534*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS, "Cannot restore CeedVector array read access, access was not granted");
5359c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
536*2b730f8bSJeremy L Thompson   }
5379c774eddSJeremy L Thompson 
53875a19770SJeremy L Thompson   vec->num_readers--;
539*2b730f8bSJeremy L Thompson   if (vec->num_readers == 0 && vec->RestoreArrayRead) CeedCall(vec->RestoreArrayRead(vec));
5400436c2adSjeremylt   *array = NULL;
54175a19770SJeremy L Thompson 
542e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5430436c2adSjeremylt }
5440436c2adSjeremylt 
5450436c2adSjeremylt /**
546171f8ca9Sjeremylt   @brief Get the norm of a CeedVector.
547171f8ca9Sjeremylt 
548171f8ca9Sjeremylt   Note: This operation is local to the CeedVector. This function will likely
549171f8ca9Sjeremylt           not provide the desired results for the norm of the libCEED portion
550171f8ca9Sjeremylt           of a parallel vector or a CeedVector with duplicated or hanging nodes.
551547d9b97Sjeremylt 
552547d9b97Sjeremylt   @param vec        CeedVector to retrieve maximum value
553d1d35e2fSjeremylt   @param norm_type  Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
554547d9b97Sjeremylt   @param[out] norm  Variable to store norm value
555547d9b97Sjeremylt 
556547d9b97Sjeremylt   @return An error code: 0 - success, otherwise - failure
557547d9b97Sjeremylt 
5587a982d89SJeremy L. Thompson   @ref User
559547d9b97Sjeremylt **/
560d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
5619c774eddSJeremy L Thompson   bool has_valid_array = true;
562*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
563*2b730f8bSJeremy L Thompson   if (!has_valid_array) {
5649c774eddSJeremy L Thompson     // LCOV_EXCL_START
5659c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
566*2b730f8bSJeremy L Thompson                      "CeedVector has no valid data to compute norm, must set data with CeedVectorSetValue or CeedVectorSetArray");
5679c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
568*2b730f8bSJeremy L Thompson   }
5699c774eddSJeremy L Thompson 
570547d9b97Sjeremylt   // Backend impl for GPU, if added
571547d9b97Sjeremylt   if (vec->Norm) {
572*2b730f8bSJeremy L Thompson     CeedCall(vec->Norm(vec, norm_type, norm));
573e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
574547d9b97Sjeremylt   }
575547d9b97Sjeremylt 
576547d9b97Sjeremylt   const CeedScalar *array;
577*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array));
578547d9b97Sjeremylt 
579547d9b97Sjeremylt   *norm = 0.;
580d1d35e2fSjeremylt   switch (norm_type) {
581547d9b97Sjeremylt     case CEED_NORM_1:
58292ae7e47SJeremy L Thompson       for (CeedInt i = 0; i < vec->length; i++) {
583547d9b97Sjeremylt         *norm += fabs(array[i]);
584547d9b97Sjeremylt       }
585547d9b97Sjeremylt       break;
586547d9b97Sjeremylt     case CEED_NORM_2:
58792ae7e47SJeremy L Thompson       for (CeedInt i = 0; i < vec->length; i++) {
588547d9b97Sjeremylt         *norm += fabs(array[i]) * fabs(array[i]);
589547d9b97Sjeremylt       }
590547d9b97Sjeremylt       break;
591547d9b97Sjeremylt     case CEED_NORM_MAX:
59292ae7e47SJeremy L Thompson       for (CeedInt i = 0; i < vec->length; i++) {
593d1d35e2fSjeremylt         const CeedScalar abs_v_i = fabs(array[i]);
594d1d35e2fSjeremylt         *norm                    = *norm > abs_v_i ? *norm : abs_v_i;
595547d9b97Sjeremylt       }
596547d9b97Sjeremylt   }
597*2b730f8bSJeremy L Thompson   if (norm_type == CEED_NORM_2) *norm = sqrt(*norm);
598547d9b97Sjeremylt 
599*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &array));
600e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
601547d9b97Sjeremylt }
602547d9b97Sjeremylt 
603547d9b97Sjeremylt /**
604e0dd3b27Sjeremylt   @brief Compute x = alpha x
605e0dd3b27Sjeremylt 
60696b902e2Sjeremylt   @param[in,out] x  vector for scaling
60796b902e2Sjeremylt   @param[in] alpha  scaling factor
608e0dd3b27Sjeremylt 
609e0dd3b27Sjeremylt   @return An error code: 0 - success, otherwise - failure
610e0dd3b27Sjeremylt 
611e0dd3b27Sjeremylt   @ref User
612e0dd3b27Sjeremylt **/
613e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) {
61473380422SJeremy L Thompson   CeedScalar *x_array = NULL;
6151f9221feSJeremy L Thompson   CeedSize    n_x;
616e0dd3b27Sjeremylt 
6179c774eddSJeremy L Thompson   bool has_valid_array = true;
618*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array));
619*2b730f8bSJeremy L Thompson   if (!has_valid_array) {
6209c774eddSJeremy L Thompson     // LCOV_EXCL_START
6219c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
622*2b730f8bSJeremy L Thompson                      "CeedVector has no valid data to scale, must set data with CeedVectorSetValue or CeedVectorSetArray");
6239c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
624*2b730f8bSJeremy L Thompson   }
6259c774eddSJeremy L Thompson 
626*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
627e0dd3b27Sjeremylt 
628e0dd3b27Sjeremylt   // Backend implementation
629*2b730f8bSJeremy L Thompson   if (x->Scale) return x->Scale(x, alpha);
630e0dd3b27Sjeremylt 
631e0dd3b27Sjeremylt   // Default implementation
632*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array));
633*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < n_x; i++) x_array[i] *= alpha;
634*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(x, &x_array));
635e0dd3b27Sjeremylt 
636e0dd3b27Sjeremylt   return CEED_ERROR_SUCCESS;
637e0dd3b27Sjeremylt }
638e0dd3b27Sjeremylt 
639e0dd3b27Sjeremylt /**
6400f7fd0f8Sjeremylt   @brief Compute y = alpha x + y
6410f7fd0f8Sjeremylt 
64296b902e2Sjeremylt   @param[in,out] y  target vector for sum
64396b902e2Sjeremylt   @param[in] alpha  scaling factor
64496b902e2Sjeremylt   @param[in] x      second vector, must be different than y
6450f7fd0f8Sjeremylt 
6460f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
6470f7fd0f8Sjeremylt 
6480f7fd0f8Sjeremylt   @ref User
6490f7fd0f8Sjeremylt **/
6500f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
65173380422SJeremy L Thompson   CeedScalar       *y_array = NULL;
65273380422SJeremy L Thompson   CeedScalar const *x_array = NULL;
6531f9221feSJeremy L Thompson   CeedSize          n_x, n_y;
6540f7fd0f8Sjeremylt 
655*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &n_y));
656*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
657*2b730f8bSJeremy L Thompson   if (n_x != n_y) {
6580f7fd0f8Sjeremylt     // LCOV_EXCL_START
659*2b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths");
6600f7fd0f8Sjeremylt     // LCOV_EXCL_STOP
661*2b730f8bSJeremy L Thompson   }
662*2b730f8bSJeremy L Thompson   if (x == y) {
6630f7fd0f8Sjeremylt     // LCOV_EXCL_START
664*2b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY");
6650f7fd0f8Sjeremylt     // LCOV_EXCL_STOP
666*2b730f8bSJeremy L Thompson   }
6670f7fd0f8Sjeremylt 
6689c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
669*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
670*2b730f8bSJeremy L Thompson   if (!has_valid_array_x) {
6719c774eddSJeremy L Thompson     // LCOV_EXCL_START
672*2b730f8bSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6739c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
674*2b730f8bSJeremy L Thompson   }
675*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
676*2b730f8bSJeremy L Thompson   if (!has_valid_array_y) {
6779c774eddSJeremy L Thompson     // LCOV_EXCL_START
678*2b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
6799c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
680*2b730f8bSJeremy L Thompson   }
6819c774eddSJeremy L Thompson 
6822d04630dSjeremylt   Ceed ceed_parent_x, ceed_parent_y;
683*2b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
684*2b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
685*2b730f8bSJeremy L Thompson   if (ceed_parent_x != ceed_parent_y) {
6862d04630dSjeremylt     // LCOV_EXCL_START
687*2b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context");
6882d04630dSjeremylt     // LCOV_EXCL_STOP
689*2b730f8bSJeremy L Thompson   }
6902d04630dSjeremylt 
6910f7fd0f8Sjeremylt   // Backend implementation
692eaf62fffSJeremy L Thompson   if (y->AXPY) {
693*2b730f8bSJeremy L Thompson     CeedCall(y->AXPY(y, alpha, x));
694eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
695eaf62fffSJeremy L Thompson   }
6960f7fd0f8Sjeremylt 
6970f7fd0f8Sjeremylt   // Default implementation
698*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array));
699*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
7000f7fd0f8Sjeremylt 
701*2b730f8bSJeremy L Thompson   assert(x_array);
702*2b730f8bSJeremy L Thompson   assert(y_array);
70373380422SJeremy L Thompson 
704*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < n_y; i++) y_array[i] += alpha * x_array[i];
7050f7fd0f8Sjeremylt 
706*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(y, &y_array));
707*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
7080f7fd0f8Sjeremylt 
7090f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
7100f7fd0f8Sjeremylt }
7110f7fd0f8Sjeremylt 
7120f7fd0f8Sjeremylt /**
7130c1bc3c2Sjeremylt   @brief Compute the pointwise multiplication w = x .* y. Any
7140f7fd0f8Sjeremylt            subset of x, y, and w may be the same vector.
7150f7fd0f8Sjeremylt 
71696b902e2Sjeremylt   @param[out] w  target vector for the product
71796b902e2Sjeremylt   @param[in] x   first vector for product
71896b902e2Sjeremylt   @param[in] y   second vector for the product
7190f7fd0f8Sjeremylt 
7200f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
7210f7fd0f8Sjeremylt 
7220f7fd0f8Sjeremylt   @ ref User
7230f7fd0f8Sjeremylt **/
7240f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
72573380422SJeremy L Thompson   CeedScalar       *w_array = NULL;
72673380422SJeremy L Thompson   CeedScalar const *x_array = NULL, *y_array = NULL;
7271f9221feSJeremy L Thompson   CeedSize          n_w, n_x, n_y;
7280f7fd0f8Sjeremylt 
729*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(w, &n_w));
730*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(x, &n_x));
731*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(y, &n_y));
732*2b730f8bSJeremy L Thompson   if (n_w != n_x || n_w != n_y) {
7330f7fd0f8Sjeremylt     // LCOV_EXCL_START
734*2b730f8bSJeremy L Thompson     return CeedError(w->ceed, CEED_ERROR_UNSUPPORTED, "Cannot multiply vectors of different lengths");
7350f7fd0f8Sjeremylt     // LCOV_EXCL_STOP
736*2b730f8bSJeremy L Thompson   }
7370f7fd0f8Sjeremylt 
7382d04630dSjeremylt   Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y;
739*2b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(w->ceed, &ceed_parent_w));
740*2b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(x->ceed, &ceed_parent_x));
741*2b730f8bSJeremy L Thompson   CeedCall(CeedGetParent(y->ceed, &ceed_parent_y));
742*2b730f8bSJeremy L Thompson   if ((ceed_parent_w != ceed_parent_x) || (ceed_parent_w != ceed_parent_y)) {
7432d04630dSjeremylt     // LCOV_EXCL_START
744*2b730f8bSJeremy L Thompson     return CeedError(w->ceed, CEED_ERROR_INCOMPATIBLE, "Vectors w, x, and y must be created by the same Ceed context");
7452d04630dSjeremylt     // LCOV_EXCL_STOP
746*2b730f8bSJeremy L Thompson   }
7472d04630dSjeremylt 
7489c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
749*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x));
750*2b730f8bSJeremy L Thompson   if (!has_valid_array_x) {
7519c774eddSJeremy L Thompson     // LCOV_EXCL_START
752*2b730f8bSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7539c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
754*2b730f8bSJeremy L Thompson   }
755*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y));
756*2b730f8bSJeremy L Thompson   if (!has_valid_array_y) {
7579c774eddSJeremy L Thompson     // LCOV_EXCL_START
758*2b730f8bSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray");
7599c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
760*2b730f8bSJeremy L Thompson   }
7619c774eddSJeremy L Thompson 
7620f7fd0f8Sjeremylt   // Backend implementation
763eaf62fffSJeremy L Thompson   if (w->PointwiseMult) {
764*2b730f8bSJeremy L Thompson     CeedCall(w->PointwiseMult(w, x, y));
765eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
766eaf62fffSJeremy L Thompson   }
7670f7fd0f8Sjeremylt 
7680f7fd0f8Sjeremylt   // Default implementation
769*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array));
7700f7fd0f8Sjeremylt   if (x != w) {
771*2b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array));
7720f7fd0f8Sjeremylt   } else {
7730f7fd0f8Sjeremylt     x_array = w_array;
7740f7fd0f8Sjeremylt   }
775*2b730f8bSJeremy L Thompson 
7760f7fd0f8Sjeremylt   if (y != w && y != x) {
777*2b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array));
7780f7fd0f8Sjeremylt   } else if (y != x) {
7790f7fd0f8Sjeremylt     y_array = w_array;
7800f7fd0f8Sjeremylt   } else {
7810f7fd0f8Sjeremylt     y_array = x_array;
7820f7fd0f8Sjeremylt   }
7830f7fd0f8Sjeremylt 
784*2b730f8bSJeremy L Thompson   assert(w_array);
785*2b730f8bSJeremy L Thompson   assert(x_array);
786*2b730f8bSJeremy L Thompson   assert(y_array);
78773380422SJeremy L Thompson 
788*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < n_w; i++) w_array[i] = x_array[i] * y_array[i];
7890f7fd0f8Sjeremylt 
790*2b730f8bSJeremy L Thompson   if (y != w && y != x) CeedCall(CeedVectorRestoreArrayRead(y, &y_array));
791*2b730f8bSJeremy L Thompson   if (x != w) CeedCall(CeedVectorRestoreArrayRead(x, &x_array));
792*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(w, &w_array));
7930f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
7940f7fd0f8Sjeremylt }
7950f7fd0f8Sjeremylt 
7960f7fd0f8Sjeremylt /**
797d99fa3c5SJeremy L Thompson   @brief Take the reciprocal of a CeedVector.
798d99fa3c5SJeremy L Thompson 
799d99fa3c5SJeremy L Thompson   @param vec  CeedVector to take reciprocal
800d99fa3c5SJeremy L Thompson 
801d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
802d99fa3c5SJeremy L Thompson 
803d99fa3c5SJeremy L Thompson   @ref User
804d99fa3c5SJeremy L Thompson **/
805d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) {
8069c774eddSJeremy L Thompson   bool has_valid_array = true;
807*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorHasValidArray(vec, &has_valid_array));
808*2b730f8bSJeremy L Thompson 
809*2b730f8bSJeremy L Thompson   if (!has_valid_array) {
8109c774eddSJeremy L Thompson     // LCOV_EXCL_START
8119c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
812*2b730f8bSJeremy L Thompson                      "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray");
8139c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
814*2b730f8bSJeremy L Thompson   }
8159c774eddSJeremy L Thompson 
816d99fa3c5SJeremy L Thompson   // Check if vector data set
817*2b730f8bSJeremy L Thompson   if (!vec->state) {
818d99fa3c5SJeremy L Thompson     // LCOV_EXCL_START
819*2b730f8bSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal");
820d99fa3c5SJeremy L Thompson     // LCOV_EXCL_STOP
821*2b730f8bSJeremy L Thompson   }
822d99fa3c5SJeremy L Thompson 
823d99fa3c5SJeremy L Thompson   // Backend impl for GPU, if added
824d99fa3c5SJeremy L Thompson   if (vec->Reciprocal) {
825*2b730f8bSJeremy L Thompson     CeedCall(vec->Reciprocal(vec));
826e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
827d99fa3c5SJeremy L Thompson   }
828d99fa3c5SJeremy L Thompson 
8291f9221feSJeremy L Thompson   CeedSize len;
830*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(vec, &len));
831d99fa3c5SJeremy L Thompson   CeedScalar *array;
832*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array));
833*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < len; i++) {
834*2b730f8bSJeremy L Thompson     if (fabs(array[i]) > CEED_EPSILON) array[i] = 1. / array[i];
835*2b730f8bSJeremy L Thompson   }
836d99fa3c5SJeremy L Thompson 
837*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(vec, &array));
838e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
839d99fa3c5SJeremy L Thompson }
840d99fa3c5SJeremy L Thompson 
841d99fa3c5SJeremy L Thompson /**
8427a982d89SJeremy L. Thompson   @brief View a CeedVector
8430436c2adSjeremylt 
8440a0da059Sjeremylt   @param[in] vec     CeedVector to view
845d1d35e2fSjeremylt   @param[in] fp_fmt  Printing format
8460a0da059Sjeremylt   @param[in] stream  Filestream to write to
8470a0da059Sjeremylt 
8480436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
8490436c2adSjeremylt 
8507a982d89SJeremy L. Thompson   @ref User
8510436c2adSjeremylt **/
852d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
8537a982d89SJeremy L. Thompson   const CeedScalar *x;
854*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x));
8557a982d89SJeremy L. Thompson 
8567a982d89SJeremy L. Thompson   char fmt[1024];
8577a982d89SJeremy L. Thompson   fprintf(stream, "CeedVector length %ld\n", (long)vec->length);
858d1d35e2fSjeremylt   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
859*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < vec->length; i++) fprintf(stream, fmt, x[i]);
8607a982d89SJeremy L. Thompson 
861*2b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(vec, &x));
862e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8630436c2adSjeremylt }
8640436c2adSjeremylt 
8650436c2adSjeremylt /**
866b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedVector
867b7c9bbdaSJeremy L Thompson 
868b7c9bbdaSJeremy L Thompson   @param vec        CeedVector to retrieve state
869b7c9bbdaSJeremy L Thompson   @param[out] ceed  Variable to store ceed
870b7c9bbdaSJeremy L Thompson 
871b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
872b7c9bbdaSJeremy L Thompson 
873b7c9bbdaSJeremy L Thompson   @ref Advanced
874b7c9bbdaSJeremy L Thompson **/
875b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
876b7c9bbdaSJeremy L Thompson   *ceed = vec->ceed;
877b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
878b7c9bbdaSJeremy L Thompson }
879b7c9bbdaSJeremy L Thompson 
880b7c9bbdaSJeremy L Thompson /**
8817a982d89SJeremy L. Thompson   @brief Get the length of a CeedVector
8820436c2adSjeremylt 
8837a982d89SJeremy L. Thompson   @param vec          CeedVector to retrieve length
8847a982d89SJeremy L. Thompson   @param[out] length  Variable to store length
8850436c2adSjeremylt 
8860436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
8870436c2adSjeremylt 
8887a982d89SJeremy L. Thompson   @ref User
8890436c2adSjeremylt **/
8901f9221feSJeremy L Thompson int CeedVectorGetLength(CeedVector vec, CeedSize *length) {
8917a982d89SJeremy L. Thompson   *length = vec->length;
892e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8930436c2adSjeremylt }
8940436c2adSjeremylt 
8950436c2adSjeremylt /**
8960436c2adSjeremylt   @brief Destroy a CeedVector
8970436c2adSjeremylt 
8980436c2adSjeremylt   @param vec  CeedVector to destroy
8990436c2adSjeremylt 
9000436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9010436c2adSjeremylt 
9027a982d89SJeremy L. Thompson   @ref User
9030436c2adSjeremylt **/
9040436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) {
905d1d35e2fSjeremylt   if (!*vec || --(*vec)->ref_count > 0) return CEED_ERROR_SUCCESS;
9060436c2adSjeremylt 
907*2b730f8bSJeremy L Thompson   if (((*vec)->state % 2) == 1) {
908*2b730f8bSJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, the writable access lock is in use");
909*2b730f8bSJeremy L Thompson   }
910*2b730f8bSJeremy L Thompson   if ((*vec)->num_readers > 0) {
911e92b641fSJeremy L Thompson     // LCOV_EXCL_START
912*2b730f8bSJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS, "Cannot destroy CeedVector, a process has read access");
913e92b641fSJeremy L Thompson     // LCOV_EXCL_STOP
9140436c2adSjeremylt   }
9150436c2adSjeremylt 
916*2b730f8bSJeremy L Thompson   if ((*vec)->Destroy) CeedCall((*vec)->Destroy(*vec));
917*2b730f8bSJeremy L Thompson 
918*2b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*vec)->ceed));
919*2b730f8bSJeremy L Thompson   CeedCall(CeedFree(vec));
920e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9210436c2adSjeremylt }
9220436c2adSjeremylt 
9230436c2adSjeremylt /// @}
924