xref: /libCEED/interface/ceed-vector.c (revision 34359f16efbc10f0e69405f262a23387c38de222) !
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 
394cc79fe7SJed Brown /// Indicate that no vector is applicable (i.e., for @ref CEED_EVAL_WEIGHTS).
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 /**
517a982d89SJeremy L. Thompson   @brief Get the Ceed associated with a CeedVector
527a982d89SJeremy L. Thompson 
537a982d89SJeremy L. Thompson   @param vec        CeedVector to retrieve state
547a982d89SJeremy L. Thompson   @param[out] ceed  Variable to store ceed
557a982d89SJeremy L. Thompson 
567a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
577a982d89SJeremy L. Thompson 
587a982d89SJeremy L. Thompson   @ref Backend
597a982d89SJeremy L. Thompson **/
607a982d89SJeremy L. Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) {
617a982d89SJeremy L. Thompson   *ceed = vec->ceed;
62e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
637a982d89SJeremy L. Thompson }
647a982d89SJeremy L. Thompson 
657a982d89SJeremy L. Thompson /**
667a982d89SJeremy L. Thompson   @brief Get the state of a CeedVector
677a982d89SJeremy L. Thompson 
687a982d89SJeremy L. Thompson   @param vec         CeedVector to retrieve state
697a982d89SJeremy L. Thompson   @param[out] state  Variable to store state
707a982d89SJeremy L. Thompson 
717a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
727a982d89SJeremy L. Thompson 
737a982d89SJeremy L. Thompson   @ref Backend
747a982d89SJeremy L. Thompson **/
757a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) {
767a982d89SJeremy L. Thompson   *state = vec->state;
77e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
787a982d89SJeremy L. Thompson }
797a982d89SJeremy L. Thompson 
807a982d89SJeremy L. Thompson /**
8127312b0fSJed Brown   @brief Add a reference to a CeedVector
827a982d89SJeremy L. Thompson 
835f67fadeSJeremy L Thompson   @param[out] vec  CeedVector to increment reference counter
847a982d89SJeremy L. Thompson 
857a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
867a982d89SJeremy L. Thompson 
877a982d89SJeremy L. Thompson   @ref Backend
887a982d89SJeremy L. Thompson **/
897a982d89SJeremy L. Thompson int CeedVectorAddReference(CeedVector vec) {
90d1d35e2fSjeremylt   vec->ref_count++;
91e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
927a982d89SJeremy L. Thompson }
937a982d89SJeremy L. Thompson 
947a982d89SJeremy L. Thompson /**
957a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedVector
967a982d89SJeremy L. Thompson 
977a982d89SJeremy L. Thompson   @param vec        CeedVector to retrieve state
987a982d89SJeremy L. Thompson   @param[out] data  Variable to store data
997a982d89SJeremy L. Thompson 
1007a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1017a982d89SJeremy L. Thompson 
1027a982d89SJeremy L. Thompson   @ref Backend
1037a982d89SJeremy L. Thompson **/
104777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) {
105777ff853SJeremy L Thompson   *(void **)data = vec->data;
106e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1077a982d89SJeremy L. Thompson }
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson /**
1107a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedVector
1117a982d89SJeremy L. Thompson 
1127a982d89SJeremy L. Thompson   @param[out] vec  CeedVector to retrieve state
1137a982d89SJeremy L. Thompson   @param data      Data to set
1147a982d89SJeremy L. Thompson 
1157a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1167a982d89SJeremy L. Thompson 
1177a982d89SJeremy L. Thompson   @ref Backend
1187a982d89SJeremy L. Thompson **/
119777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) {
120777ff853SJeremy L Thompson   vec->data = data;
121e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1227a982d89SJeremy L. Thompson }
1237a982d89SJeremy L. Thompson 
124*34359f16Sjeremylt /**
125*34359f16Sjeremylt   @brief Increment the reference counter for a CeedVector
126*34359f16Sjeremylt 
127*34359f16Sjeremylt   @param vec  CeedVector to increment the reference counter
128*34359f16Sjeremylt 
129*34359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
130*34359f16Sjeremylt 
131*34359f16Sjeremylt   @ref Backend
132*34359f16Sjeremylt **/
133*34359f16Sjeremylt int CeedVectorIncrementRefCounter(CeedVector vec) {
134*34359f16Sjeremylt   vec->ref_count++;
135*34359f16Sjeremylt   return CEED_ERROR_SUCCESS;
136*34359f16Sjeremylt }
137*34359f16Sjeremylt 
1387a982d89SJeremy L. Thompson /// @}
1397a982d89SJeremy L. Thompson 
1407a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1417a982d89SJeremy L. Thompson /// CeedVector Public API
1427a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1437a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser
1440436c2adSjeremylt /// @{
1450436c2adSjeremylt 
1460436c2adSjeremylt /**
1470436c2adSjeremylt   @brief Create a CeedVector of the specified length (does not allocate memory)
1480436c2adSjeremylt 
1490436c2adSjeremylt   @param ceed      Ceed object where the CeedVector will be created
1500436c2adSjeremylt   @param length    Length of vector
1510436c2adSjeremylt   @param[out] vec  Address of the variable where the newly created
1520436c2adSjeremylt                      CeedVector will be stored
1530436c2adSjeremylt 
1540436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
1550436c2adSjeremylt 
1567a982d89SJeremy L. Thompson   @ref User
1570436c2adSjeremylt **/
1580436c2adSjeremylt int CeedVectorCreate(Ceed ceed, CeedInt length, CeedVector *vec) {
1590436c2adSjeremylt   int ierr;
1600436c2adSjeremylt 
1610436c2adSjeremylt   if (!ceed->VectorCreate) {
1620436c2adSjeremylt     Ceed delegate;
1630436c2adSjeremylt     ierr = CeedGetObjectDelegate(ceed, &delegate, "Vector"); CeedChk(ierr);
1640436c2adSjeremylt 
1650436c2adSjeremylt     if (!delegate)
1660436c2adSjeremylt       // LCOV_EXCL_START
167e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
168e15f9bd0SJeremy L Thompson                        "Backend does not support VectorCreate");
1690436c2adSjeremylt     // LCOV_EXCL_STOP
1700436c2adSjeremylt 
1710436c2adSjeremylt     ierr = CeedVectorCreate(delegate, length, vec); CeedChk(ierr);
172e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
1730436c2adSjeremylt   }
1740436c2adSjeremylt 
1750436c2adSjeremylt   ierr = CeedCalloc(1, vec); CeedChk(ierr);
1760436c2adSjeremylt   (*vec)->ceed = ceed;
177*34359f16Sjeremylt   ierr = CeedIncrementRefCounter(ceed); CeedChk(ierr);
178d1d35e2fSjeremylt   (*vec)->ref_count = 1;
1790436c2adSjeremylt   (*vec)->length = length;
1800436c2adSjeremylt   (*vec)->state = 0;
1810436c2adSjeremylt   ierr = ceed->VectorCreate(length, *vec); CeedChk(ierr);
182e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1830436c2adSjeremylt }
1840436c2adSjeremylt 
1850436c2adSjeremylt /**
1860436c2adSjeremylt   @brief Set the array used by a CeedVector, freeing any previously allocated
187b3cf021fSjeremylt            array if applicable. The backend may copy values to a different
188b3cf021fSjeremylt            memtype, such as during @ref CeedOperatorApply().
1896a6c615bSJeremy L Thompson            See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray().
1900436c2adSjeremylt 
1910436c2adSjeremylt   @param vec        CeedVector
192d1d35e2fSjeremylt   @param mem_type   Memory type of the array being passed
193d1d35e2fSjeremylt   @param copy_mode  Copy mode for the array
1944cc79fe7SJed Brown   @param array      Array to be used, or NULL with @ref CEED_COPY_VALUES to have the
1950436c2adSjeremylt                       library allocate
1960436c2adSjeremylt 
1970436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
1980436c2adSjeremylt 
1997a982d89SJeremy L. Thompson   @ref User
2000436c2adSjeremylt **/
201d1d35e2fSjeremylt int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type,
202d1d35e2fSjeremylt                        CeedCopyMode copy_mode,
2030436c2adSjeremylt                        CeedScalar *array) {
2040436c2adSjeremylt   int ierr;
2050436c2adSjeremylt 
2060436c2adSjeremylt   if (!vec->SetArray)
2070436c2adSjeremylt     // LCOV_EXCL_START
208e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
209e15f9bd0SJeremy L Thompson                      "Backend does not support VectorSetArray");
2100436c2adSjeremylt   // LCOV_EXCL_STOP
2110436c2adSjeremylt 
2120436c2adSjeremylt   if (vec->state % 2 == 1)
213e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
214e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, the "
2150436c2adSjeremylt                      "access lock is already in use");
2160436c2adSjeremylt 
217d1d35e2fSjeremylt   if (vec->num_readers > 0)
218e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
219e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, a "
2200436c2adSjeremylt                      "process has read access");
2210436c2adSjeremylt 
222d1d35e2fSjeremylt   ierr = vec->SetArray(vec, mem_type, copy_mode, array); CeedChk(ierr);
2230436c2adSjeremylt   vec->state += 2;
224e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2250436c2adSjeremylt }
2260436c2adSjeremylt 
2270436c2adSjeremylt /**
2280436c2adSjeremylt   @brief Set the CeedVector to a constant value
2290436c2adSjeremylt 
2300436c2adSjeremylt   @param vec        CeedVector
2310436c2adSjeremylt   @param[in] value  Value to be used
2320436c2adSjeremylt 
2330436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2340436c2adSjeremylt 
2357a982d89SJeremy L. Thompson   @ref User
2360436c2adSjeremylt **/
2370436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) {
2380436c2adSjeremylt   int ierr;
2390436c2adSjeremylt 
2400436c2adSjeremylt   if (vec->state % 2 == 1)
241e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
242e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, the "
2430436c2adSjeremylt                      "access lock is already in use");
2440436c2adSjeremylt 
2450436c2adSjeremylt   if (vec->SetValue) {
2460436c2adSjeremylt     ierr = vec->SetValue(vec, value); CeedChk(ierr);
2470436c2adSjeremylt   } else {
2480436c2adSjeremylt     CeedScalar *array;
2490436c2adSjeremylt     ierr = CeedVectorGetArray(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
2500436c2adSjeremylt     for (int i=0; i<vec->length; i++) array[i] = value;
2510436c2adSjeremylt     ierr = CeedVectorRestoreArray(vec, &array); CeedChk(ierr);
2520436c2adSjeremylt   }
2530436c2adSjeremylt   vec->state += 2;
254e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2550436c2adSjeremylt }
2560436c2adSjeremylt 
2570436c2adSjeremylt /**
258b3cf021fSjeremylt   @brief Sync the CeedVector to a specified memtype. This function is used to
259b3cf021fSjeremylt            force synchronization of arrays set with @ref CeedVectorSetArray().
260b3cf021fSjeremylt            If the requested memtype is already synchronized, this function
261b3cf021fSjeremylt            results in a no-op.
2620436c2adSjeremylt 
2630436c2adSjeremylt   @param vec       CeedVector
264d1d35e2fSjeremylt   @param mem_type  Memtype to be synced
2650436c2adSjeremylt 
2660436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
2670436c2adSjeremylt 
2687a982d89SJeremy L. Thompson   @ref User
2690436c2adSjeremylt **/
270d1d35e2fSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) {
2710436c2adSjeremylt   int ierr;
2720436c2adSjeremylt 
2730436c2adSjeremylt   if (vec->state % 2 == 1)
274e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
275e15f9bd0SJeremy L Thompson                      "Cannot sync CeedVector, the access lock is "
2760436c2adSjeremylt                      "already in use");
2770436c2adSjeremylt 
2780436c2adSjeremylt   if (vec->SyncArray) {
279d1d35e2fSjeremylt     ierr = vec->SyncArray(vec, mem_type); CeedChk(ierr);
2800436c2adSjeremylt   } else {
2810436c2adSjeremylt     const CeedScalar *array;
282d1d35e2fSjeremylt     ierr = CeedVectorGetArrayRead(vec, mem_type, &array); CeedChk(ierr);
2830436c2adSjeremylt     ierr = CeedVectorRestoreArrayRead(vec, &array); CeedChk(ierr);
2840436c2adSjeremylt   }
285e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2860436c2adSjeremylt }
2870436c2adSjeremylt 
2880436c2adSjeremylt /**
2896a6c615bSJeremy L Thompson   @brief Take ownership of the CeedVector array and remove the array from the
29058921e9fSJeremy L Thompson            CeedVector. The caller is responsible for managing and freeing
29158921e9fSJeremy L Thompson            the array.
2926a6c615bSJeremy L Thompson 
2936a6c615bSJeremy L Thompson   @param vec         CeedVector
294d1d35e2fSjeremylt   @param mem_type    Memory type on which to take the array. If the backend
2956a6c615bSJeremy L Thompson                        uses a different memory type, this will perform a copy.
296d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type, or NULL if array pointer is
2976a6c615bSJeremy L Thompson                        not required
2986a6c615bSJeremy L Thompson 
2996a6c615bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3006a6c615bSJeremy L Thompson 
3016a6c615bSJeremy L Thompson   @ref User
3026a6c615bSJeremy L Thompson **/
303d1d35e2fSjeremylt int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type,
304d1d35e2fSjeremylt                         CeedScalar **array) {
3056a6c615bSJeremy L Thompson   int ierr;
3066a6c615bSJeremy L Thompson 
3076a6c615bSJeremy L Thompson   if (vec->state % 2 == 1)
3086a6c615bSJeremy L Thompson     // LCOV_EXCL_START
309e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
310e15f9bd0SJeremy L Thompson                      "Cannot take CeedVector array, the access "
3116a6c615bSJeremy L Thompson                      "lock is already in use");
3126a6c615bSJeremy L Thompson   // LCOV_EXCL_STOP
313d1d35e2fSjeremylt   if (vec->num_readers > 0)
3146a6c615bSJeremy L Thompson     // LCOV_EXCL_START
315e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
316e15f9bd0SJeremy L Thompson                      "Cannot take CeedVector array, a process "
3176a6c615bSJeremy L Thompson                      "has read access");
3186a6c615bSJeremy L Thompson   // LCOV_EXCL_STOP
3196a6c615bSJeremy L Thompson 
320d1d35e2fSjeremylt   CeedScalar *temp_array = NULL;
321d1d35e2fSjeremylt   ierr = vec->TakeArray(vec, mem_type, &temp_array); CeedChk(ierr);
322d1d35e2fSjeremylt   if (array) (*array) = temp_array;
323e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3246a6c615bSJeremy L Thompson }
3256a6c615bSJeremy L Thompson 
3266a6c615bSJeremy L Thompson /**
327b3cf021fSjeremylt   @brief Get read/write access to a CeedVector via the specified memory type.
328b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArray().
3290436c2adSjeremylt 
3300436c2adSjeremylt   @param vec         CeedVector to access
331d1d35e2fSjeremylt   @param mem_type    Memory type on which to access the array. If the backend
332b3cf021fSjeremylt                        uses a different memory type, this will perform a copy.
333d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type
3340436c2adSjeremylt 
3350436c2adSjeremylt   @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide
3360436c2adSjeremylt     access to array pointers in the desired memory space. Pairing get/restore
3370436c2adSjeremylt     allows the Vector to track access, thus knowing if norms or other
3380436c2adSjeremylt     operations may need to be recomputed.
3390436c2adSjeremylt 
3400436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3410436c2adSjeremylt 
3427a982d89SJeremy L. Thompson   @ref User
3430436c2adSjeremylt **/
344d1d35e2fSjeremylt int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type,
345d1d35e2fSjeremylt                        CeedScalar **array) {
3460436c2adSjeremylt   int ierr;
3470436c2adSjeremylt 
3480436c2adSjeremylt   if (!vec->GetArray)
3490436c2adSjeremylt     // LCOV_EXCL_START
350e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
351e15f9bd0SJeremy L Thompson                      "Backend does not support GetArray");
3520436c2adSjeremylt   // LCOV_EXCL_STOP
3530436c2adSjeremylt 
3540436c2adSjeremylt   if (vec->state % 2 == 1)
355e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
356e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, the "
3570436c2adSjeremylt                      "access lock is already in use");
3580436c2adSjeremylt 
359d1d35e2fSjeremylt   if (vec->num_readers > 0)
360e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
361e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector array access, a "
3620436c2adSjeremylt                      "process has read access");
3630436c2adSjeremylt 
364d1d35e2fSjeremylt   ierr = vec->GetArray(vec, mem_type, array); CeedChk(ierr);
3650436c2adSjeremylt   vec->state += 1;
366e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3670436c2adSjeremylt }
3680436c2adSjeremylt 
3690436c2adSjeremylt /**
370b3cf021fSjeremylt   @brief Get read-only access to a CeedVector via the specified memory type.
371b3cf021fSjeremylt            Restore access with @ref CeedVectorRestoreArrayRead().
3720436c2adSjeremylt 
3730436c2adSjeremylt   @param vec         CeedVector to access
374d1d35e2fSjeremylt   @param mem_type    Memory type on which to access the array.  If the backend
3750436c2adSjeremylt                        uses a different memory type, this will perform a copy
3760436c2adSjeremylt                        (possibly cached).
377d1d35e2fSjeremylt   @param[out] array  Array on memory type mem_type
3780436c2adSjeremylt 
3790436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
3800436c2adSjeremylt 
3817a982d89SJeremy L. Thompson   @ref User
3820436c2adSjeremylt **/
383d1d35e2fSjeremylt int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type,
3840436c2adSjeremylt                            const CeedScalar **array) {
3850436c2adSjeremylt   int ierr;
3860436c2adSjeremylt 
3870436c2adSjeremylt   if (!vec->GetArrayRead)
3880436c2adSjeremylt     // LCOV_EXCL_START
389e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
390e15f9bd0SJeremy L Thompson                      "Backend does not support GetArrayRead");
3910436c2adSjeremylt   // LCOV_EXCL_STOP
3920436c2adSjeremylt 
3930436c2adSjeremylt   if (vec->state % 2 == 1)
394e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
395e15f9bd0SJeremy L Thompson                      "Cannot grant CeedVector read-only array "
3960436c2adSjeremylt                      "access, the access lock is already in use");
3970436c2adSjeremylt 
398d1d35e2fSjeremylt   ierr = vec->GetArrayRead(vec, mem_type, array); CeedChk(ierr);
399d1d35e2fSjeremylt   vec->num_readers++;
400e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4010436c2adSjeremylt }
4020436c2adSjeremylt 
4030436c2adSjeremylt /**
404b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArray()
4050436c2adSjeremylt 
4060436c2adSjeremylt   @param vec    CeedVector to restore
4070436c2adSjeremylt   @param array  Array of vector data
4080436c2adSjeremylt 
4090436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4100436c2adSjeremylt 
4117a982d89SJeremy L. Thompson   @ref User
4120436c2adSjeremylt **/
4130436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) {
4140436c2adSjeremylt   int ierr;
4150436c2adSjeremylt 
4160436c2adSjeremylt   if (!vec->RestoreArray)
4170436c2adSjeremylt     // LCOV_EXCL_START
418e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
419e15f9bd0SJeremy L Thompson                      "Backend does not support RestoreArray");
4200436c2adSjeremylt   // LCOV_EXCL_STOP
4210436c2adSjeremylt 
4220436c2adSjeremylt   if (vec->state % 2 != 1)
423e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_ACCESS,
424e15f9bd0SJeremy L Thompson                      "Cannot restore CeedVector array access, "
4250436c2adSjeremylt                      "access was not granted");
4260436c2adSjeremylt 
4270436c2adSjeremylt   ierr = vec->RestoreArray(vec); CeedChk(ierr);
4280436c2adSjeremylt   *array = NULL;
4290436c2adSjeremylt   vec->state += 1;
430e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4310436c2adSjeremylt }
4320436c2adSjeremylt 
4330436c2adSjeremylt /**
434b3cf021fSjeremylt   @brief Restore an array obtained using @ref CeedVectorGetArrayRead()
4350436c2adSjeremylt 
4360436c2adSjeremylt   @param vec    CeedVector to restore
4370436c2adSjeremylt   @param array  Array of vector data
4380436c2adSjeremylt 
4390436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
4400436c2adSjeremylt 
4417a982d89SJeremy L. Thompson   @ref User
4420436c2adSjeremylt **/
4430436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) {
4440436c2adSjeremylt   int ierr;
4450436c2adSjeremylt 
4460436c2adSjeremylt   if (!vec->RestoreArrayRead)
4470436c2adSjeremylt     // LCOV_EXCL_START
448e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_UNSUPPORTED,
449e15f9bd0SJeremy L Thompson                      "Backend does not support RestoreArrayRead");
4500436c2adSjeremylt   // LCOV_EXCL_STOP
4510436c2adSjeremylt 
4520436c2adSjeremylt   ierr = vec->RestoreArrayRead(vec); CeedChk(ierr);
4530436c2adSjeremylt   *array = NULL;
454d1d35e2fSjeremylt   vec->num_readers--;
455e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4560436c2adSjeremylt }
4570436c2adSjeremylt 
4580436c2adSjeremylt /**
459171f8ca9Sjeremylt   @brief Get the norm of a CeedVector.
460171f8ca9Sjeremylt 
461171f8ca9Sjeremylt   Note: This operation is local to the CeedVector. This function will likely
462171f8ca9Sjeremylt           not provide the desired results for the norm of the libCEED portion
463171f8ca9Sjeremylt           of a parallel vector or a CeedVector with duplicated or hanging nodes.
464547d9b97Sjeremylt 
465547d9b97Sjeremylt   @param vec        CeedVector to retrieve maximum value
466d1d35e2fSjeremylt   @param norm_type  Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX
467547d9b97Sjeremylt   @param[out] norm  Variable to store norm value
468547d9b97Sjeremylt 
469547d9b97Sjeremylt   @return An error code: 0 - success, otherwise - failure
470547d9b97Sjeremylt 
4717a982d89SJeremy L. Thompson   @ref User
472547d9b97Sjeremylt **/
473d1d35e2fSjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType norm_type, CeedScalar *norm) {
474547d9b97Sjeremylt   int ierr;
475547d9b97Sjeremylt 
476547d9b97Sjeremylt   // Backend impl for GPU, if added
477547d9b97Sjeremylt   if (vec->Norm) {
478d1d35e2fSjeremylt     ierr = vec->Norm(vec, norm_type, norm); CeedChk(ierr);
479e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
480547d9b97Sjeremylt   }
481547d9b97Sjeremylt 
482547d9b97Sjeremylt   const CeedScalar *array;
483547d9b97Sjeremylt   ierr = CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
484547d9b97Sjeremylt 
485547d9b97Sjeremylt   *norm = 0.;
486d1d35e2fSjeremylt   switch (norm_type) {
487547d9b97Sjeremylt   case CEED_NORM_1:
488547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
489547d9b97Sjeremylt       *norm += fabs(array[i]);
490547d9b97Sjeremylt     }
491547d9b97Sjeremylt     break;
492547d9b97Sjeremylt   case CEED_NORM_2:
493547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
494547d9b97Sjeremylt       *norm += fabs(array[i])*fabs(array[i]);
495547d9b97Sjeremylt     }
496547d9b97Sjeremylt     break;
497547d9b97Sjeremylt   case CEED_NORM_MAX:
498547d9b97Sjeremylt     for (int i=0; i<vec->length; i++) {
499d1d35e2fSjeremylt       const CeedScalar abs_v_i = fabs(array[i]);
500d1d35e2fSjeremylt       *norm = *norm > abs_v_i ? *norm : abs_v_i;
501547d9b97Sjeremylt     }
502547d9b97Sjeremylt   }
503d1d35e2fSjeremylt   if (norm_type == CEED_NORM_2)
504547d9b97Sjeremylt     *norm = sqrt(*norm);
505547d9b97Sjeremylt 
506547d9b97Sjeremylt   ierr = CeedVectorRestoreArrayRead(vec, &array); CeedChk(ierr);
507e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
508547d9b97Sjeremylt }
509547d9b97Sjeremylt 
510547d9b97Sjeremylt /**
511d99fa3c5SJeremy L Thompson   @brief Take the reciprocal of a CeedVector.
512d99fa3c5SJeremy L Thompson 
513d99fa3c5SJeremy L Thompson   @param vec  CeedVector to take reciprocal
514d99fa3c5SJeremy L Thompson 
515d99fa3c5SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
516d99fa3c5SJeremy L Thompson 
517d99fa3c5SJeremy L Thompson   @ref User
518d99fa3c5SJeremy L Thompson **/
519d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) {
520d99fa3c5SJeremy L Thompson   int ierr;
521d99fa3c5SJeremy L Thompson 
522d99fa3c5SJeremy L Thompson   // Check if vector data set
523d99fa3c5SJeremy L Thompson   if (!vec->state)
524d99fa3c5SJeremy L Thompson     // LCOV_EXCL_START
525e15f9bd0SJeremy L Thompson     return CeedError(vec->ceed, CEED_ERROR_INCOMPLETE,
526d99fa3c5SJeremy L Thompson                      "CeedVector must have data set to take reciprocal");
527d99fa3c5SJeremy L Thompson   // LCOV_EXCL_STOP
528d99fa3c5SJeremy L Thompson 
529d99fa3c5SJeremy L Thompson   // Backend impl for GPU, if added
530d99fa3c5SJeremy L Thompson   if (vec->Reciprocal) {
531d99fa3c5SJeremy L Thompson     ierr = vec->Reciprocal(vec); CeedChk(ierr);
532e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
533d99fa3c5SJeremy L Thompson   }
534d99fa3c5SJeremy L Thompson 
535d99fa3c5SJeremy L Thompson   CeedInt len;
536d99fa3c5SJeremy L Thompson   ierr = CeedVectorGetLength(vec, &len); CeedChk(ierr);
537d99fa3c5SJeremy L Thompson   CeedScalar *array;
538d99fa3c5SJeremy L Thompson   ierr = CeedVectorGetArray(vec, CEED_MEM_HOST, &array); CeedChk(ierr);
539d99fa3c5SJeremy L Thompson   for (CeedInt i=0; i<len; i++)
540d99fa3c5SJeremy L Thompson     if (fabs(array[i]) > CEED_EPSILON)
541d99fa3c5SJeremy L Thompson       array[i] = 1./array[i];
542d99fa3c5SJeremy L Thompson 
543e15f9bd0SJeremy L Thompson   ierr = CeedVectorRestoreArray(vec, &array); CeedChk(ierr);
544e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
545d99fa3c5SJeremy L Thompson }
546d99fa3c5SJeremy L Thompson 
547d99fa3c5SJeremy L Thompson /**
5487a982d89SJeremy L. Thompson   @brief View a CeedVector
5490436c2adSjeremylt 
5500a0da059Sjeremylt   @param[in] vec     CeedVector to view
551d1d35e2fSjeremylt   @param[in] fp_fmt  Printing format
5520a0da059Sjeremylt   @param[in] stream  Filestream to write to
5530a0da059Sjeremylt 
5540436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5550436c2adSjeremylt 
5567a982d89SJeremy L. Thompson   @ref User
5570436c2adSjeremylt **/
558d1d35e2fSjeremylt int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) {
5597a982d89SJeremy L. Thompson   const CeedScalar *x;
5607a982d89SJeremy L. Thompson 
5617a982d89SJeremy L. Thompson   int ierr = CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x); CeedChk(ierr);
5627a982d89SJeremy L. Thompson 
5637a982d89SJeremy L. Thompson   char fmt[1024];
5647a982d89SJeremy L. Thompson   fprintf(stream, "CeedVector length %ld\n", (long)vec->length);
565d1d35e2fSjeremylt   snprintf(fmt, sizeof fmt, "  %s\n", fp_fmt ? fp_fmt : "%g");
5667a982d89SJeremy L. Thompson   for (CeedInt i=0; i<vec->length; i++)
5677a982d89SJeremy L. Thompson     fprintf(stream, fmt, x[i]);
5687a982d89SJeremy L. Thompson 
5697a982d89SJeremy L. Thompson   ierr = CeedVectorRestoreArrayRead(vec, &x); CeedChk(ierr);
570e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5710436c2adSjeremylt }
5720436c2adSjeremylt 
5730436c2adSjeremylt /**
5747a982d89SJeremy L. Thompson   @brief Get the length of a CeedVector
5750436c2adSjeremylt 
5767a982d89SJeremy L. Thompson   @param vec          CeedVector to retrieve length
5777a982d89SJeremy L. Thompson   @param[out] length  Variable to store length
5780436c2adSjeremylt 
5790436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5800436c2adSjeremylt 
5817a982d89SJeremy L. Thompson   @ref User
5820436c2adSjeremylt **/
5837a982d89SJeremy L. Thompson int CeedVectorGetLength(CeedVector vec, CeedInt *length) {
5847a982d89SJeremy L. Thompson   *length = vec->length;
585e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5860436c2adSjeremylt }
5870436c2adSjeremylt 
5880436c2adSjeremylt /**
5890436c2adSjeremylt   @brief Destroy a CeedVector
5900436c2adSjeremylt 
5910436c2adSjeremylt   @param vec  CeedVector to destroy
5920436c2adSjeremylt 
5930436c2adSjeremylt   @return An error code: 0 - success, otherwise - failure
5940436c2adSjeremylt 
5957a982d89SJeremy L. Thompson   @ref User
5960436c2adSjeremylt **/
5970436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) {
5980436c2adSjeremylt   int ierr;
5990436c2adSjeremylt 
600d1d35e2fSjeremylt   if (!*vec || --(*vec)->ref_count > 0) return CEED_ERROR_SUCCESS;
6010436c2adSjeremylt 
602187168c7SJeremy L Thompson   if (((*vec)->state % 2) == 1)
603e15f9bd0SJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS,
604187168c7SJeremy L Thompson                      "Cannot destroy CeedVector, the writable access "
6050436c2adSjeremylt                      "lock is in use");
6060436c2adSjeremylt 
607d1d35e2fSjeremylt   if ((*vec)->num_readers > 0)
608e15f9bd0SJeremy L Thompson     return CeedError((*vec)->ceed, CEED_ERROR_ACCESS,
609e15f9bd0SJeremy L Thompson                      "Cannot destroy CeedVector, a process has "
610187168c7SJeremy L Thompson                      "read access");
611187168c7SJeremy L Thompson 
6120436c2adSjeremylt   if ((*vec)->Destroy) {
6130436c2adSjeremylt     ierr = (*vec)->Destroy(*vec); CeedChk(ierr);
6140436c2adSjeremylt   }
6150436c2adSjeremylt 
6160436c2adSjeremylt   ierr = CeedDestroy(&(*vec)->ceed); CeedChk(ierr);
6170436c2adSjeremylt   ierr = CeedFree(vec); CeedChk(ierr);
618e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6190436c2adSjeremylt }
6200436c2adSjeremylt 
6210436c2adSjeremylt /// @}
622