xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-vector.c (revision 9c774eddf8c0b4f5416196d32c5355c9591a7190)
10436c2adSjeremylt // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
20436c2adSjeremylt // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
30436c2adSjeremylt // reserved. See files LICENSE and NOTICE for details.
40436c2adSjeremylt //
50436c2adSjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
60436c2adSjeremylt // libraries and APIs for efficient high-order finite element and spectral
70436c2adSjeremylt // element discretizations for exascale applications. For more information and
80436c2adSjeremylt // source code availability see http://github.com/ceed.
90436c2adSjeremylt //
100436c2adSjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
110436c2adSjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
120436c2adSjeremylt // of Science and the National Nuclear Security Administration) responsible for
130436c2adSjeremylt // the planning and preparation of a capable exascale ecosystem, including
140436c2adSjeremylt // software, applications, hardware, advanced system engineering and early
150436c2adSjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
160436c2adSjeremylt 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <ceed-impl.h>
20547d9b97Sjeremylt #include <math.h>
213d576824SJeremy L Thompson #include <stdint.h>
223d576824SJeremy L Thompson #include <stdio.h>
230436c2adSjeremylt 
247a982d89SJeremy L. Thompson /// @file
257a982d89SJeremy L. Thompson /// Implementation of public CeedVector interfaces
267a982d89SJeremy L. Thompson 
270436c2adSjeremylt /// @cond DOXYGEN_SKIP
280436c2adSjeremylt static struct CeedVector_private ceed_vector_active;
290436c2adSjeremylt static struct CeedVector_private ceed_vector_none;
300436c2adSjeremylt /// @endcond
310436c2adSjeremylt 
327a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser
337a982d89SJeremy L. Thompson /// @{
347a982d89SJeremy L. Thompson 
357a982d89SJeremy L. Thompson /// Indicate that vector will be provided as an explicit argument to
367a982d89SJeremy L. Thompson ///   CeedOperatorApply().
377a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_ACTIVE = &ceed_vector_active;
387a982d89SJeremy L. Thompson 
3996b902e2Sjeremylt /// Indicate that no vector is applicable (i.e., for @ref CEED_EVAL_WEIGHT).
407a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_NONE = &ceed_vector_none;
417a982d89SJeremy L. Thompson 
427a982d89SJeremy L. Thompson /// @}
437a982d89SJeremy L. Thompson 
447a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
457a982d89SJeremy L. Thompson /// CeedVector Backend API
467a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
477a982d89SJeremy L. Thompson /// @addtogroup CeedVectorBackend
487a982d89SJeremy L. Thompson /// @{
497a982d89SJeremy L. Thompson 
507a982d89SJeremy L. Thompson /**
51*9c774eddSJeremy L Thompson   @brief Check for valid data in a CeedVector
52*9c774eddSJeremy L Thompson 
53*9c774eddSJeremy L Thompson   @param vec                   CeedVector to check validity
54*9c774eddSJeremy L Thompson   @param[out] has_valid_array  Variable to store validity
55*9c774eddSJeremy L Thompson 
56*9c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
57*9c774eddSJeremy L Thompson 
58*9c774eddSJeremy L Thompson   @ref Backend
59*9c774eddSJeremy L Thompson **/
60*9c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) {
61*9c774eddSJeremy L Thompson   int ierr;
62*9c774eddSJeremy L Thompson 
63*9c774eddSJeremy L Thompson   if (!vec->HasValidArray)
64*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
65*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
66*9c774eddSJeremy L Thompson                      "Backend does not support HasValidArray");
67*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
68*9c774eddSJeremy L Thompson 
69*9c774eddSJeremy L Thompson   ierr = vec->HasValidArray(vec, has_valid_array); CeedChk(ierr);
70*9c774eddSJeremy L Thompson 
71*9c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
72*9c774eddSJeremy L Thompson }
73*9c774eddSJeremy L Thompson 
74*9c774eddSJeremy L Thompson /**
75*9c774eddSJeremy L Thompson   @brief Check for borrowed array of a specific CeedMemType in a CeedVector
76*9c774eddSJeremy L Thompson 
77*9c774eddSJeremy L Thompson   @param vec                              CeedVector to check
78*9c774eddSJeremy L Thompson   @param mem_type                         Memory type to check
79*9c774eddSJeremy L Thompson   @param[out] has_borrowed_array_of_type  Variable to store result
80*9c774eddSJeremy L Thompson 
81*9c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82*9c774eddSJeremy L Thompson 
83*9c774eddSJeremy L Thompson   @ref Backend
84*9c774eddSJeremy L Thompson **/
85*9c774eddSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type,
86*9c774eddSJeremy L Thompson                                      bool *has_borrowed_array_of_type) {
87*9c774eddSJeremy L Thompson   int ierr;
88*9c774eddSJeremy L Thompson 
89*9c774eddSJeremy L Thompson   if (!vec->HasBorrowedArrayOfType)
90*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
91*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
92*9c774eddSJeremy L Thompson                      "Backend does not support HasBorrowedArrayOfType");
93*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
94*9c774eddSJeremy L Thompson 
95*9c774eddSJeremy L Thompson   ierr = vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type);
96*9c774eddSJeremy L Thompson   CeedChk(ierr);
97*9c774eddSJeremy L Thompson 
98*9c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
99*9c774eddSJeremy L Thompson }
100*9c774eddSJeremy L Thompson 
101*9c774eddSJeremy L Thompson /**
1027a982d89SJeremy L. Thompson   @brief Get the state of a CeedVector
1037a982d89SJeremy L. Thompson 
1047a982d89SJeremy L. Thompson   @param vec         CeedVector to retrieve state
1057a982d89SJeremy L. Thompson   @param[out] state  Variable to store state
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 CeedVectorGetState(CeedVector vec, uint64_t *state) {
1127a982d89SJeremy L. Thompson   *state = vec->state;
113e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1147a982d89SJeremy L. Thompson }
1157a982d89SJeremy L. Thompson 
1167a982d89SJeremy L. Thompson /**
11727312b0fSJed Brown   @brief Add a reference to a CeedVector
1187a982d89SJeremy L. Thompson 
1195f67fadeSJeremy L Thompson   @param[out] vec  CeedVector to increment reference counter
1207a982d89SJeremy L. Thompson 
1217a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1227a982d89SJeremy L. Thompson 
1237a982d89SJeremy L. Thompson   @ref Backend
1247a982d89SJeremy L. Thompson **/
1257a982d89SJeremy L. Thompson int CeedVectorAddReference(CeedVector vec) {
126d1d35e2fSjeremylt   vec->ref_count++;
127e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1287a982d89SJeremy L. Thompson }
1297a982d89SJeremy L. Thompson 
1307a982d89SJeremy L. Thompson /**
1317a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedVector
1327a982d89SJeremy L. Thompson 
1337a982d89SJeremy L. Thompson   @param vec        CeedVector to retrieve state
1347a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
1357a982d89SJeremy L. Thompson 
1367a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1377a982d89SJeremy L. Thompson 
1387a982d89SJeremy L. Thompson   @ref Backend
1397a982d89SJeremy L. Thompson **/
140777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) {
141777ff853SJeremy L Thompson   *(void **)data = vec->data;
142e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1437a982d89SJeremy L. Thompson }
1447a982d89SJeremy L. Thompson 
1457a982d89SJeremy L. Thompson /**
1467a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedVector
1477a982d89SJeremy L. Thompson 
1487a982d89SJeremy L. Thompson   @param[out] vec  CeedVector to retrieve state
1497a982d89SJeremy L. Thompson   @param data      Data to set
1507a982d89SJeremy L. Thompson 
1517a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1527a982d89SJeremy L. Thompson 
1537a982d89SJeremy L. Thompson   @ref Backend
1547a982d89SJeremy L. Thompson **/
155777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) {
156777ff853SJeremy L Thompson   vec->data = data;
157e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1587a982d89SJeremy L. Thompson }
1597a982d89SJeremy L. Thompson 
16034359f16Sjeremylt /**
16134359f16Sjeremylt   @brief Increment the reference counter for a CeedVector
16234359f16Sjeremylt 
16334359f16Sjeremylt   @param vec  CeedVector to increment the reference counter
16434359f16Sjeremylt 
16534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
16634359f16Sjeremylt 
16734359f16Sjeremylt   @ref Backend
16834359f16Sjeremylt **/
1699560d06aSjeremylt int CeedVectorReference(CeedVector vec) {
17034359f16Sjeremylt   vec->ref_count++;
17134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
17234359f16Sjeremylt }
17334359f16Sjeremylt 
1747a982d89SJeremy L. Thompson /// @}
1757a982d89SJeremy L. Thompson 
1767a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1777a982d89SJeremy L. Thompson /// CeedVector Public API
1787a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1797a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser
1800436c2adSjeremylt /// @{
1810436c2adSjeremylt 
1820436c2adSjeremylt /**
1830436c2adSjeremylt   @brief Create a CeedVector of the specified length (does not allocate memory)
1840436c2adSjeremylt 
1850436c2adSjeremylt   @param ceed      Ceed object where the CeedVector will be created
1860436c2adSjeremylt   @param length    Length of vector
1870436c2adSjeremylt   @param[out] vec  Address of the variable where the newly created
1880436c2adSjeremylt                      CeedVector will be stored
1890436c2adSjeremylt 
1900436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
1910436c2adSjeremylt 
1927a982d89SJeremy L. Thompson   @ref User
1930436c2adSjeremylt **/
1940436c2adSjeremylt int CeedVectorCreate(Ceed ceed, CeedInt length, CeedVector *vec) {
1950436c2adSjeremylt   int ierr;
1960436c2adSjeremylt 
1970436c2adSjeremylt   if (!ceed->VectorCreate) {
1980436c2adSjeremylt     Ceed delegate;
1990436c2adSjeremylt     ierr = CeedGetObjectDelegate(ceed, &delegate, "Vector"); CeedChk(ierr);
2000436c2adSjeremylt 
2010436c2adSjeremylt     if (!delegate)
2020436c2adSjeremylt       // LCOV_EXCL_START
203e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
204e15f9bd0SJeremy L Thompson                        "Backend does not support VectorCreate");
2050436c2adSjeremylt     // LCOV_EXCL_STOP
2060436c2adSjeremylt 
2070436c2adSjeremylt     ierr = CeedVectorCreate(delegate, length, vec); CeedChk(ierr);
208e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
2090436c2adSjeremylt   }
2100436c2adSjeremylt 
2110436c2adSjeremylt   ierr = CeedCalloc(1, vec); CeedChk(ierr);
2120436c2adSjeremylt   (*vec)->ceed = ceed;
2139560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
214d1d35e2fSjeremylt   (*vec)->ref_count = 1;
2150436c2adSjeremylt   (*vec)->length = length;
2160436c2adSjeremylt   (*vec)->state = 0;
2170436c2adSjeremylt   ierr = ceed->VectorCreate(length, *vec); CeedChk(ierr);
218e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2190436c2adSjeremylt }
2200436c2adSjeremylt 
2210436c2adSjeremylt /**
2229560d06aSjeremylt   @brief Copy the pointer to a CeedVector. Both pointers should
2239560d06aSjeremylt            be destroyed with `CeedVectorDestroy()`;
2249560d06aSjeremylt            Note: If `*vec_copy` is non-NULL, then it is assumed that
2259560d06aSjeremylt            `*vec_copy` is a pointer to a CeedVector. This
2269560d06aSjeremylt            CeedVector will be destroyed if `*vec_copy` is the only
2279560d06aSjeremylt            reference to this CeedVector.
2289560d06aSjeremylt 
2299560d06aSjeremylt   @param vec            CeedVector to copy reference to
2309560d06aSjeremylt   @param[out] vec_copy  Variable to store copied reference
2319560d06aSjeremylt 
2329560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
2339560d06aSjeremylt 
2349560d06aSjeremylt   @ref User
2359560d06aSjeremylt **/
2369560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) {
2379560d06aSjeremylt   int ierr;
2389560d06aSjeremylt 
2399560d06aSjeremylt   ierr = CeedVectorReference(vec); CeedChk(ierr);
2409560d06aSjeremylt   ierr = CeedVectorDestroy(vec_copy); CeedChk(ierr);
2419560d06aSjeremylt   *vec_copy = vec;
2429560d06aSjeremylt   return CEED_ERROR_SUCCESS;
2439560d06aSjeremylt }
2449560d06aSjeremylt 
2459560d06aSjeremylt /**
2460436c2adSjeremylt   @brief Set the array used by a CeedVector, freeing any previously allocated
247b3cf021fSjeremylt            array if applicable. The backend may copy values to a different
248b3cf021fSjeremylt            memtype, such as during @ref CeedOperatorApply().
2496a6c615bSJeremy L Thompson            See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray().
2500436c2adSjeremylt 
2510436c2adSjeremylt   @param vec        CeedVector
252d1d35e2fSjeremylt   @param mem_type   Memory type of the array being passed
253d1d35e2fSjeremylt   @param copy_mode  Copy mode for the array
2544cc79fe7SJed Brown   @param array      Array to be used, or NULL with @ref CEED_COPY_VALUES to have the
2550436c2adSjeremylt                       library allocate
2560436c2adSjeremylt 
2570436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2580436c2adSjeremylt 
2597a982d89SJeremy L. Thompson   @ref User
2600436c2adSjeremylt **/
261d1d35e2fSjeremylt int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type,
262d1d35e2fSjeremylt                        CeedCopyMode copy_mode,
2630436c2adSjeremylt                        CeedScalar *array) {
2640436c2adSjeremylt   int ierr;
2650436c2adSjeremylt 
2660436c2adSjeremylt   if (!vec->SetArray)
2670436c2adSjeremylt     // LCOV_EXCL_START
268e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
269e15f9bd0SJeremy L Thompson                      "Backend does not support VectorSetArray");
2700436c2adSjeremylt   // LCOV_EXCL_STOP
2710436c2adSjeremylt 
2720436c2adSjeremylt   if (vec->state % 2 == 1)
273e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
274e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, the "
2750436c2adSjeremylt                      "access lock is already in use");
2760436c2adSjeremylt 
277d1d35e2fSjeremylt   if (vec->num_readers > 0)
278e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
279e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, a "
2800436c2adSjeremylt                      "process has read access");
2810436c2adSjeremylt 
282d1d35e2fSjeremylt   ierr = vec->SetArray(vec, mem_type, copy_mode, array); CeedChk(ierr);
2830436c2adSjeremylt   vec->state += 2;
284e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2850436c2adSjeremylt }
2860436c2adSjeremylt 
2870436c2adSjeremylt /**
2880436c2adSjeremylt   @brief Set the CeedVector to a constant value
2890436c2adSjeremylt 
2900436c2adSjeremylt   @param vec        CeedVector
2910436c2adSjeremylt   @param[in] value  Value to be used
2920436c2adSjeremylt 
2930436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2940436c2adSjeremylt 
2957a982d89SJeremy L. Thompson   @ref User
2960436c2adSjeremylt **/
2970436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) {
2980436c2adSjeremylt   int ierr;
2990436c2adSjeremylt 
3000436c2adSjeremylt   if (vec->state % 2 == 1)
301*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
302e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
303e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, the "
3040436c2adSjeremylt                      "access lock is already in use");
305*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
306*9c774eddSJeremy L Thompson 
307*9c774eddSJeremy L Thompson   if (vec->num_readers > 0)
308*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
309*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
310*9c774eddSJeremy L Thompson                      "Cannot grant CeedVector array access, a "
311*9c774eddSJeremy L Thompson                      "process has read access");
312*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
3130436c2adSjeremylt 
3140436c2adSjeremylt   if (vec->SetValue) {
3150436c2adSjeremylt     ierr = vec->SetValue(vec, value); CeedChk(ierr);
3160436c2adSjeremylt   } else {
3170436c2adSjeremylt     CeedScalar *array;
318*9c774eddSJeremy L Thompson     ierr = CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
3190436c2adSjeremylt     for (int i=0; i<vec->length; i++) array[i] = value;
3200436c2adSjeremylt     ierr = CeedVectorRestoreArray(vec, &array); CeedChk(ierr);
3210436c2adSjeremylt   }
3220436c2adSjeremylt   vec->state += 2;
323e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3240436c2adSjeremylt }
3250436c2adSjeremylt 
3260436c2adSjeremylt /**
327b3cf021fSjeremylt   @brief Sync the CeedVector to a specified memtype. This function is used to
328b3cf021fSjeremylt            force synchronization of arrays set with @ref CeedVectorSetArray().
329b3cf021fSjeremylt            If the requested memtype is already synchronized, this function
330b3cf021fSjeremylt            results in a no-op.
3310436c2adSjeremylt 
3320436c2adSjeremylt   @param vec       CeedVector
333d1d35e2fSjeremylt   @param mem_type  Memtype to be synced
3340436c2adSjeremylt 
3350436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3360436c2adSjeremylt 
3377a982d89SJeremy L. Thompson   @ref User
3380436c2adSjeremylt **/
339d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) {
3400436c2adSjeremylt   int ierr;
3410436c2adSjeremylt 
3420436c2adSjeremylt   if (vec->state % 2 == 1)
343e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
344e15f9bd0SJeremy L Thompson                      "Cannot sync CeedVector, the access lock is "
3450436c2adSjeremylt                      "already in use");
3460436c2adSjeremylt 
3470436c2adSjeremylt   if (vec->SyncArray) {
348d1d35e2fSjeremylt     ierr = vec->SyncArray(vec, mem_type); CeedChk(ierr);
3490436c2adSjeremylt   } else {
3500436c2adSjeremylt     const CeedScalar *array;
351d1d35e2fSjeremylt     ierr = CeedVectorGetArrayRead(vec, mem_type, &array); CeedChk(ierr);
3520436c2adSjeremylt     ierr = CeedVectorRestoreArrayRead(vec, &array); CeedChk(ierr);
3530436c2adSjeremylt   }
354e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3550436c2adSjeremylt }
3560436c2adSjeremylt 
3570436c2adSjeremylt /**
358*9c774eddSJeremy L Thompson   @brief Take ownership of the CeedVector array set by @ref CeedVectorSetArray()
359*9c774eddSJeremy L Thompson            with @ref CEED_USE_POINTER and remove the array from the CeedVector.
360*9c774eddSJeremy L Thompson            The caller is responsible for managing and freeing the array.
361*9c774eddSJeremy L Thompson            This function will error if @ref CeedVectorSetArray() was not previously
362*9c774eddSJeremy L Thompson            called with @ref CEED_USE_POINTER for the corresponding mem_type.
3636a6c615bSJeremy L Thompson 
3646a6c615bSJeremy L Thompson   @param vec         CeedVector
365d1d35e2fSjeremylt   @param mem_type    Memory type on which to take the array. If the backend
3666a6c615bSJeremy L Thompson                        uses a different memory type, this will perform a copy.
367d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type, or NULL if array pointer is
3686a6c615bSJeremy L Thompson                        not required
3696a6c615bSJeremy L Thompson 
3706a6c615bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3716a6c615bSJeremy L Thompson 
3726a6c615bSJeremy L Thompson   @ref User
3736a6c615bSJeremy L Thompson **/
374d1d35e2fSjeremylt int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type,
375d1d35e2fSjeremylt                         CeedScalar **array) {
3766a6c615bSJeremy L Thompson   int ierr;
3776a6c615bSJeremy L Thompson 
3786a6c615bSJeremy L Thompson   if (vec->state % 2 == 1)
3796a6c615bSJeremy L Thompson     // LCOV_EXCL_START
380e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
381e15f9bd0SJeremy L Thompson                      "Cannot take CeedVector array, the access "
3826a6c615bSJeremy L Thompson                      "lock is already in use");
3836a6c615bSJeremy L Thompson   // LCOV_EXCL_STOP
384d1d35e2fSjeremylt   if (vec->num_readers > 0)
3856a6c615bSJeremy L Thompson     // LCOV_EXCL_START
386e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
387e15f9bd0SJeremy L Thompson                      "Cannot take CeedVector array, a process "
3886a6c615bSJeremy L Thompson                      "has read access");
3896a6c615bSJeremy L Thompson   // LCOV_EXCL_STOP
3906a6c615bSJeremy L Thompson 
391*9c774eddSJeremy L Thompson   bool has_borrowed_array_of_type = true;
392*9c774eddSJeremy L Thompson   ierr = CeedVectorHasBorrowedArrayOfType(vec, mem_type,
393*9c774eddSJeremy L Thompson                                           &has_borrowed_array_of_type);
394*9c774eddSJeremy L Thompson   CeedChk(ierr);
395*9c774eddSJeremy L Thompson   if (!has_borrowed_array_of_type)
396*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
397*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
398*9c774eddSJeremy L Thompson                      "CeedVector has no borrowed %s array, "
399*9c774eddSJeremy L Thompson                      "must set array with CeedVectorSetArray", CeedMemTypes[mem_type]);
400*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
401*9c774eddSJeremy L Thompson 
402*9c774eddSJeremy L Thompson   bool has_valid_array = true;
403*9c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
404*9c774eddSJeremy L Thompson   if (!has_valid_array)
405*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
406*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
407*9c774eddSJeremy L Thompson                      "CeedVector has no valid data to take, "
408*9c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
409*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
410*9c774eddSJeremy L Thompson 
411d1d35e2fSjeremylt   CeedScalar *temp_array = NULL;
412d1d35e2fSjeremylt   ierr = vec->TakeArray(vec, mem_type, &temp_array); CeedChk(ierr);
413d1d35e2fSjeremylt   if (array) (*array) = temp_array;
414e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4156a6c615bSJeremy L Thompson }
4166a6c615bSJeremy L Thompson 
4176a6c615bSJeremy L Thompson /**
418b3cf021fSjeremylt   @brief Get read/write access to a CeedVector via the specified memory type.
419b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArray().
4200436c2adSjeremylt 
4210436c2adSjeremylt   @param vec         CeedVector to access
422d1d35e2fSjeremylt   @param mem_type    Memory type on which to access the array. If the backend
423b3cf021fSjeremylt                        uses a different memory type, this will perform a copy.
424d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type
4250436c2adSjeremylt 
4260436c2adSjeremylt   @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide
4270436c2adSjeremylt     access to array pointers in the desired memory space. Pairing get/restore
4280436c2adSjeremylt     allows the Vector to track access, thus knowing if norms or other
4290436c2adSjeremylt     operations may need to be recomputed.
4300436c2adSjeremylt 
4310436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4320436c2adSjeremylt 
4337a982d89SJeremy L. Thompson   @ref User
4340436c2adSjeremylt **/
435d1d35e2fSjeremylt int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type,
436d1d35e2fSjeremylt                        CeedScalar **array) {
4370436c2adSjeremylt   int ierr;
4380436c2adSjeremylt 
4390436c2adSjeremylt   if (!vec->GetArray)
4400436c2adSjeremylt     // LCOV_EXCL_START
441e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
442e15f9bd0SJeremy L Thompson                      "Backend does not support GetArray");
4430436c2adSjeremylt   // LCOV_EXCL_STOP
4440436c2adSjeremylt 
4450436c2adSjeremylt   if (vec->state % 2 == 1)
446e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
447e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, the "
4480436c2adSjeremylt                      "access lock is already in use");
4490436c2adSjeremylt 
450d1d35e2fSjeremylt   if (vec->num_readers > 0)
451e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
452e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, a "
4530436c2adSjeremylt                      "process has read access");
4540436c2adSjeremylt 
455*9c774eddSJeremy L Thompson   bool has_valid_array = true;
456*9c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
457*9c774eddSJeremy L Thompson   if (!has_valid_array)
458*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
459*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
460*9c774eddSJeremy L Thompson                      "CeedVector has no valid data to read, "
461*9c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
462*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
463*9c774eddSJeremy L Thompson 
464d1d35e2fSjeremylt   ierr = vec->GetArray(vec, mem_type, array); CeedChk(ierr);
4650436c2adSjeremylt   vec->state += 1;
466e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4670436c2adSjeremylt }
4680436c2adSjeremylt 
4690436c2adSjeremylt /**
470b3cf021fSjeremylt   @brief Get read-only access to a CeedVector via the specified memory type.
471b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArrayRead().
4720436c2adSjeremylt 
4730436c2adSjeremylt   @param vec         CeedVector to access
474d1d35e2fSjeremylt   @param mem_type    Memory type on which to access the array.  If the backend
4750436c2adSjeremylt                        uses a different memory type, this will perform a copy
4760436c2adSjeremylt                        (possibly cached).
477d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type
4780436c2adSjeremylt 
4790436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4800436c2adSjeremylt 
4817a982d89SJeremy L. Thompson   @ref User
4820436c2adSjeremylt **/
483d1d35e2fSjeremylt int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type,
4840436c2adSjeremylt                            const CeedScalar **array) {
4850436c2adSjeremylt   int ierr;
4860436c2adSjeremylt 
4870436c2adSjeremylt   if (!vec->GetArrayRead)
4880436c2adSjeremylt     // LCOV_EXCL_START
489e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
490e15f9bd0SJeremy L Thompson                      "Backend does not support GetArrayRead");
4910436c2adSjeremylt   // LCOV_EXCL_STOP
4920436c2adSjeremylt 
4930436c2adSjeremylt   if (vec->state % 2 == 1)
494e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
495e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector read-only array "
4960436c2adSjeremylt                      "access, the access lock is already in use");
4970436c2adSjeremylt 
498*9c774eddSJeremy L Thompson   bool has_valid_array = true;
499*9c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
500*9c774eddSJeremy L Thompson   if (!has_valid_array)
501*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
502*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
503*9c774eddSJeremy L Thompson                      "CeedVector has no valid data to read, "
504*9c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
505*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
506*9c774eddSJeremy L Thompson 
507d1d35e2fSjeremylt   ierr = vec->GetArrayRead(vec, mem_type, array); CeedChk(ierr);
508d1d35e2fSjeremylt   vec->num_readers++;
509e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5100436c2adSjeremylt }
5110436c2adSjeremylt 
5120436c2adSjeremylt /**
513*9c774eddSJeremy L Thompson   @brief Get write access to a CeedVector via the specified memory type.
514*9c774eddSJeremy L Thompson            Restore access with @ref CeedVectorRestoreArray(). All old
515*9c774eddSJeremy L Thompson            values should be assumed to be invalid.
516*9c774eddSJeremy L Thompson 
517*9c774eddSJeremy L Thompson   @param vec         CeedVector to access
518*9c774eddSJeremy L Thompson   @param mem_type    Memory type on which to access the array.
519*9c774eddSJeremy L Thompson   @param[out] array  Array on memory type mem_type
520*9c774eddSJeremy L Thompson 
521*9c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
522*9c774eddSJeremy L Thompson 
523*9c774eddSJeremy L Thompson   @ref User
524*9c774eddSJeremy L Thompson **/
525*9c774eddSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type,
526*9c774eddSJeremy L Thompson                             CeedScalar **array) {
527*9c774eddSJeremy L Thompson   int ierr;
528*9c774eddSJeremy L Thompson 
529*9c774eddSJeremy L Thompson   if (!vec->GetArrayWrite)
530*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
531*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
532*9c774eddSJeremy L Thompson                      "Backend does not support GetArrayWrite");
533*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
534*9c774eddSJeremy L Thompson 
535*9c774eddSJeremy L Thompson   if (vec->state % 2 == 1)
536*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
537*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
538*9c774eddSJeremy L Thompson                      "Cannot grant CeedVector array access, the "
539*9c774eddSJeremy L Thompson                      "access lock is already in use");
540*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
541*9c774eddSJeremy L Thompson 
542*9c774eddSJeremy L Thompson   if (vec->num_readers > 0)
543*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
544*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
545*9c774eddSJeremy L Thompson                      "Cannot grant CeedVector array access, a "
546*9c774eddSJeremy L Thompson                      "process has read access");
547*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
548*9c774eddSJeremy L Thompson 
549*9c774eddSJeremy L Thompson   ierr = vec->GetArrayWrite(vec, mem_type, array); CeedChk(ierr);
550*9c774eddSJeremy L Thompson   vec->state += 1;
551*9c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
552*9c774eddSJeremy L Thompson }
553*9c774eddSJeremy L Thompson 
554*9c774eddSJeremy L Thompson /**
555b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArray()
5560436c2adSjeremylt 
5570436c2adSjeremylt   @param vec    CeedVector to restore
5580436c2adSjeremylt   @param array  Array of vector data
5590436c2adSjeremylt 
5600436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5610436c2adSjeremylt 
5627a982d89SJeremy L. Thompson   @ref User
5630436c2adSjeremylt **/
5640436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
5650436c2adSjeremylt   int ierr;
5660436c2adSjeremylt 
5670436c2adSjeremylt   if (!vec->RestoreArray)
5680436c2adSjeremylt     // LCOV_EXCL_START
569e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
570e15f9bd0SJeremy L Thompson                      "Backend does not support RestoreArray");
5710436c2adSjeremylt   // LCOV_EXCL_STOP
5720436c2adSjeremylt 
5730436c2adSjeremylt   if (vec->state % 2 != 1)
574e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
575e15f9bd0SJeremy L Thompson                      "Cannot restore CeedVector array access, "
5760436c2adSjeremylt                      "access was not granted");
5770436c2adSjeremylt 
5780436c2adSjeremylt   ierr = vec->RestoreArray(vec); CeedChk(ierr);
5790436c2adSjeremylt   *array = NULL;
5800436c2adSjeremylt   vec->state += 1;
581e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5820436c2adSjeremylt }
5830436c2adSjeremylt 
5840436c2adSjeremylt /**
585b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArrayRead()
5860436c2adSjeremylt 
5870436c2adSjeremylt   @param vec    CeedVector to restore
5880436c2adSjeremylt   @param array  Array of vector data
5890436c2adSjeremylt 
5900436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5910436c2adSjeremylt 
5927a982d89SJeremy L. Thompson   @ref User
5930436c2adSjeremylt **/
5940436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) {
5950436c2adSjeremylt   int ierr;
5960436c2adSjeremylt 
5970436c2adSjeremylt   if (!vec->RestoreArrayRead)
5980436c2adSjeremylt     // LCOV_EXCL_START
599e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
600e15f9bd0SJeremy L Thompson                      "Backend does not support RestoreArrayRead");
6010436c2adSjeremylt   // LCOV_EXCL_STOP
6020436c2adSjeremylt 
603*9c774eddSJeremy L Thompson   if (vec->num_readers == 0)
604*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
605*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
606*9c774eddSJeremy L Thompson                      "Cannot restore CeedVector array read access, "
607*9c774eddSJeremy L Thompson                      "access was not granted");
608*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
609*9c774eddSJeremy L Thompson 
6100436c2adSjeremylt   ierr = vec->RestoreArrayRead(vec); CeedChk(ierr);
6110436c2adSjeremylt   *array = NULL;
612d1d35e2fSjeremylt   vec->num_readers--;
613e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6140436c2adSjeremylt }
6150436c2adSjeremylt 
6160436c2adSjeremylt /**
617171f8ca9Sjeremylt   @brief Get the norm of a CeedVector.
618171f8ca9Sjeremylt 
619171f8ca9Sjeremylt   Note: This operation is local to the CeedVector. This function will likely
620171f8ca9Sjeremylt           not provide the desired results for the norm of the libCEED portion
621171f8ca9Sjeremylt           of a parallel vector or a CeedVector with duplicated or hanging nodes.
622547d9b97Sjeremylt 
623547d9b97Sjeremylt   @param vec        CeedVector to retrieve maximum value
624d1d35e2fSjeremylt   @param norm_type  Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
625547d9b97Sjeremylt   @param[out] norm  Variable to store norm value
626547d9b97Sjeremylt 
627547d9b97Sjeremylt   @return An error code: 0 - success, otherwise - failure
628547d9b97Sjeremylt 
6297a982d89SJeremy L. Thompson   @ref User
630547d9b97Sjeremylt **/
631d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
632547d9b97Sjeremylt   int ierr;
633547d9b97Sjeremylt 
634*9c774eddSJeremy L Thompson   bool has_valid_array = true;
635*9c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
636*9c774eddSJeremy L Thompson   if (!has_valid_array)
637*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
638*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
639*9c774eddSJeremy L Thompson                      "CeedVector has no valid data to compute norm, "
640*9c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
641*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
642*9c774eddSJeremy L Thompson 
643547d9b97Sjeremylt   // Backend impl for GPU, if added
644547d9b97Sjeremylt   if (vec->Norm) {
645d1d35e2fSjeremylt     ierr = vec->Norm(vec, norm_type, norm); CeedChk(ierr);
646e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
647547d9b97Sjeremylt   }
648547d9b97Sjeremylt 
649547d9b97Sjeremylt   const CeedScalar *array;
650547d9b97Sjeremylt   ierr = CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
651547d9b97Sjeremylt 
652547d9b97Sjeremylt   *norm = 0.;
653d1d35e2fSjeremylt   switch (norm_type) {
654547d9b97Sjeremylt   case CEED_NORM_1:
655547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
656547d9b97Sjeremylt       *norm += fabs(array[i]);
657547d9b97Sjeremylt     }
658547d9b97Sjeremylt     break;
659547d9b97Sjeremylt   case CEED_NORM_2:
660547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
661547d9b97Sjeremylt       *norm += fabs(array[i])*fabs(array[i]);
662547d9b97Sjeremylt     }
663547d9b97Sjeremylt     break;
664547d9b97Sjeremylt   case CEED_NORM_MAX:
665547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
666d1d35e2fSjeremylt       const CeedScalar abs_v_i = fabs(array[i]);
667d1d35e2fSjeremylt       *norm = *norm > abs_v_i ? *norm : abs_v_i;
668547d9b97Sjeremylt     }
669547d9b97Sjeremylt   }
670d1d35e2fSjeremylt   if (norm_type == CEED_NORM_2)
671547d9b97Sjeremylt     *norm = sqrt(*norm);
672547d9b97Sjeremylt 
673547d9b97Sjeremylt   ierr = CeedVectorRestoreArrayRead(vec, &array); CeedChk(ierr);
674e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
675547d9b97Sjeremylt }
676547d9b97Sjeremylt 
677547d9b97Sjeremylt /**
678e0dd3b27Sjeremylt   @brief Compute x = alpha x
679e0dd3b27Sjeremylt 
68096b902e2Sjeremylt   @param[in,out] x  vector for scaling
68196b902e2Sjeremylt   @param[in] alpha  scaling factor
682e0dd3b27Sjeremylt 
683e0dd3b27Sjeremylt   @return An error code: 0 - success, otherwise - failure
684e0dd3b27Sjeremylt 
685e0dd3b27Sjeremylt   @ref User
686e0dd3b27Sjeremylt **/
687e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) {
688e0dd3b27Sjeremylt   int ierr;
689e0dd3b27Sjeremylt   CeedScalar *x_array;
690e0dd3b27Sjeremylt   CeedInt n_x;
691e0dd3b27Sjeremylt 
692*9c774eddSJeremy L Thompson   bool has_valid_array = true;
693*9c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(x, &has_valid_array); CeedChk(ierr);
694*9c774eddSJeremy L Thompson   if (!has_valid_array)
695*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
696*9c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
697*9c774eddSJeremy L Thompson                      "CeedVector has no valid data to scale, "
698*9c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
699*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
700*9c774eddSJeremy L Thompson 
701e0dd3b27Sjeremylt   ierr = CeedVectorGetLength(x, &n_x); CeedChk(ierr);
702e0dd3b27Sjeremylt 
703e0dd3b27Sjeremylt   // Backend implementation
704e0dd3b27Sjeremylt   if (x->Scale)
705e0dd3b27Sjeremylt     return x->Scale(x, alpha);
706e0dd3b27Sjeremylt 
707e0dd3b27Sjeremylt   // Default implementation
708*9c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array); CeedChk(ierr);
709e0dd3b27Sjeremylt   for (CeedInt i=0; i<n_x; i++)
710e0dd3b27Sjeremylt     x_array[i] *= alpha;
711e0dd3b27Sjeremylt   ierr = CeedVectorRestoreArray(x, &x_array); CeedChk(ierr);
712e0dd3b27Sjeremylt 
713e0dd3b27Sjeremylt   return CEED_ERROR_SUCCESS;
714e0dd3b27Sjeremylt }
715e0dd3b27Sjeremylt 
716e0dd3b27Sjeremylt /**
7170f7fd0f8Sjeremylt   @brief Compute y = alpha x + y
7180f7fd0f8Sjeremylt 
71996b902e2Sjeremylt   @param[in,out] y  target vector for sum
72096b902e2Sjeremylt   @param[in] alpha  scaling factor
72196b902e2Sjeremylt   @param[in] x      second vector, must be different than y
7220f7fd0f8Sjeremylt 
7230f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
7240f7fd0f8Sjeremylt 
7250f7fd0f8Sjeremylt   @ref User
7260f7fd0f8Sjeremylt **/
7270f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
7280f7fd0f8Sjeremylt   int ierr;
7290f7fd0f8Sjeremylt   CeedScalar *y_array;
7300f7fd0f8Sjeremylt   CeedScalar const *x_array;
7310f7fd0f8Sjeremylt   CeedInt n_x, n_y;
7320f7fd0f8Sjeremylt 
7330f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(y, &n_y); CeedChk(ierr);
7340f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(x, &n_x); CeedChk(ierr);
7350f7fd0f8Sjeremylt   if (n_x != n_y)
7360f7fd0f8Sjeremylt     // LCOV_EXCL_START
7370f7fd0f8Sjeremylt     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED,
7380f7fd0f8Sjeremylt                      "Cannot add vector of different lengths");
7390f7fd0f8Sjeremylt   // LCOV_EXCL_STOP
7400f7fd0f8Sjeremylt   if (x == y)
7410f7fd0f8Sjeremylt     // LCOV_EXCL_START
7420f7fd0f8Sjeremylt     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED,
7430f7fd0f8Sjeremylt                      "Cannot use same vector for x and y in CeedVectorAXPY");
7440f7fd0f8Sjeremylt   // LCOV_EXCL_STOP
7450f7fd0f8Sjeremylt 
746*9c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
747*9c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(x, &has_valid_array_x); CeedChk(ierr);
748*9c774eddSJeremy L Thompson   if (!has_valid_array_x)
749*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
750*9c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
751*9c774eddSJeremy L Thompson                      "CeedVector x has no valid data, "
752*9c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
753*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
754*9c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(y, &has_valid_array_y); CeedChk(ierr);
755*9c774eddSJeremy L Thompson   if (!has_valid_array_y)
756*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
757*9c774eddSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND,
758*9c774eddSJeremy L Thompson                      "CeedVector y has no valid data, "
759*9c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
760*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
761*9c774eddSJeremy L Thompson 
7622d04630dSjeremylt   Ceed ceed_parent_x, ceed_parent_y;
7632d04630dSjeremylt   ierr = CeedGetParent(x->ceed, &ceed_parent_x); CeedChk(ierr);
7642d04630dSjeremylt   ierr = CeedGetParent(y->ceed, &ceed_parent_y); CeedChk(ierr);
7652d04630dSjeremylt   if (ceed_parent_x != ceed_parent_y)
7662d04630dSjeremylt     // LCOV_EXCL_START
7672d04630dSjeremylt     return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE,
7682d04630dSjeremylt                      "Vectors x and y must be created by the same Ceed context");
7692d04630dSjeremylt   // LCOV_EXCL_STOP
7702d04630dSjeremylt 
7710f7fd0f8Sjeremylt   // Backend implementation
772eaf62fffSJeremy L Thompson   if (y->AXPY) {
773eaf62fffSJeremy L Thompson     ierr = y->AXPY(y, alpha, x); CeedChk(ierr);
774eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
775eaf62fffSJeremy L Thompson   }
7760f7fd0f8Sjeremylt 
7770f7fd0f8Sjeremylt   // Default implementation
778*9c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array); CeedChk(ierr);
7790f7fd0f8Sjeremylt   ierr = CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array); CeedChk(ierr);
7800f7fd0f8Sjeremylt 
7810f7fd0f8Sjeremylt   for (CeedInt i=0; i<n_y; i++)
7820f7fd0f8Sjeremylt     y_array[i] += alpha * x_array[i];
7830f7fd0f8Sjeremylt 
7840f7fd0f8Sjeremylt   ierr = CeedVectorRestoreArray(y, &y_array); CeedChk(ierr);
7850f7fd0f8Sjeremylt   ierr = CeedVectorRestoreArrayRead(x, &x_array); CeedChk(ierr);
7860f7fd0f8Sjeremylt 
7870f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
7880f7fd0f8Sjeremylt }
7890f7fd0f8Sjeremylt 
7900f7fd0f8Sjeremylt /**
7910c1bc3c2Sjeremylt   @brief Compute the pointwise multiplication w = x .* y. Any
7920f7fd0f8Sjeremylt            subset of x, y, and w may be the same vector.
7930f7fd0f8Sjeremylt 
79496b902e2Sjeremylt   @param[out] w  target vector for the product
79596b902e2Sjeremylt   @param[in] x   first vector for product
79696b902e2Sjeremylt   @param[in] y   second vector for the product
7970f7fd0f8Sjeremylt 
7980f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
7990f7fd0f8Sjeremylt 
8000f7fd0f8Sjeremylt   @ ref User
8010f7fd0f8Sjeremylt **/
8020f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
8030f7fd0f8Sjeremylt   int ierr;
8040f7fd0f8Sjeremylt   CeedScalar *w_array;
8050f7fd0f8Sjeremylt   CeedScalar const *x_array, *y_array;
8062d04630dSjeremylt   CeedInt n_w, n_x, n_y;
8070f7fd0f8Sjeremylt 
8080f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(w, &n_w); CeedChk(ierr);
8090f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(x, &n_x); CeedChk(ierr);
8100f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(y, &n_y); CeedChk(ierr);
8110f7fd0f8Sjeremylt   if (n_w != n_x || n_w != n_y)
8120f7fd0f8Sjeremylt     // LCOV_EXCL_START
8130f7fd0f8Sjeremylt     return CeedError(w->ceed, CEED_ERROR_UNSUPPORTED,
8140f7fd0f8Sjeremylt                      "Cannot multiply vectors of different lengths");
8150f7fd0f8Sjeremylt   // LCOV_EXCL_STOP
8160f7fd0f8Sjeremylt 
8172d04630dSjeremylt   Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y;
8182d04630dSjeremylt   ierr = CeedGetParent(w->ceed, &ceed_parent_w); CeedChk(ierr);
8192d04630dSjeremylt   ierr = CeedGetParent(x->ceed, &ceed_parent_x); CeedChk(ierr);
8202d04630dSjeremylt   ierr = CeedGetParent(y->ceed, &ceed_parent_y); CeedChk(ierr);
821*9c774eddSJeremy L Thompson   if ((ceed_parent_w != ceed_parent_x) ||
8222d04630dSjeremylt       (ceed_parent_w != ceed_parent_y))
8232d04630dSjeremylt     // LCOV_EXCL_START
8242d04630dSjeremylt     return CeedError(w->ceed, CEED_ERROR_INCOMPATIBLE,
8252d04630dSjeremylt                      "Vectors w, x, and y must be created by the same Ceed context");
8262d04630dSjeremylt   // LCOV_EXCL_STOP
8272d04630dSjeremylt 
828*9c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
829*9c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(x, &has_valid_array_x); CeedChk(ierr);
830*9c774eddSJeremy L Thompson   if (!has_valid_array_x)
831*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
832*9c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
833*9c774eddSJeremy L Thompson                      "CeedVector x has no valid data, "
834*9c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
835*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
836*9c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(y, &has_valid_array_y); CeedChk(ierr);
837*9c774eddSJeremy L Thompson   if (!has_valid_array_y)
838*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
839*9c774eddSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND,
840*9c774eddSJeremy L Thompson                      "CeedVector y has no valid data, "
841*9c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
842*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
843*9c774eddSJeremy L Thompson 
8440f7fd0f8Sjeremylt   // Backend implementation
845eaf62fffSJeremy L Thompson   if (w->PointwiseMult) {
846eaf62fffSJeremy L Thompson     ierr = w->PointwiseMult(w, x, y); CeedChk(ierr);
847eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
848eaf62fffSJeremy L Thompson   }
8490f7fd0f8Sjeremylt 
8500f7fd0f8Sjeremylt   // Default implementation
851*9c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array); CeedChk(ierr);
8520f7fd0f8Sjeremylt   if (x != w) {
8530f7fd0f8Sjeremylt     ierr = CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array); CeedChk(ierr);
8540f7fd0f8Sjeremylt   } else {
8550f7fd0f8Sjeremylt     x_array = w_array;
8560f7fd0f8Sjeremylt   }
8570f7fd0f8Sjeremylt   if (y != w && y != x) {
8580f7fd0f8Sjeremylt     ierr = CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array); CeedChk(ierr);
8590f7fd0f8Sjeremylt   } else if (y != x) {
8600f7fd0f8Sjeremylt     y_array = w_array;
8610f7fd0f8Sjeremylt   } else {
8620f7fd0f8Sjeremylt     y_array = x_array;
8630f7fd0f8Sjeremylt   }
8640f7fd0f8Sjeremylt 
8650f7fd0f8Sjeremylt   for (CeedInt i=0; i<n_w; i++)
8660f7fd0f8Sjeremylt     w_array[i] = x_array[i] * y_array[i];
8670f7fd0f8Sjeremylt 
8680f7fd0f8Sjeremylt   if (y != w && y != x) {
8690f7fd0f8Sjeremylt     ierr = CeedVectorRestoreArrayRead(y, &y_array); CeedChk(ierr);
8700f7fd0f8Sjeremylt   }
8710f7fd0f8Sjeremylt   if (x != w) {
8720f7fd0f8Sjeremylt     ierr = CeedVectorRestoreArrayRead(x, &x_array); CeedChk(ierr);
8730f7fd0f8Sjeremylt   }
8740f7fd0f8Sjeremylt   ierr = CeedVectorRestoreArray(w, &w_array); CeedChk(ierr);
8750f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
8760f7fd0f8Sjeremylt }
8770f7fd0f8Sjeremylt 
8780f7fd0f8Sjeremylt /**
879d99fa3c5SJeremy L Thompson   @brief Take the reciprocal of a CeedVector.
880d99fa3c5SJeremy L Thompson 
881d99fa3c5SJeremy L Thompson   @param vec  CeedVector to take reciprocal
882d99fa3c5SJeremy L Thompson 
883d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
884d99fa3c5SJeremy L Thompson 
885d99fa3c5SJeremy L Thompson   @ref User
886d99fa3c5SJeremy L Thompson **/
887d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) {
888d99fa3c5SJeremy L Thompson   int ierr;
889d99fa3c5SJeremy L Thompson 
890*9c774eddSJeremy L Thompson   bool has_valid_array = true;
891*9c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
892*9c774eddSJeremy L Thompson   if (!has_valid_array)
893*9c774eddSJeremy L Thompson     // LCOV_EXCL_START
894*9c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
895*9c774eddSJeremy L Thompson                      "CeedVector has no valid data to compute reciprocal, "
896*9c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
897*9c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
898*9c774eddSJeremy L Thompson 
899d99fa3c5SJeremy L Thompson   // Check if vector data set
900d99fa3c5SJeremy L Thompson   if (!vec->state)
901d99fa3c5SJeremy L Thompson     // LCOV_EXCL_START
902e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE,
903d99fa3c5SJeremy L Thompson                      "CeedVector must have data set to take reciprocal");
904d99fa3c5SJeremy L Thompson   // LCOV_EXCL_STOP
905d99fa3c5SJeremy L Thompson 
906d99fa3c5SJeremy L Thompson   // Backend impl for GPU, if added
907d99fa3c5SJeremy L Thompson   if (vec->Reciprocal) {
908d99fa3c5SJeremy L Thompson     ierr = vec->Reciprocal(vec); CeedChk(ierr);
909e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
910d99fa3c5SJeremy L Thompson   }
911d99fa3c5SJeremy L Thompson 
912d99fa3c5SJeremy L Thompson   CeedInt len;
913d99fa3c5SJeremy L Thompson   ierr = CeedVectorGetLength(vec, &len); CeedChk(ierr);
914d99fa3c5SJeremy L Thompson   CeedScalar *array;
915*9c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
916d99fa3c5SJeremy L Thompson   for (CeedInt i=0; i<len; i++)
917d99fa3c5SJeremy L Thompson     if (fabs(array[i]) > CEED_EPSILON)
918d99fa3c5SJeremy L Thompson       array[i] = 1./array[i];
919d99fa3c5SJeremy L Thompson 
920e15f9bd0SJeremy L Thompson   ierr = CeedVectorRestoreArray(vec, &array); CeedChk(ierr);
921e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
922d99fa3c5SJeremy L Thompson }
923d99fa3c5SJeremy L Thompson 
924d99fa3c5SJeremy L Thompson /**
9257a982d89SJeremy L. Thompson   @brief View a CeedVector
9260436c2adSjeremylt 
9270a0da059Sjeremylt   @param[in] vec     CeedVector to view
928d1d35e2fSjeremylt   @param[in] fp_fmt  Printing format
9290a0da059Sjeremylt   @param[in] stream  Filestream to write to
9300a0da059Sjeremylt 
9310436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9320436c2adSjeremylt 
9337a982d89SJeremy L. Thompson   @ref User
9340436c2adSjeremylt **/
935d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
9367a982d89SJeremy L. Thompson   const CeedScalar *x;
9377a982d89SJeremy L. Thompson 
9387a982d89SJeremy L. Thompson   int ierr = CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x); CeedChk(ierr);
9397a982d89SJeremy L. Thompson 
9407a982d89SJeremy L. Thompson   char fmt[1024];
9417a982d89SJeremy L. Thompson   fprintf(stream, "CeedVector length %ld\n", (long)vec->length);
942d1d35e2fSjeremylt   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
9437a982d89SJeremy L. Thompson   for (CeedInt i=0; i<vec->length; i++)
9447a982d89SJeremy L. Thompson     fprintf(stream, fmt, x[i]);
9457a982d89SJeremy L. Thompson 
9467a982d89SJeremy L. Thompson   ierr = CeedVectorRestoreArrayRead(vec, &x); CeedChk(ierr);
947e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9480436c2adSjeremylt }
9490436c2adSjeremylt 
9500436c2adSjeremylt /**
951b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedVector
952b7c9bbdaSJeremy L Thompson 
953b7c9bbdaSJeremy L Thompson   @param vec        CeedVector to retrieve state
954b7c9bbdaSJeremy L Thompson   @param[out] ceed  Variable to store ceed
955b7c9bbdaSJeremy L Thompson 
956b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
957b7c9bbdaSJeremy L Thompson 
958b7c9bbdaSJeremy L Thompson   @ref Advanced
959b7c9bbdaSJeremy L Thompson **/
960b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
961b7c9bbdaSJeremy L Thompson   *ceed = vec->ceed;
962b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
963b7c9bbdaSJeremy L Thompson }
964b7c9bbdaSJeremy L Thompson 
965b7c9bbdaSJeremy L Thompson /**
9667a982d89SJeremy L. Thompson   @brief Get the length of a CeedVector
9670436c2adSjeremylt 
9687a982d89SJeremy L. Thompson   @param vec          CeedVector to retrieve length
9697a982d89SJeremy L. Thompson   @param[out] length  Variable to store length
9700436c2adSjeremylt 
9710436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9720436c2adSjeremylt 
9737a982d89SJeremy L. Thompson   @ref User
9740436c2adSjeremylt **/
9757a982d89SJeremy L. Thompson int CeedVectorGetLength(CeedVector vec, CeedInt *length) {
9767a982d89SJeremy L. Thompson   *length = vec->length;
977e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9780436c2adSjeremylt }
9790436c2adSjeremylt 
9800436c2adSjeremylt /**
9810436c2adSjeremylt   @brief Destroy a CeedVector
9820436c2adSjeremylt 
9830436c2adSjeremylt   @param vec  CeedVector to destroy
9840436c2adSjeremylt 
9850436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9860436c2adSjeremylt 
9877a982d89SJeremy L. Thompson   @ref User
9880436c2adSjeremylt **/
9890436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) {
9900436c2adSjeremylt   int ierr;
9910436c2adSjeremylt 
992d1d35e2fSjeremylt   if (!*vec || --(*vec)->ref_count > 0) return CEED_ERROR_SUCCESS;
9930436c2adSjeremylt 
994187168c7SJeremy L Thompson   if (((*vec)->state % 2) == 1)
995e15f9bd0SJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS,
996187168c7SJeremy L Thompson                      "Cannot destroy CeedVector, the writable access "
9970436c2adSjeremylt                      "lock is in use");
9980436c2adSjeremylt 
999d1d35e2fSjeremylt   if ((*vec)->num_readers > 0)
1000e92b641fSJeremy L Thompson     // LCOV_EXCL_START
1001e15f9bd0SJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS,
1002e15f9bd0SJeremy L Thompson                      "Cannot destroy CeedVector, a process has "
1003187168c7SJeremy L Thompson                      "read access");
1004e92b641fSJeremy L Thompson   // LCOV_EXCL_STOP
1005187168c7SJeremy L Thompson 
10060436c2adSjeremylt   if ((*vec)->Destroy) {
10070436c2adSjeremylt     ierr = (*vec)->Destroy(*vec); CeedChk(ierr);
10080436c2adSjeremylt   }
10090436c2adSjeremylt 
10100436c2adSjeremylt   ierr = CeedDestroy(&(*vec)->ceed); CeedChk(ierr);
10110436c2adSjeremylt   ierr = CeedFree(vec); CeedChk(ierr);
1012e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10130436c2adSjeremylt }
10140436c2adSjeremylt 
10150436c2adSjeremylt /// @}
1016