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 849aac155SJeremy L Thompson #include <ceed.h> 90d0321e0SJeremy L Thompson #include <ceed/backend.h> 100d0321e0SJeremy L Thompson #include <math.h> 1149aac155SJeremy L Thompson #include <stdbool.h> 120d0321e0SJeremy L Thompson #include <string.h> 13c85e8640SSebastian Grimberg #include <hip/hip_runtime.h> 140d0321e0SJeremy L Thompson 1549aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h" 162b730f8bSJeremy L Thompson #include "ceed-hip-ref.h" 17f48ed27dSnbeams 18f48ed27dSnbeams //------------------------------------------------------------------------------ 19f48ed27dSnbeams // Check if host/device sync is needed 20f48ed27dSnbeams //------------------------------------------------------------------------------ 212b730f8bSJeremy L Thompson static inline int CeedVectorNeedSync_Hip(const CeedVector vec, CeedMemType mem_type, bool *need_sync) { 22f48ed27dSnbeams CeedVector_Hip *impl; 23f48ed27dSnbeams bool has_valid_array = false; 24*b7453713SJeremy L Thompson 25*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 262b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorHasValidArray(vec, &has_valid_array)); 27f48ed27dSnbeams switch (mem_type) { 28f48ed27dSnbeams case CEED_MEM_HOST: 29f48ed27dSnbeams *need_sync = has_valid_array && !impl->h_array; 30f48ed27dSnbeams break; 31f48ed27dSnbeams case CEED_MEM_DEVICE: 32f48ed27dSnbeams *need_sync = has_valid_array && !impl->d_array; 33f48ed27dSnbeams break; 34f48ed27dSnbeams } 35f48ed27dSnbeams return CEED_ERROR_SUCCESS; 36f48ed27dSnbeams } 37f48ed27dSnbeams 380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 390d0321e0SJeremy L Thompson // Sync host to device 400d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 410d0321e0SJeremy L Thompson static inline int CeedVectorSyncH2D_Hip(const CeedVector vec) { 420d0321e0SJeremy L Thompson Ceed ceed; 43*b7453713SJeremy L Thompson CeedSize length; 440d0321e0SJeremy L Thompson CeedVector_Hip *impl; 45*b7453713SJeremy L Thompson 46*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 472b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 480d0321e0SJeremy L Thompson 492b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 50539ec17dSJeremy L Thompson size_t bytes = length * sizeof(CeedScalar); 51539ec17dSJeremy L Thompson 526574a04fSJeremy L Thompson CeedCheck(impl->h_array, ceed, CEED_ERROR_BACKEND, "No valid host data to sync to device"); 530d0321e0SJeremy L Thompson 540d0321e0SJeremy L Thompson if (impl->d_array_borrowed) { 550d0321e0SJeremy L Thompson impl->d_array = impl->d_array_borrowed; 560d0321e0SJeremy L Thompson } else if (impl->d_array_owned) { 570d0321e0SJeremy L Thompson impl->d_array = impl->d_array_owned; 580d0321e0SJeremy L Thompson } else { 592b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_array_owned, bytes)); 600d0321e0SJeremy L Thompson impl->d_array = impl->d_array_owned; 610d0321e0SJeremy L Thompson } 622b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->d_array, impl->h_array, bytes, hipMemcpyHostToDevice)); 630d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 640d0321e0SJeremy L Thompson } 650d0321e0SJeremy L Thompson 660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 670d0321e0SJeremy L Thompson // Sync device to host 680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 690d0321e0SJeremy L Thompson static inline int CeedVectorSyncD2H_Hip(const CeedVector vec) { 700d0321e0SJeremy L Thompson Ceed ceed; 71*b7453713SJeremy L Thompson CeedSize length; 720d0321e0SJeremy L Thompson CeedVector_Hip *impl; 73*b7453713SJeremy L Thompson 74*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 752b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 760d0321e0SJeremy L Thompson 776574a04fSJeremy L Thompson CeedCheck(impl->d_array, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host"); 780d0321e0SJeremy L Thompson 790d0321e0SJeremy L Thompson if (impl->h_array_borrowed) { 800d0321e0SJeremy L Thompson impl->h_array = impl->h_array_borrowed; 810d0321e0SJeremy L Thompson } else if (impl->h_array_owned) { 820d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 830d0321e0SJeremy L Thompson } else { 841f9221feSJeremy L Thompson CeedSize length; 852b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 862b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(length, &impl->h_array_owned)); 870d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 880d0321e0SJeremy L Thompson } 890d0321e0SJeremy L Thompson 902b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 91539ec17dSJeremy L Thompson size_t bytes = length * sizeof(CeedScalar); 920d0321e0SJeremy L Thompson 93*b7453713SJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->h_array, impl->d_array, bytes, hipMemcpyDeviceToHost)); 940d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 950d0321e0SJeremy L Thompson } 960d0321e0SJeremy L Thompson 970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 980d0321e0SJeremy L Thompson // Sync arrays 990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1002b730f8bSJeremy L Thompson static int CeedVectorSyncArray_Hip(const CeedVector vec, CeedMemType mem_type) { 101f48ed27dSnbeams bool need_sync = false; 102*b7453713SJeremy L Thompson 103*b7453713SJeremy L Thompson // Check whether device/host sync is needed 1042b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorNeedSync_Hip(vec, mem_type, &need_sync)); 1052b730f8bSJeremy L Thompson if (!need_sync) return CEED_ERROR_SUCCESS; 106f48ed27dSnbeams 10743c928f4SJeremy L Thompson switch (mem_type) { 1082b730f8bSJeremy L Thompson case CEED_MEM_HOST: 1092b730f8bSJeremy L Thompson return CeedVectorSyncD2H_Hip(vec); 1102b730f8bSJeremy L Thompson case CEED_MEM_DEVICE: 1112b730f8bSJeremy L Thompson return CeedVectorSyncH2D_Hip(vec); 1120d0321e0SJeremy L Thompson } 1130d0321e0SJeremy L Thompson return CEED_ERROR_UNSUPPORTED; 1140d0321e0SJeremy L Thompson } 1150d0321e0SJeremy L Thompson 1160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1170d0321e0SJeremy L Thompson // Set all pointers as invalid 1180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1190d0321e0SJeremy L Thompson static inline int CeedVectorSetAllInvalid_Hip(const CeedVector vec) { 1200d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1210d0321e0SJeremy L Thompson 122*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1230d0321e0SJeremy L Thompson impl->h_array = NULL; 1240d0321e0SJeremy L Thompson impl->d_array = NULL; 1250d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1260d0321e0SJeremy L Thompson } 1270d0321e0SJeremy L Thompson 1280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 129b2165e7aSSebastian Grimberg // Check if CeedVector has any valid pointer 1300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1312b730f8bSJeremy L Thompson static inline int CeedVectorHasValidArray_Hip(const CeedVector vec, bool *has_valid_array) { 1320d0321e0SJeremy L Thompson CeedVector_Hip *impl; 133*b7453713SJeremy L Thompson 1342b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1351c66c397SJeremy L Thompson *has_valid_array = impl->h_array || impl->d_array; 1360d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1370d0321e0SJeremy L Thompson } 1380d0321e0SJeremy L Thompson 1390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 140b2165e7aSSebastian Grimberg // Check if has array of given type 1410d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1422b730f8bSJeremy L Thompson static inline int CeedVectorHasArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_array_of_type) { 1430d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1440d0321e0SJeremy L Thompson 145*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 14643c928f4SJeremy L Thompson switch (mem_type) { 1470d0321e0SJeremy L Thompson case CEED_MEM_HOST: 1481c66c397SJeremy L Thompson *has_array_of_type = impl->h_array_borrowed || impl->h_array_owned; 1490d0321e0SJeremy L Thompson break; 1500d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 1511c66c397SJeremy L Thompson *has_array_of_type = impl->d_array_borrowed || impl->d_array_owned; 1520d0321e0SJeremy L Thompson break; 1530d0321e0SJeremy L Thompson } 1540d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1550d0321e0SJeremy L Thompson } 1560d0321e0SJeremy L Thompson 1570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1580d0321e0SJeremy L Thompson // Check if has borrowed array of given type 1590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1602b730f8bSJeremy L Thompson static inline int CeedVectorHasBorrowedArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 1610d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1620d0321e0SJeremy L Thompson 163*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 16443c928f4SJeremy L Thompson switch (mem_type) { 1650d0321e0SJeremy L Thompson case CEED_MEM_HOST: 1661c66c397SJeremy L Thompson *has_borrowed_array_of_type = impl->h_array_borrowed; 1670d0321e0SJeremy L Thompson break; 1680d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 1691c66c397SJeremy L Thompson *has_borrowed_array_of_type = impl->d_array_borrowed; 1700d0321e0SJeremy L Thompson break; 1710d0321e0SJeremy L Thompson } 1720d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1730d0321e0SJeremy L Thompson } 1740d0321e0SJeremy L Thompson 1750d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1760d0321e0SJeremy L Thompson // Set array from host 1770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1782b730f8bSJeremy L Thompson static int CeedVectorSetArrayHost_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) { 1790d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1800d0321e0SJeremy L Thompson 181*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 18243c928f4SJeremy L Thompson switch (copy_mode) { 1830d0321e0SJeremy L Thompson case CEED_COPY_VALUES: { 1841f9221feSJeremy L Thompson CeedSize length; 185*b7453713SJeremy L Thompson 1860d0321e0SJeremy L Thompson if (!impl->h_array_owned) { 1872b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 1882b730f8bSJeremy L Thompson CeedCallBackend(CeedMalloc(length, &impl->h_array_owned)); 1890d0321e0SJeremy L Thompson } 1900d0321e0SJeremy L Thompson impl->h_array_borrowed = NULL; 1910d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 192539ec17dSJeremy L Thompson if (array) { 193539ec17dSJeremy L Thompson CeedSize length; 194*b7453713SJeremy L Thompson 1952b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 196539ec17dSJeremy L Thompson size_t bytes = length * sizeof(CeedScalar); 197539ec17dSJeremy L Thompson memcpy(impl->h_array, array, bytes); 198539ec17dSJeremy L Thompson } 1990d0321e0SJeremy L Thompson } break; 2000d0321e0SJeremy L Thompson case CEED_OWN_POINTER: 2012b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->h_array_owned)); 2020d0321e0SJeremy L Thompson impl->h_array_owned = array; 2030d0321e0SJeremy L Thompson impl->h_array_borrowed = NULL; 2040d0321e0SJeremy L Thompson impl->h_array = array; 2050d0321e0SJeremy L Thompson break; 2060d0321e0SJeremy L Thompson case CEED_USE_POINTER: 2072b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->h_array_owned)); 2080d0321e0SJeremy L Thompson impl->h_array_borrowed = array; 2090d0321e0SJeremy L Thompson impl->h_array = array; 2100d0321e0SJeremy L Thompson break; 2110d0321e0SJeremy L Thompson } 2120d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2130d0321e0SJeremy L Thompson } 2140d0321e0SJeremy L Thompson 2150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2160d0321e0SJeremy L Thompson // Set array from device 2170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2182b730f8bSJeremy L Thompson static int CeedVectorSetArrayDevice_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) { 2190d0321e0SJeremy L Thompson Ceed ceed; 2200d0321e0SJeremy L Thompson CeedVector_Hip *impl; 2210d0321e0SJeremy L Thompson 222*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 223*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 22443c928f4SJeremy L Thompson switch (copy_mode) { 225539ec17dSJeremy L Thompson case CEED_COPY_VALUES: { 226539ec17dSJeremy L Thompson CeedSize length; 227*b7453713SJeremy L Thompson 2282b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 229539ec17dSJeremy L Thompson size_t bytes = length * sizeof(CeedScalar); 230*b7453713SJeremy L Thompson 2310d0321e0SJeremy L Thompson if (!impl->d_array_owned) { 2322b730f8bSJeremy L Thompson CeedCallHip(ceed, hipMalloc((void **)&impl->d_array_owned, bytes)); 2330d0321e0SJeremy L Thompson } 2340d0321e0SJeremy L Thompson impl->d_array_borrowed = NULL; 2353ce2313bSJeremy L Thompson impl->d_array = impl->d_array_owned; 236b2165e7aSSebastian Grimberg if (array) CeedCallHip(ceed, hipMemcpy(impl->d_array, array, bytes, hipMemcpyDeviceToDevice)); 237539ec17dSJeremy L Thompson } break; 2380d0321e0SJeremy L Thompson case CEED_OWN_POINTER: 2392b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_array_owned)); 2400d0321e0SJeremy L Thompson impl->d_array_owned = array; 2410d0321e0SJeremy L Thompson impl->d_array_borrowed = NULL; 2420d0321e0SJeremy L Thompson impl->d_array = array; 2430d0321e0SJeremy L Thompson break; 2440d0321e0SJeremy L Thompson case CEED_USE_POINTER: 2452b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_array_owned)); 2460d0321e0SJeremy L Thompson impl->d_array_owned = NULL; 2470d0321e0SJeremy L Thompson impl->d_array_borrowed = array; 2480d0321e0SJeremy L Thompson impl->d_array = array; 2490d0321e0SJeremy L Thompson break; 2500d0321e0SJeremy L Thompson } 2510d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2520d0321e0SJeremy L Thompson } 2530d0321e0SJeremy L Thompson 2540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2550d0321e0SJeremy L Thompson // Set the array used by a vector, 2560d0321e0SJeremy L Thompson // freeing any previously allocated array if applicable 2570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2582b730f8bSJeremy L Thompson static int CeedVectorSetArray_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedCopyMode copy_mode, CeedScalar *array) { 2590d0321e0SJeremy L Thompson Ceed ceed; 2600d0321e0SJeremy L Thompson CeedVector_Hip *impl; 2610d0321e0SJeremy L Thompson 262*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 263*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 2642b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec)); 26543c928f4SJeremy L Thompson switch (mem_type) { 2660d0321e0SJeremy L Thompson case CEED_MEM_HOST: 26743c928f4SJeremy L Thompson return CeedVectorSetArrayHost_Hip(vec, copy_mode, array); 2680d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 26943c928f4SJeremy L Thompson return CeedVectorSetArrayDevice_Hip(vec, copy_mode, array); 2700d0321e0SJeremy L Thompson } 2710d0321e0SJeremy L Thompson return CEED_ERROR_UNSUPPORTED; 2720d0321e0SJeremy L Thompson } 2730d0321e0SJeremy L Thompson 2740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2750d0321e0SJeremy L Thompson // Set host array to value 2760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2779330daecSnbeams static int CeedHostSetValue_Hip(CeedScalar *h_array, CeedSize length, CeedScalar val) { 2789330daecSnbeams for (CeedSize i = 0; i < length; i++) h_array[i] = val; 2790d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2800d0321e0SJeremy L Thompson } 2810d0321e0SJeremy L Thompson 2820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2830d0321e0SJeremy L Thompson // Set device array to value (impl in .hip file) 2840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2859330daecSnbeams int CeedDeviceSetValue_Hip(CeedScalar *d_array, CeedSize length, CeedScalar val); 2860d0321e0SJeremy L Thompson 2870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 288b2165e7aSSebastian Grimberg // Set a vector to a value 2890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2900d0321e0SJeremy L Thompson static int CeedVectorSetValue_Hip(CeedVector vec, CeedScalar val) { 2910d0321e0SJeremy L Thompson Ceed ceed; 2921f9221feSJeremy L Thompson CeedSize length; 293*b7453713SJeremy L Thompson CeedVector_Hip *impl; 2940d0321e0SJeremy L Thompson 295*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 296*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 297*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 2980d0321e0SJeremy L Thompson // Set value for synced device/host array 2990d0321e0SJeremy L Thompson if (!impl->d_array && !impl->h_array) { 3000d0321e0SJeremy L Thompson if (impl->d_array_borrowed) { 3010d0321e0SJeremy L Thompson impl->d_array = impl->d_array_borrowed; 3020d0321e0SJeremy L Thompson } else if (impl->h_array_borrowed) { 3030d0321e0SJeremy L Thompson impl->h_array = impl->h_array_borrowed; 3040d0321e0SJeremy L Thompson } else if (impl->d_array_owned) { 3050d0321e0SJeremy L Thompson impl->d_array = impl->d_array_owned; 3060d0321e0SJeremy L Thompson } else if (impl->h_array_owned) { 3070d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 3080d0321e0SJeremy L Thompson } else { 3092b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_DEVICE, CEED_COPY_VALUES, NULL)); 3100d0321e0SJeremy L Thompson } 3110d0321e0SJeremy L Thompson } 3120d0321e0SJeremy L Thompson if (impl->d_array) { 3132b730f8bSJeremy L Thompson CeedCallBackend(CeedDeviceSetValue_Hip(impl->d_array, length, val)); 314b2165e7aSSebastian Grimberg impl->h_array = NULL; 3150d0321e0SJeremy L Thompson } 3160d0321e0SJeremy L Thompson if (impl->h_array) { 3172b730f8bSJeremy L Thompson CeedCallBackend(CeedHostSetValue_Hip(impl->h_array, length, val)); 318b2165e7aSSebastian Grimberg impl->d_array = NULL; 3190d0321e0SJeremy L Thompson } 3200d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3210d0321e0SJeremy L Thompson } 3220d0321e0SJeremy L Thompson 3230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3240d0321e0SJeremy L Thompson // Vector Take Array 3250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3262b730f8bSJeremy L Thompson static int CeedVectorTakeArray_Hip(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 3270d0321e0SJeremy L Thompson Ceed ceed; 3280d0321e0SJeremy L Thompson CeedVector_Hip *impl; 329*b7453713SJeremy L Thompson 330*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 3312b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 3320d0321e0SJeremy L Thompson 33343c928f4SJeremy L Thompson // Sync array to requested mem_type 3342b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(vec, mem_type)); 3350d0321e0SJeremy L Thompson 3360d0321e0SJeremy L Thompson // Update pointer 33743c928f4SJeremy L Thompson switch (mem_type) { 3380d0321e0SJeremy L Thompson case CEED_MEM_HOST: 3390d0321e0SJeremy L Thompson (*array) = impl->h_array_borrowed; 3400d0321e0SJeremy L Thompson impl->h_array_borrowed = NULL; 3410d0321e0SJeremy L Thompson impl->h_array = NULL; 3420d0321e0SJeremy L Thompson break; 3430d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 3440d0321e0SJeremy L Thompson (*array) = impl->d_array_borrowed; 3450d0321e0SJeremy L Thompson impl->d_array_borrowed = NULL; 3460d0321e0SJeremy L Thompson impl->d_array = NULL; 3470d0321e0SJeremy L Thompson break; 3480d0321e0SJeremy L Thompson } 3490d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3500d0321e0SJeremy L Thompson } 3510d0321e0SJeremy L Thompson 3520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3530d0321e0SJeremy L Thompson // Core logic for array syncronization for GetArray. 3540d0321e0SJeremy L Thompson // If a different memory type is most up to date, this will perform a copy 3550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3562b730f8bSJeremy L Thompson static int CeedVectorGetArrayCore_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 3570d0321e0SJeremy L Thompson Ceed ceed; 3580d0321e0SJeremy L Thompson CeedVector_Hip *impl; 359*b7453713SJeremy L Thompson 360*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 3612b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 3620d0321e0SJeremy L Thompson 36343c928f4SJeremy L Thompson // Sync array to requested mem_type 3642b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(vec, mem_type)); 3650d0321e0SJeremy L Thompson 3660d0321e0SJeremy L Thompson // Update pointer 36743c928f4SJeremy L Thompson switch (mem_type) { 3680d0321e0SJeremy L Thompson case CEED_MEM_HOST: 3690d0321e0SJeremy L Thompson *array = impl->h_array; 3700d0321e0SJeremy L Thompson break; 3710d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 3720d0321e0SJeremy L Thompson *array = impl->d_array; 3730d0321e0SJeremy L Thompson break; 3740d0321e0SJeremy L Thompson } 3750d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3760d0321e0SJeremy L Thompson } 3770d0321e0SJeremy L Thompson 3780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 37943c928f4SJeremy L Thompson // Get read-only access to a vector via the specified mem_type 3800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3812b730f8bSJeremy L Thompson static int CeedVectorGetArrayRead_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedScalar **array) { 38243c928f4SJeremy L Thompson return CeedVectorGetArrayCore_Hip(vec, mem_type, (CeedScalar **)array); 3830d0321e0SJeremy L Thompson } 3840d0321e0SJeremy L Thompson 3850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 38643c928f4SJeremy L Thompson // Get read/write access to a vector via the specified mem_type 3870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3882b730f8bSJeremy L Thompson static int CeedVectorGetArray_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 3890d0321e0SJeremy L Thompson CeedVector_Hip *impl; 390*b7453713SJeremy L Thompson 3912b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 3922b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayCore_Hip(vec, mem_type, array)); 3932b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec)); 39443c928f4SJeremy L Thompson switch (mem_type) { 3950d0321e0SJeremy L Thompson case CEED_MEM_HOST: 3960d0321e0SJeremy L Thompson impl->h_array = *array; 3970d0321e0SJeremy L Thompson break; 3980d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 3990d0321e0SJeremy L Thompson impl->d_array = *array; 4000d0321e0SJeremy L Thompson break; 4010d0321e0SJeremy L Thompson } 4020d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4030d0321e0SJeremy L Thompson } 4040d0321e0SJeremy L Thompson 4050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 40643c928f4SJeremy L Thompson // Get write access to a vector via the specified mem_type 4070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4082b730f8bSJeremy L Thompson static int CeedVectorGetArrayWrite_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 4090d0321e0SJeremy L Thompson bool has_array_of_type = true; 410*b7453713SJeremy L Thompson CeedVector_Hip *impl; 411*b7453713SJeremy L Thompson 412*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 4132b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorHasArrayOfType_Hip(vec, mem_type, &has_array_of_type)); 4140d0321e0SJeremy L Thompson if (!has_array_of_type) { 4150d0321e0SJeremy L Thompson // Allocate if array is not yet allocated 4162b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL)); 4170d0321e0SJeremy L Thompson } else { 4180d0321e0SJeremy L Thompson // Select dirty array 41943c928f4SJeremy L Thompson switch (mem_type) { 4200d0321e0SJeremy L Thompson case CEED_MEM_HOST: 4212b730f8bSJeremy L Thompson if (impl->h_array_borrowed) impl->h_array = impl->h_array_borrowed; 4222b730f8bSJeremy L Thompson else impl->h_array = impl->h_array_owned; 4230d0321e0SJeremy L Thompson break; 4240d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 4252b730f8bSJeremy L Thompson if (impl->d_array_borrowed) impl->d_array = impl->d_array_borrowed; 4262b730f8bSJeremy L Thompson else impl->d_array = impl->d_array_owned; 4270d0321e0SJeremy L Thompson } 4280d0321e0SJeremy L Thompson } 42943c928f4SJeremy L Thompson return CeedVectorGetArray_Hip(vec, mem_type, array); 4300d0321e0SJeremy L Thompson } 4310d0321e0SJeremy L Thompson 4320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4330d0321e0SJeremy L Thompson // Get the norm of a CeedVector 4340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4352b730f8bSJeremy L Thompson static int CeedVectorNorm_Hip(CeedVector vec, CeedNormType type, CeedScalar *norm) { 4360d0321e0SJeremy L Thompson Ceed ceed; 4371f9221feSJeremy L Thompson CeedSize length; 438*b7453713SJeremy L Thompson const CeedScalar *d_array; 439*b7453713SJeremy L Thompson CeedVector_Hip *impl; 4400d0321e0SJeremy L Thompson hipblasHandle_t handle; 441*b7453713SJeremy L Thompson 442*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 443*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 444*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 445eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetHipblasHandle_Hip(ceed, &handle)); 4460d0321e0SJeremy L Thompson 4479330daecSnbeams // Is the vector too long to handle with int32? If so, we will divide 4489330daecSnbeams // it up into "int32-sized" subsections and make repeated BLAS calls. 4499330daecSnbeams CeedSize num_calls = length / INT_MAX; 450*b7453713SJeremy L Thompson 4519330daecSnbeams if (length % INT_MAX > 0) num_calls += 1; 4529330daecSnbeams 4530d0321e0SJeremy L Thompson // Compute norm 4542b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array)); 4550d0321e0SJeremy L Thompson switch (type) { 4560d0321e0SJeremy L Thompson case CEED_NORM_1: { 457f6f49adbSnbeams *norm = 0.0; 4580d0321e0SJeremy L Thompson if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 4599330daecSnbeams float sub_norm = 0.0; 4609330daecSnbeams float *d_array_start; 461*b7453713SJeremy L Thompson 4629330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 4639330daecSnbeams d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 4649330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 4659330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 466*b7453713SJeremy L Thompson 4679330daecSnbeams CeedCallHipblas(ceed, hipblasSasum(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm)); 4689330daecSnbeams *norm += sub_norm; 4699330daecSnbeams } 4700d0321e0SJeremy L Thompson } else { 4719330daecSnbeams double sub_norm = 0.0; 4729330daecSnbeams double *d_array_start; 473*b7453713SJeremy L Thompson 4749330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 4759330daecSnbeams d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 4769330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 4779330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 478*b7453713SJeremy L Thompson 4799330daecSnbeams CeedCallHipblas(ceed, hipblasDasum(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm)); 4809330daecSnbeams *norm += sub_norm; 4819330daecSnbeams } 4829330daecSnbeams } 4830d0321e0SJeremy L Thompson break; 4840d0321e0SJeremy L Thompson } 4850d0321e0SJeremy L Thompson case CEED_NORM_2: { 4860d0321e0SJeremy L Thompson if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 4879330daecSnbeams float sub_norm = 0.0, norm_sum = 0.0; 4889330daecSnbeams float *d_array_start; 489*b7453713SJeremy L Thompson 4909330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 4919330daecSnbeams d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 4929330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 4939330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 494*b7453713SJeremy L Thompson 4959330daecSnbeams CeedCallHipblas(ceed, hipblasSnrm2(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm)); 4969330daecSnbeams norm_sum += sub_norm * sub_norm; 4979330daecSnbeams } 4989330daecSnbeams *norm = sqrt(norm_sum); 4990d0321e0SJeremy L Thompson } else { 5009330daecSnbeams double sub_norm = 0.0, norm_sum = 0.0; 5019330daecSnbeams double *d_array_start; 502*b7453713SJeremy L Thompson 5039330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 5049330daecSnbeams d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 5059330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 5069330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 507*b7453713SJeremy L Thompson 5089330daecSnbeams CeedCallHipblas(ceed, hipblasDnrm2(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm)); 5099330daecSnbeams norm_sum += sub_norm * sub_norm; 5109330daecSnbeams } 5119330daecSnbeams *norm = sqrt(norm_sum); 5129330daecSnbeams } 5130d0321e0SJeremy L Thompson break; 5140d0321e0SJeremy L Thompson } 5150d0321e0SJeremy L Thompson case CEED_NORM_MAX: { 516*b7453713SJeremy L Thompson CeedInt index; 517*b7453713SJeremy L Thompson 5180d0321e0SJeremy L Thompson if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 5199330daecSnbeams float sub_max = 0.0, current_max = 0.0; 5209330daecSnbeams float *d_array_start; 5219330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 5229330daecSnbeams d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 5239330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 5249330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 525*b7453713SJeremy L Thompson 526*b7453713SJeremy L Thompson CeedCallHipblas(ceed, hipblasIsamax(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &index)); 527*b7453713SJeremy L Thompson CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost)); 5289330daecSnbeams if (fabs(sub_max) > current_max) current_max = fabs(sub_max); 5299330daecSnbeams } 5309330daecSnbeams *norm = current_max; 5319330daecSnbeams } else { 5329330daecSnbeams double sub_max = 0.0, current_max = 0.0; 5339330daecSnbeams double *d_array_start; 534*b7453713SJeremy L Thompson 5359330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 5369330daecSnbeams d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 5379330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 5389330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 539*b7453713SJeremy L Thompson 540*b7453713SJeremy L Thompson CeedCallHipblas(ceed, hipblasIdamax(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &index)); 541*b7453713SJeremy L Thompson CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost)); 5429330daecSnbeams if (fabs(sub_max) > current_max) current_max = fabs(sub_max); 5439330daecSnbeams } 5449330daecSnbeams *norm = current_max; 5459330daecSnbeams } 5460d0321e0SJeremy L Thompson break; 5470d0321e0SJeremy L Thompson } 5480d0321e0SJeremy L Thompson } 5492b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array)); 5500d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5510d0321e0SJeremy L Thompson } 5520d0321e0SJeremy L Thompson 5530d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5540d0321e0SJeremy L Thompson // Take reciprocal of a vector on host 5550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5569330daecSnbeams static int CeedHostReciprocal_Hip(CeedScalar *h_array, CeedSize length) { 5579330daecSnbeams for (CeedSize i = 0; i < length; i++) { 5582b730f8bSJeremy L Thompson if (fabs(h_array[i]) > CEED_EPSILON) h_array[i] = 1. / h_array[i]; 5592b730f8bSJeremy L Thompson } 5600d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5610d0321e0SJeremy L Thompson } 5620d0321e0SJeremy L Thompson 5630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5640d0321e0SJeremy L Thompson // Take reciprocal of a vector on device (impl in .cu file) 5650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5669330daecSnbeams int CeedDeviceReciprocal_Hip(CeedScalar *d_array, CeedSize length); 5670d0321e0SJeremy L Thompson 5680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5690d0321e0SJeremy L Thompson // Take reciprocal of a vector 5700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5710d0321e0SJeremy L Thompson static int CeedVectorReciprocal_Hip(CeedVector vec) { 5720d0321e0SJeremy L Thompson Ceed ceed; 5731f9221feSJeremy L Thompson CeedSize length; 574*b7453713SJeremy L Thompson CeedVector_Hip *impl; 5750d0321e0SJeremy L Thompson 576*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 577*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 578*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 5790d0321e0SJeremy L Thompson // Set value for synced device/host array 5802b730f8bSJeremy L Thompson if (impl->d_array) CeedCallBackend(CeedDeviceReciprocal_Hip(impl->d_array, length)); 5812b730f8bSJeremy L Thompson if (impl->h_array) CeedCallBackend(CeedHostReciprocal_Hip(impl->h_array, length)); 5820d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5830d0321e0SJeremy L Thompson } 5840d0321e0SJeremy L Thompson 5850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5860d0321e0SJeremy L Thompson // Compute x = alpha x on the host 5870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5889330daecSnbeams static int CeedHostScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length) { 5899330daecSnbeams for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha; 5900d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5910d0321e0SJeremy L Thompson } 5920d0321e0SJeremy L Thompson 5930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5940d0321e0SJeremy L Thompson // Compute x = alpha x on device (impl in .cu file) 5950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5969330daecSnbeams int CeedDeviceScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length); 5970d0321e0SJeremy L Thompson 5980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5990d0321e0SJeremy L Thompson // Compute x = alpha x 6000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6010d0321e0SJeremy L Thompson static int CeedVectorScale_Hip(CeedVector x, CeedScalar alpha) { 6020d0321e0SJeremy L Thompson Ceed ceed; 6031f9221feSJeremy L Thompson CeedSize length; 604*b7453713SJeremy L Thompson CeedVector_Hip *x_impl; 6050d0321e0SJeremy L Thompson 606*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(x, &ceed)); 607*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(x, &x_impl)); 608*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(x, &length)); 6090d0321e0SJeremy L Thompson // Set value for synced device/host array 6102b730f8bSJeremy L Thompson if (x_impl->d_array) CeedCallBackend(CeedDeviceScale_Hip(x_impl->d_array, alpha, length)); 6112b730f8bSJeremy L Thompson if (x_impl->h_array) CeedCallBackend(CeedHostScale_Hip(x_impl->h_array, alpha, length)); 6120d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6130d0321e0SJeremy L Thompson } 6140d0321e0SJeremy L Thompson 6150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6160d0321e0SJeremy L Thompson // Compute y = alpha x + y on the host 6170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6189330daecSnbeams static int CeedHostAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length) { 6199330daecSnbeams for (CeedSize i = 0; i < length; i++) y_array[i] += alpha * x_array[i]; 6200d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6210d0321e0SJeremy L Thompson } 6220d0321e0SJeremy L Thompson 6230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6240d0321e0SJeremy L Thompson // Compute y = alpha x + y on device (impl in .cu file) 6250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6269330daecSnbeams int CeedDeviceAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length); 6270d0321e0SJeremy L Thompson 6280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6290d0321e0SJeremy L Thompson // Compute y = alpha x + y 6300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6310d0321e0SJeremy L Thompson static int CeedVectorAXPY_Hip(CeedVector y, CeedScalar alpha, CeedVector x) { 6320d0321e0SJeremy L Thompson Ceed ceed; 633*b7453713SJeremy L Thompson CeedSize length; 6340d0321e0SJeremy L Thompson CeedVector_Hip *y_impl, *x_impl; 635*b7453713SJeremy L Thompson 636*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(y, &ceed)); 6372b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(y, &y_impl)); 6382b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(x, &x_impl)); 6392b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(y, &length)); 6400d0321e0SJeremy L Thompson // Set value for synced device/host array 6410d0321e0SJeremy L Thompson if (y_impl->d_array) { 6422b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 6432b730f8bSJeremy L Thompson CeedCallBackend(CeedDeviceAXPY_Hip(y_impl->d_array, alpha, x_impl->d_array, length)); 6440d0321e0SJeremy L Thompson } 6450d0321e0SJeremy L Thompson if (y_impl->h_array) { 6462b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 6472b730f8bSJeremy L Thompson CeedCallBackend(CeedHostAXPY_Hip(y_impl->h_array, alpha, x_impl->h_array, length)); 6480d0321e0SJeremy L Thompson } 6490d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6500d0321e0SJeremy L Thompson } 651ff1e7120SSebastian Grimberg 6525fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6535fb68f37SKaren (Ren) Stengel // Compute y = alpha x + beta y on the host 6545fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6559330daecSnbeams static int CeedHostAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length) { 6569330daecSnbeams for (CeedSize i = 0; i < length; i++) y_array[i] += alpha * x_array[i] + beta * y_array[i]; 6575fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 6585fb68f37SKaren (Ren) Stengel } 6595fb68f37SKaren (Ren) Stengel 6605fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6615fb68f37SKaren (Ren) Stengel // Compute y = alpha x + beta y on device (impl in .cu file) 6625fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6639330daecSnbeams int CeedDeviceAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length); 6645fb68f37SKaren (Ren) Stengel 6655fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6665fb68f37SKaren (Ren) Stengel // Compute y = alpha x + beta y 6675fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6685fb68f37SKaren (Ren) Stengel static int CeedVectorAXPBY_Hip(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 6695fb68f37SKaren (Ren) Stengel Ceed ceed; 670*b7453713SJeremy L Thompson CeedSize length; 6715fb68f37SKaren (Ren) Stengel CeedVector_Hip *y_impl, *x_impl; 672*b7453713SJeremy L Thompson 673*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(y, &ceed)); 6745fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedVectorGetData(y, &y_impl)); 6755fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedVectorGetData(x, &x_impl)); 6765fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedVectorGetLength(y, &length)); 6775fb68f37SKaren (Ren) Stengel // Set value for synced device/host array 6785fb68f37SKaren (Ren) Stengel if (y_impl->d_array) { 6795fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 6805fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedDeviceAXPBY_Hip(y_impl->d_array, alpha, beta, x_impl->d_array, length)); 6815fb68f37SKaren (Ren) Stengel } 6825fb68f37SKaren (Ren) Stengel if (y_impl->h_array) { 6835fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 6845fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedHostAXPBY_Hip(y_impl->h_array, alpha, beta, x_impl->h_array, length)); 6855fb68f37SKaren (Ren) Stengel } 6865fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 6875fb68f37SKaren (Ren) Stengel } 6880d0321e0SJeremy L Thompson 6890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6900d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y on the host 6910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6929330daecSnbeams static int CeedHostPointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length) { 6939330daecSnbeams for (CeedSize i = 0; i < length; i++) w_array[i] = x_array[i] * y_array[i]; 6940d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6950d0321e0SJeremy L Thompson } 6960d0321e0SJeremy L Thompson 6970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6980d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y on device (impl in .cu file) 6990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7009330daecSnbeams int CeedDevicePointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length); 7010d0321e0SJeremy L Thompson 7020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7030d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y 7040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7052b730f8bSJeremy L Thompson static int CeedVectorPointwiseMult_Hip(CeedVector w, CeedVector x, CeedVector y) { 7060d0321e0SJeremy L Thompson Ceed ceed; 707*b7453713SJeremy L Thompson CeedSize length; 7080d0321e0SJeremy L Thompson CeedVector_Hip *w_impl, *x_impl, *y_impl; 709*b7453713SJeremy L Thompson 710*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(w, &ceed)); 7112b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(w, &w_impl)); 7122b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(x, &x_impl)); 7132b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(y, &y_impl)); 7142b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(w, &length)); 7150d0321e0SJeremy L Thompson 7160d0321e0SJeremy L Thompson // Set value for synced device/host array 7170d0321e0SJeremy L Thompson if (!w_impl->d_array && !w_impl->h_array) { 7182b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(w, 0.0)); 7190d0321e0SJeremy L Thompson } 7200d0321e0SJeremy L Thompson if (w_impl->d_array) { 7212b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 7222b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_DEVICE)); 7232b730f8bSJeremy L Thompson CeedCallBackend(CeedDevicePointwiseMult_Hip(w_impl->d_array, x_impl->d_array, y_impl->d_array, length)); 7240d0321e0SJeremy L Thompson } 7250d0321e0SJeremy L Thompson if (w_impl->h_array) { 7262b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 7272b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_HOST)); 7282b730f8bSJeremy L Thompson CeedCallBackend(CeedHostPointwiseMult_Hip(w_impl->h_array, x_impl->h_array, y_impl->h_array, length)); 7290d0321e0SJeremy L Thompson } 7300d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7310d0321e0SJeremy L Thompson } 7320d0321e0SJeremy L Thompson 7330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7340d0321e0SJeremy L Thompson // Destroy the vector 7350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7360d0321e0SJeremy L Thompson static int CeedVectorDestroy_Hip(const CeedVector vec) { 7370d0321e0SJeremy L Thompson Ceed ceed; 7380d0321e0SJeremy L Thompson CeedVector_Hip *impl; 7390d0321e0SJeremy L Thompson 740*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 741*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 7422b730f8bSJeremy L Thompson CeedCallHip(ceed, hipFree(impl->d_array_owned)); 7432b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->h_array_owned)); 7442b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 7450d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7460d0321e0SJeremy L Thompson } 7470d0321e0SJeremy L Thompson 7480d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7490d0321e0SJeremy L Thompson // Create a vector of the specified length (does not allocate memory) 7500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7511f9221feSJeremy L Thompson int CeedVectorCreate_Hip(CeedSize n, CeedVector vec) { 7520d0321e0SJeremy L Thompson CeedVector_Hip *impl; 7530d0321e0SJeremy L Thompson Ceed ceed; 7540d0321e0SJeremy L Thompson 755*b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 7562b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Hip)); 7572b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Hip)); 7582b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Hip)); 7592b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Hip)); 760008736bdSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValue", (int (*)())CeedVectorSetValue_Hip)); 7612b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SyncArray", CeedVectorSyncArray_Hip)); 7622b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Hip)); 7632b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Hip)); 7642b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Hip)); 7652b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Norm", CeedVectorNorm_Hip)); 7662b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Reciprocal", CeedVectorReciprocal_Hip)); 767008736bdSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Scale", (int (*)())CeedVectorScale_Hip)); 768008736bdSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPY", (int (*)())CeedVectorAXPY_Hip)); 769008736bdSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", (int (*)())CeedVectorAXPBY_Hip)); 7702b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Hip)); 7712b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Hip)); 7722b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 7732b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetData(vec, impl)); 7740d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7750d0321e0SJeremy L Thompson } 7762a86cc9dSSebastian Grimberg 7772a86cc9dSSebastian Grimberg //------------------------------------------------------------------------------ 778