13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 30d0321e0SJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 50d0321e0SJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 70d0321e0SJeremy L Thompson 80d0321e0SJeremy L Thompson #include <ceed/ceed.h> 90d0321e0SJeremy L Thompson #include <ceed/backend.h> 100d0321e0SJeremy L Thompson #include <hip/hip_runtime.h> 110d0321e0SJeremy L Thompson #include <hipblas.h> 120d0321e0SJeremy L Thompson #include <math.h> 130d0321e0SJeremy L Thompson #include <string.h> 140d0321e0SJeremy L Thompson #include "ceed-hip-ref.h" 150d0321e0SJeremy L Thompson 16*f48ed27dSnbeams 17*f48ed27dSnbeams //------------------------------------------------------------------------------ 18*f48ed27dSnbeams // Check if host/device sync is needed 19*f48ed27dSnbeams //------------------------------------------------------------------------------ 20*f48ed27dSnbeams static inline int CeedVectorNeedSync_Hip(const CeedVector vec, 21*f48ed27dSnbeams CeedMemType mem_type, bool *need_sync) { 22*f48ed27dSnbeams int ierr; 23*f48ed27dSnbeams CeedVector_Hip *impl; 24*f48ed27dSnbeams ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 25*f48ed27dSnbeams 26*f48ed27dSnbeams bool has_valid_array = false; 27*f48ed27dSnbeams ierr = CeedVectorHasValidArray(vec, &has_valid_array); CeedChkBackend(ierr); 28*f48ed27dSnbeams switch (mem_type) { 29*f48ed27dSnbeams case CEED_MEM_HOST: 30*f48ed27dSnbeams *need_sync = has_valid_array && !impl->h_array; 31*f48ed27dSnbeams break; 32*f48ed27dSnbeams case CEED_MEM_DEVICE: 33*f48ed27dSnbeams *need_sync = has_valid_array && !impl->d_array; 34*f48ed27dSnbeams break; 35*f48ed27dSnbeams } 36*f48ed27dSnbeams 37*f48ed27dSnbeams return CEED_ERROR_SUCCESS; 38*f48ed27dSnbeams } 39*f48ed27dSnbeams 400d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 410d0321e0SJeremy L Thompson // Sync host to device 420d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 430d0321e0SJeremy L Thompson static inline int CeedVectorSyncH2D_Hip(const CeedVector vec) { 440d0321e0SJeremy L Thompson int ierr; 450d0321e0SJeremy L Thompson Ceed ceed; 460d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 470d0321e0SJeremy L Thompson CeedVector_Hip *impl; 480d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 490d0321e0SJeremy L Thompson 50539ec17dSJeremy L Thompson CeedSize length; 51539ec17dSJeremy L Thompson ierr = CeedVectorGetLength(vec, &length); CeedChkBackend(ierr); 52539ec17dSJeremy L Thompson size_t bytes = length * sizeof(CeedScalar); 53539ec17dSJeremy L Thompson 540d0321e0SJeremy L Thompson if (!impl->h_array) 550d0321e0SJeremy L Thompson // LCOV_EXCL_START 560d0321e0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, 570d0321e0SJeremy L Thompson "No valid host data to sync to device"); 580d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 590d0321e0SJeremy L Thompson 600d0321e0SJeremy L Thompson if (impl->d_array_borrowed) { 610d0321e0SJeremy L Thompson impl->d_array = impl->d_array_borrowed; 620d0321e0SJeremy L Thompson } else if (impl->d_array_owned) { 630d0321e0SJeremy L Thompson impl->d_array = impl->d_array_owned; 640d0321e0SJeremy L Thompson } else { 65539ec17dSJeremy L Thompson ierr = hipMalloc((void **)&impl->d_array_owned, bytes); 660d0321e0SJeremy L Thompson CeedChk_Hip(ceed, ierr); 670d0321e0SJeremy L Thompson impl->d_array = impl->d_array_owned; 680d0321e0SJeremy L Thompson } 690d0321e0SJeremy L Thompson 70539ec17dSJeremy L Thompson ierr = hipMemcpy(impl->d_array, impl->h_array, bytes, 710d0321e0SJeremy L Thompson hipMemcpyHostToDevice); CeedChk_Hip(ceed, ierr); 720d0321e0SJeremy L Thompson 730d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 740d0321e0SJeremy L Thompson } 750d0321e0SJeremy L Thompson 760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 770d0321e0SJeremy L Thompson // Sync device to host 780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 790d0321e0SJeremy L Thompson static inline int CeedVectorSyncD2H_Hip(const CeedVector vec) { 800d0321e0SJeremy L Thompson int ierr; 810d0321e0SJeremy L Thompson Ceed ceed; 820d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 830d0321e0SJeremy L Thompson CeedVector_Hip *impl; 840d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 850d0321e0SJeremy L Thompson 860d0321e0SJeremy L Thompson if (!impl->d_array) 870d0321e0SJeremy L Thompson // LCOV_EXCL_START 880d0321e0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, 890d0321e0SJeremy L Thompson "No valid device data to sync to host"); 900d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 910d0321e0SJeremy L Thompson 920d0321e0SJeremy L Thompson if (impl->h_array_borrowed) { 930d0321e0SJeremy L Thompson impl->h_array = impl->h_array_borrowed; 940d0321e0SJeremy L Thompson } else if (impl->h_array_owned) { 950d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 960d0321e0SJeremy L Thompson } else { 971f9221feSJeremy L Thompson CeedSize length; 980d0321e0SJeremy L Thompson ierr = CeedVectorGetLength(vec, &length); CeedChkBackend(ierr); 990d0321e0SJeremy L Thompson ierr = CeedCalloc(length, &impl->h_array_owned); CeedChkBackend(ierr); 1000d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 1010d0321e0SJeremy L Thompson } 1020d0321e0SJeremy L Thompson 103539ec17dSJeremy L Thompson CeedSize length; 104539ec17dSJeremy L Thompson ierr = CeedVectorGetLength(vec, &length); CeedChkBackend(ierr); 105539ec17dSJeremy L Thompson size_t bytes = length * sizeof(CeedScalar); 106539ec17dSJeremy L Thompson ierr = hipMemcpy(impl->h_array, impl->d_array, bytes, 1070d0321e0SJeremy L Thompson hipMemcpyDeviceToHost); CeedChk_Hip(ceed, ierr); 1080d0321e0SJeremy L Thompson 1090d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1100d0321e0SJeremy L Thompson } 1110d0321e0SJeremy L Thompson 1120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1130d0321e0SJeremy L Thompson // Sync arrays 1140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 115*f48ed27dSnbeams static int CeedVectorSyncArray_Hip(const CeedVector vec, 11643c928f4SJeremy L Thompson CeedMemType mem_type) { 117*f48ed27dSnbeams int ierr; 118*f48ed27dSnbeams // Check whether device/host sync is needed 119*f48ed27dSnbeams bool need_sync = false; 120*f48ed27dSnbeams ierr = CeedVectorNeedSync_Hip(vec, mem_type, &need_sync); 121*f48ed27dSnbeams CeedChkBackend(ierr); 122*f48ed27dSnbeams if (!need_sync) 123*f48ed27dSnbeams return CEED_ERROR_SUCCESS; 124*f48ed27dSnbeams 12543c928f4SJeremy L Thompson switch (mem_type) { 1260d0321e0SJeremy L Thompson case CEED_MEM_HOST: return CeedVectorSyncD2H_Hip(vec); 1270d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: return CeedVectorSyncH2D_Hip(vec); 1280d0321e0SJeremy L Thompson } 1290d0321e0SJeremy L Thompson return CEED_ERROR_UNSUPPORTED; 1300d0321e0SJeremy L Thompson } 1310d0321e0SJeremy L Thompson 1320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1330d0321e0SJeremy L Thompson // Set all pointers as invalid 1340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1350d0321e0SJeremy L Thompson static inline int CeedVectorSetAllInvalid_Hip(const CeedVector vec) { 1360d0321e0SJeremy L Thompson int ierr; 1370d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1380d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 1390d0321e0SJeremy L Thompson 1400d0321e0SJeremy L Thompson impl->h_array = NULL; 1410d0321e0SJeremy L Thompson impl->d_array = NULL; 1420d0321e0SJeremy L Thompson 1430d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1440d0321e0SJeremy L Thompson } 1450d0321e0SJeremy L Thompson 1460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1470d0321e0SJeremy L Thompson // Check if CeedVector has any valid pointers 1480d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1490d0321e0SJeremy L Thompson static inline int CeedVectorHasValidArray_Hip(const CeedVector vec, 1500d0321e0SJeremy L Thompson bool *has_valid_array) { 1510d0321e0SJeremy L Thompson int ierr; 1520d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1530d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 1540d0321e0SJeremy L Thompson 1550d0321e0SJeremy L Thompson *has_valid_array = !!impl->h_array || !!impl->d_array; 1560d0321e0SJeremy L Thompson 1570d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1580d0321e0SJeremy L Thompson } 1590d0321e0SJeremy L Thompson 1600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1610d0321e0SJeremy L Thompson // Check if has any array of given type 1620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1630d0321e0SJeremy L Thompson static inline int CeedVectorHasArrayOfType_Hip(const CeedVector vec, 16443c928f4SJeremy L Thompson CeedMemType mem_type, bool *has_array_of_type) { 1650d0321e0SJeremy L Thompson int ierr; 1660d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1670d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 1680d0321e0SJeremy L Thompson 16943c928f4SJeremy L Thompson switch (mem_type) { 1700d0321e0SJeremy L Thompson case CEED_MEM_HOST: 1710d0321e0SJeremy L Thompson *has_array_of_type = !!impl->h_array_borrowed || !!impl->h_array_owned; 1720d0321e0SJeremy L Thompson break; 1730d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 1740d0321e0SJeremy L Thompson *has_array_of_type = !!impl->d_array_borrowed || !!impl->d_array_owned; 1750d0321e0SJeremy L Thompson break; 1760d0321e0SJeremy L Thompson } 1770d0321e0SJeremy L Thompson 1780d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1790d0321e0SJeremy L Thompson } 1800d0321e0SJeremy L Thompson 1810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1820d0321e0SJeremy L Thompson // Check if has borrowed array of given type 1830d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1840d0321e0SJeremy L Thompson static inline int CeedVectorHasBorrowedArrayOfType_Hip(const CeedVector vec, 18543c928f4SJeremy L Thompson CeedMemType mem_type, bool *has_borrowed_array_of_type) { 1860d0321e0SJeremy L Thompson int ierr; 1870d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1880d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 1890d0321e0SJeremy L Thompson 19043c928f4SJeremy L Thompson switch (mem_type) { 1910d0321e0SJeremy L Thompson case CEED_MEM_HOST: 1920d0321e0SJeremy L Thompson *has_borrowed_array_of_type = !!impl->h_array_borrowed; 1930d0321e0SJeremy L Thompson break; 1940d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 1950d0321e0SJeremy L Thompson *has_borrowed_array_of_type = !!impl->d_array_borrowed; 1960d0321e0SJeremy L Thompson break; 1970d0321e0SJeremy L Thompson } 1980d0321e0SJeremy L Thompson 1990d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2000d0321e0SJeremy L Thompson } 2010d0321e0SJeremy L Thompson 2020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2030d0321e0SJeremy L Thompson // Set array from host 2040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2050d0321e0SJeremy L Thompson static int CeedVectorSetArrayHost_Hip(const CeedVector vec, 20643c928f4SJeremy L Thompson const CeedCopyMode copy_mode, CeedScalar *array) { 2070d0321e0SJeremy L Thompson int ierr; 2080d0321e0SJeremy L Thompson CeedVector_Hip *impl; 2090d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 2100d0321e0SJeremy L Thompson 21143c928f4SJeremy L Thompson switch (copy_mode) { 2120d0321e0SJeremy L Thompson case CEED_COPY_VALUES: { 2131f9221feSJeremy L Thompson CeedSize length; 2140d0321e0SJeremy L Thompson if (!impl->h_array_owned) { 2150d0321e0SJeremy L Thompson ierr = CeedVectorGetLength(vec, &length); CeedChkBackend(ierr); 2160d0321e0SJeremy L Thompson ierr = CeedMalloc(length, &impl->h_array_owned); CeedChkBackend(ierr); 2170d0321e0SJeremy L Thompson } 2180d0321e0SJeremy L Thompson impl->h_array_borrowed = NULL; 2190d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 220539ec17dSJeremy L Thompson if (array) { 221539ec17dSJeremy L Thompson CeedSize length; 222539ec17dSJeremy L Thompson ierr = CeedVectorGetLength(vec, &length); CeedChkBackend(ierr); 223539ec17dSJeremy L Thompson size_t bytes = length * sizeof(CeedScalar); 224539ec17dSJeremy L Thompson memcpy(impl->h_array, array, bytes); 225539ec17dSJeremy L Thompson } 2260d0321e0SJeremy L Thompson } break; 2270d0321e0SJeremy L Thompson case CEED_OWN_POINTER: 2280d0321e0SJeremy L Thompson ierr = CeedFree(&impl->h_array_owned); CeedChkBackend(ierr); 2290d0321e0SJeremy L Thompson impl->h_array_owned = array; 2300d0321e0SJeremy L Thompson impl->h_array_borrowed = NULL; 2310d0321e0SJeremy L Thompson impl->h_array = array; 2320d0321e0SJeremy L Thompson break; 2330d0321e0SJeremy L Thompson case CEED_USE_POINTER: 2340d0321e0SJeremy L Thompson ierr = CeedFree(&impl->h_array_owned); CeedChkBackend(ierr); 2350d0321e0SJeremy L Thompson impl->h_array_borrowed = array; 2360d0321e0SJeremy L Thompson impl->h_array = array; 2370d0321e0SJeremy L Thompson break; 2380d0321e0SJeremy L Thompson } 2390d0321e0SJeremy L Thompson 2400d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2410d0321e0SJeremy L Thompson } 2420d0321e0SJeremy L Thompson 2430d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2440d0321e0SJeremy L Thompson // Set array from device 2450d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2460d0321e0SJeremy L Thompson static int CeedVectorSetArrayDevice_Hip(const CeedVector vec, 24743c928f4SJeremy L Thompson const CeedCopyMode copy_mode, CeedScalar *array) { 2480d0321e0SJeremy L Thompson int ierr; 2490d0321e0SJeremy L Thompson Ceed ceed; 2500d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 2510d0321e0SJeremy L Thompson CeedVector_Hip *impl; 2520d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 2530d0321e0SJeremy L Thompson 25443c928f4SJeremy L Thompson switch (copy_mode) { 255539ec17dSJeremy L Thompson case CEED_COPY_VALUES: { 256539ec17dSJeremy L Thompson CeedSize length; 257539ec17dSJeremy L Thompson ierr = CeedVectorGetLength(vec, &length); CeedChkBackend(ierr); 258539ec17dSJeremy L Thompson size_t bytes = length * sizeof(CeedScalar); 2590d0321e0SJeremy L Thompson if (!impl->d_array_owned) { 260539ec17dSJeremy L Thompson ierr = hipMalloc((void **)&impl->d_array_owned, bytes); 2610d0321e0SJeremy L Thompson CeedChk_Hip(ceed, ierr); 2620d0321e0SJeremy L Thompson } 2630d0321e0SJeremy L Thompson impl->d_array_borrowed = NULL; 2640d0321e0SJeremy L Thompson impl->d_array = impl->d_array_owned; 2650d0321e0SJeremy L Thompson if (array) { 266539ec17dSJeremy L Thompson ierr = hipMemcpy(impl->d_array, array, bytes, 2670d0321e0SJeremy L Thompson hipMemcpyDeviceToDevice); CeedChk_Hip(ceed, ierr); 2680d0321e0SJeremy L Thompson } 269539ec17dSJeremy L Thompson } break; 2700d0321e0SJeremy L Thompson case CEED_OWN_POINTER: 2710d0321e0SJeremy L Thompson ierr = hipFree(impl->d_array_owned); CeedChk_Hip(ceed, ierr); 2720d0321e0SJeremy L Thompson impl->d_array_owned = array; 2730d0321e0SJeremy L Thompson impl->d_array_borrowed = NULL; 2740d0321e0SJeremy L Thompson impl->d_array = array; 2750d0321e0SJeremy L Thompson break; 2760d0321e0SJeremy L Thompson case CEED_USE_POINTER: 2770d0321e0SJeremy L Thompson ierr = hipFree(impl->d_array_owned); CeedChk_Hip(ceed, ierr); 2780d0321e0SJeremy L Thompson impl->d_array_owned = NULL; 2790d0321e0SJeremy L Thompson impl->d_array_borrowed = array; 2800d0321e0SJeremy L Thompson impl->d_array = array; 2810d0321e0SJeremy L Thompson break; 2820d0321e0SJeremy L Thompson } 2830d0321e0SJeremy L Thompson 2840d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2850d0321e0SJeremy L Thompson } 2860d0321e0SJeremy L Thompson 2870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2880d0321e0SJeremy L Thompson // Set the array used by a vector, 2890d0321e0SJeremy L Thompson // freeing any previously allocated array if applicable 2900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 29143c928f4SJeremy L Thompson static int CeedVectorSetArray_Hip(const CeedVector vec, 29243c928f4SJeremy L Thompson const CeedMemType mem_type, 29343c928f4SJeremy L Thompson const CeedCopyMode copy_mode, CeedScalar *array) { 2940d0321e0SJeremy L Thompson int ierr; 2950d0321e0SJeremy L Thompson Ceed ceed; 2960d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 2970d0321e0SJeremy L Thompson CeedVector_Hip *impl; 2980d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 2990d0321e0SJeremy L Thompson 3000d0321e0SJeremy L Thompson ierr = CeedVectorSetAllInvalid_Hip(vec); CeedChkBackend(ierr); 30143c928f4SJeremy L Thompson switch (mem_type) { 3020d0321e0SJeremy L Thompson case CEED_MEM_HOST: 30343c928f4SJeremy L Thompson return CeedVectorSetArrayHost_Hip(vec, copy_mode, array); 3040d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 30543c928f4SJeremy L Thompson return CeedVectorSetArrayDevice_Hip(vec, copy_mode, array); 3060d0321e0SJeremy L Thompson } 3070d0321e0SJeremy L Thompson 3080d0321e0SJeremy L Thompson return CEED_ERROR_UNSUPPORTED; 3090d0321e0SJeremy L Thompson } 3100d0321e0SJeremy L Thompson 3110d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3120d0321e0SJeremy L Thompson // Set host array to value 3130d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3140d0321e0SJeremy L Thompson static int CeedHostSetValue_Hip(CeedScalar *h_array, CeedInt length, 3150d0321e0SJeremy L Thompson CeedScalar val) { 3160d0321e0SJeremy L Thompson for (int i = 0; i < length; i++) 3170d0321e0SJeremy L Thompson h_array[i] = val; 3180d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3190d0321e0SJeremy L Thompson } 3200d0321e0SJeremy L Thompson 3210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3220d0321e0SJeremy L Thompson // Set device array to value (impl in .hip file) 3230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3240d0321e0SJeremy L Thompson int CeedDeviceSetValue_Hip(CeedScalar *d_array, CeedInt length, CeedScalar val); 3250d0321e0SJeremy L Thompson 3260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3270d0321e0SJeremy L Thompson // Set a vector to a value, 3280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3290d0321e0SJeremy L Thompson static int CeedVectorSetValue_Hip(CeedVector vec, CeedScalar val) { 3300d0321e0SJeremy L Thompson int ierr; 3310d0321e0SJeremy L Thompson Ceed ceed; 3320d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 3330d0321e0SJeremy L Thompson CeedVector_Hip *impl; 3340d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 3351f9221feSJeremy L Thompson CeedSize length; 3360d0321e0SJeremy L Thompson ierr = CeedVectorGetLength(vec, &length); CeedChkBackend(ierr); 3370d0321e0SJeremy L Thompson 3380d0321e0SJeremy L Thompson // Set value for synced device/host array 3390d0321e0SJeremy L Thompson if (!impl->d_array && !impl->h_array) { 3400d0321e0SJeremy L Thompson if (impl->d_array_borrowed) { 3410d0321e0SJeremy L Thompson impl->d_array = impl->d_array_borrowed; 3420d0321e0SJeremy L Thompson } else if (impl->h_array_borrowed) { 3430d0321e0SJeremy L Thompson impl->h_array = impl->h_array_borrowed; 3440d0321e0SJeremy L Thompson } else if (impl->d_array_owned) { 3450d0321e0SJeremy L Thompson impl->d_array = impl->d_array_owned; 3460d0321e0SJeremy L Thompson } else if (impl->h_array_owned) { 3470d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 3480d0321e0SJeremy L Thompson } else { 3490d0321e0SJeremy L Thompson ierr = CeedVectorSetArray(vec, CEED_MEM_DEVICE, CEED_COPY_VALUES, NULL); 3500d0321e0SJeremy L Thompson CeedChkBackend(ierr); 3510d0321e0SJeremy L Thompson } 3520d0321e0SJeremy L Thompson } 3530d0321e0SJeremy L Thompson if (impl->d_array) { 3540d0321e0SJeremy L Thompson ierr = CeedDeviceSetValue_Hip(impl->d_array, length, val); CeedChkBackend(ierr); 3550d0321e0SJeremy L Thompson } 3560d0321e0SJeremy L Thompson if (impl->h_array) { 3570d0321e0SJeremy L Thompson ierr = CeedHostSetValue_Hip(impl->h_array, length, val); CeedChkBackend(ierr); 3580d0321e0SJeremy L Thompson } 3590d0321e0SJeremy L Thompson 3600d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3610d0321e0SJeremy L Thompson } 3620d0321e0SJeremy L Thompson 3630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3640d0321e0SJeremy L Thompson // Vector Take Array 3650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 36643c928f4SJeremy L Thompson static int CeedVectorTakeArray_Hip(CeedVector vec, CeedMemType mem_type, 3670d0321e0SJeremy L Thompson CeedScalar **array) { 3680d0321e0SJeremy L Thompson int ierr; 3690d0321e0SJeremy L Thompson Ceed ceed; 3700d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 3710d0321e0SJeremy L Thompson CeedVector_Hip *impl; 3720d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 3730d0321e0SJeremy L Thompson 37443c928f4SJeremy L Thompson // Sync array to requested mem_type 375*f48ed27dSnbeams ierr = CeedVectorSyncArray(vec, mem_type); CeedChkBackend(ierr); 3760d0321e0SJeremy L Thompson 3770d0321e0SJeremy L Thompson // Update pointer 37843c928f4SJeremy L Thompson switch (mem_type) { 3790d0321e0SJeremy L Thompson case CEED_MEM_HOST: 3800d0321e0SJeremy L Thompson (*array) = impl->h_array_borrowed; 3810d0321e0SJeremy L Thompson impl->h_array_borrowed = NULL; 3820d0321e0SJeremy L Thompson impl->h_array = NULL; 3830d0321e0SJeremy L Thompson break; 3840d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 3850d0321e0SJeremy L Thompson (*array) = impl->d_array_borrowed; 3860d0321e0SJeremy L Thompson impl->d_array_borrowed = NULL; 3870d0321e0SJeremy L Thompson impl->d_array = NULL; 3880d0321e0SJeremy L Thompson break; 3890d0321e0SJeremy L Thompson } 3900d0321e0SJeremy L Thompson 3910d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3920d0321e0SJeremy L Thompson } 3930d0321e0SJeremy L Thompson 3940d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3950d0321e0SJeremy L Thompson // Core logic for array syncronization for GetArray. 3960d0321e0SJeremy L Thompson // If a different memory type is most up to date, this will perform a copy 3970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3980d0321e0SJeremy L Thompson static int CeedVectorGetArrayCore_Hip(const CeedVector vec, 39943c928f4SJeremy L Thompson const CeedMemType mem_type, CeedScalar **array) { 4000d0321e0SJeremy L Thompson int ierr; 4010d0321e0SJeremy L Thompson Ceed ceed; 4020d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 4030d0321e0SJeremy L Thompson CeedVector_Hip *impl; 4040d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 4050d0321e0SJeremy L Thompson 40643c928f4SJeremy L Thompson // Sync array to requested mem_type 407*f48ed27dSnbeams ierr = CeedVectorSyncArray(vec, mem_type); CeedChkBackend(ierr); 4080d0321e0SJeremy L Thompson 4090d0321e0SJeremy L Thompson // Update pointer 41043c928f4SJeremy L Thompson switch (mem_type) { 4110d0321e0SJeremy L Thompson case CEED_MEM_HOST: 4120d0321e0SJeremy L Thompson *array = impl->h_array; 4130d0321e0SJeremy L Thompson break; 4140d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 4150d0321e0SJeremy L Thompson *array = impl->d_array; 4160d0321e0SJeremy L Thompson break; 4170d0321e0SJeremy L Thompson } 4180d0321e0SJeremy L Thompson 4190d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4200d0321e0SJeremy L Thompson } 4210d0321e0SJeremy L Thompson 4220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 42343c928f4SJeremy L Thompson // Get read-only access to a vector via the specified mem_type 4240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4250d0321e0SJeremy L Thompson static int CeedVectorGetArrayRead_Hip(const CeedVector vec, 42643c928f4SJeremy L Thompson const CeedMemType mem_type, const CeedScalar **array) { 42743c928f4SJeremy L Thompson return CeedVectorGetArrayCore_Hip(vec, mem_type, (CeedScalar **)array); 4280d0321e0SJeremy L Thompson } 4290d0321e0SJeremy L Thompson 4300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 43143c928f4SJeremy L Thompson // Get read/write access to a vector via the specified mem_type 4320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 43343c928f4SJeremy L Thompson static int CeedVectorGetArray_Hip(const CeedVector vec, 43443c928f4SJeremy L Thompson const CeedMemType mem_type, 4350d0321e0SJeremy L Thompson CeedScalar **array) { 4360d0321e0SJeremy L Thompson int ierr; 4370d0321e0SJeremy L Thompson CeedVector_Hip *impl; 4380d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 4390d0321e0SJeremy L Thompson 44043c928f4SJeremy L Thompson ierr = CeedVectorGetArrayCore_Hip(vec, mem_type, array); CeedChkBackend(ierr); 4410d0321e0SJeremy L Thompson 4420d0321e0SJeremy L Thompson ierr = CeedVectorSetAllInvalid_Hip(vec); CeedChkBackend(ierr); 44343c928f4SJeremy L Thompson switch (mem_type) { 4440d0321e0SJeremy L Thompson case CEED_MEM_HOST: 4450d0321e0SJeremy L Thompson impl->h_array = *array; 4460d0321e0SJeremy L Thompson break; 4470d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 4480d0321e0SJeremy L Thompson impl->d_array = *array; 4490d0321e0SJeremy L Thompson break; 4500d0321e0SJeremy L Thompson } 4510d0321e0SJeremy L Thompson 4520d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4530d0321e0SJeremy L Thompson } 4540d0321e0SJeremy L Thompson 4550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 45643c928f4SJeremy L Thompson // Get write access to a vector via the specified mem_type 4570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4580d0321e0SJeremy L Thompson static int CeedVectorGetArrayWrite_Hip(const CeedVector vec, 45943c928f4SJeremy L Thompson const CeedMemType mem_type, CeedScalar **array) { 4600d0321e0SJeremy L Thompson int ierr; 4610d0321e0SJeremy L Thompson CeedVector_Hip *impl; 4620d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 4630d0321e0SJeremy L Thompson 4640d0321e0SJeremy L Thompson bool has_array_of_type = true; 46543c928f4SJeremy L Thompson ierr = CeedVectorHasArrayOfType_Hip(vec, mem_type, &has_array_of_type); 4660d0321e0SJeremy L Thompson CeedChkBackend(ierr); 4670d0321e0SJeremy L Thompson if (!has_array_of_type) { 4680d0321e0SJeremy L Thompson // Allocate if array is not yet allocated 46943c928f4SJeremy L Thompson ierr = CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL); 4700d0321e0SJeremy L Thompson CeedChkBackend(ierr); 4710d0321e0SJeremy L Thompson } else { 4720d0321e0SJeremy L Thompson // Select dirty array 47343c928f4SJeremy L Thompson switch (mem_type) { 4740d0321e0SJeremy L Thompson case CEED_MEM_HOST: 4750d0321e0SJeremy L Thompson if (impl->h_array_borrowed) 4760d0321e0SJeremy L Thompson impl->h_array = impl->h_array_borrowed; 4770d0321e0SJeremy L Thompson else 4780d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 4790d0321e0SJeremy L Thompson break; 4800d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 4810d0321e0SJeremy L Thompson if (impl->d_array_borrowed) 4820d0321e0SJeremy L Thompson impl->d_array = impl->d_array_borrowed; 4830d0321e0SJeremy L Thompson else 4840d0321e0SJeremy L Thompson impl->d_array = impl->d_array_owned; 4850d0321e0SJeremy L Thompson } 4860d0321e0SJeremy L Thompson } 4870d0321e0SJeremy L Thompson 48843c928f4SJeremy L Thompson return CeedVectorGetArray_Hip(vec, mem_type, array); 4890d0321e0SJeremy L Thompson } 4900d0321e0SJeremy L Thompson 4910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4920d0321e0SJeremy L Thompson // Get the norm of a CeedVector 4930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4940d0321e0SJeremy L Thompson static int CeedVectorNorm_Hip(CeedVector vec, CeedNormType type, 4950d0321e0SJeremy L Thompson CeedScalar *norm) { 4960d0321e0SJeremy L Thompson int ierr; 4970d0321e0SJeremy L Thompson Ceed ceed; 4980d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 4990d0321e0SJeremy L Thompson CeedVector_Hip *impl; 5000d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 5011f9221feSJeremy L Thompson CeedSize length; 5020d0321e0SJeremy L Thompson ierr = CeedVectorGetLength(vec, &length); CeedChkBackend(ierr); 5030d0321e0SJeremy L Thompson hipblasHandle_t handle; 5040d0321e0SJeremy L Thompson ierr = CeedHipGetHipblasHandle(ceed, &handle); CeedChkBackend(ierr); 5050d0321e0SJeremy L Thompson 5060d0321e0SJeremy L Thompson // Compute norm 5070d0321e0SJeremy L Thompson const CeedScalar *d_array; 5080d0321e0SJeremy L Thompson ierr = CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array); 5090d0321e0SJeremy L Thompson CeedChkBackend(ierr); 5100d0321e0SJeremy L Thompson switch (type) { 5110d0321e0SJeremy L Thompson case CEED_NORM_1: { 5120d0321e0SJeremy L Thompson if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 5130d0321e0SJeremy L Thompson ierr = hipblasSasum(handle, length, (float *) d_array, 1, (float *) norm); 5140d0321e0SJeremy L Thompson } else { 5150d0321e0SJeremy L Thompson ierr = hipblasDasum(handle, length, (double *) d_array, 1, (double *) norm); 5160d0321e0SJeremy L Thompson } 5170d0321e0SJeremy L Thompson CeedChk_Hipblas(ceed, ierr); 5180d0321e0SJeremy L Thompson break; 5190d0321e0SJeremy L Thompson } 5200d0321e0SJeremy L Thompson case CEED_NORM_2: { 5210d0321e0SJeremy L Thompson if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 5220d0321e0SJeremy L Thompson ierr = hipblasSnrm2(handle, length, (float *) d_array, 1, (float *) norm); 5230d0321e0SJeremy L Thompson } else { 5240d0321e0SJeremy L Thompson ierr = hipblasDnrm2(handle, length, (double *) d_array, 1, (double *) norm); 5250d0321e0SJeremy L Thompson } 5260d0321e0SJeremy L Thompson CeedChk_Hipblas(ceed, ierr); 5270d0321e0SJeremy L Thompson break; 5280d0321e0SJeremy L Thompson } 5290d0321e0SJeremy L Thompson case CEED_NORM_MAX: { 5300d0321e0SJeremy L Thompson CeedInt indx; 5310d0321e0SJeremy L Thompson if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 5320d0321e0SJeremy L Thompson ierr = hipblasIsamax(handle, length, (float *) d_array, 1, &indx); 5330d0321e0SJeremy L Thompson } else { 5340d0321e0SJeremy L Thompson ierr = hipblasIdamax(handle, length, (double *) d_array, 1, &indx); 5350d0321e0SJeremy L Thompson } 5360d0321e0SJeremy L Thompson CeedChk_Hipblas(ceed, ierr); 5370d0321e0SJeremy L Thompson CeedScalar normNoAbs; 5380d0321e0SJeremy L Thompson ierr = hipMemcpy(&normNoAbs, impl->d_array+indx-1, sizeof(CeedScalar), 5390d0321e0SJeremy L Thompson hipMemcpyDeviceToHost); CeedChk_Hip(ceed, ierr); 5400d0321e0SJeremy L Thompson *norm = fabs(normNoAbs); 5410d0321e0SJeremy L Thompson break; 5420d0321e0SJeremy L Thompson } 5430d0321e0SJeremy L Thompson } 5440d0321e0SJeremy L Thompson ierr = CeedVectorRestoreArrayRead(vec, &d_array); CeedChkBackend(ierr); 5450d0321e0SJeremy L Thompson 5460d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5470d0321e0SJeremy L Thompson } 5480d0321e0SJeremy L Thompson 5490d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5500d0321e0SJeremy L Thompson // Take reciprocal of a vector on host 5510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5520d0321e0SJeremy L Thompson static int CeedHostReciprocal_Hip(CeedScalar *h_array, CeedInt length) { 5530d0321e0SJeremy L Thompson for (int i = 0; i < length; i++) 5540d0321e0SJeremy L Thompson if (fabs(h_array[i]) > CEED_EPSILON) 5550d0321e0SJeremy L Thompson h_array[i] = 1./h_array[i]; 5560d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5570d0321e0SJeremy L Thompson } 5580d0321e0SJeremy L Thompson 5590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5600d0321e0SJeremy L Thompson // Take reciprocal of a vector on device (impl in .cu file) 5610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5620d0321e0SJeremy L Thompson int CeedDeviceReciprocal_Hip(CeedScalar *d_array, CeedInt length); 5630d0321e0SJeremy L Thompson 5640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5650d0321e0SJeremy L Thompson // Take reciprocal of a vector 5660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5670d0321e0SJeremy L Thompson static int CeedVectorReciprocal_Hip(CeedVector vec) { 5680d0321e0SJeremy L Thompson int ierr; 5690d0321e0SJeremy L Thompson Ceed ceed; 5700d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 5710d0321e0SJeremy L Thompson CeedVector_Hip *impl; 5720d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 5731f9221feSJeremy L Thompson CeedSize length; 5740d0321e0SJeremy L Thompson ierr = CeedVectorGetLength(vec, &length); CeedChkBackend(ierr); 5750d0321e0SJeremy L Thompson 5760d0321e0SJeremy L Thompson // Set value for synced device/host array 5770d0321e0SJeremy L Thompson if (impl->d_array) { 5780d0321e0SJeremy L Thompson ierr = CeedDeviceReciprocal_Hip(impl->d_array, length); CeedChkBackend(ierr); 5790d0321e0SJeremy L Thompson } 5800d0321e0SJeremy L Thompson if (impl->h_array) { 5810d0321e0SJeremy L Thompson ierr = CeedHostReciprocal_Hip(impl->h_array, length); CeedChkBackend(ierr); 5820d0321e0SJeremy L Thompson } 5830d0321e0SJeremy L Thompson 5840d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5850d0321e0SJeremy L Thompson } 5860d0321e0SJeremy L Thompson 5870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5880d0321e0SJeremy L Thompson // Compute x = alpha x on the host 5890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5900d0321e0SJeremy L Thompson static int CeedHostScale_Hip(CeedScalar *x_array, CeedScalar alpha, 5910d0321e0SJeremy L Thompson CeedInt length) { 5920d0321e0SJeremy L Thompson for (int i = 0; i < length; i++) 5930d0321e0SJeremy L Thompson x_array[i] *= alpha; 5940d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5950d0321e0SJeremy L Thompson } 5960d0321e0SJeremy L Thompson 5970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5980d0321e0SJeremy L Thompson // Compute x = alpha x on device (impl in .cu file) 5990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6000d0321e0SJeremy L Thompson int CeedDeviceScale_Hip(CeedScalar *x_array, CeedScalar alpha, 6010d0321e0SJeremy L Thompson CeedInt length); 6020d0321e0SJeremy L Thompson 6030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6040d0321e0SJeremy L Thompson // Compute x = alpha x 6050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6060d0321e0SJeremy L Thompson static int CeedVectorScale_Hip(CeedVector x, CeedScalar alpha) { 6070d0321e0SJeremy L Thompson int ierr; 6080d0321e0SJeremy L Thompson Ceed ceed; 6090d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(x, &ceed); CeedChkBackend(ierr); 6100d0321e0SJeremy L Thompson CeedVector_Hip *x_impl; 6110d0321e0SJeremy L Thompson ierr = CeedVectorGetData(x, &x_impl); CeedChkBackend(ierr); 6121f9221feSJeremy L Thompson CeedSize length; 6130d0321e0SJeremy L Thompson ierr = CeedVectorGetLength(x, &length); CeedChkBackend(ierr); 6140d0321e0SJeremy L Thompson 6150d0321e0SJeremy L Thompson // Set value for synced device/host array 6160d0321e0SJeremy L Thompson if (x_impl->d_array) { 6170d0321e0SJeremy L Thompson ierr = CeedDeviceScale_Hip(x_impl->d_array, alpha, length); 6180d0321e0SJeremy L Thompson CeedChkBackend(ierr); 6190d0321e0SJeremy L Thompson } 6200d0321e0SJeremy L Thompson if (x_impl->h_array) { 6210d0321e0SJeremy L Thompson ierr = CeedHostScale_Hip(x_impl->h_array, alpha, length); CeedChkBackend(ierr); 6220d0321e0SJeremy L Thompson } 6230d0321e0SJeremy L Thompson 6240d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6250d0321e0SJeremy L Thompson } 6260d0321e0SJeremy L Thompson 6270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6280d0321e0SJeremy L Thompson // Compute y = alpha x + y on the host 6290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6300d0321e0SJeremy L Thompson static int CeedHostAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, 6310d0321e0SJeremy L Thompson CeedScalar *x_array, CeedInt length) { 6320d0321e0SJeremy L Thompson for (int i = 0; i < length; i++) 6330d0321e0SJeremy L Thompson y_array[i] += alpha * x_array[i]; 6340d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6350d0321e0SJeremy L Thompson } 6360d0321e0SJeremy L Thompson 6370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6380d0321e0SJeremy L Thompson // Compute y = alpha x + y on device (impl in .cu file) 6390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6400d0321e0SJeremy L Thompson int CeedDeviceAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, 6410d0321e0SJeremy L Thompson CeedScalar *x_array, CeedInt length); 6420d0321e0SJeremy L Thompson 6430d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6440d0321e0SJeremy L Thompson // Compute y = alpha x + y 6450d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6460d0321e0SJeremy L Thompson static int CeedVectorAXPY_Hip(CeedVector y, CeedScalar alpha, CeedVector x) { 6470d0321e0SJeremy L Thompson int ierr; 6480d0321e0SJeremy L Thompson Ceed ceed; 6490d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(y, &ceed); CeedChkBackend(ierr); 6500d0321e0SJeremy L Thompson CeedVector_Hip *y_impl, *x_impl; 6510d0321e0SJeremy L Thompson ierr = CeedVectorGetData(y, &y_impl); CeedChkBackend(ierr); 6520d0321e0SJeremy L Thompson ierr = CeedVectorGetData(x, &x_impl); CeedChkBackend(ierr); 6531f9221feSJeremy L Thompson CeedSize length; 6540d0321e0SJeremy L Thompson ierr = CeedVectorGetLength(y, &length); CeedChkBackend(ierr); 6550d0321e0SJeremy L Thompson 6560d0321e0SJeremy L Thompson // Set value for synced device/host array 6570d0321e0SJeremy L Thompson if (y_impl->d_array) { 6580d0321e0SJeremy L Thompson ierr = CeedVectorSyncArray(x, CEED_MEM_DEVICE); CeedChkBackend(ierr); 6590d0321e0SJeremy L Thompson ierr = CeedDeviceAXPY_Hip(y_impl->d_array, alpha, x_impl->d_array, length); 6600d0321e0SJeremy L Thompson CeedChkBackend(ierr); 6610d0321e0SJeremy L Thompson } 6620d0321e0SJeremy L Thompson if (y_impl->h_array) { 6630d0321e0SJeremy L Thompson ierr = CeedVectorSyncArray(x, CEED_MEM_HOST); CeedChkBackend(ierr); 6640d0321e0SJeremy L Thompson ierr = CeedHostAXPY_Hip(y_impl->h_array, alpha, x_impl->h_array, length); 6650d0321e0SJeremy L Thompson CeedChkBackend(ierr); 6660d0321e0SJeremy L Thompson } 6670d0321e0SJeremy L Thompson 6680d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6690d0321e0SJeremy L Thompson } 6700d0321e0SJeremy L Thompson 6710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6720d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y on the host 6730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6740d0321e0SJeremy L Thompson static int CeedHostPointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, 6750d0321e0SJeremy L Thompson CeedScalar *y_array, CeedInt length) { 6760d0321e0SJeremy L Thompson for (int i = 0; i < length; i++) 6770d0321e0SJeremy L Thompson w_array[i] = x_array[i] * y_array[i]; 6780d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6790d0321e0SJeremy L Thompson } 6800d0321e0SJeremy L Thompson 6810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6820d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y on device (impl in .cu file) 6830d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6840d0321e0SJeremy L Thompson int CeedDevicePointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, 6850d0321e0SJeremy L Thompson CeedScalar *y_array, CeedInt length); 6860d0321e0SJeremy L Thompson 6870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6880d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y 6890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6900d0321e0SJeremy L Thompson static int CeedVectorPointwiseMult_Hip(CeedVector w, CeedVector x, 6910d0321e0SJeremy L Thompson CeedVector y) { 6920d0321e0SJeremy L Thompson int ierr; 6930d0321e0SJeremy L Thompson Ceed ceed; 6940d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(w, &ceed); CeedChkBackend(ierr); 6950d0321e0SJeremy L Thompson CeedVector_Hip *w_impl, *x_impl, *y_impl; 6960d0321e0SJeremy L Thompson ierr = CeedVectorGetData(w, &w_impl); CeedChkBackend(ierr); 6970d0321e0SJeremy L Thompson ierr = CeedVectorGetData(x, &x_impl); CeedChkBackend(ierr); 6980d0321e0SJeremy L Thompson ierr = CeedVectorGetData(y, &y_impl); CeedChkBackend(ierr); 6991f9221feSJeremy L Thompson CeedSize length; 7000d0321e0SJeremy L Thompson ierr = CeedVectorGetLength(w, &length); CeedChkBackend(ierr); 7010d0321e0SJeremy L Thompson 7020d0321e0SJeremy L Thompson // Set value for synced device/host array 7030d0321e0SJeremy L Thompson if (!w_impl->d_array && !w_impl->h_array) { 7040d0321e0SJeremy L Thompson ierr = CeedVectorSetValue(w, 0.0); CeedChkBackend(ierr); 7050d0321e0SJeremy L Thompson } 7060d0321e0SJeremy L Thompson if (w_impl->d_array) { 7070d0321e0SJeremy L Thompson ierr = CeedVectorSyncArray(x, CEED_MEM_DEVICE); CeedChkBackend(ierr); 7080d0321e0SJeremy L Thompson ierr = CeedVectorSyncArray(y, CEED_MEM_DEVICE); CeedChkBackend(ierr); 7090d0321e0SJeremy L Thompson ierr = CeedDevicePointwiseMult_Hip(w_impl->d_array, x_impl->d_array, 7100d0321e0SJeremy L Thompson y_impl->d_array, length); 7110d0321e0SJeremy L Thompson CeedChkBackend(ierr); 7120d0321e0SJeremy L Thompson } 7130d0321e0SJeremy L Thompson if (w_impl->h_array) { 7140d0321e0SJeremy L Thompson ierr = CeedVectorSyncArray(x, CEED_MEM_HOST); CeedChkBackend(ierr); 7150d0321e0SJeremy L Thompson ierr = CeedVectorSyncArray(y, CEED_MEM_HOST); CeedChkBackend(ierr); 7160d0321e0SJeremy L Thompson ierr = CeedHostPointwiseMult_Hip(w_impl->h_array, x_impl->h_array, 7170d0321e0SJeremy L Thompson y_impl->h_array, length); 7180d0321e0SJeremy L Thompson CeedChkBackend(ierr); 7190d0321e0SJeremy L Thompson } 7200d0321e0SJeremy L Thompson 7210d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7220d0321e0SJeremy L Thompson } 7230d0321e0SJeremy L Thompson 7240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7250d0321e0SJeremy L Thompson // Destroy the vector 7260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7270d0321e0SJeremy L Thompson static int CeedVectorDestroy_Hip(const CeedVector vec) { 7280d0321e0SJeremy L Thompson int ierr; 7290d0321e0SJeremy L Thompson Ceed ceed; 7300d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 7310d0321e0SJeremy L Thompson CeedVector_Hip *impl; 7320d0321e0SJeremy L Thompson ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr); 7330d0321e0SJeremy L Thompson 7340d0321e0SJeremy L Thompson ierr = hipFree(impl->d_array_owned); CeedChk_Hip(ceed, ierr); 7350d0321e0SJeremy L Thompson ierr = CeedFree(&impl->h_array_owned); CeedChkBackend(ierr); 7360d0321e0SJeremy L Thompson ierr = CeedFree(&impl); CeedChkBackend(ierr); 7370d0321e0SJeremy L Thompson 7380d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7390d0321e0SJeremy L Thompson } 7400d0321e0SJeremy L Thompson 7410d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7420d0321e0SJeremy L Thompson // Create a vector of the specified length (does not allocate memory) 7430d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7441f9221feSJeremy L Thompson int CeedVectorCreate_Hip(CeedSize n, CeedVector vec) { 7450d0321e0SJeremy L Thompson CeedVector_Hip *impl; 7460d0321e0SJeremy L Thompson int ierr; 7470d0321e0SJeremy L Thompson Ceed ceed; 7480d0321e0SJeremy L Thompson ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr); 7490d0321e0SJeremy L Thompson 7500d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", 7510d0321e0SJeremy L Thompson CeedVectorHasValidArray_Hip); CeedChkBackend(ierr); 7520d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", 7530d0321e0SJeremy L Thompson CeedVectorHasBorrowedArrayOfType_Hip); 7540d0321e0SJeremy L Thompson CeedChkBackend(ierr); 7550d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", 7560d0321e0SJeremy L Thompson CeedVectorSetArray_Hip); CeedChkBackend(ierr); 7570d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", 7580d0321e0SJeremy L Thompson CeedVectorTakeArray_Hip); CeedChkBackend(ierr); 7590d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "SetValue", 7600d0321e0SJeremy L Thompson (int (*)())(CeedVectorSetValue_Hip)); CeedChkBackend(ierr); 761*f48ed27dSnbeams ierr = CeedSetBackendFunction(ceed, "Vector", vec, "SyncArray", 762*f48ed27dSnbeams CeedVectorSyncArray_Hip); CeedChkBackend(ierr); 7630d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", 7640d0321e0SJeremy L Thompson CeedVectorGetArray_Hip); CeedChkBackend(ierr); 7650d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", 7660d0321e0SJeremy L Thompson CeedVectorGetArrayRead_Hip); CeedChkBackend(ierr); 7670d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", 7680d0321e0SJeremy L Thompson CeedVectorGetArrayWrite_Hip); CeedChkBackend(ierr); 7690d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "Norm", 7700d0321e0SJeremy L Thompson CeedVectorNorm_Hip); CeedChkBackend(ierr); 7710d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "Reciprocal", 7720d0321e0SJeremy L Thompson CeedVectorReciprocal_Hip); CeedChkBackend(ierr); 7730d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "Scale", 7740d0321e0SJeremy L Thompson (int (*)())(CeedVectorScale_Hip)); CeedChkBackend(ierr); 7750d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "AXPY", 7760d0321e0SJeremy L Thompson (int (*)())(CeedVectorAXPY_Hip)); CeedChkBackend(ierr); 7770d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", 7780d0321e0SJeremy L Thompson CeedVectorPointwiseMult_Hip); CeedChkBackend(ierr); 7790d0321e0SJeremy L Thompson ierr = CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", 7800d0321e0SJeremy L Thompson CeedVectorDestroy_Hip); CeedChkBackend(ierr); 7810d0321e0SJeremy L Thompson 7820d0321e0SJeremy L Thompson ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr); 7830d0321e0SJeremy L Thompson ierr = CeedVectorSetData(vec, impl); CeedChkBackend(ierr); 7840d0321e0SJeremy L Thompson 7850d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7860d0321e0SJeremy L Thompson } 787