15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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; 24b7453713SJeremy L Thompson 25b7453713SJeremy 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; 43b7453713SJeremy L Thompson CeedSize length; 44672b0f2aSSebastian Grimberg size_t bytes; 450d0321e0SJeremy L Thompson CeedVector_Hip *impl; 46b7453713SJeremy L Thompson 47b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 482b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 490d0321e0SJeremy L Thompson 506574a04fSJeremy L Thompson CeedCheck(impl->h_array, ceed, CEED_ERROR_BACKEND, "No valid host data to sync to device"); 510d0321e0SJeremy L Thompson 52672b0f2aSSebastian Grimberg CeedCallBackend(CeedVectorGetLength(vec, &length)); 53672b0f2aSSebastian Grimberg bytes = length * sizeof(CeedScalar); 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; 71b7453713SJeremy L Thompson CeedSize length; 72672b0f2aSSebastian Grimberg size_t bytes; 730d0321e0SJeremy L Thompson CeedVector_Hip *impl; 74b7453713SJeremy L Thompson 75b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 762b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 770d0321e0SJeremy L Thompson 786574a04fSJeremy L Thompson CeedCheck(impl->d_array, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host"); 790d0321e0SJeremy L Thompson 800d0321e0SJeremy L Thompson if (impl->h_array_borrowed) { 810d0321e0SJeremy L Thompson impl->h_array = impl->h_array_borrowed; 820d0321e0SJeremy L Thompson } else if (impl->h_array_owned) { 830d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 840d0321e0SJeremy L Thompson } else { 851f9221feSJeremy L Thompson CeedSize length; 86672b0f2aSSebastian Grimberg 872b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 882b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(length, &impl->h_array_owned)); 890d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 900d0321e0SJeremy L Thompson } 910d0321e0SJeremy L Thompson 922b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 93672b0f2aSSebastian Grimberg bytes = length * sizeof(CeedScalar); 94b7453713SJeremy L Thompson CeedCallHip(ceed, hipMemcpy(impl->h_array, impl->d_array, bytes, hipMemcpyDeviceToHost)); 950d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 960d0321e0SJeremy L Thompson } 970d0321e0SJeremy L Thompson 980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 990d0321e0SJeremy L Thompson // Sync arrays 1000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1012b730f8bSJeremy L Thompson static int CeedVectorSyncArray_Hip(const CeedVector vec, CeedMemType mem_type) { 102f48ed27dSnbeams bool need_sync = false; 103b7453713SJeremy L Thompson 104b7453713SJeremy L Thompson // Check whether device/host sync is needed 1052b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorNeedSync_Hip(vec, mem_type, &need_sync)); 1062b730f8bSJeremy L Thompson if (!need_sync) return CEED_ERROR_SUCCESS; 107f48ed27dSnbeams 10843c928f4SJeremy L Thompson switch (mem_type) { 1092b730f8bSJeremy L Thompson case CEED_MEM_HOST: 1102b730f8bSJeremy L Thompson return CeedVectorSyncD2H_Hip(vec); 1112b730f8bSJeremy L Thompson case CEED_MEM_DEVICE: 1122b730f8bSJeremy L Thompson return CeedVectorSyncH2D_Hip(vec); 1130d0321e0SJeremy L Thompson } 1140d0321e0SJeremy L Thompson return CEED_ERROR_UNSUPPORTED; 1150d0321e0SJeremy L Thompson } 1160d0321e0SJeremy L Thompson 1170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1180d0321e0SJeremy L Thompson // Set all pointers as invalid 1190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1200d0321e0SJeremy L Thompson static inline int CeedVectorSetAllInvalid_Hip(const CeedVector vec) { 1210d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1220d0321e0SJeremy L Thompson 123b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1240d0321e0SJeremy L Thompson impl->h_array = NULL; 1250d0321e0SJeremy L Thompson impl->d_array = NULL; 1260d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1270d0321e0SJeremy L Thompson } 1280d0321e0SJeremy L Thompson 1290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 130b2165e7aSSebastian Grimberg // Check if CeedVector has any valid pointer 1310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1322b730f8bSJeremy L Thompson static inline int CeedVectorHasValidArray_Hip(const CeedVector vec, bool *has_valid_array) { 1330d0321e0SJeremy L Thompson CeedVector_Hip *impl; 134b7453713SJeremy L Thompson 1352b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 1361c66c397SJeremy L Thompson *has_valid_array = impl->h_array || impl->d_array; 1370d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1380d0321e0SJeremy L Thompson } 1390d0321e0SJeremy L Thompson 1400d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 141b2165e7aSSebastian Grimberg // Check if has array of given type 1420d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1432b730f8bSJeremy L Thompson static inline int CeedVectorHasArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_array_of_type) { 1440d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1450d0321e0SJeremy L Thompson 146b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 14743c928f4SJeremy L Thompson switch (mem_type) { 1480d0321e0SJeremy L Thompson case CEED_MEM_HOST: 1491c66c397SJeremy L Thompson *has_array_of_type = impl->h_array_borrowed || impl->h_array_owned; 1500d0321e0SJeremy L Thompson break; 1510d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 1521c66c397SJeremy L Thompson *has_array_of_type = impl->d_array_borrowed || impl->d_array_owned; 1530d0321e0SJeremy L Thompson break; 1540d0321e0SJeremy L Thompson } 1550d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1560d0321e0SJeremy L Thompson } 1570d0321e0SJeremy L Thompson 1580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1590d0321e0SJeremy L Thompson // Check if has borrowed array of given type 1600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1612b730f8bSJeremy L Thompson static inline int CeedVectorHasBorrowedArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) { 1620d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1630d0321e0SJeremy L Thompson 164b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 16543c928f4SJeremy L Thompson switch (mem_type) { 1660d0321e0SJeremy L Thompson case CEED_MEM_HOST: 1671c66c397SJeremy L Thompson *has_borrowed_array_of_type = impl->h_array_borrowed; 1680d0321e0SJeremy L Thompson break; 1690d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 1701c66c397SJeremy L Thompson *has_borrowed_array_of_type = impl->d_array_borrowed; 1710d0321e0SJeremy L Thompson break; 1720d0321e0SJeremy L Thompson } 1730d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1740d0321e0SJeremy L Thompson } 1750d0321e0SJeremy L Thompson 1760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1770d0321e0SJeremy L Thompson // Set array from host 1780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1792b730f8bSJeremy L Thompson static int CeedVectorSetArrayHost_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) { 180a267acd1SJeremy L Thompson CeedSize length; 1810d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1820d0321e0SJeremy L Thompson 183b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 184a267acd1SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 185a267acd1SJeremy L Thompson 186f5d1e504SJeremy L Thompson CeedCallBackend(CeedSetHostCeedScalarArray(array, copy_mode, length, (const CeedScalar **)&impl->h_array_owned, 187f5d1e504SJeremy L Thompson (const CeedScalar **)&impl->h_array_borrowed, (const CeedScalar **)&impl->h_array)); 1880d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1890d0321e0SJeremy L Thompson } 1900d0321e0SJeremy L Thompson 1910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1920d0321e0SJeremy L Thompson // Set array from device 1930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 1942b730f8bSJeremy L Thompson static int CeedVectorSetArrayDevice_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) { 195a267acd1SJeremy L Thompson CeedSize length; 1960d0321e0SJeremy L Thompson Ceed ceed; 1970d0321e0SJeremy L Thompson CeedVector_Hip *impl; 1980d0321e0SJeremy L Thompson 199b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 200b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 201a267acd1SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 202f5d1e504SJeremy L Thompson 203f5d1e504SJeremy L Thompson CeedCallBackend(CeedSetDeviceCeedScalarArray_Hip(ceed, array, copy_mode, length, (const CeedScalar **)&impl->d_array_owned, 204f5d1e504SJeremy L Thompson (const CeedScalar **)&impl->d_array_borrowed, (const CeedScalar **)&impl->d_array)); 2050d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2060d0321e0SJeremy L Thompson } 2070d0321e0SJeremy L Thompson 2080d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2090d0321e0SJeremy L Thompson // Set the array used by a vector, 2100d0321e0SJeremy L Thompson // freeing any previously allocated array if applicable 2110d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2122b730f8bSJeremy L Thompson static int CeedVectorSetArray_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedCopyMode copy_mode, CeedScalar *array) { 2130d0321e0SJeremy L Thompson CeedVector_Hip *impl; 2140d0321e0SJeremy L Thompson 215b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 2162b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec)); 21743c928f4SJeremy L Thompson switch (mem_type) { 2180d0321e0SJeremy L Thompson case CEED_MEM_HOST: 21943c928f4SJeremy L Thompson return CeedVectorSetArrayHost_Hip(vec, copy_mode, array); 2200d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 22143c928f4SJeremy L Thompson return CeedVectorSetArrayDevice_Hip(vec, copy_mode, array); 2220d0321e0SJeremy L Thompson } 2230d0321e0SJeremy L Thompson return CEED_ERROR_UNSUPPORTED; 2240d0321e0SJeremy L Thompson } 2250d0321e0SJeremy L Thompson 2260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2273196072fSJeremy L Thompson // Copy host array to value strided 2283196072fSJeremy L Thompson //------------------------------------------------------------------------------ 2293196072fSJeremy L Thompson static int CeedHostCopyStrided_Hip(CeedScalar *h_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar *h_copy_array) { 2303196072fSJeremy L Thompson for (CeedSize i = start; i < length; i += step) h_copy_array[i] = h_array[i]; 2313196072fSJeremy L Thompson return CEED_ERROR_SUCCESS; 2323196072fSJeremy L Thompson } 2333196072fSJeremy L Thompson 2343196072fSJeremy L Thompson //------------------------------------------------------------------------------ 235*956a3dbaSJeremy L Thompson // Copy device array to value strided (impl in .hip.cpp file) 2363196072fSJeremy L Thompson //------------------------------------------------------------------------------ 2373196072fSJeremy L Thompson int CeedDeviceCopyStrided_Hip(CeedScalar *d_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar *d_copy_array); 2383196072fSJeremy L Thompson 2393196072fSJeremy L Thompson //------------------------------------------------------------------------------ 2403196072fSJeremy L Thompson // Copy a vector to a value strided 2413196072fSJeremy L Thompson //------------------------------------------------------------------------------ 2423196072fSJeremy L Thompson static int CeedVectorCopyStrided_Hip(CeedVector vec, CeedSize start, CeedSize step, CeedVector vec_copy) { 2433196072fSJeremy L Thompson CeedSize length; 2443196072fSJeremy L Thompson CeedVector_Hip *impl; 2453196072fSJeremy L Thompson 2463196072fSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 2473196072fSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 2483196072fSJeremy L Thompson // Set value for synced device/host array 2493196072fSJeremy L Thompson if (impl->d_array) { 2503196072fSJeremy L Thompson CeedScalar *copy_array; 2513196072fSJeremy L Thompson 2523196072fSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_DEVICE, ©_array)); 2533196072fSJeremy L Thompson CeedCallBackend(CeedDeviceCopyStrided_Hip(impl->d_array, start, step, length, copy_array)); 2543196072fSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(vec_copy, ©_array)); 2553196072fSJeremy L Thompson } else if (impl->h_array) { 2563196072fSJeremy L Thompson CeedScalar *copy_array; 2573196072fSJeremy L Thompson 2583196072fSJeremy L Thompson CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, ©_array)); 2593196072fSJeremy L Thompson CeedCallBackend(CeedHostCopyStrided_Hip(impl->h_array, start, step, length, copy_array)); 2603196072fSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(vec_copy, ©_array)); 2613196072fSJeremy L Thompson } else { 2623196072fSJeremy L Thompson return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set"); 2633196072fSJeremy L Thompson } 2643196072fSJeremy L Thompson return CEED_ERROR_SUCCESS; 2653196072fSJeremy L Thompson } 2663196072fSJeremy L Thompson 2673196072fSJeremy L Thompson //------------------------------------------------------------------------------ 2680d0321e0SJeremy L Thompson // Set host array to value 2690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2709330daecSnbeams static int CeedHostSetValue_Hip(CeedScalar *h_array, CeedSize length, CeedScalar val) { 2719330daecSnbeams for (CeedSize i = 0; i < length; i++) h_array[i] = val; 2720d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2730d0321e0SJeremy L Thompson } 2740d0321e0SJeremy L Thompson 2750d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2760d0321e0SJeremy L Thompson // Set device array to value (impl in .hip file) 2770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2789330daecSnbeams int CeedDeviceSetValue_Hip(CeedScalar *d_array, CeedSize length, CeedScalar val); 2790d0321e0SJeremy L Thompson 2800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 281b2165e7aSSebastian Grimberg // Set a vector to a value 2820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 2830d0321e0SJeremy L Thompson static int CeedVectorSetValue_Hip(CeedVector vec, CeedScalar val) { 2841f9221feSJeremy L Thompson CeedSize length; 285b7453713SJeremy L Thompson CeedVector_Hip *impl; 2860d0321e0SJeremy L Thompson 287b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 288b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 2890d0321e0SJeremy L Thompson // Set value for synced device/host array 2900d0321e0SJeremy L Thompson if (!impl->d_array && !impl->h_array) { 2910d0321e0SJeremy L Thompson if (impl->d_array_borrowed) { 2920d0321e0SJeremy L Thompson impl->d_array = impl->d_array_borrowed; 2930d0321e0SJeremy L Thompson } else if (impl->h_array_borrowed) { 2940d0321e0SJeremy L Thompson impl->h_array = impl->h_array_borrowed; 2950d0321e0SJeremy L Thompson } else if (impl->d_array_owned) { 2960d0321e0SJeremy L Thompson impl->d_array = impl->d_array_owned; 2970d0321e0SJeremy L Thompson } else if (impl->h_array_owned) { 2980d0321e0SJeremy L Thompson impl->h_array = impl->h_array_owned; 2990d0321e0SJeremy L Thompson } else { 3002b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_DEVICE, CEED_COPY_VALUES, NULL)); 3010d0321e0SJeremy L Thompson } 3020d0321e0SJeremy L Thompson } 3030d0321e0SJeremy L Thompson if (impl->d_array) { 3042b730f8bSJeremy L Thompson CeedCallBackend(CeedDeviceSetValue_Hip(impl->d_array, length, val)); 305b2165e7aSSebastian Grimberg impl->h_array = NULL; 3060d0321e0SJeremy L Thompson } 3070d0321e0SJeremy L Thompson if (impl->h_array) { 3082b730f8bSJeremy L Thompson CeedCallBackend(CeedHostSetValue_Hip(impl->h_array, length, val)); 309b2165e7aSSebastian Grimberg impl->d_array = NULL; 3100d0321e0SJeremy L Thompson } 3110d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3120d0321e0SJeremy L Thompson } 3130d0321e0SJeremy L Thompson 3140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3153196072fSJeremy L Thompson // Set host array to value strided 3163196072fSJeremy L Thompson //------------------------------------------------------------------------------ 3173196072fSJeremy L Thompson static int CeedHostSetValueStrided_Hip(CeedScalar *h_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar val) { 3183196072fSJeremy L Thompson for (CeedSize i = start; i < length; i += step) h_array[i] = val; 3193196072fSJeremy L Thompson return CEED_ERROR_SUCCESS; 3203196072fSJeremy L Thompson } 3213196072fSJeremy L Thompson 3223196072fSJeremy L Thompson //------------------------------------------------------------------------------ 323*956a3dbaSJeremy L Thompson // Set device array to value strided (impl in .hip.cpp file) 3243196072fSJeremy L Thompson //------------------------------------------------------------------------------ 3253196072fSJeremy L Thompson int CeedDeviceSetValueStrided_Hip(CeedScalar *d_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar val); 3263196072fSJeremy L Thompson 3273196072fSJeremy L Thompson //------------------------------------------------------------------------------ 3283196072fSJeremy L Thompson // Set a vector to a value strided 3293196072fSJeremy L Thompson //------------------------------------------------------------------------------ 3303196072fSJeremy L Thompson static int CeedVectorSetValueStrided_Hip(CeedVector vec, CeedSize start, CeedSize step, CeedScalar val) { 3313196072fSJeremy L Thompson CeedSize length; 3323196072fSJeremy L Thompson CeedVector_Hip *impl; 3333196072fSJeremy L Thompson 3343196072fSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 3353196072fSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 3363196072fSJeremy L Thompson // Set value for synced device/host array 3373196072fSJeremy L Thompson if (impl->d_array) { 3383196072fSJeremy L Thompson CeedCallBackend(CeedDeviceSetValueStrided_Hip(impl->d_array, start, step, length, val)); 3393196072fSJeremy L Thompson impl->h_array = NULL; 3403196072fSJeremy L Thompson } else if (impl->h_array) { 3413196072fSJeremy L Thompson CeedCallBackend(CeedHostSetValueStrided_Hip(impl->h_array, start, step, length, val)); 3423196072fSJeremy L Thompson impl->d_array = NULL; 3433196072fSJeremy L Thompson } else { 3443196072fSJeremy L Thompson return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set"); 3453196072fSJeremy L Thompson } 3463196072fSJeremy L Thompson return CEED_ERROR_SUCCESS; 3473196072fSJeremy L Thompson } 3483196072fSJeremy L Thompson 3493196072fSJeremy L Thompson //------------------------------------------------------------------------------ 3500d0321e0SJeremy L Thompson // Vector Take Array 3510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3522b730f8bSJeremy L Thompson static int CeedVectorTakeArray_Hip(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { 3530d0321e0SJeremy L Thompson CeedVector_Hip *impl; 354b7453713SJeremy L Thompson 3552b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 3560d0321e0SJeremy L Thompson 35743c928f4SJeremy L Thompson // Sync array to requested mem_type 3582b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(vec, mem_type)); 3590d0321e0SJeremy L Thompson 3600d0321e0SJeremy L Thompson // Update pointer 36143c928f4SJeremy L Thompson switch (mem_type) { 3620d0321e0SJeremy L Thompson case CEED_MEM_HOST: 3630d0321e0SJeremy L Thompson (*array) = impl->h_array_borrowed; 3640d0321e0SJeremy L Thompson impl->h_array_borrowed = NULL; 3650d0321e0SJeremy L Thompson impl->h_array = NULL; 3660d0321e0SJeremy L Thompson break; 3670d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 3680d0321e0SJeremy L Thompson (*array) = impl->d_array_borrowed; 3690d0321e0SJeremy L Thompson impl->d_array_borrowed = NULL; 3700d0321e0SJeremy L Thompson impl->d_array = NULL; 3710d0321e0SJeremy L Thompson break; 3720d0321e0SJeremy L Thompson } 3730d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3740d0321e0SJeremy L Thompson } 3750d0321e0SJeremy L Thompson 3760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3770d0321e0SJeremy L Thompson // Core logic for array syncronization for GetArray. 3780d0321e0SJeremy L Thompson // If a different memory type is most up to date, this will perform a copy 3790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3802b730f8bSJeremy L Thompson static int CeedVectorGetArrayCore_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 3810d0321e0SJeremy L Thompson CeedVector_Hip *impl; 382b7453713SJeremy L Thompson 3832b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 3840d0321e0SJeremy L Thompson 38543c928f4SJeremy L Thompson // Sync array to requested mem_type 3862b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(vec, mem_type)); 3870d0321e0SJeremy L Thompson 3880d0321e0SJeremy L Thompson // Update pointer 38943c928f4SJeremy L Thompson switch (mem_type) { 3900d0321e0SJeremy L Thompson case CEED_MEM_HOST: 3910d0321e0SJeremy L Thompson *array = impl->h_array; 3920d0321e0SJeremy L Thompson break; 3930d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 3940d0321e0SJeremy L Thompson *array = impl->d_array; 3950d0321e0SJeremy L Thompson break; 3960d0321e0SJeremy L Thompson } 3970d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3980d0321e0SJeremy L Thompson } 3990d0321e0SJeremy L Thompson 4000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 40143c928f4SJeremy L Thompson // Get read-only access to a vector via the specified mem_type 4020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4032b730f8bSJeremy L Thompson static int CeedVectorGetArrayRead_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedScalar **array) { 40443c928f4SJeremy L Thompson return CeedVectorGetArrayCore_Hip(vec, mem_type, (CeedScalar **)array); 4050d0321e0SJeremy L Thompson } 4060d0321e0SJeremy L Thompson 4070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 40843c928f4SJeremy L Thompson // Get read/write access to a vector via the specified mem_type 4090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4102b730f8bSJeremy L Thompson static int CeedVectorGetArray_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 4110d0321e0SJeremy L Thompson CeedVector_Hip *impl; 412b7453713SJeremy L Thompson 4132b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 4142b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayCore_Hip(vec, mem_type, array)); 4152b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec)); 41643c928f4SJeremy L Thompson switch (mem_type) { 4170d0321e0SJeremy L Thompson case CEED_MEM_HOST: 4180d0321e0SJeremy L Thompson impl->h_array = *array; 4190d0321e0SJeremy L Thompson break; 4200d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 4210d0321e0SJeremy L Thompson impl->d_array = *array; 4220d0321e0SJeremy L Thompson break; 4230d0321e0SJeremy L Thompson } 4240d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4250d0321e0SJeremy L Thompson } 4260d0321e0SJeremy L Thompson 4270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 42843c928f4SJeremy L Thompson // Get write access to a vector via the specified mem_type 4290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4302b730f8bSJeremy L Thompson static int CeedVectorGetArrayWrite_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) { 4310d0321e0SJeremy L Thompson bool has_array_of_type = true; 432b7453713SJeremy L Thompson CeedVector_Hip *impl; 433b7453713SJeremy L Thompson 434b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 4352b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorHasArrayOfType_Hip(vec, mem_type, &has_array_of_type)); 4360d0321e0SJeremy L Thompson if (!has_array_of_type) { 4370d0321e0SJeremy L Thompson // Allocate if array is not yet allocated 4382b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL)); 4390d0321e0SJeremy L Thompson } else { 4400d0321e0SJeremy L Thompson // Select dirty array 44143c928f4SJeremy L Thompson switch (mem_type) { 4420d0321e0SJeremy L Thompson case CEED_MEM_HOST: 4432b730f8bSJeremy L Thompson if (impl->h_array_borrowed) impl->h_array = impl->h_array_borrowed; 4442b730f8bSJeremy L Thompson else impl->h_array = impl->h_array_owned; 4450d0321e0SJeremy L Thompson break; 4460d0321e0SJeremy L Thompson case CEED_MEM_DEVICE: 4472b730f8bSJeremy L Thompson if (impl->d_array_borrowed) impl->d_array = impl->d_array_borrowed; 4482b730f8bSJeremy L Thompson else impl->d_array = impl->d_array_owned; 4490d0321e0SJeremy L Thompson } 4500d0321e0SJeremy L Thompson } 45143c928f4SJeremy L Thompson return CeedVectorGetArray_Hip(vec, mem_type, array); 4520d0321e0SJeremy L Thompson } 4530d0321e0SJeremy L Thompson 4540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4550d0321e0SJeremy L Thompson // Get the norm of a CeedVector 4560d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4572b730f8bSJeremy L Thompson static int CeedVectorNorm_Hip(CeedVector vec, CeedNormType type, CeedScalar *norm) { 4580d0321e0SJeremy L Thompson Ceed ceed; 459672b0f2aSSebastian Grimberg CeedSize length, num_calls; 460b7453713SJeremy L Thompson const CeedScalar *d_array; 461b7453713SJeremy L Thompson CeedVector_Hip *impl; 4620d0321e0SJeremy L Thompson hipblasHandle_t handle; 463b7453713SJeremy L Thompson 464b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 465b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 466b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 467eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetHipblasHandle_Hip(ceed, &handle)); 4680d0321e0SJeremy L Thompson 4699330daecSnbeams // Is the vector too long to handle with int32? If so, we will divide 4709330daecSnbeams // it up into "int32-sized" subsections and make repeated BLAS calls. 471672b0f2aSSebastian Grimberg num_calls = length / INT_MAX; 4729330daecSnbeams if (length % INT_MAX > 0) num_calls += 1; 4739330daecSnbeams 4740d0321e0SJeremy L Thompson // Compute norm 4752b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array)); 4760d0321e0SJeremy L Thompson switch (type) { 4770d0321e0SJeremy L Thompson case CEED_NORM_1: { 478f6f49adbSnbeams *norm = 0.0; 4790d0321e0SJeremy L Thompson if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 4809330daecSnbeams float sub_norm = 0.0; 4819330daecSnbeams float *d_array_start; 482b7453713SJeremy L Thompson 4839330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 4849330daecSnbeams d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 4859330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 4869330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 487b7453713SJeremy L Thompson 4889330daecSnbeams CeedCallHipblas(ceed, hipblasSasum(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm)); 4899330daecSnbeams *norm += sub_norm; 4909330daecSnbeams } 4910d0321e0SJeremy L Thompson } else { 4929330daecSnbeams double sub_norm = 0.0; 4939330daecSnbeams double *d_array_start; 494b7453713SJeremy L Thompson 4959330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 4969330daecSnbeams d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 4979330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 4989330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 499b7453713SJeremy L Thompson 5009330daecSnbeams CeedCallHipblas(ceed, hipblasDasum(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm)); 5019330daecSnbeams *norm += sub_norm; 5029330daecSnbeams } 5039330daecSnbeams } 5040d0321e0SJeremy L Thompson break; 5050d0321e0SJeremy L Thompson } 5060d0321e0SJeremy L Thompson case CEED_NORM_2: { 5070d0321e0SJeremy L Thompson if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 5089330daecSnbeams float sub_norm = 0.0, norm_sum = 0.0; 5099330daecSnbeams float *d_array_start; 510b7453713SJeremy L Thompson 5119330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 5129330daecSnbeams d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 5139330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 5149330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 515b7453713SJeremy L Thompson 5169330daecSnbeams CeedCallHipblas(ceed, hipblasSnrm2(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm)); 5179330daecSnbeams norm_sum += sub_norm * sub_norm; 5189330daecSnbeams } 5199330daecSnbeams *norm = sqrt(norm_sum); 5200d0321e0SJeremy L Thompson } else { 5219330daecSnbeams double sub_norm = 0.0, norm_sum = 0.0; 5229330daecSnbeams double *d_array_start; 523b7453713SJeremy L Thompson 5249330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 5259330daecSnbeams d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 5269330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 5279330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 528b7453713SJeremy L Thompson 5299330daecSnbeams CeedCallHipblas(ceed, hipblasDnrm2(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm)); 5309330daecSnbeams norm_sum += sub_norm * sub_norm; 5319330daecSnbeams } 5329330daecSnbeams *norm = sqrt(norm_sum); 5339330daecSnbeams } 5340d0321e0SJeremy L Thompson break; 5350d0321e0SJeremy L Thompson } 5360d0321e0SJeremy L Thompson case CEED_NORM_MAX: { 537b7453713SJeremy L Thompson CeedInt index; 538b7453713SJeremy L Thompson 5390d0321e0SJeremy L Thompson if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 5409330daecSnbeams float sub_max = 0.0, current_max = 0.0; 5419330daecSnbeams float *d_array_start; 5429330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 5439330daecSnbeams d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX; 5449330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 5459330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 546b7453713SJeremy L Thompson 547b7453713SJeremy L Thompson CeedCallHipblas(ceed, hipblasIsamax(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &index)); 548b7453713SJeremy L Thompson CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost)); 5499330daecSnbeams if (fabs(sub_max) > current_max) current_max = fabs(sub_max); 5509330daecSnbeams } 5519330daecSnbeams *norm = current_max; 5529330daecSnbeams } else { 5539330daecSnbeams double sub_max = 0.0, current_max = 0.0; 5549330daecSnbeams double *d_array_start; 555b7453713SJeremy L Thompson 5569330daecSnbeams for (CeedInt i = 0; i < num_calls; i++) { 5579330daecSnbeams d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX; 5589330daecSnbeams CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX; 5599330daecSnbeams CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX; 560b7453713SJeremy L Thompson 561b7453713SJeremy L Thompson CeedCallHipblas(ceed, hipblasIdamax(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &index)); 562b7453713SJeremy L Thompson CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost)); 5639330daecSnbeams if (fabs(sub_max) > current_max) current_max = fabs(sub_max); 5649330daecSnbeams } 5659330daecSnbeams *norm = current_max; 5669330daecSnbeams } 5670d0321e0SJeremy L Thompson break; 5680d0321e0SJeremy L Thompson } 5690d0321e0SJeremy L Thompson } 5702b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array)); 5710d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5720d0321e0SJeremy L Thompson } 5730d0321e0SJeremy L Thompson 5740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5750d0321e0SJeremy L Thompson // Take reciprocal of a vector on host 5760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5779330daecSnbeams static int CeedHostReciprocal_Hip(CeedScalar *h_array, CeedSize length) { 5789330daecSnbeams for (CeedSize i = 0; i < length; i++) { 5792b730f8bSJeremy L Thompson if (fabs(h_array[i]) > CEED_EPSILON) h_array[i] = 1. / h_array[i]; 5802b730f8bSJeremy L Thompson } 5810d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5820d0321e0SJeremy L Thompson } 5830d0321e0SJeremy L Thompson 5840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 585*956a3dbaSJeremy L Thompson // Take reciprocal of a vector on device (impl in .hip.cpp file) 5860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5879330daecSnbeams int CeedDeviceReciprocal_Hip(CeedScalar *d_array, CeedSize length); 5880d0321e0SJeremy L Thompson 5890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5900d0321e0SJeremy L Thompson // Take reciprocal of a vector 5910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 5920d0321e0SJeremy L Thompson static int CeedVectorReciprocal_Hip(CeedVector vec) { 5931f9221feSJeremy L Thompson CeedSize length; 594b7453713SJeremy L Thompson CeedVector_Hip *impl; 5950d0321e0SJeremy L Thompson 596b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 597b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(vec, &length)); 5980d0321e0SJeremy L Thompson // Set value for synced device/host array 5992b730f8bSJeremy L Thompson if (impl->d_array) CeedCallBackend(CeedDeviceReciprocal_Hip(impl->d_array, length)); 6002b730f8bSJeremy L Thompson if (impl->h_array) CeedCallBackend(CeedHostReciprocal_Hip(impl->h_array, length)); 6010d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6020d0321e0SJeremy L Thompson } 6030d0321e0SJeremy L Thompson 6040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6050d0321e0SJeremy L Thompson // Compute x = alpha x on the host 6060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6079330daecSnbeams static int CeedHostScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length) { 6089330daecSnbeams for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha; 6090d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6100d0321e0SJeremy L Thompson } 6110d0321e0SJeremy L Thompson 6120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 613*956a3dbaSJeremy L Thompson // Compute x = alpha x on device (impl in .hip.cpp file) 6140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6159330daecSnbeams int CeedDeviceScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length); 6160d0321e0SJeremy L Thompson 6170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6180d0321e0SJeremy L Thompson // Compute x = alpha x 6190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6200d0321e0SJeremy L Thompson static int CeedVectorScale_Hip(CeedVector x, CeedScalar alpha) { 6211f9221feSJeremy L Thompson CeedSize length; 622b7453713SJeremy L Thompson CeedVector_Hip *x_impl; 6230d0321e0SJeremy L Thompson 624b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(x, &x_impl)); 625b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(x, &length)); 6260d0321e0SJeremy L Thompson // Set value for synced device/host array 6272b730f8bSJeremy L Thompson if (x_impl->d_array) CeedCallBackend(CeedDeviceScale_Hip(x_impl->d_array, alpha, length)); 6282b730f8bSJeremy L Thompson if (x_impl->h_array) CeedCallBackend(CeedHostScale_Hip(x_impl->h_array, alpha, length)); 6290d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6300d0321e0SJeremy L Thompson } 6310d0321e0SJeremy L Thompson 6320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6330d0321e0SJeremy L Thompson // Compute y = alpha x + y on the host 6340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6359330daecSnbeams static int CeedHostAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length) { 6369330daecSnbeams for (CeedSize i = 0; i < length; i++) y_array[i] += alpha * x_array[i]; 6370d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6380d0321e0SJeremy L Thompson } 6390d0321e0SJeremy L Thompson 6400d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 641*956a3dbaSJeremy L Thompson // Compute y = alpha x + y on device (impl in .hip.cpp file) 6420d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6439330daecSnbeams int CeedDeviceAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length); 6440d0321e0SJeremy L Thompson 6450d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6460d0321e0SJeremy L Thompson // Compute y = alpha x + y 6470d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 6480d0321e0SJeremy L Thompson static int CeedVectorAXPY_Hip(CeedVector y, CeedScalar alpha, CeedVector x) { 649b7453713SJeremy L Thompson CeedSize length; 6500d0321e0SJeremy L Thompson CeedVector_Hip *y_impl, *x_impl; 651b7453713SJeremy L Thompson 6522b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(y, &y_impl)); 6532b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(x, &x_impl)); 6542b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(y, &length)); 6550d0321e0SJeremy L Thompson // Set value for synced device/host array 6560d0321e0SJeremy L Thompson if (y_impl->d_array) { 6572b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 6582b730f8bSJeremy L Thompson CeedCallBackend(CeedDeviceAXPY_Hip(y_impl->d_array, alpha, x_impl->d_array, length)); 6590d0321e0SJeremy L Thompson } 6600d0321e0SJeremy L Thompson if (y_impl->h_array) { 6612b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 6622b730f8bSJeremy L Thompson CeedCallBackend(CeedHostAXPY_Hip(y_impl->h_array, alpha, x_impl->h_array, length)); 6630d0321e0SJeremy L Thompson } 6640d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6650d0321e0SJeremy L Thompson } 666ff1e7120SSebastian Grimberg 6675fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6685fb68f37SKaren (Ren) Stengel // Compute y = alpha x + beta y on the host 6695fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6709330daecSnbeams static int CeedHostAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length) { 671aa67b842SZach Atkins for (CeedSize i = 0; i < length; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i]; 6725fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 6735fb68f37SKaren (Ren) Stengel } 6745fb68f37SKaren (Ren) Stengel 6755fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 676*956a3dbaSJeremy L Thompson // Compute y = alpha x + beta y on device (impl in .hip.cpp file) 6775fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6789330daecSnbeams int CeedDeviceAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length); 6795fb68f37SKaren (Ren) Stengel 6805fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6815fb68f37SKaren (Ren) Stengel // Compute y = alpha x + beta y 6825fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------ 6835fb68f37SKaren (Ren) Stengel static int CeedVectorAXPBY_Hip(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) { 684b7453713SJeremy L Thompson CeedSize length; 6855fb68f37SKaren (Ren) Stengel CeedVector_Hip *y_impl, *x_impl; 686b7453713SJeremy L Thompson 6875fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedVectorGetData(y, &y_impl)); 6885fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedVectorGetData(x, &x_impl)); 6895fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedVectorGetLength(y, &length)); 6905fb68f37SKaren (Ren) Stengel // Set value for synced device/host array 6915fb68f37SKaren (Ren) Stengel if (y_impl->d_array) { 6925fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 6935fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedDeviceAXPBY_Hip(y_impl->d_array, alpha, beta, x_impl->d_array, length)); 6945fb68f37SKaren (Ren) Stengel } 6955fb68f37SKaren (Ren) Stengel if (y_impl->h_array) { 6965fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 6975fb68f37SKaren (Ren) Stengel CeedCallBackend(CeedHostAXPBY_Hip(y_impl->h_array, alpha, beta, x_impl->h_array, length)); 6985fb68f37SKaren (Ren) Stengel } 6995fb68f37SKaren (Ren) Stengel return CEED_ERROR_SUCCESS; 7005fb68f37SKaren (Ren) Stengel } 7010d0321e0SJeremy L Thompson 7020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7030d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y on the host 7040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7059330daecSnbeams static int CeedHostPointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length) { 7069330daecSnbeams for (CeedSize i = 0; i < length; i++) w_array[i] = x_array[i] * y_array[i]; 7070d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7080d0321e0SJeremy L Thompson } 7090d0321e0SJeremy L Thompson 7100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 711*956a3dbaSJeremy L Thompson // Compute the pointwise multiplication w = x .* y on device (impl in .hip.cpp file) 7120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7139330daecSnbeams int CeedDevicePointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length); 7140d0321e0SJeremy L Thompson 7150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7160d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y 7170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7182b730f8bSJeremy L Thompson static int CeedVectorPointwiseMult_Hip(CeedVector w, CeedVector x, CeedVector y) { 719b7453713SJeremy L Thompson CeedSize length; 7200d0321e0SJeremy L Thompson CeedVector_Hip *w_impl, *x_impl, *y_impl; 721b7453713SJeremy L Thompson 7222b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(w, &w_impl)); 7232b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(x, &x_impl)); 7242b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetData(y, &y_impl)); 7252b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(w, &length)); 7260d0321e0SJeremy L Thompson 7270d0321e0SJeremy L Thompson // Set value for synced device/host array 7280d0321e0SJeremy L Thompson if (!w_impl->d_array && !w_impl->h_array) { 7292b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(w, 0.0)); 7300d0321e0SJeremy L Thompson } 7310d0321e0SJeremy L Thompson if (w_impl->d_array) { 7322b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE)); 7332b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_DEVICE)); 7342b730f8bSJeremy L Thompson CeedCallBackend(CeedDevicePointwiseMult_Hip(w_impl->d_array, x_impl->d_array, y_impl->d_array, length)); 7350d0321e0SJeremy L Thompson } 7360d0321e0SJeremy L Thompson if (w_impl->h_array) { 7372b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST)); 7382b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_HOST)); 7392b730f8bSJeremy L Thompson CeedCallBackend(CeedHostPointwiseMult_Hip(w_impl->h_array, x_impl->h_array, y_impl->h_array, length)); 7400d0321e0SJeremy L Thompson } 7410d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7420d0321e0SJeremy L Thompson } 7430d0321e0SJeremy L Thompson 7440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7450d0321e0SJeremy L Thompson // Destroy the vector 7460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7470d0321e0SJeremy L Thompson static int CeedVectorDestroy_Hip(const CeedVector vec) { 7480d0321e0SJeremy L Thompson CeedVector_Hip *impl; 7490d0321e0SJeremy L Thompson 750b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetData(vec, &impl)); 7516e536b99SJeremy L Thompson CeedCallHip(CeedVectorReturnCeed(vec), hipFree(impl->d_array_owned)); 7522b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->h_array_owned)); 7532b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 7540d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7550d0321e0SJeremy L Thompson } 7560d0321e0SJeremy L Thompson 7570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7580d0321e0SJeremy L Thompson // Create a vector of the specified length (does not allocate memory) 7590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 7601f9221feSJeremy L Thompson int CeedVectorCreate_Hip(CeedSize n, CeedVector vec) { 7610d0321e0SJeremy L Thompson CeedVector_Hip *impl; 7620d0321e0SJeremy L Thompson Ceed ceed; 7630d0321e0SJeremy L Thompson 764b7453713SJeremy L Thompson CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); 7652b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Hip)); 7662b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Hip)); 7672b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Hip)); 7682b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Hip)); 7693196072fSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "CopyStrided", (int (*)())CeedVectorCopyStrided_Hip)); 770008736bdSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValue", (int (*)())CeedVectorSetValue_Hip)); 7713196072fSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValueStrided", (int (*)())CeedVectorSetValueStrided_Hip)); 7722b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SyncArray", CeedVectorSyncArray_Hip)); 7732b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Hip)); 7742b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Hip)); 7752b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Hip)); 7762b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Norm", CeedVectorNorm_Hip)); 7772b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Reciprocal", CeedVectorReciprocal_Hip)); 778008736bdSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Scale", (int (*)())CeedVectorScale_Hip)); 779008736bdSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPY", (int (*)())CeedVectorAXPY_Hip)); 780008736bdSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", (int (*)())CeedVectorAXPBY_Hip)); 7812b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Hip)); 7822b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Hip)); 7832b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 7842b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorSetData(vec, impl)); 7850d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7860d0321e0SJeremy L Thompson } 7872a86cc9dSSebastian Grimberg 7882a86cc9dSSebastian Grimberg //------------------------------------------------------------------------------ 789