xref: /libCEED/interface/ceed-vector.c (revision 733804229a5710f81e8d194be8505d805020e1dc)
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>
20*73380422SJeremy L Thompson #include <assert.h>
21547d9b97Sjeremylt #include <math.h>
223d576824SJeremy L Thompson #include <stdint.h>
233d576824SJeremy L Thompson #include <stdio.h>
240436c2adSjeremylt 
257a982d89SJeremy L. Thompson /// @file
267a982d89SJeremy L. Thompson /// Implementation of public CeedVector interfaces
277a982d89SJeremy L. Thompson 
280436c2adSjeremylt /// @cond DOXYGEN_SKIP
290436c2adSjeremylt static struct CeedVector_private ceed_vector_active;
300436c2adSjeremylt static struct CeedVector_private ceed_vector_none;
310436c2adSjeremylt /// @endcond
320436c2adSjeremylt 
337a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser
347a982d89SJeremy L. Thompson /// @{
357a982d89SJeremy L. Thompson 
367a982d89SJeremy L. Thompson /// Indicate that vector will be provided as an explicit argument to
377a982d89SJeremy L. Thompson ///   CeedOperatorApply().
387a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_ACTIVE = &ceed_vector_active;
397a982d89SJeremy L. Thompson 
4096b902e2Sjeremylt /// Indicate that no vector is applicable (i.e., for @ref CEED_EVAL_WEIGHT).
417a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_NONE = &ceed_vector_none;
427a982d89SJeremy L. Thompson 
437a982d89SJeremy L. Thompson /// @}
447a982d89SJeremy L. Thompson 
457a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
467a982d89SJeremy L. Thompson /// CeedVector Backend API
477a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
487a982d89SJeremy L. Thompson /// @addtogroup CeedVectorBackend
497a982d89SJeremy L. Thompson /// @{
507a982d89SJeremy L. Thompson 
517a982d89SJeremy L. Thompson /**
529c774eddSJeremy L Thompson   @brief Check for valid data in a CeedVector
539c774eddSJeremy L Thompson 
549c774eddSJeremy L Thompson   @param vec                   CeedVector to check validity
559c774eddSJeremy L Thompson   @param[out] has_valid_array  Variable to store validity
569c774eddSJeremy L Thompson 
579c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
589c774eddSJeremy L Thompson 
599c774eddSJeremy L Thompson   @ref Backend
609c774eddSJeremy L Thompson **/
619c774eddSJeremy L Thompson int CeedVectorHasValidArray(CeedVector vec, bool *has_valid_array) {
629c774eddSJeremy L Thompson   int ierr;
639c774eddSJeremy L Thompson 
649c774eddSJeremy L Thompson   if (!vec->HasValidArray)
659c774eddSJeremy L Thompson     // LCOV_EXCL_START
669c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
679c774eddSJeremy L Thompson                      "Backend does not support HasValidArray");
689c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
699c774eddSJeremy L Thompson 
709c774eddSJeremy L Thompson   ierr = vec->HasValidArray(vec, has_valid_array); CeedChk(ierr);
719c774eddSJeremy L Thompson 
729c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
739c774eddSJeremy L Thompson }
749c774eddSJeremy L Thompson 
759c774eddSJeremy L Thompson /**
769c774eddSJeremy L Thompson   @brief Check for borrowed array of a specific CeedMemType in a CeedVector
779c774eddSJeremy L Thompson 
789c774eddSJeremy L Thompson   @param vec                              CeedVector to check
799c774eddSJeremy L Thompson   @param mem_type                         Memory type to check
809c774eddSJeremy L Thompson   @param[out] has_borrowed_array_of_type  Variable to store result
819c774eddSJeremy L Thompson 
829c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
839c774eddSJeremy L Thompson 
849c774eddSJeremy L Thompson   @ref Backend
859c774eddSJeremy L Thompson **/
869c774eddSJeremy L Thompson int CeedVectorHasBorrowedArrayOfType(CeedVector vec, CeedMemType mem_type,
879c774eddSJeremy L Thompson                                      bool *has_borrowed_array_of_type) {
889c774eddSJeremy L Thompson   int ierr;
899c774eddSJeremy L Thompson 
909c774eddSJeremy L Thompson   if (!vec->HasBorrowedArrayOfType)
919c774eddSJeremy L Thompson     // LCOV_EXCL_START
929c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
939c774eddSJeremy L Thompson                      "Backend does not support HasBorrowedArrayOfType");
949c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
959c774eddSJeremy L Thompson 
969c774eddSJeremy L Thompson   ierr = vec->HasBorrowedArrayOfType(vec, mem_type, has_borrowed_array_of_type);
979c774eddSJeremy L Thompson   CeedChk(ierr);
989c774eddSJeremy L Thompson 
999c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1009c774eddSJeremy L Thompson }
1019c774eddSJeremy L Thompson 
1029c774eddSJeremy L Thompson /**
1037a982d89SJeremy L. Thompson   @brief Get the state of a CeedVector
1047a982d89SJeremy L. Thompson 
1057a982d89SJeremy L. Thompson   @param vec         CeedVector to retrieve state
1067a982d89SJeremy L. Thompson   @param[out] state  Variable to store state
1077a982d89SJeremy L. Thompson 
1087a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1097a982d89SJeremy L. Thompson 
1107a982d89SJeremy L. Thompson   @ref Backend
1117a982d89SJeremy L. Thompson **/
1127a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) {
1137a982d89SJeremy L. Thompson   *state = vec->state;
114e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1157a982d89SJeremy L. Thompson }
1167a982d89SJeremy L. Thompson 
1177a982d89SJeremy L. Thompson /**
11827312b0fSJed Brown   @brief Add a reference to a CeedVector
1197a982d89SJeremy L. Thompson 
1205f67fadeSJeremy L Thompson   @param[out] vec  CeedVector to increment reference counter
1217a982d89SJeremy L. Thompson 
1227a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1237a982d89SJeremy L. Thompson 
1247a982d89SJeremy L. Thompson   @ref Backend
1257a982d89SJeremy L. Thompson **/
1267a982d89SJeremy L. Thompson int CeedVectorAddReference(CeedVector vec) {
127d1d35e2fSjeremylt   vec->ref_count++;
128e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1297a982d89SJeremy L. Thompson }
1307a982d89SJeremy L. Thompson 
1317a982d89SJeremy L. Thompson /**
1327a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedVector
1337a982d89SJeremy L. Thompson 
1347a982d89SJeremy L. Thompson   @param vec        CeedVector to retrieve state
1357a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
1367a982d89SJeremy L. Thompson 
1377a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1387a982d89SJeremy L. Thompson 
1397a982d89SJeremy L. Thompson   @ref Backend
1407a982d89SJeremy L. Thompson **/
141777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) {
142777ff853SJeremy L Thompson   *(void **)data = vec->data;
143e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1447a982d89SJeremy L. Thompson }
1457a982d89SJeremy L. Thompson 
1467a982d89SJeremy L. Thompson /**
1477a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedVector
1487a982d89SJeremy L. Thompson 
1497a982d89SJeremy L. Thompson   @param[out] vec  CeedVector to retrieve state
1507a982d89SJeremy L. Thompson   @param data      Data to set
1517a982d89SJeremy L. Thompson 
1527a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1537a982d89SJeremy L. Thompson 
1547a982d89SJeremy L. Thompson   @ref Backend
1557a982d89SJeremy L. Thompson **/
156777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) {
157777ff853SJeremy L Thompson   vec->data = data;
158e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1597a982d89SJeremy L. Thompson }
1607a982d89SJeremy L. Thompson 
16134359f16Sjeremylt /**
16234359f16Sjeremylt   @brief Increment the reference counter for a CeedVector
16334359f16Sjeremylt 
16434359f16Sjeremylt   @param vec  CeedVector to increment the reference counter
16534359f16Sjeremylt 
16634359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
16734359f16Sjeremylt 
16834359f16Sjeremylt   @ref Backend
16934359f16Sjeremylt **/
1709560d06aSjeremylt int CeedVectorReference(CeedVector vec) {
17134359f16Sjeremylt   vec->ref_count++;
17234359f16Sjeremylt   return CEED_ERROR_SUCCESS;
17334359f16Sjeremylt }
17434359f16Sjeremylt 
1757a982d89SJeremy L. Thompson /// @}
1767a982d89SJeremy L. Thompson 
1777a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1787a982d89SJeremy L. Thompson /// CeedVector Public API
1797a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1807a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser
1810436c2adSjeremylt /// @{
1820436c2adSjeremylt 
1830436c2adSjeremylt /**
1840436c2adSjeremylt   @brief Create a CeedVector of the specified length (does not allocate memory)
1850436c2adSjeremylt 
1860436c2adSjeremylt   @param ceed      Ceed object where the CeedVector will be created
1870436c2adSjeremylt   @param length    Length of vector
1880436c2adSjeremylt   @param[out] vec  Address of the variable where the newly created
1890436c2adSjeremylt                      CeedVector will be stored
1900436c2adSjeremylt 
1910436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
1920436c2adSjeremylt 
1937a982d89SJeremy L. Thompson   @ref User
1940436c2adSjeremylt **/
1950436c2adSjeremylt int CeedVectorCreate(Ceed ceed, CeedInt length, CeedVector *vec) {
1960436c2adSjeremylt   int ierr;
1970436c2adSjeremylt 
1980436c2adSjeremylt   if (!ceed->VectorCreate) {
1990436c2adSjeremylt     Ceed delegate;
2000436c2adSjeremylt     ierr = CeedGetObjectDelegate(ceed, &delegate, "Vector"); CeedChk(ierr);
2010436c2adSjeremylt 
2020436c2adSjeremylt     if (!delegate)
2030436c2adSjeremylt       // LCOV_EXCL_START
204e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
205e15f9bd0SJeremy L Thompson                        "Backend does not support VectorCreate");
2060436c2adSjeremylt     // LCOV_EXCL_STOP
2070436c2adSjeremylt 
2080436c2adSjeremylt     ierr = CeedVectorCreate(delegate, length, vec); CeedChk(ierr);
209e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
2100436c2adSjeremylt   }
2110436c2adSjeremylt 
2120436c2adSjeremylt   ierr = CeedCalloc(1, vec); CeedChk(ierr);
2130436c2adSjeremylt   (*vec)->ceed = ceed;
2149560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
215d1d35e2fSjeremylt   (*vec)->ref_count = 1;
2160436c2adSjeremylt   (*vec)->length = length;
2170436c2adSjeremylt   (*vec)->state = 0;
2180436c2adSjeremylt   ierr = ceed->VectorCreate(length, *vec); CeedChk(ierr);
219e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2200436c2adSjeremylt }
2210436c2adSjeremylt 
2220436c2adSjeremylt /**
2239560d06aSjeremylt   @brief Copy the pointer to a CeedVector. Both pointers should
2249560d06aSjeremylt            be destroyed with `CeedVectorDestroy()`;
2259560d06aSjeremylt            Note: If `*vec_copy` is non-NULL, then it is assumed that
2269560d06aSjeremylt            `*vec_copy` is a pointer to a CeedVector. This
2279560d06aSjeremylt            CeedVector will be destroyed if `*vec_copy` is the only
2289560d06aSjeremylt            reference to this CeedVector.
2299560d06aSjeremylt 
2309560d06aSjeremylt   @param vec            CeedVector to copy reference to
2319560d06aSjeremylt   @param[out] vec_copy  Variable to store copied reference
2329560d06aSjeremylt 
2339560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
2349560d06aSjeremylt 
2359560d06aSjeremylt   @ref User
2369560d06aSjeremylt **/
2379560d06aSjeremylt int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) {
2389560d06aSjeremylt   int ierr;
2399560d06aSjeremylt 
2409560d06aSjeremylt   ierr = CeedVectorReference(vec); CeedChk(ierr);
2419560d06aSjeremylt   ierr = CeedVectorDestroy(vec_copy); CeedChk(ierr);
2429560d06aSjeremylt   *vec_copy = vec;
2439560d06aSjeremylt   return CEED_ERROR_SUCCESS;
2449560d06aSjeremylt }
2459560d06aSjeremylt 
2469560d06aSjeremylt /**
2470436c2adSjeremylt   @brief Set the array used by a CeedVector, freeing any previously allocated
248b3cf021fSjeremylt            array if applicable. The backend may copy values to a different
249b3cf021fSjeremylt            memtype, such as during @ref CeedOperatorApply().
2506a6c615bSJeremy L Thompson            See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray().
2510436c2adSjeremylt 
2520436c2adSjeremylt   @param vec        CeedVector
253d1d35e2fSjeremylt   @param mem_type   Memory type of the array being passed
254d1d35e2fSjeremylt   @param copy_mode  Copy mode for the array
2554cc79fe7SJed Brown   @param array      Array to be used, or NULL with @ref CEED_COPY_VALUES to have the
2560436c2adSjeremylt                       library allocate
2570436c2adSjeremylt 
2580436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2590436c2adSjeremylt 
2607a982d89SJeremy L. Thompson   @ref User
2610436c2adSjeremylt **/
262d1d35e2fSjeremylt int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type,
263d1d35e2fSjeremylt                        CeedCopyMode copy_mode,
2640436c2adSjeremylt                        CeedScalar *array) {
2650436c2adSjeremylt   int ierr;
2660436c2adSjeremylt 
2670436c2adSjeremylt   if (!vec->SetArray)
2680436c2adSjeremylt     // LCOV_EXCL_START
269e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
270e15f9bd0SJeremy L Thompson                      "Backend does not support VectorSetArray");
2710436c2adSjeremylt   // LCOV_EXCL_STOP
2720436c2adSjeremylt 
2730436c2adSjeremylt   if (vec->state % 2 == 1)
274e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
275e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, the "
2760436c2adSjeremylt                      "access lock is already in use");
2770436c2adSjeremylt 
278d1d35e2fSjeremylt   if (vec->num_readers > 0)
279e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
280e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, a "
2810436c2adSjeremylt                      "process has read access");
2820436c2adSjeremylt 
283d1d35e2fSjeremylt   ierr = vec->SetArray(vec, mem_type, copy_mode, array); CeedChk(ierr);
2840436c2adSjeremylt   vec->state += 2;
285e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2860436c2adSjeremylt }
2870436c2adSjeremylt 
2880436c2adSjeremylt /**
2890436c2adSjeremylt   @brief Set the CeedVector to a constant value
2900436c2adSjeremylt 
2910436c2adSjeremylt   @param vec        CeedVector
2920436c2adSjeremylt   @param[in] value  Value to be used
2930436c2adSjeremylt 
2940436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2950436c2adSjeremylt 
2967a982d89SJeremy L. Thompson   @ref User
2970436c2adSjeremylt **/
2980436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) {
2990436c2adSjeremylt   int ierr;
3000436c2adSjeremylt 
3010436c2adSjeremylt   if (vec->state % 2 == 1)
3029c774eddSJeremy L Thompson     // LCOV_EXCL_START
303e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
304e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, the "
3050436c2adSjeremylt                      "access lock is already in use");
3069c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
3079c774eddSJeremy L Thompson 
3089c774eddSJeremy L Thompson   if (vec->num_readers > 0)
3099c774eddSJeremy L Thompson     // LCOV_EXCL_START
3109c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
3119c774eddSJeremy L Thompson                      "Cannot grant CeedVector array access, a "
3129c774eddSJeremy L Thompson                      "process has read access");
3139c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
3140436c2adSjeremylt 
3150436c2adSjeremylt   if (vec->SetValue) {
3160436c2adSjeremylt     ierr = vec->SetValue(vec, value); CeedChk(ierr);
3170436c2adSjeremylt   } else {
3180436c2adSjeremylt     CeedScalar *array;
3199c774eddSJeremy L Thompson     ierr = CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
3200436c2adSjeremylt     for (int i=0; i<vec->length; i++) array[i] = value;
3210436c2adSjeremylt     ierr = CeedVectorRestoreArray(vec, &array); CeedChk(ierr);
3220436c2adSjeremylt   }
3230436c2adSjeremylt   vec->state += 2;
324e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3250436c2adSjeremylt }
3260436c2adSjeremylt 
3270436c2adSjeremylt /**
328b3cf021fSjeremylt   @brief Sync the CeedVector to a specified memtype. This function is used to
329b3cf021fSjeremylt            force synchronization of arrays set with @ref CeedVectorSetArray().
330b3cf021fSjeremylt            If the requested memtype is already synchronized, this function
331b3cf021fSjeremylt            results in a no-op.
3320436c2adSjeremylt 
3330436c2adSjeremylt   @param vec       CeedVector
334d1d35e2fSjeremylt   @param mem_type  Memtype to be synced
3350436c2adSjeremylt 
3360436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3370436c2adSjeremylt 
3387a982d89SJeremy L. Thompson   @ref User
3390436c2adSjeremylt **/
340d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) {
3410436c2adSjeremylt   int ierr;
3420436c2adSjeremylt 
3430436c2adSjeremylt   if (vec->state % 2 == 1)
344e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
345e15f9bd0SJeremy L Thompson                      "Cannot sync CeedVector, the access lock is "
3460436c2adSjeremylt                      "already in use");
3470436c2adSjeremylt 
3480436c2adSjeremylt   if (vec->SyncArray) {
349d1d35e2fSjeremylt     ierr = vec->SyncArray(vec, mem_type); CeedChk(ierr);
3500436c2adSjeremylt   } else {
3510436c2adSjeremylt     const CeedScalar *array;
352d1d35e2fSjeremylt     ierr = CeedVectorGetArrayRead(vec, mem_type, &array); CeedChk(ierr);
3530436c2adSjeremylt     ierr = CeedVectorRestoreArrayRead(vec, &array); CeedChk(ierr);
3540436c2adSjeremylt   }
355e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3560436c2adSjeremylt }
3570436c2adSjeremylt 
3580436c2adSjeremylt /**
3599c774eddSJeremy L Thompson   @brief Take ownership of the CeedVector array set by @ref CeedVectorSetArray()
3609c774eddSJeremy L Thompson            with @ref CEED_USE_POINTER and remove the array from the CeedVector.
3619c774eddSJeremy L Thompson            The caller is responsible for managing and freeing the array.
3629c774eddSJeremy L Thompson            This function will error if @ref CeedVectorSetArray() was not previously
3639c774eddSJeremy L Thompson            called with @ref CEED_USE_POINTER for the corresponding mem_type.
3646a6c615bSJeremy L Thompson 
3656a6c615bSJeremy L Thompson   @param vec         CeedVector
366d1d35e2fSjeremylt   @param mem_type    Memory type on which to take the array. If the backend
3676a6c615bSJeremy L Thompson                        uses a different memory type, this will perform a copy.
368d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type, or NULL if array pointer is
3696a6c615bSJeremy L Thompson                        not required
3706a6c615bSJeremy L Thompson 
3716a6c615bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3726a6c615bSJeremy L Thompson 
3736a6c615bSJeremy L Thompson   @ref User
3746a6c615bSJeremy L Thompson **/
375d1d35e2fSjeremylt int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type,
376d1d35e2fSjeremylt                         CeedScalar **array) {
3776a6c615bSJeremy L Thompson   int ierr;
3786a6c615bSJeremy L Thompson 
3796a6c615bSJeremy L Thompson   if (vec->state % 2 == 1)
3806a6c615bSJeremy L Thompson     // LCOV_EXCL_START
381e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
382e15f9bd0SJeremy L Thompson                      "Cannot take CeedVector array, the access "
3836a6c615bSJeremy L Thompson                      "lock is already in use");
3846a6c615bSJeremy L Thompson   // LCOV_EXCL_STOP
385d1d35e2fSjeremylt   if (vec->num_readers > 0)
3866a6c615bSJeremy L Thompson     // LCOV_EXCL_START
387e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
388e15f9bd0SJeremy L Thompson                      "Cannot take CeedVector array, a process "
3896a6c615bSJeremy L Thompson                      "has read access");
3906a6c615bSJeremy L Thompson   // LCOV_EXCL_STOP
39150c643e1SJed Brown   CeedScalar *temp_array = NULL;
39250c643e1SJed Brown   if (vec->length > 0) {
3939c774eddSJeremy L Thompson     bool has_borrowed_array_of_type = true;
3949c774eddSJeremy L Thompson     ierr = CeedVectorHasBorrowedArrayOfType(vec, mem_type,
3959c774eddSJeremy L Thompson                                             &has_borrowed_array_of_type);
3969c774eddSJeremy L Thompson     CeedChk(ierr);
3979c774eddSJeremy L Thompson     if (!has_borrowed_array_of_type)
3989c774eddSJeremy L Thompson       // LCOV_EXCL_START
3999c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
4009c774eddSJeremy L Thompson                        "CeedVector has no borrowed %s array, "
4019c774eddSJeremy L Thompson                        "must set array with CeedVectorSetArray", CeedMemTypes[mem_type]);
4029c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
4039c774eddSJeremy L Thompson 
4049c774eddSJeremy L Thompson     bool has_valid_array = true;
4059c774eddSJeremy L Thompson     ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
4069c774eddSJeremy L Thompson     if (!has_valid_array)
4079c774eddSJeremy L Thompson       // LCOV_EXCL_START
4089c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
4099c774eddSJeremy L Thompson                        "CeedVector has no valid data to take, "
4109c774eddSJeremy L Thompson                        "must set data with CeedVectorSetValue or CeedVectorSetArray");
4119c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
4129c774eddSJeremy L Thompson 
413d1d35e2fSjeremylt     ierr = vec->TakeArray(vec, mem_type, &temp_array); CeedChk(ierr);
41450c643e1SJed Brown   }
415d1d35e2fSjeremylt   if (array) (*array) = temp_array;
416e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4176a6c615bSJeremy L Thompson }
4186a6c615bSJeremy L Thompson 
4196a6c615bSJeremy L Thompson /**
420b3cf021fSjeremylt   @brief Get read/write access to a CeedVector via the specified memory type.
421b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArray().
4220436c2adSjeremylt 
4230436c2adSjeremylt   @param vec         CeedVector to access
424d1d35e2fSjeremylt   @param mem_type    Memory type on which to access the array. If the backend
425b3cf021fSjeremylt                        uses a different memory type, this will perform a copy.
426d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type
4270436c2adSjeremylt 
4280436c2adSjeremylt   @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide
4290436c2adSjeremylt     access to array pointers in the desired memory space. Pairing get/restore
4300436c2adSjeremylt     allows the Vector to track access, thus knowing if norms or other
4310436c2adSjeremylt     operations may need to be recomputed.
4320436c2adSjeremylt 
4330436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4340436c2adSjeremylt 
4357a982d89SJeremy L. Thompson   @ref User
4360436c2adSjeremylt **/
437d1d35e2fSjeremylt int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type,
438d1d35e2fSjeremylt                        CeedScalar **array) {
4390436c2adSjeremylt   int ierr;
4400436c2adSjeremylt 
4410436c2adSjeremylt   if (!vec->GetArray)
4420436c2adSjeremylt     // LCOV_EXCL_START
443e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
444e15f9bd0SJeremy L Thompson                      "Backend does not support GetArray");
4450436c2adSjeremylt   // LCOV_EXCL_STOP
4460436c2adSjeremylt 
4470436c2adSjeremylt   if (vec->state % 2 == 1)
448e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
449e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, the "
4500436c2adSjeremylt                      "access lock is already in use");
4510436c2adSjeremylt 
452d1d35e2fSjeremylt   if (vec->num_readers > 0)
453e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
454e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, a "
4550436c2adSjeremylt                      "process has read access");
4560436c2adSjeremylt 
4579c774eddSJeremy L Thompson   bool has_valid_array = true;
4589c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
4599c774eddSJeremy L Thompson   if (!has_valid_array)
4609c774eddSJeremy L Thompson     // LCOV_EXCL_START
4619c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
4629c774eddSJeremy L Thompson                      "CeedVector has no valid data to read, "
4639c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
4649c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
4659c774eddSJeremy L Thompson 
466d1d35e2fSjeremylt   ierr = vec->GetArray(vec, mem_type, array); CeedChk(ierr);
46728bfd0b7SJeremy L Thompson   vec->state++;
468e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4690436c2adSjeremylt }
4700436c2adSjeremylt 
4710436c2adSjeremylt /**
472b3cf021fSjeremylt   @brief Get read-only access to a CeedVector via the specified memory type.
473b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArrayRead().
4740436c2adSjeremylt 
4750436c2adSjeremylt   @param vec         CeedVector to access
476d1d35e2fSjeremylt   @param mem_type    Memory type on which to access the array.  If the backend
4770436c2adSjeremylt                        uses a different memory type, this will perform a copy
4780436c2adSjeremylt                        (possibly cached).
479d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type
4800436c2adSjeremylt 
4810436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4820436c2adSjeremylt 
4837a982d89SJeremy L. Thompson   @ref User
4840436c2adSjeremylt **/
485d1d35e2fSjeremylt int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type,
4860436c2adSjeremylt                            const CeedScalar **array) {
4870436c2adSjeremylt   int ierr;
4880436c2adSjeremylt 
4890436c2adSjeremylt   if (!vec->GetArrayRead)
4900436c2adSjeremylt     // LCOV_EXCL_START
491e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
492e15f9bd0SJeremy L Thompson                      "Backend does not support GetArrayRead");
4930436c2adSjeremylt   // LCOV_EXCL_STOP
4940436c2adSjeremylt 
4950436c2adSjeremylt   if (vec->state % 2 == 1)
496e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
497e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector read-only array "
4980436c2adSjeremylt                      "access, the access lock is already in use");
4990436c2adSjeremylt 
50050c643e1SJed Brown   if (vec->length > 0) {
5019c774eddSJeremy L Thompson     bool has_valid_array = true;
5029c774eddSJeremy L Thompson     ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
5039c774eddSJeremy L Thompson     if (!has_valid_array)
5049c774eddSJeremy L Thompson       // LCOV_EXCL_START
5059c774eddSJeremy L Thompson       return CeedError(vec->ceed, CEED_ERROR_BACKEND,
5069c774eddSJeremy L Thompson                        "CeedVector has no valid data to read, "
5079c774eddSJeremy L Thompson                        "must set data with CeedVectorSetValue or CeedVectorSetArray");
5089c774eddSJeremy L Thompson     // LCOV_EXCL_STOP
5099c774eddSJeremy L Thompson 
510d1d35e2fSjeremylt     ierr = vec->GetArrayRead(vec, mem_type, array); CeedChk(ierr);
51150c643e1SJed Brown   } else {
51250c643e1SJed Brown     *array = NULL;
51350c643e1SJed Brown   }
514d1d35e2fSjeremylt   vec->num_readers++;
515e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5160436c2adSjeremylt }
5170436c2adSjeremylt 
5180436c2adSjeremylt /**
5199c774eddSJeremy L Thompson   @brief Get write access to a CeedVector via the specified memory type.
5209c774eddSJeremy L Thompson            Restore access with @ref CeedVectorRestoreArray(). All old
5219c774eddSJeremy L Thompson            values should be assumed to be invalid.
5229c774eddSJeremy L Thompson 
5239c774eddSJeremy L Thompson   @param vec         CeedVector to access
5249c774eddSJeremy L Thompson   @param mem_type    Memory type on which to access the array.
5259c774eddSJeremy L Thompson   @param[out] array  Array on memory type mem_type
5269c774eddSJeremy L Thompson 
5279c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5289c774eddSJeremy L Thompson 
5299c774eddSJeremy L Thompson   @ref User
5309c774eddSJeremy L Thompson **/
5319c774eddSJeremy L Thompson int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type,
5329c774eddSJeremy L Thompson                             CeedScalar **array) {
5339c774eddSJeremy L Thompson   int ierr;
5349c774eddSJeremy L Thompson 
5359c774eddSJeremy L Thompson   if (!vec->GetArrayWrite)
5369c774eddSJeremy L Thompson     // LCOV_EXCL_START
5379c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
5389c774eddSJeremy L Thompson                      "Backend does not support GetArrayWrite");
5399c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
5409c774eddSJeremy L Thompson 
5419c774eddSJeremy L Thompson   if (vec->state % 2 == 1)
5429c774eddSJeremy L Thompson     // LCOV_EXCL_START
5439c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
5449c774eddSJeremy L Thompson                      "Cannot grant CeedVector array access, the "
5459c774eddSJeremy L Thompson                      "access lock is already in use");
5469c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
5479c774eddSJeremy L Thompson 
5489c774eddSJeremy L Thompson   if (vec->num_readers > 0)
5499c774eddSJeremy L Thompson     // LCOV_EXCL_START
5509c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
5519c774eddSJeremy L Thompson                      "Cannot grant CeedVector array access, a "
5529c774eddSJeremy L Thompson                      "process has read access");
5539c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
5549c774eddSJeremy L Thompson 
5559c774eddSJeremy L Thompson   ierr = vec->GetArrayWrite(vec, mem_type, array); CeedChk(ierr);
55628bfd0b7SJeremy L Thompson   vec->state++;
5579c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5589c774eddSJeremy L Thompson }
5599c774eddSJeremy L Thompson 
5609c774eddSJeremy L Thompson /**
561b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArray()
5620436c2adSjeremylt 
5630436c2adSjeremylt   @param vec    CeedVector to restore
5640436c2adSjeremylt   @param array  Array of vector data
5650436c2adSjeremylt 
5660436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5670436c2adSjeremylt 
5687a982d89SJeremy L. Thompson   @ref User
5690436c2adSjeremylt **/
5700436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
5710436c2adSjeremylt   int ierr;
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");
577706efda3SJeremy L Thompson   if (vec->RestoreArray) {
5780436c2adSjeremylt     ierr = vec->RestoreArray(vec); CeedChk(ierr);
579706efda3SJeremy L Thompson   }
5800436c2adSjeremylt   *array = NULL;
58128bfd0b7SJeremy L Thompson   vec->state++;
582e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5830436c2adSjeremylt }
5840436c2adSjeremylt 
5850436c2adSjeremylt /**
586b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArrayRead()
5870436c2adSjeremylt 
5880436c2adSjeremylt   @param vec    CeedVector to restore
5890436c2adSjeremylt   @param array  Array of vector data
5900436c2adSjeremylt 
5910436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5920436c2adSjeremylt 
5937a982d89SJeremy L. Thompson   @ref User
5940436c2adSjeremylt **/
5950436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) {
5960436c2adSjeremylt   int ierr;
5970436c2adSjeremylt 
5989c774eddSJeremy L Thompson   if (vec->num_readers == 0)
5999c774eddSJeremy L Thompson     // LCOV_EXCL_START
6009c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
6019c774eddSJeremy L Thompson                      "Cannot restore CeedVector array read access, "
6029c774eddSJeremy L Thompson                      "access was not granted");
6039c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
6049c774eddSJeremy L Thompson 
605706efda3SJeremy L Thompson   if (vec->RestoreArrayRead) {
6060436c2adSjeremylt     ierr = vec->RestoreArrayRead(vec); CeedChk(ierr);
607706efda3SJeremy L Thompson   }
6080436c2adSjeremylt   *array = NULL;
609d1d35e2fSjeremylt   vec->num_readers--;
610e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6110436c2adSjeremylt }
6120436c2adSjeremylt 
6130436c2adSjeremylt /**
614171f8ca9Sjeremylt   @brief Get the norm of a CeedVector.
615171f8ca9Sjeremylt 
616171f8ca9Sjeremylt   Note: This operation is local to the CeedVector. This function will likely
617171f8ca9Sjeremylt           not provide the desired results for the norm of the libCEED portion
618171f8ca9Sjeremylt           of a parallel vector or a CeedVector with duplicated or hanging nodes.
619547d9b97Sjeremylt 
620547d9b97Sjeremylt   @param vec        CeedVector to retrieve maximum value
621d1d35e2fSjeremylt   @param norm_type  Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
622547d9b97Sjeremylt   @param[out] norm  Variable to store norm value
623547d9b97Sjeremylt 
624547d9b97Sjeremylt   @return An error code: 0 - success, otherwise - failure
625547d9b97Sjeremylt 
6267a982d89SJeremy L. Thompson   @ref User
627547d9b97Sjeremylt **/
628d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
629547d9b97Sjeremylt   int ierr;
630547d9b97Sjeremylt 
6319c774eddSJeremy L Thompson   bool has_valid_array = true;
6329c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
6339c774eddSJeremy L Thompson   if (!has_valid_array)
6349c774eddSJeremy L Thompson     // LCOV_EXCL_START
6359c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
6369c774eddSJeremy L Thompson                      "CeedVector has no valid data to compute norm, "
6379c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
6389c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
6399c774eddSJeremy L Thompson 
640547d9b97Sjeremylt   // Backend impl for GPU, if added
641547d9b97Sjeremylt   if (vec->Norm) {
642d1d35e2fSjeremylt     ierr = vec->Norm(vec, norm_type, norm); CeedChk(ierr);
643e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
644547d9b97Sjeremylt   }
645547d9b97Sjeremylt 
646547d9b97Sjeremylt   const CeedScalar *array;
647547d9b97Sjeremylt   ierr = CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
648547d9b97Sjeremylt 
649547d9b97Sjeremylt   *norm = 0.;
650d1d35e2fSjeremylt   switch (norm_type) {
651547d9b97Sjeremylt   case CEED_NORM_1:
652547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
653547d9b97Sjeremylt       *norm += fabs(array[i]);
654547d9b97Sjeremylt     }
655547d9b97Sjeremylt     break;
656547d9b97Sjeremylt   case CEED_NORM_2:
657547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
658547d9b97Sjeremylt       *norm += fabs(array[i])*fabs(array[i]);
659547d9b97Sjeremylt     }
660547d9b97Sjeremylt     break;
661547d9b97Sjeremylt   case CEED_NORM_MAX:
662547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
663d1d35e2fSjeremylt       const CeedScalar abs_v_i = fabs(array[i]);
664d1d35e2fSjeremylt       *norm = *norm > abs_v_i ? *norm : abs_v_i;
665547d9b97Sjeremylt     }
666547d9b97Sjeremylt   }
667d1d35e2fSjeremylt   if (norm_type == CEED_NORM_2)
668547d9b97Sjeremylt     *norm = sqrt(*norm);
669547d9b97Sjeremylt 
670547d9b97Sjeremylt   ierr = CeedVectorRestoreArrayRead(vec, &array); CeedChk(ierr);
671e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
672547d9b97Sjeremylt }
673547d9b97Sjeremylt 
674547d9b97Sjeremylt /**
675e0dd3b27Sjeremylt   @brief Compute x = alpha x
676e0dd3b27Sjeremylt 
67796b902e2Sjeremylt   @param[in,out] x  vector for scaling
67896b902e2Sjeremylt   @param[in] alpha  scaling factor
679e0dd3b27Sjeremylt 
680e0dd3b27Sjeremylt   @return An error code: 0 - success, otherwise - failure
681e0dd3b27Sjeremylt 
682e0dd3b27Sjeremylt   @ref User
683e0dd3b27Sjeremylt **/
684e0dd3b27Sjeremylt int CeedVectorScale(CeedVector x, CeedScalar alpha) {
685e0dd3b27Sjeremylt   int ierr;
686*73380422SJeremy L Thompson   CeedScalar *x_array = NULL;
687e0dd3b27Sjeremylt   CeedInt n_x;
688e0dd3b27Sjeremylt 
6899c774eddSJeremy L Thompson   bool has_valid_array = true;
6909c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(x, &has_valid_array); CeedChk(ierr);
6919c774eddSJeremy L Thompson   if (!has_valid_array)
6929c774eddSJeremy L Thompson     // LCOV_EXCL_START
6939c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
6949c774eddSJeremy L Thompson                      "CeedVector has no valid data to scale, "
6959c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
6969c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
6979c774eddSJeremy L Thompson 
698e0dd3b27Sjeremylt   ierr = CeedVectorGetLength(x, &n_x); CeedChk(ierr);
699e0dd3b27Sjeremylt 
700e0dd3b27Sjeremylt   // Backend implementation
701e0dd3b27Sjeremylt   if (x->Scale)
702e0dd3b27Sjeremylt     return x->Scale(x, alpha);
703e0dd3b27Sjeremylt 
704e0dd3b27Sjeremylt   // Default implementation
7059c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(x, CEED_MEM_HOST, &x_array); CeedChk(ierr);
706e0dd3b27Sjeremylt   for (CeedInt i=0; i<n_x; i++)
707e0dd3b27Sjeremylt     x_array[i] *= alpha;
708e0dd3b27Sjeremylt   ierr = CeedVectorRestoreArray(x, &x_array); CeedChk(ierr);
709e0dd3b27Sjeremylt 
710e0dd3b27Sjeremylt   return CEED_ERROR_SUCCESS;
711e0dd3b27Sjeremylt }
712e0dd3b27Sjeremylt 
713e0dd3b27Sjeremylt /**
7140f7fd0f8Sjeremylt   @brief Compute y = alpha x + y
7150f7fd0f8Sjeremylt 
71696b902e2Sjeremylt   @param[in,out] y  target vector for sum
71796b902e2Sjeremylt   @param[in] alpha  scaling factor
71896b902e2Sjeremylt   @param[in] x      second vector, must be different than y
7190f7fd0f8Sjeremylt 
7200f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
7210f7fd0f8Sjeremylt 
7220f7fd0f8Sjeremylt   @ref User
7230f7fd0f8Sjeremylt **/
7240f7fd0f8Sjeremylt int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) {
7250f7fd0f8Sjeremylt   int ierr;
726*73380422SJeremy L Thompson   CeedScalar *y_array = NULL;
727*73380422SJeremy L Thompson   CeedScalar const *x_array = NULL;
7280f7fd0f8Sjeremylt   CeedInt n_x, n_y;
7290f7fd0f8Sjeremylt 
7300f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(y, &n_y); CeedChk(ierr);
7310f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(x, &n_x); CeedChk(ierr);
7320f7fd0f8Sjeremylt   if (n_x != n_y)
7330f7fd0f8Sjeremylt     // LCOV_EXCL_START
7340f7fd0f8Sjeremylt     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED,
7350f7fd0f8Sjeremylt                      "Cannot add vector of different lengths");
7360f7fd0f8Sjeremylt   // LCOV_EXCL_STOP
7370f7fd0f8Sjeremylt   if (x == y)
7380f7fd0f8Sjeremylt     // LCOV_EXCL_START
7390f7fd0f8Sjeremylt     return CeedError(y->ceed, CEED_ERROR_UNSUPPORTED,
7400f7fd0f8Sjeremylt                      "Cannot use same vector for x and y in CeedVectorAXPY");
7410f7fd0f8Sjeremylt   // LCOV_EXCL_STOP
7420f7fd0f8Sjeremylt 
7439c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
7449c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(x, &has_valid_array_x); CeedChk(ierr);
7459c774eddSJeremy L Thompson   if (!has_valid_array_x)
7469c774eddSJeremy L Thompson     // LCOV_EXCL_START
7479c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
7489c774eddSJeremy L Thompson                      "CeedVector x has no valid data, "
7499c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
7509c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
7519c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(y, &has_valid_array_y); CeedChk(ierr);
7529c774eddSJeremy L Thompson   if (!has_valid_array_y)
7539c774eddSJeremy L Thompson     // LCOV_EXCL_START
7549c774eddSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND,
7559c774eddSJeremy L Thompson                      "CeedVector y has no valid data, "
7569c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
7579c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
7589c774eddSJeremy L Thompson 
7592d04630dSjeremylt   Ceed ceed_parent_x, ceed_parent_y;
7602d04630dSjeremylt   ierr = CeedGetParent(x->ceed, &ceed_parent_x); CeedChk(ierr);
7612d04630dSjeremylt   ierr = CeedGetParent(y->ceed, &ceed_parent_y); CeedChk(ierr);
7622d04630dSjeremylt   if (ceed_parent_x != ceed_parent_y)
7632d04630dSjeremylt     // LCOV_EXCL_START
7642d04630dSjeremylt     return CeedError(y->ceed, CEED_ERROR_INCOMPATIBLE,
7652d04630dSjeremylt                      "Vectors x and y must be created by the same Ceed context");
7662d04630dSjeremylt   // LCOV_EXCL_STOP
7672d04630dSjeremylt 
7680f7fd0f8Sjeremylt   // Backend implementation
769eaf62fffSJeremy L Thompson   if (y->AXPY) {
770eaf62fffSJeremy L Thompson     ierr = y->AXPY(y, alpha, x); CeedChk(ierr);
771eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
772eaf62fffSJeremy L Thompson   }
7730f7fd0f8Sjeremylt 
7740f7fd0f8Sjeremylt   // Default implementation
7759c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(y, CEED_MEM_HOST, &y_array); CeedChk(ierr);
7760f7fd0f8Sjeremylt   ierr = CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array); CeedChk(ierr);
7770f7fd0f8Sjeremylt 
778*73380422SJeremy L Thompson   assert(x_array); assert(y_array);
779*73380422SJeremy L Thompson 
7800f7fd0f8Sjeremylt   for (CeedInt i=0; i<n_y; i++)
7810f7fd0f8Sjeremylt     y_array[i] += alpha * x_array[i];
7820f7fd0f8Sjeremylt 
7830f7fd0f8Sjeremylt   ierr = CeedVectorRestoreArray(y, &y_array); CeedChk(ierr);
7840f7fd0f8Sjeremylt   ierr = CeedVectorRestoreArrayRead(x, &x_array); CeedChk(ierr);
7850f7fd0f8Sjeremylt 
7860f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
7870f7fd0f8Sjeremylt }
7880f7fd0f8Sjeremylt 
7890f7fd0f8Sjeremylt /**
7900c1bc3c2Sjeremylt   @brief Compute the pointwise multiplication w = x .* y. Any
7910f7fd0f8Sjeremylt            subset of x, y, and w may be the same vector.
7920f7fd0f8Sjeremylt 
79396b902e2Sjeremylt   @param[out] w  target vector for the product
79496b902e2Sjeremylt   @param[in] x   first vector for product
79596b902e2Sjeremylt   @param[in] y   second vector for the product
7960f7fd0f8Sjeremylt 
7970f7fd0f8Sjeremylt   @return An error code: 0 - success, otherwise - failure
7980f7fd0f8Sjeremylt 
7990f7fd0f8Sjeremylt   @ ref User
8000f7fd0f8Sjeremylt **/
8010f7fd0f8Sjeremylt int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) {
8020f7fd0f8Sjeremylt   int ierr;
803*73380422SJeremy L Thompson   CeedScalar *w_array = NULL;
804*73380422SJeremy L Thompson   CeedScalar const *x_array = NULL, *y_array = NULL;
8052d04630dSjeremylt   CeedInt n_w, n_x, n_y;
8060f7fd0f8Sjeremylt 
8070f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(w, &n_w); CeedChk(ierr);
8080f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(x, &n_x); CeedChk(ierr);
8090f7fd0f8Sjeremylt   ierr = CeedVectorGetLength(y, &n_y); CeedChk(ierr);
8100f7fd0f8Sjeremylt   if (n_w != n_x || n_w != n_y)
8110f7fd0f8Sjeremylt     // LCOV_EXCL_START
8120f7fd0f8Sjeremylt     return CeedError(w->ceed, CEED_ERROR_UNSUPPORTED,
8130f7fd0f8Sjeremylt                      "Cannot multiply vectors of different lengths");
8140f7fd0f8Sjeremylt   // LCOV_EXCL_STOP
8150f7fd0f8Sjeremylt 
8162d04630dSjeremylt   Ceed ceed_parent_w, ceed_parent_x, ceed_parent_y;
8172d04630dSjeremylt   ierr = CeedGetParent(w->ceed, &ceed_parent_w); CeedChk(ierr);
8182d04630dSjeremylt   ierr = CeedGetParent(x->ceed, &ceed_parent_x); CeedChk(ierr);
8192d04630dSjeremylt   ierr = CeedGetParent(y->ceed, &ceed_parent_y); CeedChk(ierr);
8209c774eddSJeremy L Thompson   if ((ceed_parent_w != ceed_parent_x) ||
8212d04630dSjeremylt       (ceed_parent_w != ceed_parent_y))
8222d04630dSjeremylt     // LCOV_EXCL_START
8232d04630dSjeremylt     return CeedError(w->ceed, CEED_ERROR_INCOMPATIBLE,
8242d04630dSjeremylt                      "Vectors w, x, and y must be created by the same Ceed context");
8252d04630dSjeremylt   // LCOV_EXCL_STOP
8262d04630dSjeremylt 
8279c774eddSJeremy L Thompson   bool has_valid_array_x = true, has_valid_array_y = true;
8289c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(x, &has_valid_array_x); CeedChk(ierr);
8299c774eddSJeremy L Thompson   if (!has_valid_array_x)
8309c774eddSJeremy L Thompson     // LCOV_EXCL_START
8319c774eddSJeremy L Thompson     return CeedError(x->ceed, CEED_ERROR_BACKEND,
8329c774eddSJeremy L Thompson                      "CeedVector x has no valid data, "
8339c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
8349c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
8359c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(y, &has_valid_array_y); CeedChk(ierr);
8369c774eddSJeremy L Thompson   if (!has_valid_array_y)
8379c774eddSJeremy L Thompson     // LCOV_EXCL_START
8389c774eddSJeremy L Thompson     return CeedError(y->ceed, CEED_ERROR_BACKEND,
8399c774eddSJeremy L Thompson                      "CeedVector y has no valid data, "
8409c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
8419c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
8429c774eddSJeremy L Thompson 
8430f7fd0f8Sjeremylt   // Backend implementation
844eaf62fffSJeremy L Thompson   if (w->PointwiseMult) {
845eaf62fffSJeremy L Thompson     ierr = w->PointwiseMult(w, x, y); CeedChk(ierr);
846eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
847eaf62fffSJeremy L Thompson   }
8480f7fd0f8Sjeremylt 
8490f7fd0f8Sjeremylt   // Default implementation
8509c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(w, CEED_MEM_HOST, &w_array); CeedChk(ierr);
8510f7fd0f8Sjeremylt   if (x != w) {
8520f7fd0f8Sjeremylt     ierr = CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array); CeedChk(ierr);
8530f7fd0f8Sjeremylt   } else {
8540f7fd0f8Sjeremylt     x_array = w_array;
8550f7fd0f8Sjeremylt   }
8560f7fd0f8Sjeremylt   if (y != w && y != x) {
8570f7fd0f8Sjeremylt     ierr = CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array); CeedChk(ierr);
8580f7fd0f8Sjeremylt   } else if (y != x) {
8590f7fd0f8Sjeremylt     y_array = w_array;
8600f7fd0f8Sjeremylt   } else {
8610f7fd0f8Sjeremylt     y_array = x_array;
8620f7fd0f8Sjeremylt   }
8630f7fd0f8Sjeremylt 
864*73380422SJeremy L Thompson   assert(w_array); assert(x_array); assert(y_array);
865*73380422SJeremy L Thompson 
8660f7fd0f8Sjeremylt   for (CeedInt i=0; i<n_w; i++)
8670f7fd0f8Sjeremylt     w_array[i] = x_array[i] * y_array[i];
8680f7fd0f8Sjeremylt 
8690f7fd0f8Sjeremylt   if (y != w && y != x) {
8700f7fd0f8Sjeremylt     ierr = CeedVectorRestoreArrayRead(y, &y_array); CeedChk(ierr);
8710f7fd0f8Sjeremylt   }
8720f7fd0f8Sjeremylt   if (x != w) {
8730f7fd0f8Sjeremylt     ierr = CeedVectorRestoreArrayRead(x, &x_array); CeedChk(ierr);
8740f7fd0f8Sjeremylt   }
8750f7fd0f8Sjeremylt   ierr = CeedVectorRestoreArray(w, &w_array); CeedChk(ierr);
8760f7fd0f8Sjeremylt   return CEED_ERROR_SUCCESS;
8770f7fd0f8Sjeremylt }
8780f7fd0f8Sjeremylt 
8790f7fd0f8Sjeremylt /**
880d99fa3c5SJeremy L Thompson   @brief Take the reciprocal of a CeedVector.
881d99fa3c5SJeremy L Thompson 
882d99fa3c5SJeremy L Thompson   @param vec  CeedVector to take reciprocal
883d99fa3c5SJeremy L Thompson 
884d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
885d99fa3c5SJeremy L Thompson 
886d99fa3c5SJeremy L Thompson   @ref User
887d99fa3c5SJeremy L Thompson **/
888d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) {
889d99fa3c5SJeremy L Thompson   int ierr;
890d99fa3c5SJeremy L Thompson 
8919c774eddSJeremy L Thompson   bool has_valid_array = true;
8929c774eddSJeremy L Thompson   ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChk(ierr);
8939c774eddSJeremy L Thompson   if (!has_valid_array)
8949c774eddSJeremy L Thompson     // LCOV_EXCL_START
8959c774eddSJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_BACKEND,
8969c774eddSJeremy L Thompson                      "CeedVector has no valid data to compute reciprocal, "
8979c774eddSJeremy L Thompson                      "must set data with CeedVectorSetValue or CeedVectorSetArray");
8989c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
8999c774eddSJeremy L Thompson 
900d99fa3c5SJeremy L Thompson   // Check if vector data set
901d99fa3c5SJeremy L Thompson   if (!vec->state)
902d99fa3c5SJeremy L Thompson     // LCOV_EXCL_START
903e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE,
904d99fa3c5SJeremy L Thompson                      "CeedVector must have data set to take reciprocal");
905d99fa3c5SJeremy L Thompson   // LCOV_EXCL_STOP
906d99fa3c5SJeremy L Thompson 
907d99fa3c5SJeremy L Thompson   // Backend impl for GPU, if added
908d99fa3c5SJeremy L Thompson   if (vec->Reciprocal) {
909d99fa3c5SJeremy L Thompson     ierr = vec->Reciprocal(vec); CeedChk(ierr);
910e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
911d99fa3c5SJeremy L Thompson   }
912d99fa3c5SJeremy L Thompson 
913d99fa3c5SJeremy L Thompson   CeedInt len;
914d99fa3c5SJeremy L Thompson   ierr = CeedVectorGetLength(vec, &len); CeedChk(ierr);
915d99fa3c5SJeremy L Thompson   CeedScalar *array;
9169c774eddSJeremy L Thompson   ierr = CeedVectorGetArrayWrite(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
917d99fa3c5SJeremy L Thompson   for (CeedInt i=0; i<len; i++)
918d99fa3c5SJeremy L Thompson     if (fabs(array[i]) > CEED_EPSILON)
919d99fa3c5SJeremy L Thompson       array[i] = 1./array[i];
920d99fa3c5SJeremy L Thompson 
921e15f9bd0SJeremy L Thompson   ierr = CeedVectorRestoreArray(vec, &array); CeedChk(ierr);
922e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
923d99fa3c5SJeremy L Thompson }
924d99fa3c5SJeremy L Thompson 
925d99fa3c5SJeremy L Thompson /**
9267a982d89SJeremy L. Thompson   @brief View a CeedVector
9270436c2adSjeremylt 
9280a0da059Sjeremylt   @param[in] vec     CeedVector to view
929d1d35e2fSjeremylt   @param[in] fp_fmt  Printing format
9300a0da059Sjeremylt   @param[in] stream  Filestream to write to
9310a0da059Sjeremylt 
9320436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9330436c2adSjeremylt 
9347a982d89SJeremy L. Thompson   @ref User
9350436c2adSjeremylt **/
936d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
9377a982d89SJeremy L. Thompson   const CeedScalar *x;
9387a982d89SJeremy L. Thompson 
9397a982d89SJeremy L. Thompson   int ierr = CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x); CeedChk(ierr);
9407a982d89SJeremy L. Thompson 
9417a982d89SJeremy L. Thompson   char fmt[1024];
9427a982d89SJeremy L. Thompson   fprintf(stream, "CeedVector length %ld\n", (long)vec->length);
943d1d35e2fSjeremylt   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
9447a982d89SJeremy L. Thompson   for (CeedInt i=0; i<vec->length; i++)
9457a982d89SJeremy L. Thompson     fprintf(stream, fmt, x[i]);
9467a982d89SJeremy L. Thompson 
9477a982d89SJeremy L. Thompson   ierr = CeedVectorRestoreArrayRead(vec, &x); CeedChk(ierr);
948e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9490436c2adSjeremylt }
9500436c2adSjeremylt 
9510436c2adSjeremylt /**
952b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedVector
953b7c9bbdaSJeremy L Thompson 
954b7c9bbdaSJeremy L Thompson   @param vec        CeedVector to retrieve state
955b7c9bbdaSJeremy L Thompson   @param[out] ceed  Variable to store ceed
956b7c9bbdaSJeremy L Thompson 
957b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
958b7c9bbdaSJeremy L Thompson 
959b7c9bbdaSJeremy L Thompson   @ref Advanced
960b7c9bbdaSJeremy L Thompson **/
961b7c9bbdaSJeremy L Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
962b7c9bbdaSJeremy L Thompson   *ceed = vec->ceed;
963b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
964b7c9bbdaSJeremy L Thompson }
965b7c9bbdaSJeremy L Thompson 
966b7c9bbdaSJeremy L Thompson /**
9677a982d89SJeremy L. Thompson   @brief Get the length of a CeedVector
9680436c2adSjeremylt 
9697a982d89SJeremy L. Thompson   @param vec          CeedVector to retrieve length
9707a982d89SJeremy L. Thompson   @param[out] length  Variable to store length
9710436c2adSjeremylt 
9720436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9730436c2adSjeremylt 
9747a982d89SJeremy L. Thompson   @ref User
9750436c2adSjeremylt **/
9767a982d89SJeremy L. Thompson int CeedVectorGetLength(CeedVector vec, CeedInt *length) {
9777a982d89SJeremy L. Thompson   *length = vec->length;
978e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9790436c2adSjeremylt }
9800436c2adSjeremylt 
9810436c2adSjeremylt /**
9820436c2adSjeremylt   @brief Destroy a CeedVector
9830436c2adSjeremylt 
9840436c2adSjeremylt   @param vec  CeedVector to destroy
9850436c2adSjeremylt 
9860436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
9870436c2adSjeremylt 
9887a982d89SJeremy L. Thompson   @ref User
9890436c2adSjeremylt **/
9900436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) {
9910436c2adSjeremylt   int ierr;
9920436c2adSjeremylt 
993d1d35e2fSjeremylt   if (!*vec || --(*vec)->ref_count > 0) return CEED_ERROR_SUCCESS;
9940436c2adSjeremylt 
995187168c7SJeremy L Thompson   if (((*vec)->state % 2) == 1)
996e15f9bd0SJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS,
997187168c7SJeremy L Thompson                      "Cannot destroy CeedVector, the writable access "
9980436c2adSjeremylt                      "lock is in use");
9990436c2adSjeremylt 
1000d1d35e2fSjeremylt   if ((*vec)->num_readers > 0)
1001e92b641fSJeremy L Thompson     // LCOV_EXCL_START
1002e15f9bd0SJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS,
1003e15f9bd0SJeremy L Thompson                      "Cannot destroy CeedVector, a process has "
1004187168c7SJeremy L Thompson                      "read access");
1005e92b641fSJeremy L Thompson   // LCOV_EXCL_STOP
1006187168c7SJeremy L Thompson 
10070436c2adSjeremylt   if ((*vec)->Destroy) {
10080436c2adSjeremylt     ierr = (*vec)->Destroy(*vec); CeedChk(ierr);
10090436c2adSjeremylt   }
10100436c2adSjeremylt 
10110436c2adSjeremylt   ierr = CeedDestroy(&(*vec)->ceed); CeedChk(ierr);
10120436c2adSjeremylt   ierr = CeedFree(vec); CeedChk(ierr);
1013e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10140436c2adSjeremylt }
10150436c2adSjeremylt 
10160436c2adSjeremylt /// @}
1017