xref: /libCEED/backends/hip-ref/ceed-hip-ref-vector.c (revision a3b195ef6dd39c849072dd5df2f934c50a4df099)
1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, 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 
489bc66399SJeremy 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 {
579bc66399SJeremy 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   }
609bc66399SJeremy 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 
749bc66399SJeremy 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);
909bc66399SJeremy 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;
99*a3b195efSJeremy L Thompson   CeedVector_Hip *impl;
100*a3b195efSJeremy L Thompson 
101*a3b195efSJeremy L Thompson   // Sync for unified memory
102*a3b195efSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
103*a3b195efSJeremy L Thompson   if (impl->has_unified_addressing && !impl->h_array_borrowed) {
104*a3b195efSJeremy L Thompson     CeedCallHip(CeedVectorReturnCeed(vec), hipDeviceSynchronize());
105*a3b195efSJeremy L Thompson     return CEED_ERROR_SUCCESS;
106*a3b195efSJeremy L Thompson   }
107b7453713SJeremy L Thompson 
108b7453713SJeremy L Thompson   // Check whether device/host sync is needed
1092b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorNeedSync_Hip(vec, mem_type, &need_sync));
1102b730f8bSJeremy L Thompson   if (!need_sync) return CEED_ERROR_SUCCESS;
111f48ed27dSnbeams 
11243c928f4SJeremy L Thompson   switch (mem_type) {
1132b730f8bSJeremy L Thompson     case CEED_MEM_HOST:
1142b730f8bSJeremy L Thompson       return CeedVectorSyncD2H_Hip(vec);
1152b730f8bSJeremy L Thompson     case CEED_MEM_DEVICE:
1162b730f8bSJeremy L Thompson       return CeedVectorSyncH2D_Hip(vec);
1170d0321e0SJeremy L Thompson   }
1180d0321e0SJeremy L Thompson   return CEED_ERROR_UNSUPPORTED;
1190d0321e0SJeremy L Thompson }
1200d0321e0SJeremy L Thompson 
1210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1220d0321e0SJeremy L Thompson // Set all pointers as invalid
1230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1240d0321e0SJeremy L Thompson static inline int CeedVectorSetAllInvalid_Hip(const CeedVector vec) {
1250d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
1260d0321e0SJeremy L Thompson 
127b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
1280d0321e0SJeremy L Thompson   impl->h_array = NULL;
1290d0321e0SJeremy L Thompson   impl->d_array = NULL;
1300d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1310d0321e0SJeremy L Thompson }
1320d0321e0SJeremy L Thompson 
1330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
134b2165e7aSSebastian Grimberg // Check if CeedVector has any valid pointer
1350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1362b730f8bSJeremy L Thompson static inline int CeedVectorHasValidArray_Hip(const CeedVector vec, bool *has_valid_array) {
1370d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
138b7453713SJeremy L Thompson 
1392b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
1401c66c397SJeremy L Thompson   *has_valid_array = impl->h_array || impl->d_array;
1410d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1420d0321e0SJeremy L Thompson }
1430d0321e0SJeremy L Thompson 
1440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
145b2165e7aSSebastian Grimberg // Check if has array of given type
1460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1472b730f8bSJeremy L Thompson static inline int CeedVectorHasArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_array_of_type) {
1480d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
1490d0321e0SJeremy L Thompson 
150b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
15143c928f4SJeremy L Thompson   switch (mem_type) {
1520d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
1531c66c397SJeremy L Thompson       *has_array_of_type = impl->h_array_borrowed || impl->h_array_owned;
1540d0321e0SJeremy L Thompson       break;
1550d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
1561c66c397SJeremy L Thompson       *has_array_of_type = impl->d_array_borrowed || impl->d_array_owned;
1570d0321e0SJeremy L Thompson       break;
1580d0321e0SJeremy L Thompson   }
1590d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1600d0321e0SJeremy L Thompson }
1610d0321e0SJeremy L Thompson 
1620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1630d0321e0SJeremy L Thompson // Check if has borrowed array of given type
1640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1652b730f8bSJeremy L Thompson static inline int CeedVectorHasBorrowedArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) {
1660d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
1670d0321e0SJeremy L Thompson 
168b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
169*a3b195efSJeremy L Thompson 
170*a3b195efSJeremy L Thompson   // Use device memory for unified memory
171*a3b195efSJeremy L Thompson   mem_type = impl->has_unified_addressing && !impl->h_array_borrowed ? CEED_MEM_DEVICE : mem_type;
172*a3b195efSJeremy L Thompson 
17343c928f4SJeremy L Thompson   switch (mem_type) {
1740d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
1751c66c397SJeremy L Thompson       *has_borrowed_array_of_type = impl->h_array_borrowed;
1760d0321e0SJeremy L Thompson       break;
1770d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
1781c66c397SJeremy L Thompson       *has_borrowed_array_of_type = impl->d_array_borrowed;
1790d0321e0SJeremy L Thompson       break;
1800d0321e0SJeremy L Thompson   }
1810d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1820d0321e0SJeremy L Thompson }
1830d0321e0SJeremy L Thompson 
1840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1850d0321e0SJeremy L Thompson // Set array from host
1860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1872b730f8bSJeremy L Thompson static int CeedVectorSetArrayHost_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) {
188a267acd1SJeremy L Thompson   CeedSize        length;
1890d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
1900d0321e0SJeremy L Thompson 
191b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
192a267acd1SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
193a267acd1SJeremy L Thompson 
194f5d1e504SJeremy L Thompson   CeedCallBackend(CeedSetHostCeedScalarArray(array, copy_mode, length, (const CeedScalar **)&impl->h_array_owned,
195f5d1e504SJeremy L Thompson                                              (const CeedScalar **)&impl->h_array_borrowed, (const CeedScalar **)&impl->h_array));
1960d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1970d0321e0SJeremy L Thompson }
1980d0321e0SJeremy L Thompson 
1990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2000d0321e0SJeremy L Thompson // Set array from device
2010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2022b730f8bSJeremy L Thompson static int CeedVectorSetArrayDevice_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) {
203a267acd1SJeremy L Thompson   CeedSize        length;
2040d0321e0SJeremy L Thompson   Ceed            ceed;
2050d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
2060d0321e0SJeremy L Thompson 
207b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
208b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
209a267acd1SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
210f5d1e504SJeremy L Thompson 
211f5d1e504SJeremy L Thompson   CeedCallBackend(CeedSetDeviceCeedScalarArray_Hip(ceed, array, copy_mode, length, (const CeedScalar **)&impl->d_array_owned,
212f5d1e504SJeremy L Thompson                                                    (const CeedScalar **)&impl->d_array_borrowed, (const CeedScalar **)&impl->d_array));
2139bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
2140d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2150d0321e0SJeremy L Thompson }
2160d0321e0SJeremy L Thompson 
2170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
218*a3b195efSJeremy L Thompson // Set array with unified memory
219*a3b195efSJeremy L Thompson //------------------------------------------------------------------------------
220*a3b195efSJeremy L Thompson static int CeedVectorSetArrayUnifiedHostToDevice_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) {
221*a3b195efSJeremy L Thompson   CeedSize        length;
222*a3b195efSJeremy L Thompson   Ceed            ceed;
223*a3b195efSJeremy L Thompson   CeedVector_Hip *impl;
224*a3b195efSJeremy L Thompson 
225*a3b195efSJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
226*a3b195efSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
227*a3b195efSJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
228*a3b195efSJeremy L Thompson 
229*a3b195efSJeremy L Thompson   switch (copy_mode) {
230*a3b195efSJeremy L Thompson     case CEED_COPY_VALUES:
231*a3b195efSJeremy L Thompson     case CEED_OWN_POINTER:
232*a3b195efSJeremy L Thompson       if (!impl->d_array) {
233*a3b195efSJeremy L Thompson         if (impl->d_array_borrowed) {
234*a3b195efSJeremy L Thompson           impl->d_array = impl->d_array_borrowed;
235*a3b195efSJeremy L Thompson         } else {
236*a3b195efSJeremy L Thompson           if (!impl->d_array_owned) CeedCallHip(ceed, hipMalloc((void **)&impl->d_array_owned, sizeof(CeedScalar) * length));
237*a3b195efSJeremy L Thompson           impl->d_array = impl->d_array_owned;
238*a3b195efSJeremy L Thompson         }
239*a3b195efSJeremy L Thompson       }
240*a3b195efSJeremy L Thompson       if (array) CeedCallHip(ceed, hipMemcpy(impl->d_array, array, sizeof(CeedScalar) * length, hipMemcpyHostToDevice));
241*a3b195efSJeremy L Thompson       if (copy_mode == CEED_OWN_POINTER) CeedCallBackend(CeedFree(&array));
242*a3b195efSJeremy L Thompson       break;
243*a3b195efSJeremy L Thompson     case CEED_USE_POINTER:
244*a3b195efSJeremy L Thompson       CeedCallHip(ceed, hipFree(impl->d_array_owned));
245*a3b195efSJeremy L Thompson       CeedCallBackend(CeedFree(&impl->h_array_owned));
246*a3b195efSJeremy L Thompson       impl->h_array_owned    = NULL;
247*a3b195efSJeremy L Thompson       impl->h_array_borrowed = array;
248*a3b195efSJeremy L Thompson       impl->d_array          = impl->h_array_borrowed;
249*a3b195efSJeremy L Thompson   }
250*a3b195efSJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
251*a3b195efSJeremy L Thompson   return CEED_ERROR_SUCCESS;
252*a3b195efSJeremy L Thompson }
253*a3b195efSJeremy L Thompson 
254*a3b195efSJeremy L Thompson //------------------------------------------------------------------------------
2550d0321e0SJeremy L Thompson // Set the array used by a vector,
2560d0321e0SJeremy L Thompson //   freeing any previously allocated array if applicable
2570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2582b730f8bSJeremy L Thompson static int CeedVectorSetArray_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedCopyMode copy_mode, CeedScalar *array) {
2590d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
2600d0321e0SJeremy L Thompson 
261b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
2622b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec));
26343c928f4SJeremy L Thompson   switch (mem_type) {
2640d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
265*a3b195efSJeremy L Thompson       if (impl->has_unified_addressing) {
266*a3b195efSJeremy L Thompson         return CeedVectorSetArrayUnifiedHostToDevice_Hip(vec, copy_mode, array);
267*a3b195efSJeremy L Thompson       } else {
26843c928f4SJeremy L Thompson         return CeedVectorSetArrayHost_Hip(vec, copy_mode, array);
269*a3b195efSJeremy L Thompson       }
2700d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
27143c928f4SJeremy L Thompson       return CeedVectorSetArrayDevice_Hip(vec, copy_mode, array);
2720d0321e0SJeremy L Thompson   }
2730d0321e0SJeremy L Thompson   return CEED_ERROR_UNSUPPORTED;
2740d0321e0SJeremy L Thompson }
2750d0321e0SJeremy L Thompson 
2760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2773196072fSJeremy L Thompson // Copy host array to value strided
2783196072fSJeremy L Thompson //------------------------------------------------------------------------------
279832a6d73SJeremy L Thompson static int CeedHostCopyStrided_Hip(CeedScalar *h_array, CeedSize start, CeedSize stop, CeedSize step, CeedScalar *h_copy_array) {
280832a6d73SJeremy L Thompson   for (CeedSize i = start; i < stop; i += step) h_copy_array[i] = h_array[i];
2813196072fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2823196072fSJeremy L Thompson }
2833196072fSJeremy L Thompson 
2843196072fSJeremy L Thompson //------------------------------------------------------------------------------
285956a3dbaSJeremy L Thompson // Copy device array to value strided (impl in .hip.cpp file)
2863196072fSJeremy L Thompson //------------------------------------------------------------------------------
287832a6d73SJeremy L Thompson int CeedDeviceCopyStrided_Hip(CeedScalar *d_array, CeedSize start, CeedSize stop, CeedSize step, CeedScalar *d_copy_array);
2883196072fSJeremy L Thompson 
2893196072fSJeremy L Thompson //------------------------------------------------------------------------------
2903196072fSJeremy L Thompson // Copy a vector to a value strided
2913196072fSJeremy L Thompson //------------------------------------------------------------------------------
292832a6d73SJeremy L Thompson static int CeedVectorCopyStrided_Hip(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedVector vec_copy) {
2933196072fSJeremy L Thompson   CeedSize        length;
2943196072fSJeremy L Thompson   CeedVector_Hip *impl;
2953196072fSJeremy L Thompson 
2963196072fSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
297a7efc114SJeremy L Thompson   {
298a7efc114SJeremy L Thompson     CeedSize length_vec, length_copy;
299a7efc114SJeremy L Thompson 
3005a5594ffSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(vec, &length_vec));
3015a5594ffSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(vec_copy, &length_copy));
302a7efc114SJeremy L Thompson     length = length_vec < length_copy ? length_vec : length_copy;
303a7efc114SJeremy L Thompson   }
304832a6d73SJeremy L Thompson   if (stop == -1) stop = length;
3053196072fSJeremy L Thompson   // Set value for synced device/host array
3063196072fSJeremy L Thompson   if (impl->d_array) {
3073196072fSJeremy L Thompson     CeedScalar *copy_array;
3083196072fSJeremy L Thompson 
3093196072fSJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_DEVICE, &copy_array));
310e84c3ebcSJeremy L Thompson #if (HIP_VERSION >= 60000000)
311e84c3ebcSJeremy L Thompson     hipblasHandle_t handle;
312e84c3ebcSJeremy L Thompson     Ceed            ceed;
313e84c3ebcSJeremy L Thompson 
314e84c3ebcSJeremy L Thompson     CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
315e84c3ebcSJeremy L Thompson     CeedCallBackend(CeedGetHipblasHandle_Hip(ceed, &handle));
316e84c3ebcSJeremy L Thompson #if defined(CEED_SCALAR_IS_FP32)
317832a6d73SJeremy L Thompson     CeedCallHipblas(ceed, hipblasScopy_64(handle, (int64_t)(stop - start), impl->d_array + start, (int64_t)step, copy_array + start, (int64_t)step));
318e84c3ebcSJeremy L Thompson #else  /* CEED_SCALAR */
319832a6d73SJeremy L Thompson     CeedCallHipblas(ceed, hipblasDcopy_64(handle, (int64_t)(stop - start), impl->d_array + start, (int64_t)step, copy_array + start, (int64_t)step));
320e84c3ebcSJeremy L Thompson #endif /* CEED_SCALAR */
321e84c3ebcSJeremy L Thompson #else  /* HIP_VERSION */
322832a6d73SJeremy L Thompson     CeedCallBackend(CeedDeviceCopyStrided_Hip(impl->d_array, start, stop, step, copy_array));
323e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
3243196072fSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(vec_copy, &copy_array));
325e84c3ebcSJeremy L Thompson     impl->h_array = NULL;
326e84c3ebcSJeremy L Thompson     CeedCallBackend(CeedDestroy(&ceed));
3273196072fSJeremy L Thompson   } else if (impl->h_array) {
3283196072fSJeremy L Thompson     CeedScalar *copy_array;
3293196072fSJeremy L Thompson 
3303196072fSJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, &copy_array));
331832a6d73SJeremy L Thompson     CeedCallBackend(CeedHostCopyStrided_Hip(impl->h_array, start, stop, step, copy_array));
3323196072fSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(vec_copy, &copy_array));
333e84c3ebcSJeremy L Thompson     impl->d_array = NULL;
3343196072fSJeremy L Thompson   } else {
3353196072fSJeremy L Thompson     return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set");
3363196072fSJeremy L Thompson   }
3373196072fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
3383196072fSJeremy L Thompson }
3393196072fSJeremy L Thompson 
3403196072fSJeremy L Thompson //------------------------------------------------------------------------------
3410d0321e0SJeremy L Thompson // Set host array to value
3420d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3439330daecSnbeams static int CeedHostSetValue_Hip(CeedScalar *h_array, CeedSize length, CeedScalar val) {
3449330daecSnbeams   for (CeedSize i = 0; i < length; i++) h_array[i] = val;
3450d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3460d0321e0SJeremy L Thompson }
3470d0321e0SJeremy L Thompson 
3480d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3490d0321e0SJeremy L Thompson // Set device array to value (impl in .hip file)
3500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3519330daecSnbeams int CeedDeviceSetValue_Hip(CeedScalar *d_array, CeedSize length, CeedScalar val);
3520d0321e0SJeremy L Thompson 
3530d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
354b2165e7aSSebastian Grimberg // Set a vector to a value
3550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3560d0321e0SJeremy L Thompson static int CeedVectorSetValue_Hip(CeedVector vec, CeedScalar val) {
3571f9221feSJeremy L Thompson   CeedSize        length;
358b7453713SJeremy L Thompson   CeedVector_Hip *impl;
359*a3b195efSJeremy L Thompson   Ceed_Hip       *hip_data;
3600d0321e0SJeremy L Thompson 
361b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
362*a3b195efSJeremy L Thompson   CeedCallBackend(CeedGetData(CeedVectorReturnCeed(vec), &hip_data));
363b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
3640d0321e0SJeremy L Thompson   // Set value for synced device/host array
3650d0321e0SJeremy L Thompson   if (!impl->d_array && !impl->h_array) {
3660d0321e0SJeremy L Thompson     if (impl->d_array_borrowed) {
3670d0321e0SJeremy L Thompson       impl->d_array = impl->d_array_borrowed;
3680d0321e0SJeremy L Thompson     } else if (impl->h_array_borrowed) {
3690d0321e0SJeremy L Thompson       impl->h_array = impl->h_array_borrowed;
3700d0321e0SJeremy L Thompson     } else if (impl->d_array_owned) {
3710d0321e0SJeremy L Thompson       impl->d_array = impl->d_array_owned;
3720d0321e0SJeremy L Thompson     } else if (impl->h_array_owned) {
3730d0321e0SJeremy L Thompson       impl->h_array = impl->h_array_owned;
3740d0321e0SJeremy L Thompson     } else {
3752b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_DEVICE, CEED_COPY_VALUES, NULL));
3760d0321e0SJeremy L Thompson     }
3770d0321e0SJeremy L Thompson   }
3780d0321e0SJeremy L Thompson   if (impl->d_array) {
379*a3b195efSJeremy L Thompson     if (val == 0 && !impl->h_array_borrowed) {
380124cc107SJeremy L Thompson       CeedCallHip(CeedVectorReturnCeed(vec), hipMemset(impl->d_array, 0, length * sizeof(CeedScalar)));
381124cc107SJeremy L Thompson     } else {
3822b730f8bSJeremy L Thompson       CeedCallBackend(CeedDeviceSetValue_Hip(impl->d_array, length, val));
3830d0321e0SJeremy L Thompson     }
384124cc107SJeremy L Thompson     impl->h_array = NULL;
385124cc107SJeremy L Thompson   } else if (impl->h_array) {
3862b730f8bSJeremy L Thompson     CeedCallBackend(CeedHostSetValue_Hip(impl->h_array, length, val));
387b2165e7aSSebastian Grimberg     impl->d_array = NULL;
3880d0321e0SJeremy L Thompson   }
3890d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3900d0321e0SJeremy L Thompson }
3910d0321e0SJeremy L Thompson 
3920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3933196072fSJeremy L Thompson // Set host array to value strided
3943196072fSJeremy L Thompson //------------------------------------------------------------------------------
39514c82621SJeremy L Thompson static int CeedHostSetValueStrided_Hip(CeedScalar *h_array, CeedSize start, CeedSize stop, CeedSize step, CeedScalar val) {
3962d73a370SJeremy L Thompson   for (CeedSize i = start; i < stop; i += step) h_array[i] = val;
3973196072fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
3983196072fSJeremy L Thompson }
3993196072fSJeremy L Thompson 
4003196072fSJeremy L Thompson //------------------------------------------------------------------------------
401956a3dbaSJeremy L Thompson // Set device array to value strided (impl in .hip.cpp file)
4023196072fSJeremy L Thompson //------------------------------------------------------------------------------
40314c82621SJeremy L Thompson int CeedDeviceSetValueStrided_Hip(CeedScalar *d_array, CeedSize start, CeedSize stop, CeedSize step, CeedScalar val);
4043196072fSJeremy L Thompson 
4053196072fSJeremy L Thompson //------------------------------------------------------------------------------
4063196072fSJeremy L Thompson // Set a vector to a value strided
4073196072fSJeremy L Thompson //------------------------------------------------------------------------------
408ff90b007SJeremy L Thompson static int CeedVectorSetValueStrided_Hip(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedScalar val) {
4093196072fSJeremy L Thompson   CeedSize        length;
4103196072fSJeremy L Thompson   CeedVector_Hip *impl;
4113196072fSJeremy L Thompson 
4123196072fSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
4133196072fSJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
4143196072fSJeremy L Thompson   // Set value for synced device/host array
415ff90b007SJeremy L Thompson   if (stop == -1) stop = length;
4163196072fSJeremy L Thompson   if (impl->d_array) {
41714c82621SJeremy L Thompson     CeedCallBackend(CeedDeviceSetValueStrided_Hip(impl->d_array, start, stop, step, val));
4183196072fSJeremy L Thompson     impl->h_array = NULL;
4193196072fSJeremy L Thompson   } else if (impl->h_array) {
42014c82621SJeremy L Thompson     CeedCallBackend(CeedHostSetValueStrided_Hip(impl->h_array, start, stop, step, val));
4213196072fSJeremy L Thompson     impl->d_array = NULL;
4223196072fSJeremy L Thompson   } else {
4233196072fSJeremy L Thompson     return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set");
4243196072fSJeremy L Thompson   }
4253196072fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
4263196072fSJeremy L Thompson }
4273196072fSJeremy L Thompson 
4283196072fSJeremy L Thompson //------------------------------------------------------------------------------
4290d0321e0SJeremy L Thompson // Vector Take Array
4300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4312b730f8bSJeremy L Thompson static int CeedVectorTakeArray_Hip(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
4320d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
433b7453713SJeremy L Thompson 
4342b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
4350d0321e0SJeremy L Thompson 
43643c928f4SJeremy L Thompson   // Sync array to requested mem_type
4372b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSyncArray(vec, mem_type));
4380d0321e0SJeremy L Thompson 
4390d0321e0SJeremy L Thompson   // Update pointer
44043c928f4SJeremy L Thompson   switch (mem_type) {
4410d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
4420d0321e0SJeremy L Thompson       (*array)               = impl->h_array_borrowed;
4430d0321e0SJeremy L Thompson       impl->h_array_borrowed = NULL;
4440d0321e0SJeremy L Thompson       impl->h_array          = NULL;
4450d0321e0SJeremy L Thompson       break;
4460d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
4470d0321e0SJeremy L Thompson       (*array)               = impl->d_array_borrowed;
4480d0321e0SJeremy L Thompson       impl->d_array_borrowed = NULL;
4490d0321e0SJeremy L Thompson       impl->d_array          = NULL;
4500d0321e0SJeremy L Thompson       break;
4510d0321e0SJeremy L Thompson   }
4520d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4530d0321e0SJeremy L Thompson }
4540d0321e0SJeremy L Thompson 
4550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
456*a3b195efSJeremy L Thompson // Core logic for array synchronization for GetArray.
4570d0321e0SJeremy L Thompson //   If a different memory type is most up to date, this will perform a copy
4580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
459*a3b195efSJeremy L Thompson static int CeedVectorGetArrayCore_Hip(const CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
4600d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
461b7453713SJeremy L Thompson 
4622b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
4630d0321e0SJeremy L Thompson 
464*a3b195efSJeremy L Thompson   // Use device memory for unified memory
465*a3b195efSJeremy L Thompson   mem_type = impl->has_unified_addressing && !impl->h_array_borrowed ? CEED_MEM_DEVICE : mem_type;
466*a3b195efSJeremy L Thompson 
46743c928f4SJeremy L Thompson   // Sync array to requested mem_type
4682b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSyncArray(vec, mem_type));
4690d0321e0SJeremy L Thompson 
4700d0321e0SJeremy L Thompson   // Update pointer
47143c928f4SJeremy L Thompson   switch (mem_type) {
4720d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
4730d0321e0SJeremy L Thompson       *array = impl->h_array;
4740d0321e0SJeremy L Thompson       break;
4750d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
4760d0321e0SJeremy L Thompson       *array = impl->d_array;
4770d0321e0SJeremy L Thompson       break;
4780d0321e0SJeremy L Thompson   }
4790d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4800d0321e0SJeremy L Thompson }
4810d0321e0SJeremy L Thompson 
4820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
48343c928f4SJeremy L Thompson // Get read-only access to a vector via the specified mem_type
4840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4852b730f8bSJeremy L Thompson static int CeedVectorGetArrayRead_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedScalar **array) {
48643c928f4SJeremy L Thompson   return CeedVectorGetArrayCore_Hip(vec, mem_type, (CeedScalar **)array);
4870d0321e0SJeremy L Thompson }
4880d0321e0SJeremy L Thompson 
4890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
49043c928f4SJeremy L Thompson // Get read/write access to a vector via the specified mem_type
4910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
492*a3b195efSJeremy L Thompson static int CeedVectorGetArray_Hip(const CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
4930d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
494b7453713SJeremy L Thompson 
4952b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
496*a3b195efSJeremy L Thompson 
497*a3b195efSJeremy L Thompson   // Use device memory for unified memory
498*a3b195efSJeremy L Thompson   mem_type = impl->has_unified_addressing && !impl->h_array_borrowed ? CEED_MEM_DEVICE : mem_type;
499*a3b195efSJeremy L Thompson 
500*a3b195efSJeremy L Thompson   // 'Get' array and set only 'get'ed array as valid
5012b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayCore_Hip(vec, mem_type, array));
5022b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec));
50343c928f4SJeremy L Thompson   switch (mem_type) {
5040d0321e0SJeremy L Thompson     case CEED_MEM_HOST:
5050d0321e0SJeremy L Thompson       impl->h_array = *array;
506*a3b195efSJeremy L Thompson       if (impl->has_unified_addressing) impl->d_array = *array;
5070d0321e0SJeremy L Thompson       break;
5080d0321e0SJeremy L Thompson     case CEED_MEM_DEVICE:
5090d0321e0SJeremy L Thompson       impl->d_array = *array;
5100d0321e0SJeremy L Thompson       break;
5110d0321e0SJeremy L Thompson   }
5120d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5130d0321e0SJeremy L Thompson }
5140d0321e0SJeremy L Thompson 
5150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
51643c928f4SJeremy L Thompson // Get write access to a vector via the specified mem_type
5170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
518*a3b195efSJeremy L Thompson static int CeedVectorGetArrayWrite_Hip(const CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
5190d0321e0SJeremy L Thompson   bool            has_array_of_type = true;
520b7453713SJeremy L Thompson   CeedVector_Hip *impl;
521*a3b195efSJeremy L Thompson   Ceed_Hip       *hip_data;
522b7453713SJeremy L Thompson 
523b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
524*a3b195efSJeremy L Thompson   CeedCallBackend(CeedGetData(CeedVectorReturnCeed(vec), &hip_data));
525*a3b195efSJeremy L Thompson 
526*a3b195efSJeremy L Thompson   // Use device memory for unified memory
527*a3b195efSJeremy L Thompson   mem_type = impl->has_unified_addressing && !impl->h_array_borrowed ? CEED_MEM_DEVICE : mem_type;
528*a3b195efSJeremy L Thompson 
5292b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorHasArrayOfType_Hip(vec, mem_type, &has_array_of_type));
5300d0321e0SJeremy L Thompson   if (!has_array_of_type) {
5310d0321e0SJeremy L Thompson     // Allocate if array is not yet allocated
5322b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL));
5330d0321e0SJeremy L Thompson   } else {
5340d0321e0SJeremy L Thompson     // Select dirty array
53543c928f4SJeremy L Thompson     switch (mem_type) {
5360d0321e0SJeremy L Thompson       case CEED_MEM_HOST:
5372b730f8bSJeremy L Thompson         if (impl->h_array_borrowed) impl->h_array = impl->h_array_borrowed;
5382b730f8bSJeremy L Thompson         else impl->h_array = impl->h_array_owned;
5390d0321e0SJeremy L Thompson         break;
5400d0321e0SJeremy L Thompson       case CEED_MEM_DEVICE:
5412b730f8bSJeremy L Thompson         if (impl->d_array_borrowed) impl->d_array = impl->d_array_borrowed;
5422b730f8bSJeremy L Thompson         else impl->d_array = impl->d_array_owned;
5430d0321e0SJeremy L Thompson     }
5440d0321e0SJeremy L Thompson   }
54543c928f4SJeremy L Thompson   return CeedVectorGetArray_Hip(vec, mem_type, array);
5460d0321e0SJeremy L Thompson }
5470d0321e0SJeremy L Thompson 
5480d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5490d0321e0SJeremy L Thompson // Get the norm of a CeedVector
5500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
5512b730f8bSJeremy L Thompson static int CeedVectorNorm_Hip(CeedVector vec, CeedNormType type, CeedScalar *norm) {
5520d0321e0SJeremy L Thompson   Ceed     ceed;
553e84c3ebcSJeremy L Thompson   CeedSize length;
554e84c3ebcSJeremy L Thompson #if (HIP_VERSION < 60000000)
555e84c3ebcSJeremy L Thompson   CeedSize num_calls;
556e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
557b7453713SJeremy L Thompson   const CeedScalar *d_array;
558b7453713SJeremy L Thompson   CeedVector_Hip   *impl;
5590d0321e0SJeremy L Thompson   hipblasHandle_t   handle;
560*a3b195efSJeremy L Thompson   Ceed_Hip         *hip_data;
561b7453713SJeremy L Thompson 
562b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
563*a3b195efSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &hip_data));
564b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
565b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
566eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetHipblasHandle_Hip(ceed, &handle));
5670d0321e0SJeremy L Thompson 
568e84c3ebcSJeremy L Thompson #if (HIP_VERSION < 60000000)
569e84c3ebcSJeremy L Thompson   // With ROCm 6, we can use the 64-bit integer interface. Prior to that,
570e84c3ebcSJeremy L Thompson   // we need to check if the vector is too long to handle with int32,
571e84c3ebcSJeremy L Thompson   // and if so, divide it into subsections for repeated hipBLAS calls.
572672b0f2aSSebastian Grimberg   num_calls = length / INT_MAX;
5739330daecSnbeams   if (length % INT_MAX > 0) num_calls += 1;
574e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
5759330daecSnbeams 
5760d0321e0SJeremy L Thompson   // Compute norm
5772b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array));
5780d0321e0SJeremy L Thompson   switch (type) {
5790d0321e0SJeremy L Thompson     case CEED_NORM_1: {
580f6f49adbSnbeams       *norm = 0.0;
581e84c3ebcSJeremy L Thompson #if defined(CEED_SCALAR_IS_FP32)
582e84c3ebcSJeremy L Thompson #if (HIP_VERSION >= 60000000)  // We have ROCm 6, and can use 64-bit integers
583e84c3ebcSJeremy L Thompson       CeedCallHipblas(ceed, hipblasSasum_64(handle, (int64_t)length, (float *)d_array, 1, (float *)norm));
584e84c3ebcSJeremy L Thompson #else  /* HIP_VERSION */
5859330daecSnbeams       float  sub_norm = 0.0;
5869330daecSnbeams       float *d_array_start;
587b7453713SJeremy L Thompson 
5889330daecSnbeams       for (CeedInt i = 0; i < num_calls; i++) {
5899330daecSnbeams         d_array_start             = (float *)d_array + (CeedSize)(i)*INT_MAX;
5909330daecSnbeams         CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
5919330daecSnbeams         CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
592b7453713SJeremy L Thompson 
593*a3b195efSJeremy L Thompson         CeedCallHipblas(ceed, hipblasSasum(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm));
5949330daecSnbeams         *norm += sub_norm;
5959330daecSnbeams       }
596e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
597e84c3ebcSJeremy L Thompson #else  /* CEED_SCALAR */
598e84c3ebcSJeremy L Thompson #if (HIP_VERSION >= 60000000)
599e84c3ebcSJeremy L Thompson       CeedCallHipblas(ceed, hipblasDasum_64(handle, (int64_t)length, (double *)d_array, 1, (double *)norm));
600e84c3ebcSJeremy L Thompson #else  /* HIP_VERSION */
6019330daecSnbeams       double  sub_norm = 0.0;
6029330daecSnbeams       double *d_array_start;
603b7453713SJeremy L Thompson 
6049330daecSnbeams       for (CeedInt i = 0; i < num_calls; i++) {
6059330daecSnbeams         d_array_start             = (double *)d_array + (CeedSize)(i)*INT_MAX;
6069330daecSnbeams         CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
6079330daecSnbeams         CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
608b7453713SJeremy L Thompson 
6099330daecSnbeams         CeedCallHipblas(ceed, hipblasDasum(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm));
6109330daecSnbeams         *norm += sub_norm;
6119330daecSnbeams       }
612e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
613e84c3ebcSJeremy L Thompson #endif /* CEED_SCALAR */
6140d0321e0SJeremy L Thompson       break;
6150d0321e0SJeremy L Thompson     }
6160d0321e0SJeremy L Thompson     case CEED_NORM_2: {
617e84c3ebcSJeremy L Thompson #if defined(CEED_SCALAR_IS_FP32)
618e84c3ebcSJeremy L Thompson #if (HIP_VERSION >= 60000000)
619e84c3ebcSJeremy L Thompson       CeedCallHipblas(ceed, hipblasSnrm2_64(handle, (int64_t)length, (float *)d_array, 1, (float *)norm));
620*a3b195efSJeremy L Thompson #else  /* HIP_VERSION */
6219330daecSnbeams       float  sub_norm = 0.0, norm_sum = 0.0;
6229330daecSnbeams       float *d_array_start;
623b7453713SJeremy L Thompson 
6249330daecSnbeams       for (CeedInt i = 0; i < num_calls; i++) {
6259330daecSnbeams         d_array_start             = (float *)d_array + (CeedSize)(i)*INT_MAX;
6269330daecSnbeams         CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
6279330daecSnbeams         CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
628b7453713SJeremy L Thompson 
6299330daecSnbeams         CeedCallHipblas(ceed, hipblasSnrm2(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm));
6309330daecSnbeams         norm_sum += sub_norm * sub_norm;
6319330daecSnbeams       }
6329330daecSnbeams       *norm = sqrt(norm_sum);
633e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
634e84c3ebcSJeremy L Thompson #else  /* CEED_SCALAR */
635e84c3ebcSJeremy L Thompson #if (HIP_VERSION >= 60000000)
636e84c3ebcSJeremy L Thompson       CeedCallHipblas(ceed, hipblasDnrm2_64(handle, (int64_t)length, (double *)d_array, 1, (double *)norm));
637*a3b195efSJeremy L Thompson #else  /* HIP_VERSION */
6389330daecSnbeams       double  sub_norm = 0.0, norm_sum = 0.0;
6399330daecSnbeams       double *d_array_start;
640b7453713SJeremy L Thompson 
6419330daecSnbeams       for (CeedInt i = 0; i < num_calls; i++) {
6429330daecSnbeams         d_array_start             = (double *)d_array + (CeedSize)(i)*INT_MAX;
6439330daecSnbeams         CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
6449330daecSnbeams         CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
645b7453713SJeremy L Thompson 
6469330daecSnbeams         CeedCallHipblas(ceed, hipblasDnrm2(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm));
6479330daecSnbeams         norm_sum += sub_norm * sub_norm;
6489330daecSnbeams       }
6499330daecSnbeams       *norm = sqrt(norm_sum);
650e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
651e84c3ebcSJeremy L Thompson #endif /* CEED_SCALAR */
6520d0321e0SJeremy L Thompson       break;
6530d0321e0SJeremy L Thompson     }
6540d0321e0SJeremy L Thompson     case CEED_NORM_MAX: {
655e84c3ebcSJeremy L Thompson #if defined(CEED_SCALAR_IS_FP32)
656e84c3ebcSJeremy L Thompson #if (HIP_VERSION >= 60000000)
657e84c3ebcSJeremy L Thompson       int64_t    index;
658e84c3ebcSJeremy L Thompson       CeedScalar norm_no_abs;
659b7453713SJeremy L Thompson 
660e84c3ebcSJeremy L Thompson       CeedCallHipblas(ceed, hipblasIsamax_64(handle, (int64_t)length, (float *)d_array, 1, &index));
661e84c3ebcSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(&norm_no_abs, impl->d_array + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost));
662e84c3ebcSJeremy L Thompson       *norm = fabs(norm_no_abs);
663e84c3ebcSJeremy L Thompson #else  /* HIP_VERSION */
664e84c3ebcSJeremy L Thompson       CeedInt index;
6659330daecSnbeams       float   sub_max = 0.0, current_max = 0.0;
6669330daecSnbeams       float  *d_array_start;
667e84c3ebcSJeremy L Thompson 
6689330daecSnbeams       for (CeedInt i = 0; i < num_calls; i++) {
6699330daecSnbeams         d_array_start             = (float *)d_array + (CeedSize)(i)*INT_MAX;
6709330daecSnbeams         CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
6719330daecSnbeams         CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
672b7453713SJeremy L Thompson 
673b7453713SJeremy L Thompson         CeedCallHipblas(ceed, hipblasIsamax(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &index));
674*a3b195efSJeremy L Thompson         if (hip_data->has_unified_addressing) {
675*a3b195efSJeremy L Thompson           CeedCallHip(ceed, hipDeviceSynchronize());
676*a3b195efSJeremy L Thompson           sub_max = fabs(d_array[index - 1]);
677*a3b195efSJeremy L Thompson         } else {
678b7453713SJeremy L Thompson           CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost));
679*a3b195efSJeremy L Thompson         }
6809330daecSnbeams         if (fabs(sub_max) > current_max) current_max = fabs(sub_max);
6819330daecSnbeams       }
6829330daecSnbeams       *norm = current_max;
683e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
684e84c3ebcSJeremy L Thompson #else  /* CEED_SCALAR */
685e84c3ebcSJeremy L Thompson #if (HIP_VERSION >= 60000000)
686e84c3ebcSJeremy L Thompson       int64_t    index;
687e84c3ebcSJeremy L Thompson       CeedScalar norm_no_abs;
688e84c3ebcSJeremy L Thompson 
689e84c3ebcSJeremy L Thompson       CeedCallHipblas(ceed, hipblasIdamax_64(handle, (int64_t)length, (double *)d_array, 1, &index));
690*a3b195efSJeremy L Thompson       if (hip_data->has_unified_addressing) {
691*a3b195efSJeremy L Thompson         CeedCallHip(ceed, hipDeviceSynchronize());
692*a3b195efSJeremy L Thompson         norm_no_abs = fabs(d_array[index - 1]);
693*a3b195efSJeremy L Thompson       } else {
694e84c3ebcSJeremy L Thompson         CeedCallHip(ceed, hipMemcpy(&norm_no_abs, impl->d_array + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost));
695*a3b195efSJeremy L Thompson       }
696e84c3ebcSJeremy L Thompson       *norm = fabs(norm_no_abs);
697e84c3ebcSJeremy L Thompson #else  /* HIP_VERSION */
698e84c3ebcSJeremy L Thompson       CeedInt index;
6999330daecSnbeams       double  sub_max = 0.0, current_max = 0.0;
7009330daecSnbeams       double *d_array_start;
701b7453713SJeremy L Thompson 
7029330daecSnbeams       for (CeedInt i = 0; i < num_calls; i++) {
7039330daecSnbeams         d_array_start             = (double *)d_array + (CeedSize)(i)*INT_MAX;
7049330daecSnbeams         CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
7059330daecSnbeams         CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
706b7453713SJeremy L Thompson 
707b7453713SJeremy L Thompson         CeedCallHipblas(ceed, hipblasIdamax(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &index));
708*a3b195efSJeremy L Thompson         if (hip_data->has_unified_addressing) {
709*a3b195efSJeremy L Thompson           CeedCallHip(ceed, hipDeviceSynchronize());
710*a3b195efSJeremy L Thompson           sub_max = fabs(d_array[index - 1]);
711*a3b195efSJeremy L Thompson         } else {
712b7453713SJeremy L Thompson           CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost));
713*a3b195efSJeremy L Thompson         }
7149330daecSnbeams         if (fabs(sub_max) > current_max) current_max = fabs(sub_max);
7159330daecSnbeams       }
7169330daecSnbeams       *norm = current_max;
717e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
718e84c3ebcSJeremy L Thompson #endif /* CEED_SCALAR */
7190d0321e0SJeremy L Thompson       break;
7200d0321e0SJeremy L Thompson     }
7210d0321e0SJeremy L Thompson   }
7222b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array));
7239bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
7240d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7250d0321e0SJeremy L Thompson }
7260d0321e0SJeremy L Thompson 
7270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7280d0321e0SJeremy L Thompson // Take reciprocal of a vector on host
7290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7309330daecSnbeams static int CeedHostReciprocal_Hip(CeedScalar *h_array, CeedSize length) {
7319330daecSnbeams   for (CeedSize i = 0; i < length; i++) {
7322b730f8bSJeremy L Thompson     if (fabs(h_array[i]) > CEED_EPSILON) h_array[i] = 1. / h_array[i];
7332b730f8bSJeremy L Thompson   }
7340d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7350d0321e0SJeremy L Thompson }
7360d0321e0SJeremy L Thompson 
7370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
738956a3dbaSJeremy L Thompson // Take reciprocal of a vector on device (impl in .hip.cpp file)
7390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7409330daecSnbeams int CeedDeviceReciprocal_Hip(CeedScalar *d_array, CeedSize length);
7410d0321e0SJeremy L Thompson 
7420d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7430d0321e0SJeremy L Thompson // Take reciprocal of a vector
7440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7450d0321e0SJeremy L Thompson static int CeedVectorReciprocal_Hip(CeedVector vec) {
7461f9221feSJeremy L Thompson   CeedSize        length;
747b7453713SJeremy L Thompson   CeedVector_Hip *impl;
7480d0321e0SJeremy L Thompson 
749b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
750b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(vec, &length));
7510d0321e0SJeremy L Thompson   // Set value for synced device/host array
7522b730f8bSJeremy L Thompson   if (impl->d_array) CeedCallBackend(CeedDeviceReciprocal_Hip(impl->d_array, length));
7532b730f8bSJeremy L Thompson   if (impl->h_array) CeedCallBackend(CeedHostReciprocal_Hip(impl->h_array, length));
7540d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7550d0321e0SJeremy L Thompson }
7560d0321e0SJeremy L Thompson 
7570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7580d0321e0SJeremy L Thompson // Compute x = alpha x on the host
7590d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7609330daecSnbeams static int CeedHostScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length) {
7619330daecSnbeams   for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha;
7620d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7630d0321e0SJeremy L Thompson }
7640d0321e0SJeremy L Thompson 
7650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
766956a3dbaSJeremy L Thompson // Compute x = alpha x on device (impl in .hip.cpp file)
7670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7689330daecSnbeams int CeedDeviceScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length);
7690d0321e0SJeremy L Thompson 
7700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7710d0321e0SJeremy L Thompson // Compute x = alpha x
7720d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7730d0321e0SJeremy L Thompson static int CeedVectorScale_Hip(CeedVector x, CeedScalar alpha) {
7741f9221feSJeremy L Thompson   CeedSize        length;
775e84c3ebcSJeremy L Thompson   CeedVector_Hip *impl;
7760d0321e0SJeremy L Thompson 
777e84c3ebcSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(x, &impl));
778b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(x, &length));
7790d0321e0SJeremy L Thompson   // Set value for synced device/host array
780e84c3ebcSJeremy L Thompson   if (impl->d_array) {
781e84c3ebcSJeremy L Thompson #if (HIP_VERSION >= 60000000)
782e84c3ebcSJeremy L Thompson     hipblasHandle_t handle;
783e84c3ebcSJeremy L Thompson 
784e84c3ebcSJeremy L Thompson     CeedCallBackend(CeedGetHipblasHandle_Hip(CeedVectorReturnCeed(x), &handle));
785e84c3ebcSJeremy L Thompson #if defined(CEED_SCALAR_IS_FP32)
786e84c3ebcSJeremy L Thompson     CeedCallHipblas(CeedVectorReturnCeed(x), hipblasSscal_64(handle, (int64_t)length, &alpha, impl->d_array, 1));
787e84c3ebcSJeremy L Thompson #else  /* CEED_SCALAR */
788e84c3ebcSJeremy L Thompson     CeedCallHipblas(CeedVectorReturnCeed(x), hipblasDscal_64(handle, (int64_t)length, &alpha, impl->d_array, 1));
789e84c3ebcSJeremy L Thompson #endif /* CEED_SCALAR */
790e84c3ebcSJeremy L Thompson #else  /* HIP_VERSION */
791e84c3ebcSJeremy L Thompson     CeedCallBackend(CeedDeviceScale_Hip(impl->d_array, alpha, length));
792e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
793e84c3ebcSJeremy L Thompson     impl->h_array = NULL;
794e84c3ebcSJeremy L Thompson   }
795e84c3ebcSJeremy L Thompson   if (impl->h_array) {
796e84c3ebcSJeremy L Thompson     CeedCallBackend(CeedHostScale_Hip(impl->h_array, alpha, length));
797e84c3ebcSJeremy L Thompson     impl->d_array = NULL;
798e84c3ebcSJeremy L Thompson   }
7990d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8000d0321e0SJeremy L Thompson }
8010d0321e0SJeremy L Thompson 
8020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8030d0321e0SJeremy L Thompson // Compute y = alpha x + y on the host
8040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8059330daecSnbeams static int CeedHostAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length) {
8069330daecSnbeams   for (CeedSize i = 0; i < length; i++) y_array[i] += alpha * x_array[i];
8070d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8080d0321e0SJeremy L Thompson }
8090d0321e0SJeremy L Thompson 
8100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
811956a3dbaSJeremy L Thompson // Compute y = alpha x + y on device (impl in .hip.cpp file)
8120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8139330daecSnbeams int CeedDeviceAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length);
8140d0321e0SJeremy L Thompson 
8150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8160d0321e0SJeremy L Thompson // Compute y = alpha x + y
8170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8180d0321e0SJeremy L Thompson static int CeedVectorAXPY_Hip(CeedVector y, CeedScalar alpha, CeedVector x) {
819b7453713SJeremy L Thompson   CeedSize        length;
8200d0321e0SJeremy L Thompson   CeedVector_Hip *y_impl, *x_impl;
821b7453713SJeremy L Thompson 
8222b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(y, &y_impl));
8232b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(x, &x_impl));
8242b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(y, &length));
8250d0321e0SJeremy L Thompson   // Set value for synced device/host array
8260d0321e0SJeremy L Thompson   if (y_impl->d_array) {
8272b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
828e84c3ebcSJeremy L Thompson #if (HIP_VERSION >= 60000000)
829e84c3ebcSJeremy L Thompson     hipblasHandle_t handle;
830e84c3ebcSJeremy L Thompson 
831e84c3ebcSJeremy L Thompson     CeedCallBackend(CeedGetHipblasHandle_Hip(CeedVectorReturnCeed(y), &handle));
832e84c3ebcSJeremy L Thompson #if defined(CEED_SCALAR_IS_FP32)
833e84c3ebcSJeremy L Thompson     CeedCallHipblas(CeedVectorReturnCeed(y), hipblasSaxpy_64(handle, (int64_t)length, &alpha, x_impl->d_array, 1, y_impl->d_array, 1));
834e84c3ebcSJeremy L Thompson #else  /* CEED_SCALAR */
835e84c3ebcSJeremy L Thompson     CeedCallHipblas(CeedVectorReturnCeed(y), hipblasDaxpy_64(handle, (int64_t)length, &alpha, x_impl->d_array, 1, y_impl->d_array, 1));
836e84c3ebcSJeremy L Thompson #endif /* CEED_SCALAR */
837e84c3ebcSJeremy L Thompson #else  /* HIP_VERSION */
8382b730f8bSJeremy L Thompson     CeedCallBackend(CeedDeviceAXPY_Hip(y_impl->d_array, alpha, x_impl->d_array, length));
839e84c3ebcSJeremy L Thompson #endif /* HIP_VERSION */
840e84c3ebcSJeremy L Thompson     y_impl->h_array = NULL;
841e84c3ebcSJeremy L Thompson   } else if (y_impl->h_array) {
8422b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
8432b730f8bSJeremy L Thompson     CeedCallBackend(CeedHostAXPY_Hip(y_impl->h_array, alpha, x_impl->h_array, length));
844e84c3ebcSJeremy L Thompson     y_impl->d_array = NULL;
8450d0321e0SJeremy L Thompson   }
8460d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8470d0321e0SJeremy L Thompson }
848ff1e7120SSebastian Grimberg 
8495fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
8505fb68f37SKaren (Ren) Stengel // Compute y = alpha x + beta y on the host
8515fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
8529330daecSnbeams static int CeedHostAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length) {
853aa67b842SZach Atkins   for (CeedSize i = 0; i < length; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i];
8545fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
8555fb68f37SKaren (Ren) Stengel }
8565fb68f37SKaren (Ren) Stengel 
8575fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
858956a3dbaSJeremy L Thompson // Compute y = alpha x + beta y on device (impl in .hip.cpp file)
8595fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
8609330daecSnbeams int CeedDeviceAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length);
8615fb68f37SKaren (Ren) Stengel 
8625fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
8635fb68f37SKaren (Ren) Stengel // Compute y = alpha x + beta y
8645fb68f37SKaren (Ren) Stengel //------------------------------------------------------------------------------
8655fb68f37SKaren (Ren) Stengel static int CeedVectorAXPBY_Hip(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
866b7453713SJeremy L Thompson   CeedSize        length;
8675fb68f37SKaren (Ren) Stengel   CeedVector_Hip *y_impl, *x_impl;
868b7453713SJeremy L Thompson 
8695fb68f37SKaren (Ren) Stengel   CeedCallBackend(CeedVectorGetData(y, &y_impl));
8705fb68f37SKaren (Ren) Stengel   CeedCallBackend(CeedVectorGetData(x, &x_impl));
8715fb68f37SKaren (Ren) Stengel   CeedCallBackend(CeedVectorGetLength(y, &length));
8725fb68f37SKaren (Ren) Stengel   // Set value for synced device/host array
8735fb68f37SKaren (Ren) Stengel   if (y_impl->d_array) {
8745fb68f37SKaren (Ren) Stengel     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
8755fb68f37SKaren (Ren) Stengel     CeedCallBackend(CeedDeviceAXPBY_Hip(y_impl->d_array, alpha, beta, x_impl->d_array, length));
8765fb68f37SKaren (Ren) Stengel   }
8775fb68f37SKaren (Ren) Stengel   if (y_impl->h_array) {
8785fb68f37SKaren (Ren) Stengel     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
8795fb68f37SKaren (Ren) Stengel     CeedCallBackend(CeedHostAXPBY_Hip(y_impl->h_array, alpha, beta, x_impl->h_array, length));
8805fb68f37SKaren (Ren) Stengel   }
8815fb68f37SKaren (Ren) Stengel   return CEED_ERROR_SUCCESS;
8825fb68f37SKaren (Ren) Stengel }
8830d0321e0SJeremy L Thompson 
8840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8850d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y on the host
8860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8879330daecSnbeams static int CeedHostPointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length) {
8889330daecSnbeams   for (CeedSize i = 0; i < length; i++) w_array[i] = x_array[i] * y_array[i];
8890d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8900d0321e0SJeremy L Thompson }
8910d0321e0SJeremy L Thompson 
8920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
893956a3dbaSJeremy L Thompson // Compute the pointwise multiplication w = x .* y on device (impl in .hip.cpp file)
8940d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8959330daecSnbeams int CeedDevicePointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length);
8960d0321e0SJeremy L Thompson 
8970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8980d0321e0SJeremy L Thompson // Compute the pointwise multiplication w = x .* y
8990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9002b730f8bSJeremy L Thompson static int CeedVectorPointwiseMult_Hip(CeedVector w, CeedVector x, CeedVector y) {
901b7453713SJeremy L Thompson   CeedSize        length;
9020d0321e0SJeremy L Thompson   CeedVector_Hip *w_impl, *x_impl, *y_impl;
903b7453713SJeremy L Thompson 
9042b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(w, &w_impl));
9052b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(x, &x_impl));
9062b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetData(y, &y_impl));
9072b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetLength(w, &length));
9080d0321e0SJeremy L Thompson 
9090d0321e0SJeremy L Thompson   // Set value for synced device/host array
9100d0321e0SJeremy L Thompson   if (!w_impl->d_array && !w_impl->h_array) {
9112b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(w, 0.0));
9120d0321e0SJeremy L Thompson   }
9130d0321e0SJeremy L Thompson   if (w_impl->d_array) {
9142b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
9152b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_DEVICE));
9162b730f8bSJeremy L Thompson     CeedCallBackend(CeedDevicePointwiseMult_Hip(w_impl->d_array, x_impl->d_array, y_impl->d_array, length));
9170d0321e0SJeremy L Thompson   }
9180d0321e0SJeremy L Thompson   if (w_impl->h_array) {
9192b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
9202b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_HOST));
9212b730f8bSJeremy L Thompson     CeedCallBackend(CeedHostPointwiseMult_Hip(w_impl->h_array, x_impl->h_array, y_impl->h_array, length));
9220d0321e0SJeremy L Thompson   }
9230d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9240d0321e0SJeremy L Thompson }
9250d0321e0SJeremy L Thompson 
9260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9270d0321e0SJeremy L Thompson // Destroy the vector
9280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9290d0321e0SJeremy L Thompson static int CeedVectorDestroy_Hip(const CeedVector vec) {
9300d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
9310d0321e0SJeremy L Thompson 
932b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetData(vec, &impl));
9336e536b99SJeremy L Thompson   CeedCallHip(CeedVectorReturnCeed(vec), hipFree(impl->d_array_owned));
9342b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->h_array_owned));
9352b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
9360d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9370d0321e0SJeremy L Thompson }
9380d0321e0SJeremy L Thompson 
9390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9400d0321e0SJeremy L Thompson // Create a vector of the specified length (does not allocate memory)
9410d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9421f9221feSJeremy L Thompson int CeedVectorCreate_Hip(CeedSize n, CeedVector vec) {
9430d0321e0SJeremy L Thompson   CeedVector_Hip *impl;
944*a3b195efSJeremy L Thompson   Ceed_Hip       *hip_impl;
9450d0321e0SJeremy L Thompson   Ceed            ceed;
9460d0321e0SJeremy L Thompson 
947b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
9482b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Hip));
9492b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Hip));
9502b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Hip));
9512b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Hip));
9523e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "CopyStrided", CeedVectorCopyStrided_Hip));
9533e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValue", CeedVectorSetValue_Hip));
9543e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValueStrided", CeedVectorSetValueStrided_Hip));
9552b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SyncArray", CeedVectorSyncArray_Hip));
9562b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Hip));
9572b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Hip));
9582b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Hip));
9592b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Norm", CeedVectorNorm_Hip));
9602b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Reciprocal", CeedVectorReciprocal_Hip));
9613e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Scale", CeedVectorScale_Hip));
9623e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPY", CeedVectorAXPY_Hip));
9633e961e14SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", CeedVectorAXPBY_Hip));
9642b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Hip));
9652b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Hip));
9662b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
967*a3b195efSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &hip_impl));
968*a3b195efSJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
969*a3b195efSJeremy L Thompson   impl->has_unified_addressing = hip_impl->has_unified_addressing;
9702b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetData(vec, impl));
9710d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9720d0321e0SJeremy L Thompson }
9732a86cc9dSSebastian Grimberg 
9742a86cc9dSSebastian Grimberg //------------------------------------------------------------------------------
975