xref: /libCEED/backends/hip-ref/ceed-hip-ref-vector.c (revision b73fa92ca232cd7a1379d6ffb54e013a90896973)
1 // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed.h>
9 #include <ceed/backend.h>
10 #include <math.h>
11 #include <stdbool.h>
12 #include <string.h>
13 #include <hip/hip_runtime.h>
14 
15 #include "../hip/ceed-hip-common.h"
16 #include "ceed-hip-ref.h"
17 
18 //------------------------------------------------------------------------------
19 // Check if host/device sync is needed
20 //------------------------------------------------------------------------------
21 static inline int CeedVectorNeedSync_Hip(const CeedVector vec, CeedMemType mem_type, bool *need_sync) {
22   CeedVector_Hip *impl;
23   bool            has_valid_array = false;
24 
25   CeedCallBackend(CeedVectorGetData(vec, &impl));
26   CeedCallBackend(CeedVectorHasValidArray(vec, &has_valid_array));
27   switch (mem_type) {
28     case CEED_MEM_HOST:
29       *need_sync = has_valid_array && !impl->h_array;
30       break;
31     case CEED_MEM_DEVICE:
32       *need_sync = has_valid_array && !impl->d_array;
33       break;
34   }
35   return CEED_ERROR_SUCCESS;
36 }
37 
38 //------------------------------------------------------------------------------
39 // Sync host to device
40 //------------------------------------------------------------------------------
41 static inline int CeedVectorSyncH2D_Hip(const CeedVector vec) {
42   Ceed            ceed;
43   CeedSize        length;
44   size_t          bytes;
45   CeedVector_Hip *impl;
46 
47   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
48   CeedCallBackend(CeedVectorGetData(vec, &impl));
49 
50   CeedCheck(impl->h_array, ceed, CEED_ERROR_BACKEND, "No valid host data to sync to device");
51 
52   CeedCallBackend(CeedVectorGetLength(vec, &length));
53   bytes = length * sizeof(CeedScalar);
54   if (impl->d_array_borrowed) {
55     impl->d_array = impl->d_array_borrowed;
56   } else if (impl->d_array_owned) {
57     impl->d_array = impl->d_array_owned;
58   } else {
59     CeedCallHip(ceed, hipMalloc((void **)&impl->d_array_owned, bytes));
60     impl->d_array = impl->d_array_owned;
61   }
62   CeedCallHip(ceed, hipMemcpy(impl->d_array, impl->h_array, bytes, hipMemcpyHostToDevice));
63   return CEED_ERROR_SUCCESS;
64 }
65 
66 //------------------------------------------------------------------------------
67 // Sync device to host
68 //------------------------------------------------------------------------------
69 static inline int CeedVectorSyncD2H_Hip(const CeedVector vec) {
70   Ceed            ceed;
71   CeedSize        length;
72   size_t          bytes;
73   CeedVector_Hip *impl;
74 
75   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
76   CeedCallBackend(CeedVectorGetData(vec, &impl));
77 
78   CeedCheck(impl->d_array, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host");
79 
80   if (impl->h_array_borrowed) {
81     impl->h_array = impl->h_array_borrowed;
82   } else if (impl->h_array_owned) {
83     impl->h_array = impl->h_array_owned;
84   } else {
85     CeedSize length;
86 
87     CeedCallBackend(CeedVectorGetLength(vec, &length));
88     CeedCallBackend(CeedCalloc(length, &impl->h_array_owned));
89     impl->h_array = impl->h_array_owned;
90   }
91 
92   CeedCallBackend(CeedVectorGetLength(vec, &length));
93   bytes = length * sizeof(CeedScalar);
94   CeedCallHip(ceed, hipMemcpy(impl->h_array, impl->d_array, bytes, hipMemcpyDeviceToHost));
95   return CEED_ERROR_SUCCESS;
96 }
97 
98 //------------------------------------------------------------------------------
99 // Sync arrays
100 //------------------------------------------------------------------------------
101 static int CeedVectorSyncArray_Hip(const CeedVector vec, CeedMemType mem_type) {
102   bool need_sync = false;
103 
104   // Check whether device/host sync is needed
105   CeedCallBackend(CeedVectorNeedSync_Hip(vec, mem_type, &need_sync));
106   if (!need_sync) return CEED_ERROR_SUCCESS;
107 
108   switch (mem_type) {
109     case CEED_MEM_HOST:
110       return CeedVectorSyncD2H_Hip(vec);
111     case CEED_MEM_DEVICE:
112       return CeedVectorSyncH2D_Hip(vec);
113   }
114   return CEED_ERROR_UNSUPPORTED;
115 }
116 
117 //------------------------------------------------------------------------------
118 // Set all pointers as invalid
119 //------------------------------------------------------------------------------
120 static inline int CeedVectorSetAllInvalid_Hip(const CeedVector vec) {
121   CeedVector_Hip *impl;
122 
123   CeedCallBackend(CeedVectorGetData(vec, &impl));
124   impl->h_array = NULL;
125   impl->d_array = NULL;
126   return CEED_ERROR_SUCCESS;
127 }
128 
129 //------------------------------------------------------------------------------
130 // Check if CeedVector has any valid pointer
131 //------------------------------------------------------------------------------
132 static inline int CeedVectorHasValidArray_Hip(const CeedVector vec, bool *has_valid_array) {
133   CeedVector_Hip *impl;
134 
135   CeedCallBackend(CeedVectorGetData(vec, &impl));
136   *has_valid_array = impl->h_array || impl->d_array;
137   return CEED_ERROR_SUCCESS;
138 }
139 
140 //------------------------------------------------------------------------------
141 // Check if has array of given type
142 //------------------------------------------------------------------------------
143 static inline int CeedVectorHasArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_array_of_type) {
144   CeedVector_Hip *impl;
145 
146   CeedCallBackend(CeedVectorGetData(vec, &impl));
147   switch (mem_type) {
148     case CEED_MEM_HOST:
149       *has_array_of_type = impl->h_array_borrowed || impl->h_array_owned;
150       break;
151     case CEED_MEM_DEVICE:
152       *has_array_of_type = impl->d_array_borrowed || impl->d_array_owned;
153       break;
154   }
155   return CEED_ERROR_SUCCESS;
156 }
157 
158 //------------------------------------------------------------------------------
159 // Check if has borrowed array of given type
160 //------------------------------------------------------------------------------
161 static inline int CeedVectorHasBorrowedArrayOfType_Hip(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) {
162   CeedVector_Hip *impl;
163 
164   CeedCallBackend(CeedVectorGetData(vec, &impl));
165   switch (mem_type) {
166     case CEED_MEM_HOST:
167       *has_borrowed_array_of_type = impl->h_array_borrowed;
168       break;
169     case CEED_MEM_DEVICE:
170       *has_borrowed_array_of_type = impl->d_array_borrowed;
171       break;
172   }
173   return CEED_ERROR_SUCCESS;
174 }
175 
176 //------------------------------------------------------------------------------
177 // Set array from host
178 //------------------------------------------------------------------------------
179 static int CeedVectorSetArrayHost_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) {
180   CeedSize        length;
181   CeedVector_Hip *impl;
182 
183   CeedCallBackend(CeedVectorGetData(vec, &impl));
184   CeedCallBackend(CeedVectorGetLength(vec, &length));
185 
186   CeedCallBackend(CeedSetHostCeedScalarArray(array, copy_mode, length, (const CeedScalar **)&impl->h_array_owned,
187                                              (const CeedScalar **)&impl->h_array_borrowed, (const CeedScalar **)&impl->h_array));
188   return CEED_ERROR_SUCCESS;
189 }
190 
191 //------------------------------------------------------------------------------
192 // Set array from device
193 //------------------------------------------------------------------------------
194 static int CeedVectorSetArrayDevice_Hip(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) {
195   CeedSize        length;
196   Ceed            ceed;
197   CeedVector_Hip *impl;
198 
199   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
200   CeedCallBackend(CeedVectorGetData(vec, &impl));
201   CeedCallBackend(CeedVectorGetLength(vec, &length));
202 
203   CeedCallBackend(CeedSetDeviceCeedScalarArray_Hip(ceed, array, copy_mode, length, (const CeedScalar **)&impl->d_array_owned,
204                                                    (const CeedScalar **)&impl->d_array_borrowed, (const CeedScalar **)&impl->d_array));
205   return CEED_ERROR_SUCCESS;
206 }
207 
208 //------------------------------------------------------------------------------
209 // Set the array used by a vector,
210 //   freeing any previously allocated array if applicable
211 //------------------------------------------------------------------------------
212 static int CeedVectorSetArray_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedCopyMode copy_mode, CeedScalar *array) {
213   CeedVector_Hip *impl;
214 
215   CeedCallBackend(CeedVectorGetData(vec, &impl));
216   CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec));
217   switch (mem_type) {
218     case CEED_MEM_HOST:
219       return CeedVectorSetArrayHost_Hip(vec, copy_mode, array);
220     case CEED_MEM_DEVICE:
221       return CeedVectorSetArrayDevice_Hip(vec, copy_mode, array);
222   }
223   return CEED_ERROR_UNSUPPORTED;
224 }
225 
226 //------------------------------------------------------------------------------
227 // Copy host array to value strided
228 //------------------------------------------------------------------------------
229 static int CeedHostCopyStrided_Hip(CeedScalar *h_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar *h_copy_array) {
230   for (CeedSize i = start; i < length; i += step) h_copy_array[i] = h_array[i];
231   return CEED_ERROR_SUCCESS;
232 }
233 
234 //------------------------------------------------------------------------------
235 // Copy device array to value strided (impl in .hip.cpp file)
236 //------------------------------------------------------------------------------
237 int CeedDeviceCopyStrided_Hip(CeedScalar *d_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar *d_copy_array);
238 
239 //------------------------------------------------------------------------------
240 // Copy a vector to a value strided
241 //------------------------------------------------------------------------------
242 static int CeedVectorCopyStrided_Hip(CeedVector vec, CeedSize start, CeedSize step, CeedVector vec_copy) {
243   CeedSize        length;
244   CeedVector_Hip *impl;
245 
246   CeedCallBackend(CeedVectorGetData(vec, &impl));
247   CeedCallBackend(CeedVectorGetLength(vec, &length));
248   // Set value for synced device/host array
249   if (impl->d_array) {
250     CeedScalar *copy_array;
251 
252     CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_DEVICE, &copy_array));
253     CeedCallBackend(CeedDeviceCopyStrided_Hip(impl->d_array, start, step, length, copy_array));
254     CeedCallBackend(CeedVectorRestoreArray(vec_copy, &copy_array));
255   } else if (impl->h_array) {
256     CeedScalar *copy_array;
257 
258     CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, &copy_array));
259     CeedCallBackend(CeedHostCopyStrided_Hip(impl->h_array, start, step, length, copy_array));
260     CeedCallBackend(CeedVectorRestoreArray(vec_copy, &copy_array));
261   } else {
262     return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set");
263   }
264   return CEED_ERROR_SUCCESS;
265 }
266 
267 //------------------------------------------------------------------------------
268 // Set host array to value
269 //------------------------------------------------------------------------------
270 static int CeedHostSetValue_Hip(CeedScalar *h_array, CeedSize length, CeedScalar val) {
271   for (CeedSize i = 0; i < length; i++) h_array[i] = val;
272   return CEED_ERROR_SUCCESS;
273 }
274 
275 //------------------------------------------------------------------------------
276 // Set device array to value (impl in .hip file)
277 //------------------------------------------------------------------------------
278 int CeedDeviceSetValue_Hip(CeedScalar *d_array, CeedSize length, CeedScalar val);
279 
280 //------------------------------------------------------------------------------
281 // Set a vector to a value
282 //------------------------------------------------------------------------------
283 static int CeedVectorSetValue_Hip(CeedVector vec, CeedScalar val) {
284   CeedSize        length;
285   CeedVector_Hip *impl;
286 
287   CeedCallBackend(CeedVectorGetData(vec, &impl));
288   CeedCallBackend(CeedVectorGetLength(vec, &length));
289   // Set value for synced device/host array
290   if (!impl->d_array && !impl->h_array) {
291     if (impl->d_array_borrowed) {
292       impl->d_array = impl->d_array_borrowed;
293     } else if (impl->h_array_borrowed) {
294       impl->h_array = impl->h_array_borrowed;
295     } else if (impl->d_array_owned) {
296       impl->d_array = impl->d_array_owned;
297     } else if (impl->h_array_owned) {
298       impl->h_array = impl->h_array_owned;
299     } else {
300       CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_DEVICE, CEED_COPY_VALUES, NULL));
301     }
302   }
303   if (impl->d_array) {
304     CeedCallBackend(CeedDeviceSetValue_Hip(impl->d_array, length, val));
305     impl->h_array = NULL;
306   }
307   if (impl->h_array) {
308     CeedCallBackend(CeedHostSetValue_Hip(impl->h_array, length, val));
309     impl->d_array = NULL;
310   }
311   return CEED_ERROR_SUCCESS;
312 }
313 
314 //------------------------------------------------------------------------------
315 // Set host array to value strided
316 //------------------------------------------------------------------------------
317 static int CeedHostSetValueStrided_Hip(CeedScalar *h_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar val) {
318   for (CeedSize i = start; i < length; i += step) h_array[i] = val;
319   return CEED_ERROR_SUCCESS;
320 }
321 
322 //------------------------------------------------------------------------------
323 // Set device array to value strided (impl in .hip.cpp file)
324 //------------------------------------------------------------------------------
325 int CeedDeviceSetValueStrided_Hip(CeedScalar *d_array, CeedSize start, CeedSize step, CeedSize length, CeedScalar val);
326 
327 //------------------------------------------------------------------------------
328 // Set a vector to a value strided
329 //------------------------------------------------------------------------------
330 static int CeedVectorSetValueStrided_Hip(CeedVector vec, CeedSize start, CeedSize step, CeedScalar val) {
331   CeedSize        length;
332   CeedVector_Hip *impl;
333 
334   CeedCallBackend(CeedVectorGetData(vec, &impl));
335   CeedCallBackend(CeedVectorGetLength(vec, &length));
336   // Set value for synced device/host array
337   if (impl->d_array) {
338     CeedCallBackend(CeedDeviceSetValueStrided_Hip(impl->d_array, start, step, length, val));
339     impl->h_array = NULL;
340   } else if (impl->h_array) {
341     CeedCallBackend(CeedHostSetValueStrided_Hip(impl->h_array, start, step, length, val));
342     impl->d_array = NULL;
343   } else {
344     return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set");
345   }
346   return CEED_ERROR_SUCCESS;
347 }
348 
349 //------------------------------------------------------------------------------
350 // Vector Take Array
351 //------------------------------------------------------------------------------
352 static int CeedVectorTakeArray_Hip(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
353   CeedVector_Hip *impl;
354 
355   CeedCallBackend(CeedVectorGetData(vec, &impl));
356 
357   // Sync array to requested mem_type
358   CeedCallBackend(CeedVectorSyncArray(vec, mem_type));
359 
360   // Update pointer
361   switch (mem_type) {
362     case CEED_MEM_HOST:
363       (*array)               = impl->h_array_borrowed;
364       impl->h_array_borrowed = NULL;
365       impl->h_array          = NULL;
366       break;
367     case CEED_MEM_DEVICE:
368       (*array)               = impl->d_array_borrowed;
369       impl->d_array_borrowed = NULL;
370       impl->d_array          = NULL;
371       break;
372   }
373   return CEED_ERROR_SUCCESS;
374 }
375 
376 //------------------------------------------------------------------------------
377 // Core logic for array syncronization for GetArray.
378 //   If a different memory type is most up to date, this will perform a copy
379 //------------------------------------------------------------------------------
380 static int CeedVectorGetArrayCore_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) {
381   CeedVector_Hip *impl;
382 
383   CeedCallBackend(CeedVectorGetData(vec, &impl));
384 
385   // Sync array to requested mem_type
386   CeedCallBackend(CeedVectorSyncArray(vec, mem_type));
387 
388   // Update pointer
389   switch (mem_type) {
390     case CEED_MEM_HOST:
391       *array = impl->h_array;
392       break;
393     case CEED_MEM_DEVICE:
394       *array = impl->d_array;
395       break;
396   }
397   return CEED_ERROR_SUCCESS;
398 }
399 
400 //------------------------------------------------------------------------------
401 // Get read-only access to a vector via the specified mem_type
402 //------------------------------------------------------------------------------
403 static int CeedVectorGetArrayRead_Hip(const CeedVector vec, const CeedMemType mem_type, const CeedScalar **array) {
404   return CeedVectorGetArrayCore_Hip(vec, mem_type, (CeedScalar **)array);
405 }
406 
407 //------------------------------------------------------------------------------
408 // Get read/write access to a vector via the specified mem_type
409 //------------------------------------------------------------------------------
410 static int CeedVectorGetArray_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) {
411   CeedVector_Hip *impl;
412 
413   CeedCallBackend(CeedVectorGetData(vec, &impl));
414   CeedCallBackend(CeedVectorGetArrayCore_Hip(vec, mem_type, array));
415   CeedCallBackend(CeedVectorSetAllInvalid_Hip(vec));
416   switch (mem_type) {
417     case CEED_MEM_HOST:
418       impl->h_array = *array;
419       break;
420     case CEED_MEM_DEVICE:
421       impl->d_array = *array;
422       break;
423   }
424   return CEED_ERROR_SUCCESS;
425 }
426 
427 //------------------------------------------------------------------------------
428 // Get write access to a vector via the specified mem_type
429 //------------------------------------------------------------------------------
430 static int CeedVectorGetArrayWrite_Hip(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) {
431   bool            has_array_of_type = true;
432   CeedVector_Hip *impl;
433 
434   CeedCallBackend(CeedVectorGetData(vec, &impl));
435   CeedCallBackend(CeedVectorHasArrayOfType_Hip(vec, mem_type, &has_array_of_type));
436   if (!has_array_of_type) {
437     // Allocate if array is not yet allocated
438     CeedCallBackend(CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL));
439   } else {
440     // Select dirty array
441     switch (mem_type) {
442       case CEED_MEM_HOST:
443         if (impl->h_array_borrowed) impl->h_array = impl->h_array_borrowed;
444         else impl->h_array = impl->h_array_owned;
445         break;
446       case CEED_MEM_DEVICE:
447         if (impl->d_array_borrowed) impl->d_array = impl->d_array_borrowed;
448         else impl->d_array = impl->d_array_owned;
449     }
450   }
451   return CeedVectorGetArray_Hip(vec, mem_type, array);
452 }
453 
454 //------------------------------------------------------------------------------
455 // Get the norm of a CeedVector
456 //------------------------------------------------------------------------------
457 static int CeedVectorNorm_Hip(CeedVector vec, CeedNormType type, CeedScalar *norm) {
458   Ceed              ceed;
459   CeedSize          length, num_calls;
460   const CeedScalar *d_array;
461   CeedVector_Hip   *impl;
462   hipblasHandle_t   handle;
463 
464   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
465   CeedCallBackend(CeedVectorGetData(vec, &impl));
466   CeedCallBackend(CeedVectorGetLength(vec, &length));
467   CeedCallBackend(CeedGetHipblasHandle_Hip(ceed, &handle));
468 
469   // Is the vector too long to handle with int32? If so, we will divide
470   // it up into "int32-sized" subsections and make repeated BLAS calls.
471   num_calls = length / INT_MAX;
472   if (length % INT_MAX > 0) num_calls += 1;
473 
474   // Compute norm
475   CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array));
476   switch (type) {
477     case CEED_NORM_1: {
478       *norm = 0.0;
479       if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) {
480         float  sub_norm = 0.0;
481         float *d_array_start;
482 
483         for (CeedInt i = 0; i < num_calls; i++) {
484           d_array_start             = (float *)d_array + (CeedSize)(i)*INT_MAX;
485           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
486           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
487 
488           CeedCallHipblas(ceed, hipblasSasum(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm));
489           *norm += sub_norm;
490         }
491       } else {
492         double  sub_norm = 0.0;
493         double *d_array_start;
494 
495         for (CeedInt i = 0; i < num_calls; i++) {
496           d_array_start             = (double *)d_array + (CeedSize)(i)*INT_MAX;
497           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
498           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
499 
500           CeedCallHipblas(ceed, hipblasDasum(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm));
501           *norm += sub_norm;
502         }
503       }
504       break;
505     }
506     case CEED_NORM_2: {
507       if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) {
508         float  sub_norm = 0.0, norm_sum = 0.0;
509         float *d_array_start;
510 
511         for (CeedInt i = 0; i < num_calls; i++) {
512           d_array_start             = (float *)d_array + (CeedSize)(i)*INT_MAX;
513           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
514           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
515 
516           CeedCallHipblas(ceed, hipblasSnrm2(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm));
517           norm_sum += sub_norm * sub_norm;
518         }
519         *norm = sqrt(norm_sum);
520       } else {
521         double  sub_norm = 0.0, norm_sum = 0.0;
522         double *d_array_start;
523 
524         for (CeedInt i = 0; i < num_calls; i++) {
525           d_array_start             = (double *)d_array + (CeedSize)(i)*INT_MAX;
526           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
527           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
528 
529           CeedCallHipblas(ceed, hipblasDnrm2(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm));
530           norm_sum += sub_norm * sub_norm;
531         }
532         *norm = sqrt(norm_sum);
533       }
534       break;
535     }
536     case CEED_NORM_MAX: {
537       CeedInt index;
538 
539       if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) {
540         float  sub_max = 0.0, current_max = 0.0;
541         float *d_array_start;
542         for (CeedInt i = 0; i < num_calls; i++) {
543           d_array_start             = (float *)d_array + (CeedSize)(i)*INT_MAX;
544           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
545           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
546 
547           CeedCallHipblas(ceed, hipblasIsamax(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &index));
548           CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost));
549           if (fabs(sub_max) > current_max) current_max = fabs(sub_max);
550         }
551         *norm = current_max;
552       } else {
553         double  sub_max = 0.0, current_max = 0.0;
554         double *d_array_start;
555 
556         for (CeedInt i = 0; i < num_calls; i++) {
557           d_array_start             = (double *)d_array + (CeedSize)(i)*INT_MAX;
558           CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
559           CeedInt  sub_length       = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
560 
561           CeedCallHipblas(ceed, hipblasIdamax(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &index));
562           CeedCallHip(ceed, hipMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), hipMemcpyDeviceToHost));
563           if (fabs(sub_max) > current_max) current_max = fabs(sub_max);
564         }
565         *norm = current_max;
566       }
567       break;
568     }
569   }
570   CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array));
571   return CEED_ERROR_SUCCESS;
572 }
573 
574 //------------------------------------------------------------------------------
575 // Take reciprocal of a vector on host
576 //------------------------------------------------------------------------------
577 static int CeedHostReciprocal_Hip(CeedScalar *h_array, CeedSize length) {
578   for (CeedSize i = 0; i < length; i++) {
579     if (fabs(h_array[i]) > CEED_EPSILON) h_array[i] = 1. / h_array[i];
580   }
581   return CEED_ERROR_SUCCESS;
582 }
583 
584 //------------------------------------------------------------------------------
585 // Take reciprocal of a vector on device (impl in .hip.cpp file)
586 //------------------------------------------------------------------------------
587 int CeedDeviceReciprocal_Hip(CeedScalar *d_array, CeedSize length);
588 
589 //------------------------------------------------------------------------------
590 // Take reciprocal of a vector
591 //------------------------------------------------------------------------------
592 static int CeedVectorReciprocal_Hip(CeedVector vec) {
593   CeedSize        length;
594   CeedVector_Hip *impl;
595 
596   CeedCallBackend(CeedVectorGetData(vec, &impl));
597   CeedCallBackend(CeedVectorGetLength(vec, &length));
598   // Set value for synced device/host array
599   if (impl->d_array) CeedCallBackend(CeedDeviceReciprocal_Hip(impl->d_array, length));
600   if (impl->h_array) CeedCallBackend(CeedHostReciprocal_Hip(impl->h_array, length));
601   return CEED_ERROR_SUCCESS;
602 }
603 
604 //------------------------------------------------------------------------------
605 // Compute x = alpha x on the host
606 //------------------------------------------------------------------------------
607 static int CeedHostScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length) {
608   for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha;
609   return CEED_ERROR_SUCCESS;
610 }
611 
612 //------------------------------------------------------------------------------
613 // Compute x = alpha x on device (impl in .hip.cpp file)
614 //------------------------------------------------------------------------------
615 int CeedDeviceScale_Hip(CeedScalar *x_array, CeedScalar alpha, CeedSize length);
616 
617 //------------------------------------------------------------------------------
618 // Compute x = alpha x
619 //------------------------------------------------------------------------------
620 static int CeedVectorScale_Hip(CeedVector x, CeedScalar alpha) {
621   CeedSize        length;
622   CeedVector_Hip *x_impl;
623 
624   CeedCallBackend(CeedVectorGetData(x, &x_impl));
625   CeedCallBackend(CeedVectorGetLength(x, &length));
626   // Set value for synced device/host array
627   if (x_impl->d_array) CeedCallBackend(CeedDeviceScale_Hip(x_impl->d_array, alpha, length));
628   if (x_impl->h_array) CeedCallBackend(CeedHostScale_Hip(x_impl->h_array, alpha, length));
629   return CEED_ERROR_SUCCESS;
630 }
631 
632 //------------------------------------------------------------------------------
633 // Compute y = alpha x + y on the host
634 //------------------------------------------------------------------------------
635 static int CeedHostAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length) {
636   for (CeedSize i = 0; i < length; i++) y_array[i] += alpha * x_array[i];
637   return CEED_ERROR_SUCCESS;
638 }
639 
640 //------------------------------------------------------------------------------
641 // Compute y = alpha x + y on device (impl in .hip.cpp file)
642 //------------------------------------------------------------------------------
643 int CeedDeviceAXPY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length);
644 
645 //------------------------------------------------------------------------------
646 // Compute y = alpha x + y
647 //------------------------------------------------------------------------------
648 static int CeedVectorAXPY_Hip(CeedVector y, CeedScalar alpha, CeedVector x) {
649   CeedSize        length;
650   CeedVector_Hip *y_impl, *x_impl;
651 
652   CeedCallBackend(CeedVectorGetData(y, &y_impl));
653   CeedCallBackend(CeedVectorGetData(x, &x_impl));
654   CeedCallBackend(CeedVectorGetLength(y, &length));
655   // Set value for synced device/host array
656   if (y_impl->d_array) {
657     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
658     CeedCallBackend(CeedDeviceAXPY_Hip(y_impl->d_array, alpha, x_impl->d_array, length));
659   }
660   if (y_impl->h_array) {
661     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
662     CeedCallBackend(CeedHostAXPY_Hip(y_impl->h_array, alpha, x_impl->h_array, length));
663   }
664   return CEED_ERROR_SUCCESS;
665 }
666 
667 //------------------------------------------------------------------------------
668 // Compute y = alpha x + beta y on the host
669 //------------------------------------------------------------------------------
670 static int CeedHostAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length) {
671   for (CeedSize i = 0; i < length; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i];
672   return CEED_ERROR_SUCCESS;
673 }
674 
675 //------------------------------------------------------------------------------
676 // Compute y = alpha x + beta y on device (impl in .hip.cpp file)
677 //------------------------------------------------------------------------------
678 int CeedDeviceAXPBY_Hip(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length);
679 
680 //------------------------------------------------------------------------------
681 // Compute y = alpha x + beta y
682 //------------------------------------------------------------------------------
683 static int CeedVectorAXPBY_Hip(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
684   CeedSize        length;
685   CeedVector_Hip *y_impl, *x_impl;
686 
687   CeedCallBackend(CeedVectorGetData(y, &y_impl));
688   CeedCallBackend(CeedVectorGetData(x, &x_impl));
689   CeedCallBackend(CeedVectorGetLength(y, &length));
690   // Set value for synced device/host array
691   if (y_impl->d_array) {
692     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
693     CeedCallBackend(CeedDeviceAXPBY_Hip(y_impl->d_array, alpha, beta, x_impl->d_array, length));
694   }
695   if (y_impl->h_array) {
696     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
697     CeedCallBackend(CeedHostAXPBY_Hip(y_impl->h_array, alpha, beta, x_impl->h_array, length));
698   }
699   return CEED_ERROR_SUCCESS;
700 }
701 
702 //------------------------------------------------------------------------------
703 // Compute the pointwise multiplication w = x .* y on the host
704 //------------------------------------------------------------------------------
705 static int CeedHostPointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length) {
706   for (CeedSize i = 0; i < length; i++) w_array[i] = x_array[i] * y_array[i];
707   return CEED_ERROR_SUCCESS;
708 }
709 
710 //------------------------------------------------------------------------------
711 // Compute the pointwise multiplication w = x .* y on device (impl in .hip.cpp file)
712 //------------------------------------------------------------------------------
713 int CeedDevicePointwiseMult_Hip(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length);
714 
715 //------------------------------------------------------------------------------
716 // Compute the pointwise multiplication w = x .* y
717 //------------------------------------------------------------------------------
718 static int CeedVectorPointwiseMult_Hip(CeedVector w, CeedVector x, CeedVector y) {
719   CeedSize        length;
720   CeedVector_Hip *w_impl, *x_impl, *y_impl;
721 
722   CeedCallBackend(CeedVectorGetData(w, &w_impl));
723   CeedCallBackend(CeedVectorGetData(x, &x_impl));
724   CeedCallBackend(CeedVectorGetData(y, &y_impl));
725   CeedCallBackend(CeedVectorGetLength(w, &length));
726 
727   // Set value for synced device/host array
728   if (!w_impl->d_array && !w_impl->h_array) {
729     CeedCallBackend(CeedVectorSetValue(w, 0.0));
730   }
731   if (w_impl->d_array) {
732     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
733     CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_DEVICE));
734     CeedCallBackend(CeedDevicePointwiseMult_Hip(w_impl->d_array, x_impl->d_array, y_impl->d_array, length));
735   }
736   if (w_impl->h_array) {
737     CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
738     CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_HOST));
739     CeedCallBackend(CeedHostPointwiseMult_Hip(w_impl->h_array, x_impl->h_array, y_impl->h_array, length));
740   }
741   return CEED_ERROR_SUCCESS;
742 }
743 
744 //------------------------------------------------------------------------------
745 // Destroy the vector
746 //------------------------------------------------------------------------------
747 static int CeedVectorDestroy_Hip(const CeedVector vec) {
748   CeedVector_Hip *impl;
749 
750   CeedCallBackend(CeedVectorGetData(vec, &impl));
751   CeedCallHip(CeedVectorReturnCeed(vec), hipFree(impl->d_array_owned));
752   CeedCallBackend(CeedFree(&impl->h_array_owned));
753   CeedCallBackend(CeedFree(&impl));
754   return CEED_ERROR_SUCCESS;
755 }
756 
757 //------------------------------------------------------------------------------
758 // Create a vector of the specified length (does not allocate memory)
759 //------------------------------------------------------------------------------
760 int CeedVectorCreate_Hip(CeedSize n, CeedVector vec) {
761   CeedVector_Hip *impl;
762   Ceed            ceed;
763 
764   CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
765   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Hip));
766   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Hip));
767   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Hip));
768   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Hip));
769   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "CopyStrided", (int (*)())CeedVectorCopyStrided_Hip));
770   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValue", (int (*)())CeedVectorSetValue_Hip));
771   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValueStrided", (int (*)())CeedVectorSetValueStrided_Hip));
772   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SyncArray", CeedVectorSyncArray_Hip));
773   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Hip));
774   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Hip));
775   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Hip));
776   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Norm", CeedVectorNorm_Hip));
777   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Reciprocal", CeedVectorReciprocal_Hip));
778   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Scale", (int (*)())CeedVectorScale_Hip));
779   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPY", (int (*)())CeedVectorAXPY_Hip));
780   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", (int (*)())CeedVectorAXPBY_Hip));
781   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Hip));
782   CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Hip));
783   CeedCallBackend(CeedCalloc(1, &impl));
784   CeedCallBackend(CeedVectorSetData(vec, impl));
785   return CEED_ERROR_SUCCESS;
786 }
787 
788 //------------------------------------------------------------------------------
789