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 170436c2adSjeremylt #include <ceed-impl.h> 180436c2adSjeremylt #include <ceed-backend.h> 19547d9b97Sjeremylt #include <math.h> 200436c2adSjeremylt 217a982d89SJeremy L. Thompson /// @file 227a982d89SJeremy L. Thompson /// Implementation of public CeedVector interfaces 237a982d89SJeremy L. Thompson 240436c2adSjeremylt /// @cond DOXYGEN_SKIP 250436c2adSjeremylt static struct CeedVector_private ceed_vector_active; 260436c2adSjeremylt static struct CeedVector_private ceed_vector_none; 270436c2adSjeremylt /// @endcond 280436c2adSjeremylt 297a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 307a982d89SJeremy L. Thompson /// @{ 317a982d89SJeremy L. Thompson 327a982d89SJeremy L. Thompson /// Indicate that vector will be provided as an explicit argument to 337a982d89SJeremy L. Thompson /// CeedOperatorApply(). 347a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_ACTIVE = &ceed_vector_active; 357a982d89SJeremy L. Thompson 364cc79fe7SJed Brown /// Indicate that no vector is applicable (i.e., for @ref CEED_EVAL_WEIGHTS). 377a982d89SJeremy L. Thompson const CeedVector CEED_VECTOR_NONE = &ceed_vector_none; 387a982d89SJeremy L. Thompson 397a982d89SJeremy L. Thompson /// @} 407a982d89SJeremy L. Thompson 417a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 427a982d89SJeremy L. Thompson /// CeedVector Backend API 437a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 447a982d89SJeremy L. Thompson /// @addtogroup CeedVectorBackend 457a982d89SJeremy L. Thompson /// @{ 467a982d89SJeremy L. Thompson 477a982d89SJeremy L. Thompson /** 487a982d89SJeremy L. Thompson @brief Get the Ceed associated with a CeedVector 497a982d89SJeremy L. Thompson 507a982d89SJeremy L. Thompson @param vec CeedVector to retrieve state 517a982d89SJeremy L. Thompson @param[out] ceed Variable to store ceed 527a982d89SJeremy L. Thompson 537a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 547a982d89SJeremy L. Thompson 557a982d89SJeremy L. Thompson @ref Backend 567a982d89SJeremy L. Thompson **/ 577a982d89SJeremy L. Thompson int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { 587a982d89SJeremy L. Thompson *ceed = vec->ceed; 597a982d89SJeremy L. Thompson return 0; 607a982d89SJeremy L. Thompson } 617a982d89SJeremy L. Thompson 627a982d89SJeremy L. Thompson /** 637a982d89SJeremy L. Thompson @brief Get the state of a CeedVector 647a982d89SJeremy L. Thompson 657a982d89SJeremy L. Thompson @param vec CeedVector to retrieve state 667a982d89SJeremy L. Thompson @param[out] state Variable to store state 677a982d89SJeremy L. Thompson 687a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 697a982d89SJeremy L. Thompson 707a982d89SJeremy L. Thompson @ref Backend 717a982d89SJeremy L. Thompson **/ 727a982d89SJeremy L. Thompson int CeedVectorGetState(CeedVector vec, uint64_t *state) { 737a982d89SJeremy L. Thompson *state = vec->state; 747a982d89SJeremy L. Thompson return 0; 757a982d89SJeremy L. Thompson } 767a982d89SJeremy L. Thompson 777a982d89SJeremy L. Thompson /** 787a982d89SJeremy L. Thompson @brief Add a refrence to a CeedVector 797a982d89SJeremy L. Thompson 805f67fadeSJeremy L Thompson @param[out] vec CeedVector to increment reference counter 817a982d89SJeremy L. Thompson 827a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 837a982d89SJeremy L. Thompson 847a982d89SJeremy L. Thompson @ref Backend 857a982d89SJeremy L. Thompson **/ 867a982d89SJeremy L. Thompson int CeedVectorAddReference(CeedVector vec) { 877a982d89SJeremy L. Thompson vec->refcount++; 887a982d89SJeremy L. Thompson return 0; 897a982d89SJeremy L. Thompson } 907a982d89SJeremy L. Thompson 917a982d89SJeremy L. Thompson /** 927a982d89SJeremy L. Thompson @brief Get the backend data of a CeedVector 937a982d89SJeremy L. Thompson 947a982d89SJeremy L. Thompson @param vec CeedVector to retrieve state 957a982d89SJeremy L. Thompson @param[out] data Variable to store data 967a982d89SJeremy L. Thompson 977a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 987a982d89SJeremy L. Thompson 997a982d89SJeremy L. Thompson @ref Backend 1007a982d89SJeremy L. Thompson **/ 101*777ff853SJeremy L Thompson int CeedVectorGetData(CeedVector vec, void *data) { 102*777ff853SJeremy L Thompson *(void **)data = vec->data; 1037a982d89SJeremy L. Thompson return 0; 1047a982d89SJeremy L. Thompson } 1057a982d89SJeremy L. Thompson 1067a982d89SJeremy L. Thompson /** 1077a982d89SJeremy L. Thompson @brief Set the backend data of a CeedVector 1087a982d89SJeremy L. Thompson 1097a982d89SJeremy L. Thompson @param[out] vec CeedVector to retrieve state 1107a982d89SJeremy L. Thompson @param data Data to set 1117a982d89SJeremy L. Thompson 1127a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1137a982d89SJeremy L. Thompson 1147a982d89SJeremy L. Thompson @ref Backend 1157a982d89SJeremy L. Thompson **/ 116*777ff853SJeremy L Thompson int CeedVectorSetData(CeedVector vec, void *data) { 117*777ff853SJeremy L Thompson vec->data = data; 1187a982d89SJeremy L. Thompson return 0; 1197a982d89SJeremy L. Thompson } 1207a982d89SJeremy L. Thompson 1217a982d89SJeremy L. Thompson /// @} 1227a982d89SJeremy L. Thompson 1237a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1247a982d89SJeremy L. Thompson /// CeedVector Public API 1257a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1267a982d89SJeremy L. Thompson /// @addtogroup CeedVectorUser 1270436c2adSjeremylt /// @{ 1280436c2adSjeremylt 1290436c2adSjeremylt /** 1300436c2adSjeremylt @brief Create a CeedVector of the specified length (does not allocate memory) 1310436c2adSjeremylt 1320436c2adSjeremylt @param ceed Ceed object where the CeedVector will be created 1330436c2adSjeremylt @param length Length of vector 1340436c2adSjeremylt @param[out] vec Address of the variable where the newly created 1350436c2adSjeremylt CeedVector will be stored 1360436c2adSjeremylt 1370436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 1380436c2adSjeremylt 1397a982d89SJeremy L. Thompson @ref User 1400436c2adSjeremylt **/ 1410436c2adSjeremylt int CeedVectorCreate(Ceed ceed, CeedInt length, CeedVector *vec) { 1420436c2adSjeremylt int ierr; 1430436c2adSjeremylt 1440436c2adSjeremylt if (!ceed->VectorCreate) { 1450436c2adSjeremylt Ceed delegate; 1460436c2adSjeremylt ierr = CeedGetObjectDelegate(ceed, &delegate, "Vector"); CeedChk(ierr); 1470436c2adSjeremylt 1480436c2adSjeremylt if (!delegate) 1490436c2adSjeremylt // LCOV_EXCL_START 1500436c2adSjeremylt return CeedError(ceed, 1, "Backend does not support VectorCreate"); 1510436c2adSjeremylt // LCOV_EXCL_STOP 1520436c2adSjeremylt 1530436c2adSjeremylt ierr = CeedVectorCreate(delegate, length, vec); CeedChk(ierr); 1540436c2adSjeremylt return 0; 1550436c2adSjeremylt } 1560436c2adSjeremylt 1570436c2adSjeremylt ierr = CeedCalloc(1,vec); CeedChk(ierr); 1580436c2adSjeremylt (*vec)->ceed = ceed; 1590436c2adSjeremylt ceed->refcount++; 1600436c2adSjeremylt (*vec)->refcount = 1; 1610436c2adSjeremylt (*vec)->length = length; 1620436c2adSjeremylt (*vec)->state = 0; 1630436c2adSjeremylt ierr = ceed->VectorCreate(length, *vec); CeedChk(ierr); 1640436c2adSjeremylt return 0; 1650436c2adSjeremylt } 1660436c2adSjeremylt 1670436c2adSjeremylt /** 1680436c2adSjeremylt @brief Set the array used by a CeedVector, freeing any previously allocated 169b3cf021fSjeremylt array if applicable. The backend may copy values to a different 170b3cf021fSjeremylt memtype, such as during @ref CeedOperatorApply(). 1716a6c615bSJeremy L Thompson See also @ref CeedVectorSyncArray() and @ref CeedVectorTakeArray(). 1720436c2adSjeremylt 1730436c2adSjeremylt @param vec CeedVector 1740436c2adSjeremylt @param mtype Memory type of the array being passed 1750436c2adSjeremylt @param cmode Copy mode for the array 1764cc79fe7SJed Brown @param array Array to be used, or NULL with @ref CEED_COPY_VALUES to have the 1770436c2adSjeremylt library allocate 1780436c2adSjeremylt 1790436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 1800436c2adSjeremylt 1817a982d89SJeremy L. Thompson @ref User 1820436c2adSjeremylt **/ 1830436c2adSjeremylt int CeedVectorSetArray(CeedVector vec, CeedMemType mtype, CeedCopyMode cmode, 1840436c2adSjeremylt CeedScalar *array) { 1850436c2adSjeremylt int ierr; 1860436c2adSjeremylt 1870436c2adSjeremylt if (!vec->SetArray) 1880436c2adSjeremylt // LCOV_EXCL_START 1890436c2adSjeremylt return CeedError(vec->ceed, 1, "Backend does not support VectorSetArray"); 1900436c2adSjeremylt // LCOV_EXCL_STOP 1910436c2adSjeremylt 1920436c2adSjeremylt if (vec->state % 2 == 1) 1930436c2adSjeremylt return CeedError(vec->ceed, 1, "Cannot grant CeedVector array access, the " 1940436c2adSjeremylt "access lock is already in use"); 1950436c2adSjeremylt 1960436c2adSjeremylt if (vec->numreaders > 0) 1970436c2adSjeremylt return CeedError(vec->ceed, 1, "Cannot grant CeedVector array access, a " 1980436c2adSjeremylt "process has read access"); 1990436c2adSjeremylt 2000436c2adSjeremylt ierr = vec->SetArray(vec, mtype, cmode, array); CeedChk(ierr); 2010436c2adSjeremylt vec->state += 2; 2020436c2adSjeremylt 2030436c2adSjeremylt return 0; 2040436c2adSjeremylt } 2050436c2adSjeremylt 2060436c2adSjeremylt /** 2070436c2adSjeremylt @brief Set the CeedVector to a constant value 2080436c2adSjeremylt 2090436c2adSjeremylt @param vec CeedVector 2100436c2adSjeremylt @param[in] value Value to be used 2110436c2adSjeremylt 2120436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2130436c2adSjeremylt 2147a982d89SJeremy L. Thompson @ref User 2150436c2adSjeremylt **/ 2160436c2adSjeremylt int CeedVectorSetValue(CeedVector vec, CeedScalar value) { 2170436c2adSjeremylt int ierr; 2180436c2adSjeremylt 2190436c2adSjeremylt if (vec->state % 2 == 1) 2200436c2adSjeremylt return CeedError(vec->ceed, 1, "Cannot grant CeedVector array access, the " 2210436c2adSjeremylt "access lock is already in use"); 2220436c2adSjeremylt 2230436c2adSjeremylt if (vec->SetValue) { 2240436c2adSjeremylt ierr = vec->SetValue(vec, value); CeedChk(ierr); 2250436c2adSjeremylt } else { 2260436c2adSjeremylt CeedScalar *array; 2270436c2adSjeremylt ierr = CeedVectorGetArray(vec, CEED_MEM_HOST, &array); CeedChk(ierr); 2280436c2adSjeremylt for (int i=0; i<vec->length; i++) array[i] = value; 2290436c2adSjeremylt ierr = CeedVectorRestoreArray(vec, &array); CeedChk(ierr); 2300436c2adSjeremylt } 2310436c2adSjeremylt 2320436c2adSjeremylt vec->state += 2; 2330436c2adSjeremylt 2340436c2adSjeremylt return 0; 2350436c2adSjeremylt } 2360436c2adSjeremylt 2370436c2adSjeremylt /** 238b3cf021fSjeremylt @brief Sync the CeedVector to a specified memtype. This function is used to 239b3cf021fSjeremylt force synchronization of arrays set with @ref CeedVectorSetArray(). 240b3cf021fSjeremylt If the requested memtype is already synchronized, this function 241b3cf021fSjeremylt results in a no-op. 2420436c2adSjeremylt 2430436c2adSjeremylt @param vec CeedVector 2440436c2adSjeremylt @param mtype Memtype to be synced 2450436c2adSjeremylt 2460436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 2470436c2adSjeremylt 2487a982d89SJeremy L. Thompson @ref User 2490436c2adSjeremylt **/ 2500436c2adSjeremylt int CeedVectorSyncArray(CeedVector vec, CeedMemType mtype) { 2510436c2adSjeremylt int ierr; 2520436c2adSjeremylt 2530436c2adSjeremylt if (vec->state % 2 == 1) 2540436c2adSjeremylt return CeedError(vec->ceed, 1, "Cannot sync CeedVector, the access lock is " 2550436c2adSjeremylt "already in use"); 2560436c2adSjeremylt 2570436c2adSjeremylt if (vec->SyncArray) { 2580436c2adSjeremylt ierr = vec->SyncArray(vec, mtype); CeedChk(ierr); 2590436c2adSjeremylt } else { 2600436c2adSjeremylt const CeedScalar *array; 2610436c2adSjeremylt ierr = CeedVectorGetArrayRead(vec, mtype, &array); CeedChk(ierr); 2620436c2adSjeremylt ierr = CeedVectorRestoreArrayRead(vec, &array); CeedChk(ierr); 2630436c2adSjeremylt } 2640436c2adSjeremylt 2650436c2adSjeremylt return 0; 2660436c2adSjeremylt } 2670436c2adSjeremylt 2680436c2adSjeremylt /** 2696a6c615bSJeremy L Thompson @brief Take ownership of the CeedVector array and remove the array from the 27058921e9fSJeremy L Thompson CeedVector. The caller is responsible for managing and freeing 27158921e9fSJeremy L Thompson the array. 2726a6c615bSJeremy L Thompson 2736a6c615bSJeremy L Thompson @param vec CeedVector 2746a6c615bSJeremy L Thompson @param mtype Memory type on which to take the array. If the backend 2756a6c615bSJeremy L Thompson uses a different memory type, this will perform a copy. 2766a6c615bSJeremy L Thompson @param[out] array Array on memory type mtype, or NULL if array pointer is 2776a6c615bSJeremy L Thompson not required 2786a6c615bSJeremy L Thompson 2796a6c615bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2806a6c615bSJeremy L Thompson 2816a6c615bSJeremy L Thompson @ref User 2826a6c615bSJeremy L Thompson **/ 2836a6c615bSJeremy L Thompson int CeedVectorTakeArray(CeedVector vec, CeedMemType mtype, CeedScalar **array) { 2846a6c615bSJeremy L Thompson int ierr; 2856a6c615bSJeremy L Thompson 2866a6c615bSJeremy L Thompson if (vec->state % 2 == 1) 2876a6c615bSJeremy L Thompson // LCOV_EXCL_START 2886a6c615bSJeremy L Thompson return CeedError(vec->ceed, 1, "Cannot take CeedVector array, the access " 2896a6c615bSJeremy L Thompson "lock is already in use"); 2906a6c615bSJeremy L Thompson // LCOV_EXCL_STOP 2916a6c615bSJeremy L Thompson if (vec->numreaders > 0) 2926a6c615bSJeremy L Thompson // LCOV_EXCL_START 2936a6c615bSJeremy L Thompson return CeedError(vec->ceed, 1, "Cannot take CeedVector array, a process " 2946a6c615bSJeremy L Thompson "has read access"); 2956a6c615bSJeremy L Thompson // LCOV_EXCL_STOP 2966a6c615bSJeremy L Thompson 2976a6c615bSJeremy L Thompson CeedScalar *tempArray = NULL; 2986a6c615bSJeremy L Thompson ierr = vec->TakeArray(vec, mtype, &tempArray); CeedChk(ierr); 2996a6c615bSJeremy L Thompson if (array) 3006a6c615bSJeremy L Thompson (*array) = tempArray; 3016a6c615bSJeremy L Thompson return 0; 3026a6c615bSJeremy L Thompson } 3036a6c615bSJeremy L Thompson 3046a6c615bSJeremy L Thompson /** 305b3cf021fSjeremylt @brief Get read/write access to a CeedVector via the specified memory type. 306b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArray(). 3070436c2adSjeremylt 3080436c2adSjeremylt @param vec CeedVector to access 3090436c2adSjeremylt @param mtype Memory type on which to access the array. If the backend 310b3cf021fSjeremylt uses a different memory type, this will perform a copy. 3110436c2adSjeremylt @param[out] array Array on memory type mtype 3120436c2adSjeremylt 3130436c2adSjeremylt @note The CeedVectorGetArray* and CeedVectorRestoreArray* functions provide 3140436c2adSjeremylt access to array pointers in the desired memory space. Pairing get/restore 3150436c2adSjeremylt allows the Vector to track access, thus knowing if norms or other 3160436c2adSjeremylt operations may need to be recomputed. 3170436c2adSjeremylt 3180436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3190436c2adSjeremylt 3207a982d89SJeremy L. Thompson @ref User 3210436c2adSjeremylt **/ 3220436c2adSjeremylt int CeedVectorGetArray(CeedVector vec, CeedMemType mtype, CeedScalar **array) { 3230436c2adSjeremylt int ierr; 3240436c2adSjeremylt 3250436c2adSjeremylt if (!vec->GetArray) 3260436c2adSjeremylt // LCOV_EXCL_START 3270436c2adSjeremylt return CeedError(vec->ceed, 1, "Backend does not support GetArray"); 3280436c2adSjeremylt // LCOV_EXCL_STOP 3290436c2adSjeremylt 3300436c2adSjeremylt if (vec->state % 2 == 1) 3310436c2adSjeremylt return CeedError(vec->ceed, 1, "Cannot grant CeedVector array access, the " 3320436c2adSjeremylt "access lock is already in use"); 3330436c2adSjeremylt 3340436c2adSjeremylt if (vec->numreaders > 0) 3350436c2adSjeremylt return CeedError(vec->ceed, 1, "Cannot grant CeedVector array access, a " 3360436c2adSjeremylt "process has read access"); 3370436c2adSjeremylt 3380436c2adSjeremylt ierr = vec->GetArray(vec, mtype, array); CeedChk(ierr); 3390436c2adSjeremylt vec->state += 1; 3400436c2adSjeremylt 3410436c2adSjeremylt return 0; 3420436c2adSjeremylt } 3430436c2adSjeremylt 3440436c2adSjeremylt /** 345b3cf021fSjeremylt @brief Get read-only access to a CeedVector via the specified memory type. 346b3cf021fSjeremylt Restore access with @ref CeedVectorRestoreArrayRead(). 3470436c2adSjeremylt 3480436c2adSjeremylt @param vec CeedVector to access 3490436c2adSjeremylt @param mtype Memory type on which to access the array. If the backend 3500436c2adSjeremylt uses a different memory type, this will perform a copy 3510436c2adSjeremylt (possibly cached). 3520436c2adSjeremylt @param[out] array Array on memory type mtype 3530436c2adSjeremylt 3540436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3550436c2adSjeremylt 3567a982d89SJeremy L. Thompson @ref User 3570436c2adSjeremylt **/ 3580436c2adSjeremylt int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mtype, 3590436c2adSjeremylt const CeedScalar **array) { 3600436c2adSjeremylt int ierr; 3610436c2adSjeremylt 3620436c2adSjeremylt if (!vec->GetArrayRead) 3630436c2adSjeremylt // LCOV_EXCL_START 3640436c2adSjeremylt return CeedError(vec->ceed, 1, "Backend does not support GetArrayRead"); 3650436c2adSjeremylt // LCOV_EXCL_STOP 3660436c2adSjeremylt 3670436c2adSjeremylt if (vec->state % 2 == 1) 3680436c2adSjeremylt return CeedError(vec->ceed, 1, "Cannot grant CeedVector read-only array " 3690436c2adSjeremylt "access, the access lock is already in use"); 3700436c2adSjeremylt 3710436c2adSjeremylt ierr = vec->GetArrayRead(vec, mtype, array); CeedChk(ierr); 3720436c2adSjeremylt vec->numreaders++; 3730436c2adSjeremylt 3740436c2adSjeremylt return 0; 3750436c2adSjeremylt } 3760436c2adSjeremylt 3770436c2adSjeremylt /** 378b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArray() 3790436c2adSjeremylt 3800436c2adSjeremylt @param vec CeedVector to restore 3810436c2adSjeremylt @param array Array of vector data 3820436c2adSjeremylt 3830436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 3840436c2adSjeremylt 3857a982d89SJeremy L. Thompson @ref User 3860436c2adSjeremylt **/ 3870436c2adSjeremylt int CeedVectorRestoreArray(CeedVector vec, CeedScalar **array) { 3880436c2adSjeremylt int ierr; 3890436c2adSjeremylt 3900436c2adSjeremylt if (!vec->RestoreArray) 3910436c2adSjeremylt // LCOV_EXCL_START 3920436c2adSjeremylt return CeedError(vec->ceed, 1, "Backend does not support RestoreArray"); 3930436c2adSjeremylt // LCOV_EXCL_STOP 3940436c2adSjeremylt 3950436c2adSjeremylt if (vec->state % 2 != 1) 3960436c2adSjeremylt return CeedError(vec->ceed, 1, "Cannot restore CeedVector array access, " 3970436c2adSjeremylt "access was not granted"); 3980436c2adSjeremylt 3990436c2adSjeremylt ierr = vec->RestoreArray(vec); CeedChk(ierr); 4000436c2adSjeremylt *array = NULL; 4010436c2adSjeremylt vec->state += 1; 4020436c2adSjeremylt 4030436c2adSjeremylt return 0; 4040436c2adSjeremylt } 4050436c2adSjeremylt 4060436c2adSjeremylt /** 407b3cf021fSjeremylt @brief Restore an array obtained using @ref CeedVectorGetArrayRead() 4080436c2adSjeremylt 4090436c2adSjeremylt @param vec CeedVector to restore 4100436c2adSjeremylt @param array Array of vector data 4110436c2adSjeremylt 4120436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 4130436c2adSjeremylt 4147a982d89SJeremy L. Thompson @ref User 4150436c2adSjeremylt **/ 4160436c2adSjeremylt int CeedVectorRestoreArrayRead(CeedVector vec, const CeedScalar **array) { 4170436c2adSjeremylt int ierr; 4180436c2adSjeremylt 4190436c2adSjeremylt if (!vec->RestoreArrayRead) 4200436c2adSjeremylt // LCOV_EXCL_START 4210436c2adSjeremylt return CeedError(vec->ceed, 1, "Backend does not support RestoreArrayRead"); 4220436c2adSjeremylt // LCOV_EXCL_STOP 4230436c2adSjeremylt 4240436c2adSjeremylt ierr = vec->RestoreArrayRead(vec); CeedChk(ierr); 4250436c2adSjeremylt *array = NULL; 4260436c2adSjeremylt vec->numreaders--; 4270436c2adSjeremylt 4280436c2adSjeremylt return 0; 4290436c2adSjeremylt } 4300436c2adSjeremylt 4310436c2adSjeremylt /** 432171f8ca9Sjeremylt @brief Get the norm of a CeedVector. 433171f8ca9Sjeremylt 434171f8ca9Sjeremylt Note: This operation is local to the CeedVector. This function will likely 435171f8ca9Sjeremylt not provide the desired results for the norm of the libCEED portion 436171f8ca9Sjeremylt of a parallel vector or a CeedVector with duplicated or hanging nodes. 437547d9b97Sjeremylt 438547d9b97Sjeremylt @param vec CeedVector to retrieve maximum value 4394cc79fe7SJed Brown @param type Norm type @ref CEED_NORM_1, @ref CEED_NORM_2, or @ref CEED_NORM_MAX 440547d9b97Sjeremylt @param[out] norm Variable to store norm value 441547d9b97Sjeremylt 442547d9b97Sjeremylt @return An error code: 0 - success, otherwise - failure 443547d9b97Sjeremylt 4447a982d89SJeremy L. Thompson @ref User 445547d9b97Sjeremylt **/ 446547d9b97Sjeremylt int CeedVectorNorm(CeedVector vec, CeedNormType type, CeedScalar *norm) { 447547d9b97Sjeremylt int ierr; 448547d9b97Sjeremylt 449547d9b97Sjeremylt // Backend impl for GPU, if added 450547d9b97Sjeremylt if (vec->Norm) { 451547d9b97Sjeremylt ierr = vec->Norm(vec, type, norm); CeedChk(ierr); 452547d9b97Sjeremylt return 0; 453547d9b97Sjeremylt } 454547d9b97Sjeremylt 455547d9b97Sjeremylt const CeedScalar *array; 456547d9b97Sjeremylt ierr = CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &array); CeedChk(ierr); 457547d9b97Sjeremylt 458547d9b97Sjeremylt *norm = 0.; 459547d9b97Sjeremylt switch (type) { 460547d9b97Sjeremylt case CEED_NORM_1: 461547d9b97Sjeremylt for (int i=0; i<vec->length; i++) { 462547d9b97Sjeremylt *norm += fabs(array[i]); 463547d9b97Sjeremylt } 464547d9b97Sjeremylt break; 465547d9b97Sjeremylt case CEED_NORM_2: 466547d9b97Sjeremylt for (int i=0; i<vec->length; i++) { 467547d9b97Sjeremylt *norm += fabs(array[i])*fabs(array[i]); 468547d9b97Sjeremylt } 469547d9b97Sjeremylt break; 470547d9b97Sjeremylt case CEED_NORM_MAX: 471547d9b97Sjeremylt for (int i=0; i<vec->length; i++) { 472547d9b97Sjeremylt const CeedScalar absi = fabs(array[i]); 473547d9b97Sjeremylt *norm = *norm > absi ? *norm : absi; 474547d9b97Sjeremylt } 475547d9b97Sjeremylt } 476547d9b97Sjeremylt if (type == CEED_NORM_2) 477547d9b97Sjeremylt *norm = sqrt(*norm); 478547d9b97Sjeremylt 479547d9b97Sjeremylt ierr = CeedVectorRestoreArrayRead(vec, &array); CeedChk(ierr); 480547d9b97Sjeremylt 481547d9b97Sjeremylt return 0; 482547d9b97Sjeremylt } 483547d9b97Sjeremylt 484547d9b97Sjeremylt /** 485d99fa3c5SJeremy L Thompson @brief Take the reciprocal of a CeedVector. 486d99fa3c5SJeremy L Thompson 487d99fa3c5SJeremy L Thompson @param vec CeedVector to take reciprocal 488d99fa3c5SJeremy L Thompson 489d99fa3c5SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 490d99fa3c5SJeremy L Thompson 491d99fa3c5SJeremy L Thompson @ref User 492d99fa3c5SJeremy L Thompson **/ 493d99fa3c5SJeremy L Thompson int CeedVectorReciprocal(CeedVector vec) { 494d99fa3c5SJeremy L Thompson int ierr; 495d99fa3c5SJeremy L Thompson 496d99fa3c5SJeremy L Thompson // Check if vector data set 497d99fa3c5SJeremy L Thompson if (!vec->state) 498d99fa3c5SJeremy L Thompson // LCOV_EXCL_START 499d99fa3c5SJeremy L Thompson return CeedError(vec->ceed, 1, 500d99fa3c5SJeremy L Thompson "CeedVector must have data set to take reciprocal"); 501d99fa3c5SJeremy L Thompson // LCOV_EXCL_STOP 502d99fa3c5SJeremy L Thompson 503d99fa3c5SJeremy L Thompson // Backend impl for GPU, if added 504d99fa3c5SJeremy L Thompson if (vec->Reciprocal) { 505d99fa3c5SJeremy L Thompson ierr = vec->Reciprocal(vec); CeedChk(ierr); 506d99fa3c5SJeremy L Thompson return 0; 507d99fa3c5SJeremy L Thompson } 508d99fa3c5SJeremy L Thompson 509d99fa3c5SJeremy L Thompson CeedInt len; 510d99fa3c5SJeremy L Thompson ierr = CeedVectorGetLength(vec, &len); CeedChk(ierr); 511d99fa3c5SJeremy L Thompson CeedScalar *array; 512d99fa3c5SJeremy L Thompson ierr = CeedVectorGetArray(vec, CEED_MEM_HOST, &array); CeedChk(ierr); 513d99fa3c5SJeremy L Thompson for (CeedInt i=0; i<len; i++) 514d99fa3c5SJeremy L Thompson if (fabs(array[i]) > CEED_EPSILON) 515d99fa3c5SJeremy L Thompson array[i] = 1./array[i]; 516d99fa3c5SJeremy L Thompson ierr = CeedVectorRestoreArray(vec, &array); CeedChk(ierr); 517d99fa3c5SJeremy L Thompson 518d99fa3c5SJeremy L Thompson return 0; 519d99fa3c5SJeremy L Thompson } 520d99fa3c5SJeremy L Thompson 521d99fa3c5SJeremy L Thompson /** 5227a982d89SJeremy L. Thompson @brief View a CeedVector 5230436c2adSjeremylt 5240a0da059Sjeremylt @param[in] vec CeedVector to view 5250a0da059Sjeremylt @param[in] fpfmt Printing format 5260a0da059Sjeremylt @param[in] stream Filestream to write to 5270a0da059Sjeremylt 5280436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5290436c2adSjeremylt 5307a982d89SJeremy L. Thompson @ref User 5310436c2adSjeremylt **/ 5327a982d89SJeremy L. Thompson int CeedVectorView(CeedVector vec, const char *fpfmt, FILE *stream) { 5337a982d89SJeremy L. Thompson const CeedScalar *x; 5347a982d89SJeremy L. Thompson 5357a982d89SJeremy L. Thompson int ierr = CeedVectorGetArrayRead(vec, CEED_MEM_HOST, &x); CeedChk(ierr); 5367a982d89SJeremy L. Thompson 5377a982d89SJeremy L. Thompson char fmt[1024]; 5387a982d89SJeremy L. Thompson fprintf(stream, "CeedVector length %ld\n", (long)vec->length); 5397a982d89SJeremy L. Thompson snprintf(fmt, sizeof fmt, " %s\n", fpfmt ? fpfmt : "%g"); 5407a982d89SJeremy L. Thompson for (CeedInt i=0; i<vec->length; i++) 5417a982d89SJeremy L. Thompson fprintf(stream, fmt, x[i]); 5427a982d89SJeremy L. Thompson 5437a982d89SJeremy L. Thompson ierr = CeedVectorRestoreArrayRead(vec, &x); CeedChk(ierr); 5447a982d89SJeremy L. Thompson 5450436c2adSjeremylt return 0; 5460436c2adSjeremylt } 5470436c2adSjeremylt 5480436c2adSjeremylt /** 5497a982d89SJeremy L. Thompson @brief Get the length of a CeedVector 5500436c2adSjeremylt 5517a982d89SJeremy L. Thompson @param vec CeedVector to retrieve length 5527a982d89SJeremy L. Thompson @param[out] length Variable to store length 5530436c2adSjeremylt 5540436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5550436c2adSjeremylt 5567a982d89SJeremy L. Thompson @ref User 5570436c2adSjeremylt **/ 5587a982d89SJeremy L. Thompson int CeedVectorGetLength(CeedVector vec, CeedInt *length) { 5597a982d89SJeremy L. Thompson *length = vec->length; 5600436c2adSjeremylt return 0; 5610436c2adSjeremylt } 5620436c2adSjeremylt 5630436c2adSjeremylt /** 5640436c2adSjeremylt @brief Destroy a CeedVector 5650436c2adSjeremylt 5660436c2adSjeremylt @param vec CeedVector to destroy 5670436c2adSjeremylt 5680436c2adSjeremylt @return An error code: 0 - success, otherwise - failure 5690436c2adSjeremylt 5707a982d89SJeremy L. Thompson @ref User 5710436c2adSjeremylt **/ 5720436c2adSjeremylt int CeedVectorDestroy(CeedVector *vec) { 5730436c2adSjeremylt int ierr; 5740436c2adSjeremylt 575752c3701SJeremy L Thompson if (!*vec || --(*vec)->refcount > 0) return 0; 5760436c2adSjeremylt 5770436c2adSjeremylt if ((*vec) && ((*vec)->state % 2) == 1) 5780436c2adSjeremylt return CeedError((*vec)->ceed, 1, "Cannot destroy CeedVector, the access " 5790436c2adSjeremylt "lock is in use"); 5800436c2adSjeremylt 5810436c2adSjeremylt if ((*vec)->Destroy) { 5820436c2adSjeremylt ierr = (*vec)->Destroy(*vec); CeedChk(ierr); 5830436c2adSjeremylt } 5840436c2adSjeremylt 5850436c2adSjeremylt ierr = CeedDestroy(&(*vec)->ceed); CeedChk(ierr); 5860436c2adSjeremylt ierr = CeedFree(vec); CeedChk(ierr); 5870436c2adSjeremylt 5880436c2adSjeremylt return 0; 5890436c2adSjeremylt } 5900436c2adSjeremylt 5910436c2adSjeremylt /// @} 592