xref: /libCEED/backends/hip-ref/ceed-hip-ref-vector.c (revision 9bc663991d6482bcb1d60b1f116148f11db83fa1)
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) {
42b7453713SJeremy L Thompson   CeedSize        length;
43672b0f2aSSebastian Grimberg   size_t          bytes;
440d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
45b7453713SJeremy L Thompson 
462b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
470d0321e0SJeremy L Thompson 
48*9bc66399SJeremy L Thompson   CeedCheck(impl->h_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid host data to sync to device");
490d0321e0SJeremy L Thompson 
50672b0f2aSSebastian Grimberg   CeedCallBackend(CeedVectorGetLength(vec, &length));
51672b0f2aSSebastian Grimberg   bytes = length * sizeof(CeedScalar);
520d0321e0SJeremy L Thompson   if (impl->d_array_borrowed) {
530d0321e0SJeremy L Thompson     impl->d_array = impl->d_array_borrowed;
540d0321e0SJeremy L Thompson   } else if (impl->d_array_owned) {
550d0321e0SJeremy L Thompson     impl->d_array = impl->d_array_owned;
560d0321e0SJeremy L Thompson   } else {
57*9bc66399SJeremy L Thompson     CeedCallHip(CeedVectorReturnCeed(vec), hipMalloc((void **)&impl->d_array_owned, bytes));
580d0321e0SJeremy L Thompson     impl->d_array = impl->d_array_owned;
590d0321e0SJeremy L Thompson   }
60*9bc66399SJeremy L Thompson   CeedCallHip(CeedVectorReturnCeed(vec), hipMemcpy(impl->d_array, impl->h_array, bytes, hipMemcpyHostToDevice));
610d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
620d0321e0SJeremy L Thompson }
630d0321e0SJeremy L Thompson 
640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
650d0321e0SJeremy L Thompson // Sync device to host
660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
670d0321e0SJeremy L Thompson static inline int CeedVectorSyncD2H_Hip(const CeedVector vec) {
68b7453713SJeremy L Thompson   CeedSize        length;
69672b0f2aSSebastian Grimberg   size_t          bytes;
700d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
71b7453713SJeremy L Thompson 
722b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
730d0321e0SJeremy L Thompson 
74*9bc66399SJeremy L Thompson   CeedCheck(impl->d_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid device data to sync to host");
750d0321e0SJeremy L Thompson 
760d0321e0SJeremy L Thompson   if (impl->h_array_borrowed) {
770d0321e0SJeremy L Thompson     impl->h_array = impl->h_array_borrowed;
780d0321e0SJeremy L Thompson   } else if (impl->h_array_owned) {
790d0321e0SJeremy L Thompson     impl->h_array = impl->h_array_owned;
800d0321e0SJeremy L Thompson   } else {
811f9221feSJeremy L Thompson     CeedSize length;
82672b0f2aSSebastian Grimberg 
832b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(vec, &length));
842b730f8bSJeremy L Thompson     CeedCallBackend(CeedCalloc(length, &impl->h_array_owned));
850d0321e0SJeremy L Thompson     impl->h_array = impl->h_array_owned;
860d0321e0SJeremy L Thompson   }
870d0321e0SJeremy L Thompson 
882b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
89672b0f2aSSebastian Grimberg   bytes = length * sizeof(CeedScalar);
90*9bc66399SJeremy L Thompson   CeedCallHip(CeedVectorReturnCeed(vec), hipMemcpy(impl->h_array, impl->d_array, bytes, hipMemcpyDeviceToHost));
910d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
920d0321e0SJeremy L Thompson }
930d0321e0SJeremy L Thompson 
940d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
950d0321e0SJeremy L Thompson // Sync arrays
960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
972b730f8bSJeremy L Thompson static int CeedVectorSyncArray_Hip(const CeedVector vec, CeedMemType mem_type) {
98f48ed27dSnbeams   bool need_sync = false;
99b7453713SJeremy L Thompson 
100b7453713SJeremy L Thompson   // Check whether device/host sync is needed
1012b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorNeedSync_Hip(vec, mem_type, &need_sync));
1022b730f8bSJeremy L Thompson   if (!need_sync) return CEED_ERROR_SUCCESS;
103f48ed27dSnbeams 
10443c928f4SJeremy L Thompson   switch (mem_type) {
1052b730f8bSJeremy L Thompson     case CEED_MEM_HOST:
1062b730f8bSJeremy L Thompson       return CeedVectorSyncD2H_Hip(vec);
1072b730f8bSJeremy L Thompson     case CEED_MEM_DEVICE:
1082b730f8bSJeremy L Thompson       return CeedVectorSyncH2D_Hip(vec);
1090d0321e0SJeremy L Thompson   }
1100d0321e0SJeremy L Thompson   return CEED_ERROR_UNSUPPORTED;
1110d0321e0SJeremy L Thompson }
1120d0321e0SJeremy L Thompson 
1130d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1140d0321e0SJeremy L Thompson // Set all pointers as invalid
1150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1160d0321e0SJeremy L Thompson static inline int CeedVectorSetAllInvalid_Hip(const CeedVector vec) {
1170d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
1180d0321e0SJeremy L Thompson 
119b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
1200d0321e0SJeremy L Thompson   impl->h_array = NULL;
1210d0321e0SJeremy L Thompson   impl->d_array = NULL;
1220d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1230d0321e0SJeremy L Thompson }
1240d0321e0SJeremy L Thompson 
1250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
126b2165e7aSSebastian Grimberg // Check if CeedVector has any valid pointer
1270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1282b730f8bSJeremy L Thompson static inline int CeedVectorHasValidArray_Hip(const CeedVector vec, bool *has_valid_array) {
1290d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
130b7453713SJeremy L Thompson 
1312b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
1321c66c397SJeremy L Thompson   *has_valid_array = impl->h_array || impl->d_array;
1330d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1340d0321e0SJeremy L Thompson }
1350d0321e0SJeremy L Thompson 
1360d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
137b2165e7aSSebastian Grimberg // Check if has array of given type
1380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1392b730f8bSJeremy L Thompson static inline int CeedVectorHasArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_array_of_type) {
1400d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
1410d0321e0SJeremy L Thompson 
142b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
14343c928f4SJeremy L Thompson   switch (mem_type) {
1440d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
1451c66c397SJeremy L Thompson       *has_array_of_type = impl->h_array_borrowed || impl->h_array_owned;
1460d0321e0SJeremy L Thompson       break;
1470d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
1481c66c397SJeremy L Thompson       *has_array_of_type = impl->d_array_borrowed || impl->d_array_owned;
1490d0321e0SJeremy L Thompson       break;
1500d0321e0SJeremy L Thompson   }
1510d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1520d0321e0SJeremy L Thompson }
1530d0321e0SJeremy L Thompson 
1540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1550d0321e0SJeremy L Thompson // Check if has borrowed array of given type
1560d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1572b730f8bSJeremy L Thompson static inline int CeedVectorHasBorrowedArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) {
1580d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
1590d0321e0SJeremy L Thompson 
160b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
16143c928f4SJeremy L Thompson   switch (mem_type) {
1620d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
1631c66c397SJeremy L Thompson       *has_borrowed_array_of_type = impl->h_array_borrowed;
1640d0321e0SJeremy L Thompson       break;
1650d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
1661c66c397SJeremy L Thompson       *has_borrowed_array_of_type = impl->d_array_borrowed;
1670d0321e0SJeremy L Thompson       break;
1680d0321e0SJeremy L Thompson   }
1690d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1700d0321e0SJeremy L Thompson }
1710d0321e0SJeremy L Thompson 
1720d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1730d0321e0SJeremy L Thompson // Set array from host
1740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1752b730f8bSJeremy L Thompson static int CeedVectorSetArrayHost_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) {
176a267acd1SJeremy L Thompson   CeedSize        length;
1770d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
1780d0321e0SJeremy L Thompson 
179b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
180a267acd1SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
181a267acd1SJeremy L Thompson 
182f5d1e504SJeremy L Thompson   CeedCallBackend(CeedSetHostCeedScalarArray(array, copy_mode, length, (const CeedScalar **)&impl->h_array_owned,
183f5d1e504SJeremy L Thompson                                              (const CeedScalar **)&impl->h_array_borrowed, (const CeedScalar **)&impl->h_array));
1840d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1850d0321e0SJeremy L Thompson }
1860d0321e0SJeremy L Thompson 
1870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1880d0321e0SJeremy L Thompson // Set array from device
1890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1902b730f8bSJeremy L Thompson static int CeedVectorSetArrayDevice_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) {
191a267acd1SJeremy L Thompson   CeedSize        length;
1920d0321e0SJeremy L Thompson   Ceed            ceed;
1930d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
1940d0321e0SJeremy L Thompson 
195b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
196b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
197a267acd1SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
198f5d1e504SJeremy L Thompson 
199f5d1e504SJeremy L Thompson   CeedCallBackend(CeedSetDeviceCeedScalarArray_Hip(ceed, array, copy_mode, length, (const CeedScalar **)&impl->d_array_owned,
200f5d1e504SJeremy L Thompson                                                    (const CeedScalar **)&impl->d_array_borrowed, (const CeedScalar **)&impl->d_array));
201*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
2020d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2030d0321e0SJeremy L Thompson }
2040d0321e0SJeremy L Thompson 
2050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2060d0321e0SJeremy L Thompson // Set the array used by a vector,
2070d0321e0SJeremy L Thompson //   freeing any previously allocated array if applicable
2080d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2092b730f8bSJeremy L Thompson static int CeedVectorSetArray_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedCopyMode copy_mode, CeedScalar *array) {
2100d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
2110d0321e0SJeremy L Thompson 
212b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
2132b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec));
21443c928f4SJeremy L Thompson   switch (mem_type) {
2150d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
21643c928f4SJeremy L Thompson       return CeedVectorSetArrayHost_Hip(vec, copy_mode, array);
2170d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
21843c928f4SJeremy L Thompson       return CeedVectorSetArrayDevice_Hip(vec, copy_mode, array);
2190d0321e0SJeremy L Thompson   }
2200d0321e0SJeremy L Thompson   return CEED_ERROR_UNSUPPORTED;
2210d0321e0SJeremy L Thompson }
2220d0321e0SJeremy L Thompson 
2230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2243196072fSJeremy L Thompson // Copy host array to value strided
2253196072fSJeremy L Thompson //------------------------------------------------------------------------------
2263196072fSJeremy L Thompson static int CeedHostCopyStrided_Hip(CeedScalar *h_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar *h_copy_array) {
2273196072fSJeremy L Thompson   for (CeedSize i = start; i < length; i += step) h_copy_array[i] = h_array[i];
2283196072fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2293196072fSJeremy L Thompson }
2303196072fSJeremy L Thompson 
2313196072fSJeremy L Thompson //------------------------------------------------------------------------------
232956a3dbaSJeremy L Thompson // Copy device array to value strided (impl in .hip.cpp file)
2333196072fSJeremy L Thompson //------------------------------------------------------------------------------
2343196072fSJeremy L Thompson int CeedDeviceCopyStrided_Hip(CeedScalar *d_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar *d_copy_array);
2353196072fSJeremy L Thompson 
2363196072fSJeremy L Thompson //------------------------------------------------------------------------------
2373196072fSJeremy L Thompson // Copy a vector to a value strided
2383196072fSJeremy L Thompson //------------------------------------------------------------------------------
2393196072fSJeremy L Thompson static int CeedVectorCopyStrided_Hip(CeedVector vec, CeedSize start, CeedSize step, CeedVector vec_copy) {
2403196072fSJeremy L Thompson   CeedSize        length;
2413196072fSJeremy L Thompson   CeedVector_Hip *impl;
2423196072fSJeremy L Thompson 
2433196072fSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
244a7efc114SJeremy L Thompson   {
245a7efc114SJeremy L Thompson     CeedSize length_vec, length_copy;
246a7efc114SJeremy L Thompson 
2475a5594ffSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(vec, &length_vec));
2485a5594ffSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(vec_copy, &length_copy));
249a7efc114SJeremy L Thompson     length = length_vec < length_copy ? length_vec : length_copy;
250a7efc114SJeremy L Thompson   }
2513196072fSJeremy L Thompson   // Set value for synced device/host array
2523196072fSJeremy L Thompson   if (impl->d_array) {
2533196072fSJeremy L Thompson     CeedScalar *copy_array;
2543196072fSJeremy L Thompson 
2553196072fSJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_DEVICE, &copy_array));
2563196072fSJeremy L Thompson     CeedCallBackend(CeedDeviceCopyStrided_Hip(impl->d_array, start, step, length, copy_array));
2573196072fSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(vec_copy, &copy_array));
2583196072fSJeremy L Thompson   } else if (impl->h_array) {
2593196072fSJeremy L Thompson     CeedScalar *copy_array;
2603196072fSJeremy L Thompson 
2613196072fSJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, &copy_array));
2623196072fSJeremy L Thompson     CeedCallBackend(CeedHostCopyStrided_Hip(impl->h_array, start, step, length, copy_array));
2633196072fSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(vec_copy, &copy_array));
2643196072fSJeremy L Thompson   } else {
2653196072fSJeremy L Thompson     return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set");
2663196072fSJeremy L Thompson   }
2673196072fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2683196072fSJeremy L Thompson }
2693196072fSJeremy L Thompson 
2703196072fSJeremy L Thompson //------------------------------------------------------------------------------
2710d0321e0SJeremy L Thompson // Set host array to value
2720d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2739330daecSnbeams static int CeedHostSetValue_Hip(CeedScalar *h_array, CeedSize length, CeedScalar val) {
2749330daecSnbeams   for (CeedSize i = 0; i < length; i++) h_array[i] = val;
2750d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2760d0321e0SJeremy L Thompson }
2770d0321e0SJeremy L Thompson 
2780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2790d0321e0SJeremy L Thompson // Set device array to value (impl in .hip file)
2800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2819330daecSnbeams int CeedDeviceSetValue_Hip(CeedScalar *d_array, CeedSize length, CeedScalar val);
2820d0321e0SJeremy L Thompson 
2830d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
284b2165e7aSSebastian Grimberg // Set a vector to a value
2850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2860d0321e0SJeremy L Thompson static int CeedVectorSetValue_Hip(CeedVector vec, CeedScalar val) {
2871f9221feSJeremy L Thompson   CeedSize        length;
288b7453713SJeremy L Thompson   CeedVector_Hip *impl;
2890d0321e0SJeremy L Thompson 
290b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
291b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
2920d0321e0SJeremy L Thompson   // Set value for synced device/host array
2930d0321e0SJeremy L Thompson   if (!impl->d_array && !impl->h_array) {
2940d0321e0SJeremy L Thompson     if (impl->d_array_borrowed) {
2950d0321e0SJeremy L Thompson       impl->d_array = impl->d_array_borrowed;
2960d0321e0SJeremy L Thompson     } else if (impl->h_array_borrowed) {
2970d0321e0SJeremy L Thompson       impl->h_array = impl->h_array_borrowed;
2980d0321e0SJeremy L Thompson     } else if (impl->d_array_owned) {
2990d0321e0SJeremy L Thompson       impl->d_array = impl->d_array_owned;
3000d0321e0SJeremy L Thompson     } else if (impl->h_array_owned) {
3010d0321e0SJeremy L Thompson       impl->h_array = impl->h_array_owned;
3020d0321e0SJeremy L Thompson     } else {
3032b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_DEVICE, CEED_COPY_VALUES, NULL));
3040d0321e0SJeremy L Thompson     }
3050d0321e0SJeremy L Thompson   }
3060d0321e0SJeremy L Thompson   if (impl->d_array) {
3072b730f8bSJeremy L Thompson     CeedCallBackend(CeedDeviceSetValue_Hip(impl->d_array, length, val));
308b2165e7aSSebastian Grimberg     impl->h_array = NULL;
3090d0321e0SJeremy L Thompson   }
3100d0321e0SJeremy L Thompson   if (impl->h_array) {
3112b730f8bSJeremy L Thompson     CeedCallBackend(CeedHostSetValue_Hip(impl->h_array, length, val));
312b2165e7aSSebastian Grimberg     impl->d_array = NULL;
3130d0321e0SJeremy L Thompson   }
3140d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3150d0321e0SJeremy L Thompson }
3160d0321e0SJeremy L Thompson 
3170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3183196072fSJeremy L Thompson // Set host array to value strided
3193196072fSJeremy L Thompson //------------------------------------------------------------------------------
3203196072fSJeremy L Thompson static int CeedHostSetValueStrided_Hip(CeedScalar *h_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar val) {
3213196072fSJeremy L Thompson   for (CeedSize i = start; i < length; i += step) h_array[i] = val;
3223196072fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
3233196072fSJeremy L Thompson }
3243196072fSJeremy L Thompson 
3253196072fSJeremy L Thompson //------------------------------------------------------------------------------
326956a3dbaSJeremy L Thompson // Set device array to value strided (impl in .hip.cpp file)
3273196072fSJeremy L Thompson //------------------------------------------------------------------------------
3283196072fSJeremy L Thompson int CeedDeviceSetValueStrided_Hip(CeedScalar *d_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar val);
3293196072fSJeremy L Thompson 
3303196072fSJeremy L Thompson //------------------------------------------------------------------------------
3313196072fSJeremy L Thompson // Set a vector to a value strided
3323196072fSJeremy L Thompson //------------------------------------------------------------------------------
3333196072fSJeremy L Thompson static int CeedVectorSetValueStrided_Hip(CeedVector vec, CeedSize start, CeedSize step, CeedScalar val) {
3343196072fSJeremy L Thompson   CeedSize        length;
3353196072fSJeremy L Thompson   CeedVector_Hip *impl;
3363196072fSJeremy L Thompson 
3373196072fSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
3383196072fSJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
3393196072fSJeremy L Thompson   // Set value for synced device/host array
3403196072fSJeremy L Thompson   if (impl->d_array) {
3413196072fSJeremy L Thompson     CeedCallBackend(CeedDeviceSetValueStrided_Hip(impl->d_array, start, step, length, val));
3423196072fSJeremy L Thompson     impl->h_array = NULL;
3433196072fSJeremy L Thompson   } else if (impl->h_array) {
3443196072fSJeremy L Thompson     CeedCallBackend(CeedHostSetValueStrided_Hip(impl->h_array, start, step, length, val));
3453196072fSJeremy L Thompson     impl->d_array = NULL;
3463196072fSJeremy L Thompson   } else {
3473196072fSJeremy L Thompson     return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set");
3483196072fSJeremy L Thompson   }
3493196072fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
3503196072fSJeremy L Thompson }
3513196072fSJeremy L Thompson 
3523196072fSJeremy L Thompson //------------------------------------------------------------------------------
3530d0321e0SJeremy L Thompson // Vector Take Array
3540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3552b730f8bSJeremy L Thompson static int CeedVectorTakeArray_Hip(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
3560d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
357b7453713SJeremy L Thompson 
3582b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
3590d0321e0SJeremy L Thompson 
36043c928f4SJeremy L Thompson   // Sync array to requested mem_type
3612b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSyncArray(vec, mem_type));
3620d0321e0SJeremy L Thompson 
3630d0321e0SJeremy L Thompson   // Update pointer
36443c928f4SJeremy L Thompson   switch (mem_type) {
3650d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
3660d0321e0SJeremy L Thompson       (*array)               = impl->h_array_borrowed;
3670d0321e0SJeremy L Thompson       impl->h_array_borrowed = NULL;
3680d0321e0SJeremy L Thompson       impl->h_array          = NULL;
3690d0321e0SJeremy L Thompson       break;
3700d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
3710d0321e0SJeremy L Thompson       (*array)               = impl->d_array_borrowed;
3720d0321e0SJeremy L Thompson       impl->d_array_borrowed = NULL;
3730d0321e0SJeremy L Thompson       impl->d_array          = NULL;
3740d0321e0SJeremy L Thompson       break;
3750d0321e0SJeremy L Thompson   }
3760d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3770d0321e0SJeremy L Thompson }
3780d0321e0SJeremy L Thompson 
3790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3800d0321e0SJeremy L Thompson // Core logic for array syncronization for GetArray.
3810d0321e0SJeremy L Thompson //   If a different memory type is most up to date, this will perform a copy
3820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3832b730f8bSJeremy L Thompson static int CeedVectorGetArrayCore_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) {
3840d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
385b7453713SJeremy L Thompson 
3862b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
3870d0321e0SJeremy L Thompson 
38843c928f4SJeremy L Thompson   // Sync array to requested mem_type
3892b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSyncArray(vec, mem_type));
3900d0321e0SJeremy L Thompson 
3910d0321e0SJeremy L Thompson   // Update pointer
39243c928f4SJeremy L Thompson   switch (mem_type) {
3930d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
3940d0321e0SJeremy L Thompson       *array = impl->h_array;
3950d0321e0SJeremy L Thompson       break;
3960d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
3970d0321e0SJeremy L Thompson       *array = impl->d_array;
3980d0321e0SJeremy L Thompson       break;
3990d0321e0SJeremy L Thompson   }
4000d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4010d0321e0SJeremy L Thompson }
4020d0321e0SJeremy L Thompson 
4030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
40443c928f4SJeremy L Thompson // Get read-only access to a vector via the specified mem_type
4050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4062b730f8bSJeremy L Thompson static int CeedVectorGetArrayRead_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedScalar **array) {
40743c928f4SJeremy L Thompson   return CeedVectorGetArrayCore_Hip(vec, mem_type, (CeedScalar **)array);
4080d0321e0SJeremy L Thompson }
4090d0321e0SJeremy L Thompson 
4100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
41143c928f4SJeremy L Thompson // Get read/write access to a vector via the specified mem_type
4120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4132b730f8bSJeremy L Thompson static int CeedVectorGetArray_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) {
4140d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
415b7453713SJeremy L Thompson 
4162b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
4172b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayCore_Hip(vec, mem_type, array));
4182b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec));
41943c928f4SJeremy L Thompson   switch (mem_type) {
4200d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
4210d0321e0SJeremy L Thompson       impl->h_array = *array;
4220d0321e0SJeremy L Thompson       break;
4230d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
4240d0321e0SJeremy L Thompson       impl->d_array = *array;
4250d0321e0SJeremy L Thompson       break;
4260d0321e0SJeremy L Thompson   }
4270d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4280d0321e0SJeremy L Thompson }
4290d0321e0SJeremy L Thompson 
4300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
43143c928f4SJeremy L Thompson // Get write access to a vector via the specified mem_type
4320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4332b730f8bSJeremy L Thompson static int CeedVectorGetArrayWrite_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) {
4340d0321e0SJeremy L Thompson   bool            has_array_of_type = true;
435b7453713SJeremy L Thompson   CeedVector_Hip *impl;
436b7453713SJeremy L Thompson 
437b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
4382b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorHasArrayOfType_Hip(vec, mem_type, &has_array_of_type));
4390d0321e0SJeremy L Thompson   if (!has_array_of_type) {
4400d0321e0SJeremy L Thompson     // Allocate if array is not yet allocated
4412b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL));
4420d0321e0SJeremy L Thompson   } else {
4430d0321e0SJeremy L Thompson     // Select dirty array
44443c928f4SJeremy L Thompson     switch (mem_type) {
4450d0321e0SJeremy L Thompson       case CEED_MEM_HOST:
4462b730f8bSJeremy L Thompson         if (impl->h_array_borrowed) impl->h_array = impl->h_array_borrowed;
4472b730f8bSJeremy L Thompson         else impl->h_array = impl->h_array_owned;
4480d0321e0SJeremy L Thompson         break;
4490d0321e0SJeremy L Thompson       case CEED_MEM_DEVICE:
4502b730f8bSJeremy L Thompson         if (impl->d_array_borrowed) impl->d_array = impl->d_array_borrowed;
4512b730f8bSJeremy L Thompson         else impl->d_array = impl->d_array_owned;
4520d0321e0SJeremy L Thompson     }
4530d0321e0SJeremy L Thompson   }
45443c928f4SJeremy L Thompson   return CeedVectorGetArray_Hip(vec, mem_type, array);
4550d0321e0SJeremy L Thompson }
4560d0321e0SJeremy L Thompson 
4570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4580d0321e0SJeremy L Thompson // Get the norm of a CeedVector
4590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4602b730f8bSJeremy L Thompson static int CeedVectorNorm_Hip(CeedVector vec, CeedNormType type, CeedScalar *norm) {
4610d0321e0SJeremy L Thompson   Ceed              ceed;
462672b0f2aSSebastian Grimberg   CeedSize          length, num_calls;
463b7453713SJeremy L Thompson   const CeedScalar *d_array;
464b7453713SJeremy L Thompson   CeedVector_Hip   *impl;
4650d0321e0SJeremy L Thompson   hipblasHandle_t   handle;
466b7453713SJeremy L Thompson 
467b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
468b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
469b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
470eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetHipblasHandle_Hip(ceed, &handle));
4710d0321e0SJeremy L Thompson 
4729330daecSnbeams   // Is the vector too long to handle with int32? If so, we will divide
4739330daecSnbeams   // it up into "int32-sized" subsections and make repeated BLAS calls.
474672b0f2aSSebastian Grimberg   num_calls = length / INT_MAX;
4759330daecSnbeams   if (length % INT_MAX > 0) num_calls += 1;
4769330daecSnbeams 
4770d0321e0SJeremy L Thompson   // Compute norm
4782b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array));
4790d0321e0SJeremy L Thompson   switch (type) {
4800d0321e0SJeremy L Thompson     case CEED_NORM_1: {
481f6f49adbSnbeams       *norm = 0.0;
4820d0321e0SJeremy L Thompson       if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) {
4839330daecSnbeams         float  sub_norm = 0.0;
4849330daecSnbeams         float *d_array_start;
485b7453713SJeremy L Thompson 
4869330daecSnbeams         for (CeedInt i = 0; i < num_calls; i++) {
4879330daecSnbeams           d_array_start             = (float *)d_array + (CeedSize)(i)*INT_MAX;
4889330daecSnbeams           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
4899330daecSnbeams           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
490b7453713SJeremy L Thompson 
4919330daecSnbeams           CeedCallHipblas(ceed, hipblasSasum(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm));
4929330daecSnbeams           *norm += sub_norm;
4939330daecSnbeams         }
4940d0321e0SJeremy L Thompson       } else {
4959330daecSnbeams         double  sub_norm = 0.0;
4969330daecSnbeams         double *d_array_start;
497b7453713SJeremy L Thompson 
4989330daecSnbeams         for (CeedInt i = 0; i < num_calls; i++) {
4999330daecSnbeams           d_array_start             = (double *)d_array + (CeedSize)(i)*INT_MAX;
5009330daecSnbeams           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
5019330daecSnbeams           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
502b7453713SJeremy L Thompson 
5039330daecSnbeams           CeedCallHipblas(ceed, hipblasDasum(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm));
5049330daecSnbeams           *norm += sub_norm;
5059330daecSnbeams         }
5069330daecSnbeams       }
5070d0321e0SJeremy L Thompson       break;
5080d0321e0SJeremy L Thompson     }
5090d0321e0SJeremy L Thompson     case CEED_NORM_2: {
5100d0321e0SJeremy L Thompson       if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) {
5119330daecSnbeams         float  sub_norm = 0.0, norm_sum = 0.0;
5129330daecSnbeams         float *d_array_start;
513b7453713SJeremy L Thompson 
5149330daecSnbeams         for (CeedInt i = 0; i < num_calls; i++) {
5159330daecSnbeams           d_array_start             = (float *)d_array + (CeedSize)(i)*INT_MAX;
5169330daecSnbeams           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
5179330daecSnbeams           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
518b7453713SJeremy L Thompson 
5199330daecSnbeams           CeedCallHipblas(ceed, hipblasSnrm2(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm));
5209330daecSnbeams           norm_sum += sub_norm * sub_norm;
5219330daecSnbeams         }
5229330daecSnbeams         *norm = sqrt(norm_sum);
5230d0321e0SJeremy L Thompson       } else {
5249330daecSnbeams         double  sub_norm = 0.0, norm_sum = 0.0;
5259330daecSnbeams         double *d_array_start;
526b7453713SJeremy L Thompson 
5279330daecSnbeams         for (CeedInt i = 0; i < num_calls; i++) {
5289330daecSnbeams           d_array_start             = (double *)d_array + (CeedSize)(i)*INT_MAX;
5299330daecSnbeams           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
5309330daecSnbeams           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
531b7453713SJeremy L Thompson 
5329330daecSnbeams           CeedCallHipblas(ceed, hipblasDnrm2(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm));
5339330daecSnbeams           norm_sum += sub_norm * sub_norm;
5349330daecSnbeams         }
5359330daecSnbeams         *norm = sqrt(norm_sum);
5369330daecSnbeams       }
5370d0321e0SJeremy L Thompson       break;
5380d0321e0SJeremy L Thompson     }
5390d0321e0SJeremy L Thompson     case CEED_NORM_MAX: {
540b7453713SJeremy L Thompson       CeedInt index;
541b7453713SJeremy L Thompson 
5420d0321e0SJeremy L Thompson       if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) {
5439330daecSnbeams         float  sub_max = 0.0, current_max = 0.0;
5449330daecSnbeams         float *d_array_start;
5459330daecSnbeams         for (CeedInt i = 0; i < num_calls; i++) {
5469330daecSnbeams           d_array_start             = (float *)d_array + (CeedSize)(i)*INT_MAX;
5479330daecSnbeams           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
5489330daecSnbeams           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
549b7453713SJeremy L Thompson 
550b7453713SJeremy L Thompson           CeedCallHipblas(ceed, hipblasIsamax(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &index));
551b7453713SJeremy L Thompson           CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost));
5529330daecSnbeams           if (fabs(sub_max) > current_max) current_max = fabs(sub_max);
5539330daecSnbeams         }
5549330daecSnbeams         *norm = current_max;
5559330daecSnbeams       } else {
5569330daecSnbeams         double  sub_max = 0.0, current_max = 0.0;
5579330daecSnbeams         double *d_array_start;
558b7453713SJeremy L Thompson 
5599330daecSnbeams         for (CeedInt i = 0; i < num_calls; i++) {
5609330daecSnbeams           d_array_start             = (double *)d_array + (CeedSize)(i)*INT_MAX;
5619330daecSnbeams           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
5629330daecSnbeams           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
563b7453713SJeremy L Thompson 
564b7453713SJeremy L Thompson           CeedCallHipblas(ceed, hipblasIdamax(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &index));
565b7453713SJeremy L Thompson           CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost));
5669330daecSnbeams           if (fabs(sub_max) > current_max) current_max = fabs(sub_max);
5679330daecSnbeams         }
5689330daecSnbeams         *norm = current_max;
5699330daecSnbeams       }
5700d0321e0SJeremy L Thompson       break;
5710d0321e0SJeremy L Thompson     }
5720d0321e0SJeremy L Thompson   }
5732b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array));
574*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
5750d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5760d0321e0SJeremy L Thompson }
5770d0321e0SJeremy L Thompson 
5780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5790d0321e0SJeremy L Thompson // Take reciprocal of a vector on host
5800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5819330daecSnbeams static int CeedHostReciprocal_Hip(CeedScalar *h_array, CeedSize length) {
5829330daecSnbeams   for (CeedSize i = 0; i < length; i++) {
5832b730f8bSJeremy L Thompson     if (fabs(h_array[i]) > CEED_EPSILON) h_array[i] = 1. / h_array[i];
5842b730f8bSJeremy L Thompson   }
5850d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5860d0321e0SJeremy L Thompson }
5870d0321e0SJeremy L Thompson 
5880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
589956a3dbaSJeremy L Thompson // Take reciprocal of a vector on device (impl in .hip.cpp file)
5900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5919330daecSnbeams int CeedDeviceReciprocal_Hip(CeedScalar *d_array, CeedSize length);
5920d0321e0SJeremy L Thompson 
5930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5940d0321e0SJeremy L Thompson // Take reciprocal of a vector
5950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5960d0321e0SJeremy L Thompson static int CeedVectorReciprocal_Hip(CeedVector vec) {
5971f9221feSJeremy L Thompson   CeedSize        length;
598b7453713SJeremy L Thompson   CeedVector_Hip *impl;
5990d0321e0SJeremy L Thompson 
600b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
601b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
6020d0321e0SJeremy L Thompson   // Set value for synced device/host array
6032b730f8bSJeremy L Thompson   if (impl->d_array) CeedCallBackend(CeedDeviceReciprocal_Hip(impl->d_array, length));
6042b730f8bSJeremy L Thompson   if (impl->h_array) CeedCallBackend(CeedHostReciprocal_Hip(impl->h_array, length));
6050d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6060d0321e0SJeremy L Thompson }
6070d0321e0SJeremy L Thompson 
6080d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6090d0321e0SJeremy L Thompson // Compute x = alpha x on the host
6100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6119330daecSnbeams static int CeedHostScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length) {
6129330daecSnbeams   for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha;
6130d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6140d0321e0SJeremy L Thompson }
6150d0321e0SJeremy L Thompson 
6160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
617956a3dbaSJeremy L Thompson // Compute x = alpha x on device (impl in .hip.cpp file)
6180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6199330daecSnbeams int CeedDeviceScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length);
6200d0321e0SJeremy L Thompson 
6210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6220d0321e0SJeremy L Thompson // Compute x = alpha x
6230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6240d0321e0SJeremy L Thompson static int CeedVectorScale_Hip(CeedVector x, CeedScalar alpha) {
6251f9221feSJeremy L Thompson   CeedSize        length;
626b7453713SJeremy L Thompson   CeedVector_Hip *x_impl;
6270d0321e0SJeremy L Thompson 
628b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(x, &x_impl));
629b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(x, &length));
6300d0321e0SJeremy L Thompson   // Set value for synced device/host array
6312b730f8bSJeremy L Thompson   if (x_impl->d_array) CeedCallBackend(CeedDeviceScale_Hip(x_impl->d_array, alpha, length));
6322b730f8bSJeremy L Thompson   if (x_impl->h_array) CeedCallBackend(CeedHostScale_Hip(x_impl->h_array, alpha, length));
6330d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6340d0321e0SJeremy L Thompson }
6350d0321e0SJeremy L Thompson 
6360d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6370d0321e0SJeremy L Thompson // Compute y = alpha x + y on the host
6380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6399330daecSnbeams static int CeedHostAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length) {
6409330daecSnbeams   for (CeedSize i = 0; i < length; i++) y_array[i] += alpha * x_array[i];
6410d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6420d0321e0SJeremy L Thompson }
6430d0321e0SJeremy L Thompson 
6440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
645956a3dbaSJeremy L Thompson // Compute y = alpha x + y on device (impl in .hip.cpp file)
6460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6479330daecSnbeams int CeedDeviceAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length);
6480d0321e0SJeremy L Thompson 
6490d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6500d0321e0SJeremy L Thompson // Compute y = alpha x + y
6510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6520d0321e0SJeremy L Thompson static int CeedVectorAXPY_Hip(CeedVector y, CeedScalar alpha, CeedVector x) {
653b7453713SJeremy L Thompson   CeedSize        length;
6540d0321e0SJeremy L Thompson   CeedVector_Hip *y_impl, *x_impl;
655b7453713SJeremy L Thompson 
6562b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(y, &y_impl));
6572b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(x, &x_impl));
6582b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(y, &length));
6590d0321e0SJeremy L Thompson   // Set value for synced device/host array
6600d0321e0SJeremy L Thompson   if (y_impl->d_array) {
6612b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
6622b730f8bSJeremy L Thompson     CeedCallBackend(CeedDeviceAXPY_Hip(y_impl->d_array, alpha, x_impl->d_array, length));
6630d0321e0SJeremy L Thompson   }
6640d0321e0SJeremy L Thompson   if (y_impl->h_array) {
6652b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
6662b730f8bSJeremy L Thompson     CeedCallBackend(CeedHostAXPY_Hip(y_impl->h_array, alpha, x_impl->h_array, length));
6670d0321e0SJeremy L Thompson   }
6680d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6690d0321e0SJeremy L Thompson }
670ff1e7120SSebastian Grimberg 
6715fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
6725fb68f37SKaren (Ren) Stengel // Compute y = alpha x + beta y on the host
6735fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
6749330daecSnbeams static int CeedHostAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length) {
675aa67b842SZach Atkins   for (CeedSize i = 0; i < length; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i];
6765fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
6775fb68f37SKaren (Ren) Stengel }
6785fb68f37SKaren (Ren) Stengel 
6795fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
680956a3dbaSJeremy L Thompson // Compute y = alpha x + beta y on device (impl in .hip.cpp file)
6815fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
6829330daecSnbeams int CeedDeviceAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length);
6835fb68f37SKaren (Ren) Stengel 
6845fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
6855fb68f37SKaren (Ren) Stengel // Compute y = alpha x + beta y
6865fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
6875fb68f37SKaren (Ren) Stengel static int CeedVectorAXPBY_Hip(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
688b7453713SJeremy L Thompson   CeedSize        length;
6895fb68f37SKaren (Ren) Stengel   CeedVector_Hip *y_impl, *x_impl;
690b7453713SJeremy L Thompson 
6915fb68f37SKaren (Ren) Stengel   CeedCallBackend(CeedVectorGetData(y, &y_impl));
6925fb68f37SKaren (Ren) Stengel   CeedCallBackend(CeedVectorGetData(x, &x_impl));
6935fb68f37SKaren (Ren) Stengel   CeedCallBackend(CeedVectorGetLength(y, &length));
6945fb68f37SKaren (Ren) Stengel   // Set value for synced device/host array
6955fb68f37SKaren (Ren) Stengel   if (y_impl->d_array) {
6965fb68f37SKaren (Ren) Stengel     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
6975fb68f37SKaren (Ren) Stengel     CeedCallBackend(CeedDeviceAXPBY_Hip(y_impl->d_array, alpha, beta, x_impl->d_array, length));
6985fb68f37SKaren (Ren) Stengel   }
6995fb68f37SKaren (Ren) Stengel   if (y_impl->h_array) {
7005fb68f37SKaren (Ren) Stengel     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
7015fb68f37SKaren (Ren) Stengel     CeedCallBackend(CeedHostAXPBY_Hip(y_impl->h_array, alpha, beta, x_impl->h_array, length));
7025fb68f37SKaren (Ren) Stengel   }
7035fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
7045fb68f37SKaren (Ren) Stengel }
7050d0321e0SJeremy L Thompson 
7060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7070d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y on the host
7080d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7099330daecSnbeams static int CeedHostPointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length) {
7109330daecSnbeams   for (CeedSize i = 0; i < length; i++) w_array[i] = x_array[i] * y_array[i];
7110d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7120d0321e0SJeremy L Thompson }
7130d0321e0SJeremy L Thompson 
7140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
715956a3dbaSJeremy L Thompson // Compute the pointwise multiplication w = x .* y on device (impl in .hip.cpp file)
7160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7179330daecSnbeams int CeedDevicePointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length);
7180d0321e0SJeremy L Thompson 
7190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7200d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y
7210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7222b730f8bSJeremy L Thompson static int CeedVectorPointwiseMult_Hip(CeedVector w, CeedVector x, CeedVector y) {
723b7453713SJeremy L Thompson   CeedSize        length;
7240d0321e0SJeremy L Thompson   CeedVector_Hip *w_impl, *x_impl, *y_impl;
725b7453713SJeremy L Thompson 
7262b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(w, &w_impl));
7272b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(x, &x_impl));
7282b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(y, &y_impl));
7292b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(w, &length));
7300d0321e0SJeremy L Thompson 
7310d0321e0SJeremy L Thompson   // Set value for synced device/host array
7320d0321e0SJeremy L Thompson   if (!w_impl->d_array && !w_impl->h_array) {
7332b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(w, 0.0));
7340d0321e0SJeremy L Thompson   }
7350d0321e0SJeremy L Thompson   if (w_impl->d_array) {
7362b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
7372b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_DEVICE));
7382b730f8bSJeremy L Thompson     CeedCallBackend(CeedDevicePointwiseMult_Hip(w_impl->d_array, x_impl->d_array, y_impl->d_array, length));
7390d0321e0SJeremy L Thompson   }
7400d0321e0SJeremy L Thompson   if (w_impl->h_array) {
7412b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
7422b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_HOST));
7432b730f8bSJeremy L Thompson     CeedCallBackend(CeedHostPointwiseMult_Hip(w_impl->h_array, x_impl->h_array, y_impl->h_array, length));
7440d0321e0SJeremy L Thompson   }
7450d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7460d0321e0SJeremy L Thompson }
7470d0321e0SJeremy L Thompson 
7480d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7490d0321e0SJeremy L Thompson // Destroy the vector
7500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7510d0321e0SJeremy L Thompson static int CeedVectorDestroy_Hip(const CeedVector vec) {
7520d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
7530d0321e0SJeremy L Thompson 
754b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
7556e536b99SJeremy L Thompson   CeedCallHip(CeedVectorReturnCeed(vec), hipFree(impl->d_array_owned));
7562b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_array_owned));
7572b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
7580d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7590d0321e0SJeremy L Thompson }
7600d0321e0SJeremy L Thompson 
7610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7620d0321e0SJeremy L Thompson // Create a vector of the specified length (does not allocate memory)
7630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7641f9221feSJeremy L Thompson int CeedVectorCreate_Hip(CeedSize n, CeedVector vec) {
7650d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
7660d0321e0SJeremy L Thompson   Ceed            ceed;
7670d0321e0SJeremy L Thompson 
768b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
7692b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Hip));
7702b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Hip));
7712b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Hip));
7722b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Hip));
7733e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "CopyStrided", CeedVectorCopyStrided_Hip));
7743e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValue", CeedVectorSetValue_Hip));
7753e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValueStrided", CeedVectorSetValueStrided_Hip));
7762b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SyncArray", CeedVectorSyncArray_Hip));
7772b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Hip));
7782b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Hip));
7792b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Hip));
7802b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Norm", CeedVectorNorm_Hip));
7812b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Reciprocal", CeedVectorReciprocal_Hip));
7823e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Scale", CeedVectorScale_Hip));
7833e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPY", CeedVectorAXPY_Hip));
7843e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", CeedVectorAXPBY_Hip));
7852b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Hip));
7862b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Hip));
787*9bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
7882b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
7892b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetData(vec, impl));
7900d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7910d0321e0SJeremy L Thompson }
7922a86cc9dSSebastian Grimberg 
7932a86cc9dSSebastian Grimberg //------------------------------------------------------------------------------
794