xref: /libCEED/interface/ceed-vector.c (revision 50c643e1903dbe750057698594f940a911295e56)
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 /**
519c774eddSJeremy L Thompson   @brief Check for valid data in a CeedVector
529c774eddSJeremy L Thompson 
539c774eddSJeremy L Thompson   @param vec                   CeedVector to check validity
549c774eddSJeremy L Thompson   @param[out] has_valid_array  Variable to store validity
559c774eddSJeremy L Thompson 
569c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
579c774eddSJeremy L Thompson 
589c774eddSJeremy L Thompson   @ref Backend
599c774eddSJeremy L Thompson **/
609c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) {
619c774eddSJeremy L Thompson   int ierr;
629c774eddSJeremy L Thompson 
639c774eddSJeremy L Thompson   if (!vec->HasValidArray)
649c774eddSJeremy L Thompson     // LCOV_EXCL_START
659c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
669c774eddSJeremy L Thompson                      "Backend does not support HasValidArray");
679c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
689c774eddSJeremy L Thompson 
699c774eddSJeremy L Thompson   ierr = vec->HasValidArray(vec, has_valid_array); CeedChk(ierr);
709c774eddSJeremy L Thompson 
719c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
729c774eddSJeremy L Thompson }
739c774eddSJeremy L Thompson 
749c774eddSJeremy L Thompson /**
759c774eddSJeremy L Thompson   @brief Check for borrowed array of a specific CeedMemType in a CeedVector
769c774eddSJeremy L Thompson 
779c774eddSJeremy L Thompson   @param vec                              CeedVector to check
789c774eddSJeremy L Thompson   @param mem_type                         Memory type to check
799c774eddSJeremy L Thompson   @param[out] has_borrowed_array_of_type  Variable to store result
809c774eddSJeremy L Thompson 
819c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
829c774eddSJeremy L Thompson 
839c774eddSJeremy L Thompson   @ref Backend
849c774eddSJeremy L Thompson **/
859c774eddSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type,
869c774eddSJeremy L Thompson                                      bool *has_borrowed_array_of_type) {
879c774eddSJeremy L Thompson   int ierr;
889c774eddSJeremy L Thompson 
899c774eddSJeremy L Thompson   if (!vec->HasBorrowedArrayOfType)
909c774eddSJeremy L Thompson     // LCOV_EXCL_START
919c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
929c774eddSJeremy L Thompson                      "Backend does not support HasBorrowedArrayOfType");
939c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
949c774eddSJeremy L Thompson 
959c774eddSJeremy L Thompson   ierr = vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type);
969c774eddSJeremy L Thompson   CeedChk(ierr);
979c774eddSJeremy L Thompson 
989c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
999c774eddSJeremy L Thompson }
1009c774eddSJeremy L Thompson 
1019c774eddSJeremy 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)
3019c774eddSJeremy 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");
3059c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
3069c774eddSJeremy L Thompson 
3079c774eddSJeremy L Thompson   if (vec->num_readers > 0)
3089c774eddSJeremy L Thompson     // LCOV_EXCL_START
3099c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
3109c774eddSJeremy L Thompson                      "Cannot grant CeedVector array access, a "
3119c774eddSJeremy L Thompson                      "process has read access");
3129c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
3130436c2adSjeremylt 
3140436c2adSjeremylt   if (vec->SetValue) {
3150436c2adSjeremylt     ierr = vec->SetValue(vec, value); CeedChk(ierr);
3160436c2adSjeremylt   } else {
3170436c2adSjeremylt     CeedScalar *array;
3189c774eddSJeremy 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 /**
3589c774eddSJeremy L Thompson   @brief Take ownership of the CeedVector array set by @ref CeedVectorSetArray()
3599c774eddSJeremy L Thompson            with @ref CEED_USE_POINTER and remove the array from the CeedVector.
3609c774eddSJeremy L Thompson            The caller is responsible for managing and freeing the array.
3619c774eddSJeremy L Thompson            This function will error if @ref CeedVectorSetArray() was not previously
3629c774eddSJeremy 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
390*50c643e1SJed Brown   CeedScalar *temp_array = NULL;
391*50c643e1SJed Brown   if (vec->length > 0) {
3929c774eddSJeremy L Thompson     bool has_borrowed_array_of_type = true;
3939c774eddSJeremy L Thompson     ierr = CeedVectorHasBorrowedArrayOfType(vec, mem_type,
3949c774eddSJeremy L Thompson                                             &has_borrowed_array_of_type);
3959c774eddSJeremy L Thompson     CeedChk(ierr);
3969c774eddSJeremy L Thompson     if (!has_borrowed_array_of_type)
3979c774eddSJeremy L Thompson       // LCOV_EXCL_START
3989c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
3999c774eddSJeremy L Thompson                        "CeedVector has no borrowed %s array, "
4009c774eddSJeremy L Thompson                        "must set array with CeedVectorSetArray", CeedMemTypes[mem_type]);
4019c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
4029c774eddSJeremy L Thompson 
4039c774eddSJeremy L Thompson     bool has_valid_array = true;
4049c774eddSJeremy L Thompson     ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
4059c774eddSJeremy L Thompson     if (!has_valid_array)
4069c774eddSJeremy L Thompson       // LCOV_EXCL_START
4079c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
4089c774eddSJeremy L Thompson                        "CeedVector has no valid data to take, "
4099c774eddSJeremy L Thompson                        "must set data with CeedVectorSetValue or CeedVectorSetArray");
4109c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
4119c774eddSJeremy L Thompson 
412d1d35e2fSjeremylt     ierr = vec->TakeArray(vec, mem_type, &temp_array); CeedChk(ierr);
413*50c643e1SJed Brown   }
414d1d35e2fSjeremylt   if (array) (*array) = temp_array;
415e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4166a6c615bSJeremy L Thompson }
4176a6c615bSJeremy L Thompson 
4186a6c615bSJeremy L Thompson /**
419b3cf021fSjeremylt   @brief Get read/write access to a CeedVector via the specified memory type.
420b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArray().
4210436c2adSjeremylt 
4220436c2adSjeremylt   @param vec         CeedVector to access
423d1d35e2fSjeremylt   @param mem_type    Memory type on which to access the array. If the backend
424b3cf021fSjeremylt                        uses a different memory type, this will perform a copy.
425d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type
4260436c2adSjeremylt 
4270436c2adSjeremylt   @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide
4280436c2adSjeremylt     access to array pointers in the desired memory space. Pairing get/restore
4290436c2adSjeremylt     allows the Vector to track access, thus knowing if norms or other
4300436c2adSjeremylt     operations may need to be recomputed.
4310436c2adSjeremylt 
4320436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4330436c2adSjeremylt 
4347a982d89SJeremy L. Thompson   @ref User
4350436c2adSjeremylt **/
436d1d35e2fSjeremylt int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type,
437d1d35e2fSjeremylt                        CeedScalar **array) {
4380436c2adSjeremylt   int ierr;
4390436c2adSjeremylt 
4400436c2adSjeremylt   if (!vec->GetArray)
4410436c2adSjeremylt     // LCOV_EXCL_START
442e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
443e15f9bd0SJeremy L Thompson                      "Backend does not support GetArray");
4440436c2adSjeremylt   // LCOV_EXCL_STOP
4450436c2adSjeremylt 
4460436c2adSjeremylt   if (vec->state % 2 == 1)
447e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
448e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, the "
4490436c2adSjeremylt                      "access lock is already in use");
4500436c2adSjeremylt 
451d1d35e2fSjeremylt   if (vec->num_readers > 0)
452e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
453e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, a "
4540436c2adSjeremylt                      "process has read access");
4550436c2adSjeremylt 
4569c774eddSJeremy L Thompson   bool has_valid_array = true;
4579c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
4589c774eddSJeremy L Thompson   if (!has_valid_array)
4599c774eddSJeremy L Thompson     // LCOV_EXCL_START
4609c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
4619c774eddSJeremy L Thompson                      "CeedVector has no valid data to read, "
4629c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
4639c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
4649c774eddSJeremy L Thompson 
465d1d35e2fSjeremylt   ierr = vec->GetArray(vec, mem_type, array); CeedChk(ierr);
4660436c2adSjeremylt   vec->state += 1;
467e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4680436c2adSjeremylt }
4690436c2adSjeremylt 
4700436c2adSjeremylt /**
471b3cf021fSjeremylt   @brief Get read-only access to a CeedVector via the specified memory type.
472b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArrayRead().
4730436c2adSjeremylt 
4740436c2adSjeremylt   @param vec         CeedVector to access
475d1d35e2fSjeremylt   @param mem_type    Memory type on which to access the array.  If the backend
4760436c2adSjeremylt                        uses a different memory type, this will perform a copy
4770436c2adSjeremylt                        (possibly cached).
478d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type
4790436c2adSjeremylt 
4800436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4810436c2adSjeremylt 
4827a982d89SJeremy L. Thompson   @ref User
4830436c2adSjeremylt **/
484d1d35e2fSjeremylt int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type,
4850436c2adSjeremylt                            const CeedScalar **array) {
4860436c2adSjeremylt   int ierr;
4870436c2adSjeremylt 
4880436c2adSjeremylt   if (!vec->GetArrayRead)
4890436c2adSjeremylt     // LCOV_EXCL_START
490e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
491e15f9bd0SJeremy L Thompson                      "Backend does not support GetArrayRead");
4920436c2adSjeremylt   // LCOV_EXCL_STOP
4930436c2adSjeremylt 
4940436c2adSjeremylt   if (vec->state % 2 == 1)
495e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
496e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector read-only array "
4970436c2adSjeremylt                      "access, the access lock is already in use");
4980436c2adSjeremylt 
499*50c643e1SJed Brown   if (vec->length > 0) {
5009c774eddSJeremy L Thompson     bool has_valid_array = true;
5019c774eddSJeremy L Thompson     ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
5029c774eddSJeremy L Thompson     if (!has_valid_array)
5039c774eddSJeremy L Thompson       // LCOV_EXCL_START
5049c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
5059c774eddSJeremy L Thompson                        "CeedVector has no valid data to read, "
5069c774eddSJeremy L Thompson                        "must set data with CeedVectorSetValue or CeedVectorSetArray");
5079c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5089c774eddSJeremy L Thompson 
509d1d35e2fSjeremylt     ierr = vec->GetArrayRead(vec, mem_type, array); CeedChk(ierr);
510*50c643e1SJed Brown   } else {
511*50c643e1SJed Brown     *array = NULL;
512*50c643e1SJed Brown   }
513d1d35e2fSjeremylt   vec->num_readers++;
514e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5150436c2adSjeremylt }
5160436c2adSjeremylt 
5170436c2adSjeremylt /**
5189c774eddSJeremy L Thompson   @brief Get write access to a CeedVector via the specified memory type.
5199c774eddSJeremy L Thompson            Restore access with @ref CeedVectorRestoreArray(). All old
5209c774eddSJeremy L Thompson            values should be assumed to be invalid.
5219c774eddSJeremy L Thompson 
5229c774eddSJeremy L Thompson   @param vec         CeedVector to access
5239c774eddSJeremy L Thompson   @param mem_type    Memory type on which to access the array.
5249c774eddSJeremy L Thompson   @param[out] array  Array on memory type mem_type
5259c774eddSJeremy L Thompson 
5269c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5279c774eddSJeremy L Thompson 
5289c774eddSJeremy L Thompson   @ref User
5299c774eddSJeremy L Thompson **/
5309c774eddSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type,
5319c774eddSJeremy L Thompson                             CeedScalar **array) {
5329c774eddSJeremy L Thompson   int ierr;
5339c774eddSJeremy L Thompson 
5349c774eddSJeremy L Thompson   if (!vec->GetArrayWrite)
5359c774eddSJeremy L Thompson     // LCOV_EXCL_START
5369c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
5379c774eddSJeremy L Thompson                      "Backend does not support GetArrayWrite");
5389c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
5399c774eddSJeremy L Thompson 
5409c774eddSJeremy L Thompson   if (vec->state % 2 == 1)
5419c774eddSJeremy L Thompson     // LCOV_EXCL_START
5429c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
5439c774eddSJeremy L Thompson                      "Cannot grant CeedVector array access, the "
5449c774eddSJeremy L Thompson                      "access lock is already in use");
5459c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
5469c774eddSJeremy L Thompson 
5479c774eddSJeremy L Thompson   if (vec->num_readers > 0)
5489c774eddSJeremy L Thompson     // LCOV_EXCL_START
5499c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
5509c774eddSJeremy L Thompson                      "Cannot grant CeedVector array access, a "
5519c774eddSJeremy L Thompson                      "process has read access");
5529c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
5539c774eddSJeremy L Thompson 
5549c774eddSJeremy L Thompson   ierr = vec->GetArrayWrite(vec, mem_type, array); CeedChk(ierr);
5559c774eddSJeremy L Thompson   vec->state += 1;
5569c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5579c774eddSJeremy L Thompson }
5589c774eddSJeremy L Thompson 
5599c774eddSJeremy L Thompson /**
560b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArray()
5610436c2adSjeremylt 
5620436c2adSjeremylt   @param vec    CeedVector to restore
5630436c2adSjeremylt   @param array  Array of vector data
5640436c2adSjeremylt 
5650436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5660436c2adSjeremylt 
5677a982d89SJeremy L. Thompson   @ref User
5680436c2adSjeremylt **/
5690436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
5700436c2adSjeremylt   int ierr;
5710436c2adSjeremylt 
5720436c2adSjeremylt   if (vec->state % 2 != 1)
573e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
574e15f9bd0SJeremy L Thompson                      "Cannot restore CeedVector array access, "
5750436c2adSjeremylt                      "access was not granted");
576706efda3SJeremy L Thompson   if (vec->RestoreArray) {
5770436c2adSjeremylt     ierr = vec->RestoreArray(vec); CeedChk(ierr);
578706efda3SJeremy L Thompson   }
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 
5979c774eddSJeremy L Thompson   if (vec->num_readers == 0)
5989c774eddSJeremy L Thompson     // LCOV_EXCL_START
5999c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
6009c774eddSJeremy L Thompson                      "Cannot restore CeedVector array read access, "
6019c774eddSJeremy L Thompson                      "access was not granted");
6029c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
6039c774eddSJeremy L Thompson 
604706efda3SJeremy L Thompson   if (vec->RestoreArrayRead) {
6050436c2adSjeremylt     ierr = vec->RestoreArrayRead(vec); CeedChk(ierr);
606706efda3SJeremy L Thompson   }
6070436c2adSjeremylt   *array = NULL;
608d1d35e2fSjeremylt   vec->num_readers--;
609e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6100436c2adSjeremylt }
6110436c2adSjeremylt 
6120436c2adSjeremylt /**
613171f8ca9Sjeremylt   @brief Get the norm of a CeedVector.
614171f8ca9Sjeremylt 
615171f8ca9Sjeremylt   Note: This operation is local to the CeedVector. This function will likely
616171f8ca9Sjeremylt           not provide the desired results for the norm of the libCEED portion
617171f8ca9Sjeremylt           of a parallel vector or a CeedVector with duplicated or hanging nodes.
618547d9b97Sjeremylt 
619547d9b97Sjeremylt   @param vec        CeedVector to retrieve maximum value
620d1d35e2fSjeremylt   @param norm_type  Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
621547d9b97Sjeremylt   @param[out] norm  Variable to store norm value
622547d9b97Sjeremylt 
623547d9b97Sjeremylt   @return An error code: 0 - success, otherwise - failure
624547d9b97Sjeremylt 
6257a982d89SJeremy L. Thompson   @ref User
626547d9b97Sjeremylt **/
627d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
628547d9b97Sjeremylt   int ierr;
629547d9b97Sjeremylt 
6309c774eddSJeremy L Thompson   bool has_valid_array = true;
6319c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
6329c774eddSJeremy L Thompson   if (!has_valid_array)
6339c774eddSJeremy L Thompson     // LCOV_EXCL_START
6349c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
6359c774eddSJeremy L Thompson                      "CeedVector has no valid data to compute norm, "
6369c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
6379c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
6389c774eddSJeremy L Thompson 
639547d9b97Sjeremylt   // Backend impl for GPU, if added
640547d9b97Sjeremylt   if (vec->Norm) {
641d1d35e2fSjeremylt     ierr = vec->Norm(vec, norm_type, norm); CeedChk(ierr);
642e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
643547d9b97Sjeremylt   }
644547d9b97Sjeremylt 
645547d9b97Sjeremylt   const CeedScalar *array;
646547d9b97Sjeremylt   ierr = CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
647547d9b97Sjeremylt 
648547d9b97Sjeremylt   *norm = 0.;
649d1d35e2fSjeremylt   switch (norm_type) {
650547d9b97Sjeremylt   case CEED_NORM_1:
651547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
652547d9b97Sjeremylt       *norm += fabs(array[i]);
653547d9b97Sjeremylt     }
654547d9b97Sjeremylt     break;
655547d9b97Sjeremylt   case CEED_NORM_2:
656547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
657547d9b97Sjeremylt       *norm += fabs(array[i])*fabs(array[i]);
658547d9b97Sjeremylt     }
659547d9b97Sjeremylt     break;
660547d9b97Sjeremylt   case CEED_NORM_MAX:
661547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
662d1d35e2fSjeremylt       const CeedScalar abs_v_i = fabs(array[i]);
663d1d35e2fSjeremylt       *norm = *norm > abs_v_i ? *norm : abs_v_i;
664547d9b97Sjeremylt     }
665547d9b97Sjeremylt   }
666d1d35e2fSjeremylt   if (norm_type == CEED_NORM_2)
667547d9b97Sjeremylt     *norm = sqrt(*norm);
668547d9b97Sjeremylt 
669547d9b97Sjeremylt   ierr = CeedVectorRestoreArrayRead(vec, &array); CeedChk(ierr);
670e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
671547d9b97Sjeremylt }
672547d9b97Sjeremylt 
673547d9b97Sjeremylt /**
674e0dd3b27Sjeremylt   @brief Compute x = alpha x
675e0dd3b27Sjeremylt 
67696b902e2Sjeremylt   @param[in,out] x  vector for scaling
67796b902e2Sjeremylt   @param[in] alpha  scaling factor
678e0dd3b27Sjeremylt 
679e0dd3b27Sjeremylt   @return An error code: 0 - success, otherwise - failure
680e0dd3b27Sjeremylt 
681e0dd3b27Sjeremylt   @ref User
682e0dd3b27Sjeremylt **/
683e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) {
684e0dd3b27Sjeremylt   int ierr;
685e0dd3b27Sjeremylt   CeedScalar *x_array;
686e0dd3b27Sjeremylt   CeedInt n_x;
687e0dd3b27Sjeremylt 
6889c774eddSJeremy L Thompson   bool has_valid_array = true;
6899c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(x, &has_valid_array); CeedChk(ierr);
6909c774eddSJeremy L Thompson   if (!has_valid_array)
6919c774eddSJeremy L Thompson     // LCOV_EXCL_START
6929c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
6939c774eddSJeremy L Thompson                      "CeedVector has no valid data to scale, "
6949c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
6959c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
6969c774eddSJeremy L Thompson 
697e0dd3b27Sjeremylt   ierr = CeedVectorGetLength(x, &n_x); CeedChk(ierr);
698e0dd3b27Sjeremylt 
699e0dd3b27Sjeremylt   // Backend implementation
700e0dd3b27Sjeremylt   if (x->Scale)
701e0dd3b27Sjeremylt     return x->Scale(x, alpha);
702e0dd3b27Sjeremylt 
703e0dd3b27Sjeremylt   // Default implementation
7049c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array); CeedChk(ierr);
705e0dd3b27Sjeremylt   for (CeedInt i=0; i<n_x; i++)
706e0dd3b27Sjeremylt     x_array[i] *= alpha;
707e0dd3b27Sjeremylt   ierr = CeedVectorRestoreArray(x, &x_array); CeedChk(ierr);
708e0dd3b27Sjeremylt 
709e0dd3b27Sjeremylt   return CEED_ERROR_SUCCESS;
710e0dd3b27Sjeremylt }
711e0dd3b27Sjeremylt 
712e0dd3b27Sjeremylt /**
7130f7fd0f8Sjeremylt   @brief Compute y = alpha x + y
7140f7fd0f8Sjeremylt 
71596b902e2Sjeremylt   @param[in,out] y  target vector for sum
71696b902e2Sjeremylt   @param[in] alpha  scaling factor
71796b902e2Sjeremylt   @param[in] x      second vector, must be different than y
7180f7fd0f8Sjeremylt 
7190f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
7200f7fd0f8Sjeremylt 
7210f7fd0f8Sjeremylt   @ref User
7220f7fd0f8Sjeremylt **/
7230f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
7240f7fd0f8Sjeremylt   int ierr;
7250f7fd0f8Sjeremylt   CeedScalar *y_array;
7260f7fd0f8Sjeremylt   CeedScalar const *x_array;
7270f7fd0f8Sjeremylt   CeedInt n_x, n_y;
7280f7fd0f8Sjeremylt 
7290f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(y, &n_y); CeedChk(ierr);
7300f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(x, &n_x); CeedChk(ierr);
7310f7fd0f8Sjeremylt   if (n_x != n_y)
7320f7fd0f8Sjeremylt     // LCOV_EXCL_START
7330f7fd0f8Sjeremylt     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED,
7340f7fd0f8Sjeremylt                      "Cannot add vector of different lengths");
7350f7fd0f8Sjeremylt   // LCOV_EXCL_STOP
7360f7fd0f8Sjeremylt   if (x == y)
7370f7fd0f8Sjeremylt     // LCOV_EXCL_START
7380f7fd0f8Sjeremylt     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED,
7390f7fd0f8Sjeremylt                      "Cannot use same vector for x and y in CeedVectorAXPY");
7400f7fd0f8Sjeremylt   // LCOV_EXCL_STOP
7410f7fd0f8Sjeremylt 
7429c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
7439c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(x, &has_valid_array_x); CeedChk(ierr);
7449c774eddSJeremy L Thompson   if (!has_valid_array_x)
7459c774eddSJeremy L Thompson     // LCOV_EXCL_START
7469c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
7479c774eddSJeremy L Thompson                      "CeedVector x has no valid data, "
7489c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
7499c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
7509c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(y, &has_valid_array_y); CeedChk(ierr);
7519c774eddSJeremy L Thompson   if (!has_valid_array_y)
7529c774eddSJeremy L Thompson     // LCOV_EXCL_START
7539c774eddSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND,
7549c774eddSJeremy L Thompson                      "CeedVector y has no valid data, "
7559c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
7569c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
7579c774eddSJeremy L Thompson 
7582d04630dSjeremylt   Ceed ceed_parent_x, ceed_parent_y;
7592d04630dSjeremylt   ierr = CeedGetParent(x->ceed, &ceed_parent_x); CeedChk(ierr);
7602d04630dSjeremylt   ierr = CeedGetParent(y->ceed, &ceed_parent_y); CeedChk(ierr);
7612d04630dSjeremylt   if (ceed_parent_x != ceed_parent_y)
7622d04630dSjeremylt     // LCOV_EXCL_START
7632d04630dSjeremylt     return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE,
7642d04630dSjeremylt                      "Vectors x and y must be created by the same Ceed context");
7652d04630dSjeremylt   // LCOV_EXCL_STOP
7662d04630dSjeremylt 
7670f7fd0f8Sjeremylt   // Backend implementation
768eaf62fffSJeremy L Thompson   if (y->AXPY) {
769eaf62fffSJeremy L Thompson     ierr = y->AXPY(y, alpha, x); CeedChk(ierr);
770eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
771eaf62fffSJeremy L Thompson   }
7720f7fd0f8Sjeremylt 
7730f7fd0f8Sjeremylt   // Default implementation
7749c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array); CeedChk(ierr);
7750f7fd0f8Sjeremylt   ierr = CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array); CeedChk(ierr);
7760f7fd0f8Sjeremylt 
7770f7fd0f8Sjeremylt   for (CeedInt i=0; i<n_y; i++)
7780f7fd0f8Sjeremylt     y_array[i] += alpha * x_array[i];
7790f7fd0f8Sjeremylt 
7800f7fd0f8Sjeremylt   ierr = CeedVectorRestoreArray(y, &y_array); CeedChk(ierr);
7810f7fd0f8Sjeremylt   ierr = CeedVectorRestoreArrayRead(x, &x_array); CeedChk(ierr);
7820f7fd0f8Sjeremylt 
7830f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
7840f7fd0f8Sjeremylt }
7850f7fd0f8Sjeremylt 
7860f7fd0f8Sjeremylt /**
7870c1bc3c2Sjeremylt   @brief Compute the pointwise multiplication w = x .* y. Any
7880f7fd0f8Sjeremylt            subset of x, y, and w may be the same vector.
7890f7fd0f8Sjeremylt 
79096b902e2Sjeremylt   @param[out] w  target vector for the product
79196b902e2Sjeremylt   @param[in] x   first vector for product
79296b902e2Sjeremylt   @param[in] y   second vector for the product
7930f7fd0f8Sjeremylt 
7940f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
7950f7fd0f8Sjeremylt 
7960f7fd0f8Sjeremylt   @ ref User
7970f7fd0f8Sjeremylt **/
7980f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
7990f7fd0f8Sjeremylt   int ierr;
8000f7fd0f8Sjeremylt   CeedScalar *w_array;
8010f7fd0f8Sjeremylt   CeedScalar const *x_array, *y_array;
8022d04630dSjeremylt   CeedInt n_w, n_x, n_y;
8030f7fd0f8Sjeremylt 
8040f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(w, &n_w); CeedChk(ierr);
8050f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(x, &n_x); CeedChk(ierr);
8060f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(y, &n_y); CeedChk(ierr);
8070f7fd0f8Sjeremylt   if (n_w != n_x || n_w != n_y)
8080f7fd0f8Sjeremylt     // LCOV_EXCL_START
8090f7fd0f8Sjeremylt     return CeedError(w->ceed, CEED_ERROR_UNSUPPORTED,
8100f7fd0f8Sjeremylt                      "Cannot multiply vectors of different lengths");
8110f7fd0f8Sjeremylt   // LCOV_EXCL_STOP
8120f7fd0f8Sjeremylt 
8132d04630dSjeremylt   Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y;
8142d04630dSjeremylt   ierr = CeedGetParent(w->ceed, &ceed_parent_w); CeedChk(ierr);
8152d04630dSjeremylt   ierr = CeedGetParent(x->ceed, &ceed_parent_x); CeedChk(ierr);
8162d04630dSjeremylt   ierr = CeedGetParent(y->ceed, &ceed_parent_y); CeedChk(ierr);
8179c774eddSJeremy L Thompson   if ((ceed_parent_w != ceed_parent_x) ||
8182d04630dSjeremylt       (ceed_parent_w != ceed_parent_y))
8192d04630dSjeremylt     // LCOV_EXCL_START
8202d04630dSjeremylt     return CeedError(w->ceed, CEED_ERROR_INCOMPATIBLE,
8212d04630dSjeremylt                      "Vectors w, x, and y must be created by the same Ceed context");
8222d04630dSjeremylt   // LCOV_EXCL_STOP
8232d04630dSjeremylt 
8249c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
8259c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(x, &has_valid_array_x); CeedChk(ierr);
8269c774eddSJeremy L Thompson   if (!has_valid_array_x)
8279c774eddSJeremy L Thompson     // LCOV_EXCL_START
8289c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
8299c774eddSJeremy L Thompson                      "CeedVector x has no valid data, "
8309c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
8319c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
8329c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(y, &has_valid_array_y); CeedChk(ierr);
8339c774eddSJeremy L Thompson   if (!has_valid_array_y)
8349c774eddSJeremy L Thompson     // LCOV_EXCL_START
8359c774eddSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND,
8369c774eddSJeremy L Thompson                      "CeedVector y has no valid data, "
8379c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
8389c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
8399c774eddSJeremy L Thompson 
8400f7fd0f8Sjeremylt   // Backend implementation
841eaf62fffSJeremy L Thompson   if (w->PointwiseMult) {
842eaf62fffSJeremy L Thompson     ierr = w->PointwiseMult(w, x, y); CeedChk(ierr);
843eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
844eaf62fffSJeremy L Thompson   }
8450f7fd0f8Sjeremylt 
8460f7fd0f8Sjeremylt   // Default implementation
8479c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array); CeedChk(ierr);
8480f7fd0f8Sjeremylt   if (x != w) {
8490f7fd0f8Sjeremylt     ierr = CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array); CeedChk(ierr);
8500f7fd0f8Sjeremylt   } else {
8510f7fd0f8Sjeremylt     x_array = w_array;
8520f7fd0f8Sjeremylt   }
8530f7fd0f8Sjeremylt   if (y != w && y != x) {
8540f7fd0f8Sjeremylt     ierr = CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array); CeedChk(ierr);
8550f7fd0f8Sjeremylt   } else if (y != x) {
8560f7fd0f8Sjeremylt     y_array = w_array;
8570f7fd0f8Sjeremylt   } else {
8580f7fd0f8Sjeremylt     y_array = x_array;
8590f7fd0f8Sjeremylt   }
8600f7fd0f8Sjeremylt 
8610f7fd0f8Sjeremylt   for (CeedInt i=0; i<n_w; i++)
8620f7fd0f8Sjeremylt     w_array[i] = x_array[i] * y_array[i];
8630f7fd0f8Sjeremylt 
8640f7fd0f8Sjeremylt   if (y != w && y != x) {
8650f7fd0f8Sjeremylt     ierr = CeedVectorRestoreArrayRead(y, &y_array); CeedChk(ierr);
8660f7fd0f8Sjeremylt   }
8670f7fd0f8Sjeremylt   if (x != w) {
8680f7fd0f8Sjeremylt     ierr = CeedVectorRestoreArrayRead(x, &x_array); CeedChk(ierr);
8690f7fd0f8Sjeremylt   }
8700f7fd0f8Sjeremylt   ierr = CeedVectorRestoreArray(w, &w_array); CeedChk(ierr);
8710f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
8720f7fd0f8Sjeremylt }
8730f7fd0f8Sjeremylt 
8740f7fd0f8Sjeremylt /**
875d99fa3c5SJeremy L Thompson   @brief Take the reciprocal of a CeedVector.
876d99fa3c5SJeremy L Thompson 
877d99fa3c5SJeremy L Thompson   @param vec  CeedVector to take reciprocal
878d99fa3c5SJeremy L Thompson 
879d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
880d99fa3c5SJeremy L Thompson 
881d99fa3c5SJeremy L Thompson   @ref User
882d99fa3c5SJeremy L Thompson **/
883d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) {
884d99fa3c5SJeremy L Thompson   int ierr;
885d99fa3c5SJeremy L Thompson 
8869c774eddSJeremy L Thompson   bool has_valid_array = true;
8879c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
8889c774eddSJeremy L Thompson   if (!has_valid_array)
8899c774eddSJeremy L Thompson     // LCOV_EXCL_START
8909c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
8919c774eddSJeremy L Thompson                      "CeedVector has no valid data to compute reciprocal, "
8929c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
8939c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
8949c774eddSJeremy L Thompson 
895d99fa3c5SJeremy L Thompson   // Check if vector data set
896d99fa3c5SJeremy L Thompson   if (!vec->state)
897d99fa3c5SJeremy L Thompson     // LCOV_EXCL_START
898e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE,
899d99fa3c5SJeremy L Thompson                      "CeedVector must have data set to take reciprocal");
900d99fa3c5SJeremy L Thompson   // LCOV_EXCL_STOP
901d99fa3c5SJeremy L Thompson 
902d99fa3c5SJeremy L Thompson   // Backend impl for GPU, if added
903d99fa3c5SJeremy L Thompson   if (vec->Reciprocal) {
904d99fa3c5SJeremy L Thompson     ierr = vec->Reciprocal(vec); CeedChk(ierr);
905e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
906d99fa3c5SJeremy L Thompson   }
907d99fa3c5SJeremy L Thompson 
908d99fa3c5SJeremy L Thompson   CeedInt len;
909d99fa3c5SJeremy L Thompson   ierr = CeedVectorGetLength(vec, &len); CeedChk(ierr);
910d99fa3c5SJeremy L Thompson   CeedScalar *array;
9119c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
912d99fa3c5SJeremy L Thompson   for (CeedInt i=0; i<len; i++)
913d99fa3c5SJeremy L Thompson     if (fabs(array[i]) > CEED_EPSILON)
914d99fa3c5SJeremy L Thompson       array[i] = 1./array[i];
915d99fa3c5SJeremy L Thompson 
916e15f9bd0SJeremy L Thompson   ierr = CeedVectorRestoreArray(vec, &array); CeedChk(ierr);
917e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
918d99fa3c5SJeremy L Thompson }
919d99fa3c5SJeremy L Thompson 
920d99fa3c5SJeremy L Thompson /**
9217a982d89SJeremy L. Thompson   @brief View a CeedVector
9220436c2adSjeremylt 
9230a0da059Sjeremylt   @param[in] vec     CeedVector to view
924d1d35e2fSjeremylt   @param[in] fp_fmt  Printing format
9250a0da059Sjeremylt   @param[in] stream  Filestream to write to
9260a0da059Sjeremylt 
9270436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9280436c2adSjeremylt 
9297a982d89SJeremy L. Thompson   @ref User
9300436c2adSjeremylt **/
931d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
9327a982d89SJeremy L. Thompson   const CeedScalar *x;
9337a982d89SJeremy L. Thompson 
9347a982d89SJeremy L. Thompson   int ierr = CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x); CeedChk(ierr);
9357a982d89SJeremy L. Thompson 
9367a982d89SJeremy L. Thompson   char fmt[1024];
9377a982d89SJeremy L. Thompson   fprintf(stream, "CeedVector length %ld\n", (long)vec->length);
938d1d35e2fSjeremylt   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
9397a982d89SJeremy L. Thompson   for (CeedInt i=0; i<vec->length; i++)
9407a982d89SJeremy L. Thompson     fprintf(stream, fmt, x[i]);
9417a982d89SJeremy L. Thompson 
9427a982d89SJeremy L. Thompson   ierr = CeedVectorRestoreArrayRead(vec, &x); CeedChk(ierr);
943e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9440436c2adSjeremylt }
9450436c2adSjeremylt 
9460436c2adSjeremylt /**
947b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedVector
948b7c9bbdaSJeremy L Thompson 
949b7c9bbdaSJeremy L Thompson   @param vec        CeedVector to retrieve state
950b7c9bbdaSJeremy L Thompson   @param[out] ceed  Variable to store ceed
951b7c9bbdaSJeremy L Thompson 
952b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
953b7c9bbdaSJeremy L Thompson 
954b7c9bbdaSJeremy L Thompson   @ref Advanced
955b7c9bbdaSJeremy L Thompson **/
956b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
957b7c9bbdaSJeremy L Thompson   *ceed = vec->ceed;
958b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
959b7c9bbdaSJeremy L Thompson }
960b7c9bbdaSJeremy L Thompson 
961b7c9bbdaSJeremy L Thompson /**
9627a982d89SJeremy L. Thompson   @brief Get the length of a CeedVector
9630436c2adSjeremylt 
9647a982d89SJeremy L. Thompson   @param vec          CeedVector to retrieve length
9657a982d89SJeremy L. Thompson   @param[out] length  Variable to store length
9660436c2adSjeremylt 
9670436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9680436c2adSjeremylt 
9697a982d89SJeremy L. Thompson   @ref User
9700436c2adSjeremylt **/
9717a982d89SJeremy L. Thompson int CeedVectorGetLength(CeedVector vec, CeedInt *length) {
9727a982d89SJeremy L. Thompson   *length = vec->length;
973e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9740436c2adSjeremylt }
9750436c2adSjeremylt 
9760436c2adSjeremylt /**
9770436c2adSjeremylt   @brief Destroy a CeedVector
9780436c2adSjeremylt 
9790436c2adSjeremylt   @param vec  CeedVector to destroy
9800436c2adSjeremylt 
9810436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9820436c2adSjeremylt 
9837a982d89SJeremy L. Thompson   @ref User
9840436c2adSjeremylt **/
9850436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) {
9860436c2adSjeremylt   int ierr;
9870436c2adSjeremylt 
988d1d35e2fSjeremylt   if (!*vec || --(*vec)->ref_count > 0) return CEED_ERROR_SUCCESS;
9890436c2adSjeremylt 
990187168c7SJeremy L Thompson   if (((*vec)->state % 2) == 1)
991e15f9bd0SJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS,
992187168c7SJeremy L Thompson                      "Cannot destroy CeedVector, the writable access "
9930436c2adSjeremylt                      "lock is in use");
9940436c2adSjeremylt 
995d1d35e2fSjeremylt   if ((*vec)->num_readers > 0)
996e92b641fSJeremy L Thompson     // LCOV_EXCL_START
997e15f9bd0SJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS,
998e15f9bd0SJeremy L Thompson                      "Cannot destroy CeedVector, a process has "
999187168c7SJeremy L Thompson                      "read access");
1000e92b641fSJeremy L Thompson   // LCOV_EXCL_STOP
1001187168c7SJeremy L Thompson 
10020436c2adSjeremylt   if ((*vec)->Destroy) {
10030436c2adSjeremylt     ierr = (*vec)->Destroy(*vec); CeedChk(ierr);
10040436c2adSjeremylt   }
10050436c2adSjeremylt 
10060436c2adSjeremylt   ierr = CeedDestroy(&(*vec)->ceed); CeedChk(ierr);
10070436c2adSjeremylt   ierr = CeedFree(vec); CeedChk(ierr);
1008e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10090436c2adSjeremylt }
10100436c2adSjeremylt 
10110436c2adSjeremylt /// @}
1012