-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathceed-cuda-ref-vector.c
More file actions
878 lines (774 loc) · 37.6 KB
/
ceed-cuda-ref-vector.c
File metadata and controls
878 lines (774 loc) · 37.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
// Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors.
// All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
//
// SPDX-License-Identifier: BSD-2-Clause
//
// This file is part of CEED: http://github.com/ceed
#include <ceed.h>
#include <ceed/backend.h>
#include <cuda_runtime.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
#include "../cuda/ceed-cuda-common.h"
#include "ceed-cuda-ref.h"
//------------------------------------------------------------------------------
// Check if host/device sync is needed
//------------------------------------------------------------------------------
static inline int CeedVectorNeedSync_Cuda(const CeedVector vec, CeedMemType mem_type, bool *need_sync) {
bool has_valid_array = false;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallBackend(CeedVectorHasValidArray(vec, &has_valid_array));
switch (mem_type) {
case CEED_MEM_HOST:
*need_sync = has_valid_array && !impl->h_array;
break;
case CEED_MEM_DEVICE:
*need_sync = has_valid_array && !impl->d_array;
break;
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Sync host to device
//------------------------------------------------------------------------------
static inline int CeedVectorSyncH2D_Cuda(const CeedVector vec) {
CeedSize length;
size_t bytes;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCheck(impl->h_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid host data to sync to device");
CeedCallBackend(CeedVectorGetLength(vec, &length));
bytes = length * sizeof(CeedScalar);
if (impl->d_array_borrowed) {
impl->d_array = impl->d_array_borrowed;
} else if (impl->d_array_owned) {
impl->d_array = impl->d_array_owned;
} else {
CeedCallCuda(CeedVectorReturnCeed(vec), cudaMalloc((void **)&impl->d_array_owned, bytes));
impl->d_array = impl->d_array_owned;
}
CeedCallCuda(CeedVectorReturnCeed(vec), cudaMemcpy(impl->d_array, impl->h_array, bytes, cudaMemcpyHostToDevice));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Sync device to host
//------------------------------------------------------------------------------
static inline int CeedVectorSyncD2H_Cuda(const CeedVector vec) {
CeedSize length;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCheck(impl->d_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid device data to sync to host");
if (impl->h_array_borrowed) {
impl->h_array = impl->h_array_borrowed;
} else if (impl->h_array_owned) {
impl->h_array = impl->h_array_owned;
} else {
CeedSize length;
CeedCallBackend(CeedVectorGetLength(vec, &length));
CeedCallBackend(CeedCalloc(length, &impl->h_array_owned));
impl->h_array = impl->h_array_owned;
}
CeedCallBackend(CeedVectorGetLength(vec, &length));
size_t bytes = length * sizeof(CeedScalar);
CeedCallCuda(CeedVectorReturnCeed(vec), cudaMemcpy(impl->h_array, impl->d_array, bytes, cudaMemcpyDeviceToHost));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Sync arrays
//------------------------------------------------------------------------------
static int CeedVectorSyncArray_Cuda(const CeedVector vec, CeedMemType mem_type) {
bool need_sync = false;
// Check whether device/host sync is needed
CeedCallBackend(CeedVectorNeedSync_Cuda(vec, mem_type, &need_sync));
if (!need_sync) return CEED_ERROR_SUCCESS;
switch (mem_type) {
case CEED_MEM_HOST:
return CeedVectorSyncD2H_Cuda(vec);
case CEED_MEM_DEVICE:
return CeedVectorSyncH2D_Cuda(vec);
}
return CEED_ERROR_UNSUPPORTED;
}
//------------------------------------------------------------------------------
// Set all pointers as invalid
//------------------------------------------------------------------------------
static inline int CeedVectorSetAllInvalid_Cuda(const CeedVector vec) {
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
impl->h_array = NULL;
impl->d_array = NULL;
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Check if CeedVector has any valid pointer
//------------------------------------------------------------------------------
static inline int CeedVectorHasValidArray_Cuda(const CeedVector vec, bool *has_valid_array) {
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
*has_valid_array = impl->h_array || impl->d_array;
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Check if has array of given type
//------------------------------------------------------------------------------
static inline int CeedVectorHasArrayOfType_Cuda(const CeedVector vec, CeedMemType mem_type, bool *has_array_of_type) {
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
switch (mem_type) {
case CEED_MEM_HOST:
*has_array_of_type = impl->h_array_borrowed || impl->h_array_owned;
break;
case CEED_MEM_DEVICE:
*has_array_of_type = impl->d_array_borrowed || impl->d_array_owned;
break;
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Check if has borrowed array of given type
//------------------------------------------------------------------------------
static inline int CeedVectorHasBorrowedArrayOfType_Cuda(const CeedVector vec, CeedMemType mem_type, bool *has_borrowed_array_of_type) {
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
switch (mem_type) {
case CEED_MEM_HOST:
*has_borrowed_array_of_type = impl->h_array_borrowed;
break;
case CEED_MEM_DEVICE:
*has_borrowed_array_of_type = impl->d_array_borrowed;
break;
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Set array from host
//------------------------------------------------------------------------------
static int CeedVectorSetArrayHost_Cuda(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) {
CeedSize length;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallBackend(CeedVectorGetLength(vec, &length));
CeedCallBackend(CeedSetHostCeedScalarArray(array, copy_mode, length, (const CeedScalar **)&impl->h_array_owned,
(const CeedScalar **)&impl->h_array_borrowed, (const CeedScalar **)&impl->h_array));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Set array from device
//------------------------------------------------------------------------------
static int CeedVectorSetArrayDevice_Cuda(const CeedVector vec, const CeedCopyMode copy_mode, CeedScalar *array) {
CeedSize length;
Ceed ceed;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallBackend(CeedVectorGetLength(vec, &length));
CeedCallBackend(CeedSetDeviceCeedScalarArray_Cuda(ceed, array, copy_mode, length, (const CeedScalar **)&impl->d_array_owned,
(const CeedScalar **)&impl->d_array_borrowed, (const CeedScalar **)&impl->d_array));
CeedCallBackend(CeedDestroy(&ceed));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Set the array used by a vector,
// freeing any previously allocated array if applicable
//------------------------------------------------------------------------------
static int CeedVectorSetArray_Cuda(const CeedVector vec, const CeedMemType mem_type, const CeedCopyMode copy_mode, CeedScalar *array) {
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallBackend(CeedVectorSetAllInvalid_Cuda(vec));
switch (mem_type) {
case CEED_MEM_HOST:
return CeedVectorSetArrayHost_Cuda(vec, copy_mode, array);
case CEED_MEM_DEVICE:
return CeedVectorSetArrayDevice_Cuda(vec, copy_mode, array);
}
return CEED_ERROR_UNSUPPORTED;
}
//------------------------------------------------------------------------------
// Copy host array to value strided
//------------------------------------------------------------------------------
static int CeedHostCopyStrided_Cuda(CeedScalar *h_array, CeedSize start, CeedSize stop, CeedSize step, CeedScalar *h_copy_array) {
for (CeedSize i = start; i < stop; i += step) h_copy_array[i] = h_array[i];
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Copy device array to value strided (impl in .cu file)
//------------------------------------------------------------------------------
int CeedDeviceCopyStrided_Cuda(CeedScalar *d_array, CeedSize start, CeedSize stop, CeedSize step, CeedScalar *d_copy_array);
//------------------------------------------------------------------------------
// Copy a vector to a value strided
//------------------------------------------------------------------------------
static int CeedVectorCopyStrided_Cuda(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedVector vec_copy) {
CeedSize length;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
{
CeedSize length_vec, length_copy;
CeedCallBackend(CeedVectorGetLength(vec, &length_vec));
CeedCallBackend(CeedVectorGetLength(vec_copy, &length_copy));
length = length_vec < length_copy ? length_vec : length_copy;
}
if (stop == -1) stop = length;
// Set value for synced device/host array
if (impl->d_array) {
CeedScalar *copy_array;
CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_DEVICE, ©_array));
#if (CUDA_VERSION >= 12000)
cublasHandle_t handle;
Ceed ceed;
CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
CeedCallBackend(CeedGetCublasHandle_Cuda(ceed, &handle));
#if defined(CEED_SCALAR_IS_FP32)
CeedCallCublas(ceed, cublasScopy_64(handle, (int64_t)(stop - start), impl->d_array + start, (int64_t)step, copy_array + start, (int64_t)step));
#else /* CEED_SCALAR */
CeedCallCublas(ceed, cublasDcopy_64(handle, (int64_t)(stop - start), impl->d_array + start, (int64_t)step, copy_array + start, (int64_t)step));
#endif /* CEED_SCALAR */
CeedCallBackend(CeedDestroy(&ceed));
#else /* CUDA_VERSION */
CeedCallBackend(CeedDeviceCopyStrided_Cuda(impl->d_array, start, stop, step, copy_array));
#endif /* CUDA_VERSION */
CeedCallBackend(CeedVectorRestoreArray(vec_copy, ©_array));
impl->h_array = NULL;
} else if (impl->h_array) {
CeedScalar *copy_array;
CeedCallBackend(CeedVectorGetArray(vec_copy, CEED_MEM_HOST, ©_array));
CeedCallBackend(CeedHostCopyStrided_Cuda(impl->h_array, start, stop, step, copy_array));
CeedCallBackend(CeedVectorRestoreArray(vec_copy, ©_array));
impl->d_array = NULL;
} else {
return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set");
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Set host array to value
//------------------------------------------------------------------------------
static int CeedHostSetValue_Cuda(CeedScalar *h_array, CeedSize length, CeedScalar val) {
for (CeedSize i = 0; i < length; i++) h_array[i] = val;
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Set device array to value (impl in .cu file)
//------------------------------------------------------------------------------
int CeedDeviceSetValue_Cuda(CeedScalar *d_array, CeedSize length, CeedScalar val);
//------------------------------------------------------------------------------
// Set a vector to a value
//------------------------------------------------------------------------------
static int CeedVectorSetValue_Cuda(CeedVector vec, CeedScalar val) {
CeedSize length;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallBackend(CeedVectorGetLength(vec, &length));
// Set value for synced device/host array
if (!impl->d_array && !impl->h_array) {
if (impl->d_array_borrowed) {
impl->d_array = impl->d_array_borrowed;
} else if (impl->h_array_borrowed) {
impl->h_array = impl->h_array_borrowed;
} else if (impl->d_array_owned) {
impl->d_array = impl->d_array_owned;
} else if (impl->h_array_owned) {
impl->h_array = impl->h_array_owned;
} else {
CeedCallBackend(CeedVectorSetArray(vec, CEED_MEM_DEVICE, CEED_COPY_VALUES, NULL));
}
}
if (impl->d_array) {
if (val == 0) {
CeedCallCuda(CeedVectorReturnCeed(vec), cudaMemset(impl->d_array, 0, length * sizeof(CeedScalar)));
} else {
CeedCallBackend(CeedDeviceSetValue_Cuda(impl->d_array, length, val));
}
impl->h_array = NULL;
} else if (impl->h_array) {
CeedCallBackend(CeedHostSetValue_Cuda(impl->h_array, length, val));
impl->d_array = NULL;
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Set host array to value strided
//------------------------------------------------------------------------------
static int CeedHostSetValueStrided_Cuda(CeedScalar *h_array, CeedSize start, CeedSize stop, CeedSize step, CeedScalar val) {
for (CeedSize i = start; i < stop; i += step) h_array[i] = val;
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Set device array to value strided (impl in .cu file)
//------------------------------------------------------------------------------
int CeedDeviceSetValueStrided_Cuda(CeedScalar *d_array, CeedSize start, CeedSize stop, CeedSize step, CeedScalar val);
//------------------------------------------------------------------------------
// Set a vector to a value strided
//------------------------------------------------------------------------------
static int CeedVectorSetValueStrided_Cuda(CeedVector vec, CeedSize start, CeedSize stop, CeedSize step, CeedScalar val) {
CeedSize length;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallBackend(CeedVectorGetLength(vec, &length));
// Set value for synced device/host array
if (stop == -1) stop = length;
if (impl->d_array) {
CeedCallBackend(CeedDeviceSetValueStrided_Cuda(impl->d_array, start, stop, step, val));
impl->h_array = NULL;
} else if (impl->h_array) {
CeedCallBackend(CeedHostSetValueStrided_Cuda(impl->h_array, start, stop, step, val));
impl->d_array = NULL;
} else {
return CeedError(CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector must have valid data set");
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Vector Take Array
//------------------------------------------------------------------------------
static int CeedVectorTakeArray_Cuda(CeedVector vec, CeedMemType mem_type, CeedScalar **array) {
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
// Sync array to requested mem_type
CeedCallBackend(CeedVectorSyncArray(vec, mem_type));
// Update pointer
switch (mem_type) {
case CEED_MEM_HOST:
(*array) = impl->h_array_borrowed;
impl->h_array_borrowed = NULL;
impl->h_array = NULL;
break;
case CEED_MEM_DEVICE:
(*array) = impl->d_array_borrowed;
impl->d_array_borrowed = NULL;
impl->d_array = NULL;
break;
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Core logic for array syncronization for GetArray.
// If a different memory type is most up to date, this will perform a copy
//------------------------------------------------------------------------------
static int CeedVectorGetArrayCore_Cuda(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) {
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
// Sync array to requested mem_type
CeedCallBackend(CeedVectorSyncArray(vec, mem_type));
// Update pointer
switch (mem_type) {
case CEED_MEM_HOST:
*array = impl->h_array;
break;
case CEED_MEM_DEVICE:
*array = impl->d_array;
break;
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Get read-only access to a vector via the specified mem_type
//------------------------------------------------------------------------------
static int CeedVectorGetArrayRead_Cuda(const CeedVector vec, const CeedMemType mem_type, const CeedScalar **array) {
return CeedVectorGetArrayCore_Cuda(vec, mem_type, (CeedScalar **)array);
}
//------------------------------------------------------------------------------
// Get read/write access to a vector via the specified mem_type
//------------------------------------------------------------------------------
static int CeedVectorGetArray_Cuda(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) {
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallBackend(CeedVectorGetArrayCore_Cuda(vec, mem_type, array));
CeedCallBackend(CeedVectorSetAllInvalid_Cuda(vec));
switch (mem_type) {
case CEED_MEM_HOST:
impl->h_array = *array;
break;
case CEED_MEM_DEVICE:
impl->d_array = *array;
break;
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Get write access to a vector via the specified mem_type
//------------------------------------------------------------------------------
static int CeedVectorGetArrayWrite_Cuda(const CeedVector vec, const CeedMemType mem_type, CeedScalar **array) {
bool has_array_of_type = true;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallBackend(CeedVectorHasArrayOfType_Cuda(vec, mem_type, &has_array_of_type));
if (!has_array_of_type) {
// Allocate if array is not yet allocated
CeedCallBackend(CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL));
} else {
// Select dirty array
switch (mem_type) {
case CEED_MEM_HOST:
if (impl->h_array_borrowed) impl->h_array = impl->h_array_borrowed;
else impl->h_array = impl->h_array_owned;
break;
case CEED_MEM_DEVICE:
if (impl->d_array_borrowed) impl->d_array = impl->d_array_borrowed;
else impl->d_array = impl->d_array_owned;
}
}
return CeedVectorGetArray_Cuda(vec, mem_type, array);
}
//------------------------------------------------------------------------------
// Get the norm of a CeedVector
//------------------------------------------------------------------------------
static int CeedVectorNorm_Cuda(CeedVector vec, CeedNormType type, CeedScalar *norm) {
Ceed ceed;
CeedSize length;
#if (CUDA_VERSION < 12000)
CeedSize num_calls;
#endif /* CUDA_VERSION */
const CeedScalar *d_array;
CeedVector_Cuda *impl;
cublasHandle_t handle;
CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallBackend(CeedVectorGetLength(vec, &length));
CeedCallBackend(CeedGetCublasHandle_Cuda(ceed, &handle));
#if (CUDA_VERSION < 12000)
// With CUDA 12, we can use the 64-bit integer interface. Prior to that,
// we need to check if the vector is too long to handle with int32,
// and if so, divide it into subsections for repeated cuBLAS calls.
num_calls = length / INT_MAX;
if (length % INT_MAX > 0) num_calls += 1;
#endif /* CUDA_VERSION */
// Compute norm
CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array));
switch (type) {
case CEED_NORM_1: {
*norm = 0.0;
#if defined(CEED_SCALAR_IS_FP32)
#if (CUDA_VERSION >= 12000) // We have CUDA 12, and can use 64-bit integers
CeedCallCublas(ceed, cublasSasum_64(handle, (int64_t)length, (float *)d_array, 1, (float *)norm));
#else /* CUDA_VERSION */
float sub_norm = 0.0;
float *d_array_start;
for (CeedInt i = 0; i < num_calls; i++) {
d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX;
CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
CeedCallCublas(ceed, cublasSasum(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm));
*norm += sub_norm;
}
#endif /* CUDA_VERSION */
#else /* CEED_SCALAR */
#if (CUDA_VERSION >= 12000)
CeedCallCublas(ceed, cublasDasum_64(handle, (int64_t)length, (double *)d_array, 1, (double *)norm));
#else /* CUDA_VERSION */
double sub_norm = 0.0;
double *d_array_start;
for (CeedInt i = 0; i < num_calls; i++) {
d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX;
CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
CeedCallCublas(ceed, cublasDasum(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm));
*norm += sub_norm;
}
#endif /* CUDA_VERSION */
#endif /* CEED_SCALAR */
break;
}
case CEED_NORM_2: {
#if defined(CEED_SCALAR_IS_FP32)
#if (CUDA_VERSION >= 12000)
CeedCallCublas(ceed, cublasSnrm2_64(handle, (int64_t)length, (float *)d_array, 1, (float *)norm));
#else /* CUDA_VERSION */
float sub_norm = 0.0, norm_sum = 0.0;
float *d_array_start;
for (CeedInt i = 0; i < num_calls; i++) {
d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX;
CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
CeedCallCublas(ceed, cublasSnrm2(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &sub_norm));
norm_sum += sub_norm * sub_norm;
}
*norm = sqrt(norm_sum);
#endif /* CUDA_VERSION */
#else /* CEED_SCALAR */
#if (CUDA_VERSION >= 12000)
CeedCallCublas(ceed, cublasDnrm2_64(handle, (int64_t)length, (double *)d_array, 1, (double *)norm));
#else /* CUDA_VERSION */
double sub_norm = 0.0, norm_sum = 0.0;
double *d_array_start;
for (CeedInt i = 0; i < num_calls; i++) {
d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX;
CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
CeedCallCublas(ceed, cublasDnrm2(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &sub_norm));
norm_sum += sub_norm * sub_norm;
}
*norm = sqrt(norm_sum);
#endif /* CUDA_VERSION */
#endif /* CEED_SCALAR */
break;
}
case CEED_NORM_MAX: {
#if defined(CEED_SCALAR_IS_FP32)
#if (CUDA_VERSION >= 12000)
int64_t index;
CeedScalar norm_no_abs;
CeedCallCublas(ceed, cublasIsamax_64(handle, (int64_t)length, (float *)d_array, 1, &index));
CeedCallCuda(ceed, cudaMemcpy(&norm_no_abs, impl->d_array + index - 1, sizeof(CeedScalar), cudaMemcpyDeviceToHost));
*norm = fabs(norm_no_abs);
#else /* CUDA_VERSION */
CeedInt index;
float sub_max = 0.0, current_max = 0.0;
float *d_array_start;
for (CeedInt i = 0; i < num_calls; i++) {
d_array_start = (float *)d_array + (CeedSize)(i)*INT_MAX;
CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
CeedCallCublas(ceed, cublasIsamax(handle, (CeedInt)sub_length, (float *)d_array_start, 1, &index));
CeedCallCuda(ceed, cudaMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), cudaMemcpyDeviceToHost));
if (fabs(sub_max) > current_max) current_max = fabs(sub_max);
}
*norm = current_max;
#endif /* CUDA_VERSION */
#else /* CEED_SCALAR */
#if (CUDA_VERSION >= 12000)
int64_t index;
CeedScalar norm_no_abs;
CeedCallCublas(ceed, cublasIdamax_64(handle, (int64_t)length, (double *)d_array, 1, &index));
CeedCallCuda(ceed, cudaMemcpy(&norm_no_abs, impl->d_array + index - 1, sizeof(CeedScalar), cudaMemcpyDeviceToHost));
*norm = fabs(norm_no_abs);
#else /* CUDA_VERSION */
CeedInt index;
double sub_max = 0.0, current_max = 0.0;
double *d_array_start;
for (CeedInt i = 0; i < num_calls; i++) {
d_array_start = (double *)d_array + (CeedSize)(i)*INT_MAX;
CeedSize remaining_length = length - (CeedSize)(i)*INT_MAX;
CeedInt sub_length = (i == num_calls - 1) ? (CeedInt)(remaining_length) : INT_MAX;
CeedCallCublas(ceed, cublasIdamax(handle, (CeedInt)sub_length, (double *)d_array_start, 1, &index));
CeedCallCuda(ceed, cudaMemcpy(&sub_max, d_array_start + index - 1, sizeof(CeedScalar), cudaMemcpyDeviceToHost));
if (fabs(sub_max) > current_max) current_max = fabs(sub_max);
}
*norm = current_max;
#endif /* CUDA_VERSION */
#endif /* CEED_SCALAR */
break;
}
}
CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array));
CeedCallBackend(CeedDestroy(&ceed));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Take reciprocal of a vector on host
//------------------------------------------------------------------------------
static int CeedHostReciprocal_Cuda(CeedScalar *h_array, CeedSize length) {
for (CeedSize i = 0; i < length; i++) {
if (fabs(h_array[i]) > CEED_EPSILON) h_array[i] = 1. / h_array[i];
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Take reciprocal of a vector on device (impl in .cu file)
//------------------------------------------------------------------------------
int CeedDeviceReciprocal_Cuda(CeedScalar *d_array, CeedSize length);
//------------------------------------------------------------------------------
// Take reciprocal of a vector
//------------------------------------------------------------------------------
static int CeedVectorReciprocal_Cuda(CeedVector vec) {
CeedSize length;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallBackend(CeedVectorGetLength(vec, &length));
// Set value for synced device/host array
if (impl->d_array) CeedCallBackend(CeedDeviceReciprocal_Cuda(impl->d_array, length));
if (impl->h_array) CeedCallBackend(CeedHostReciprocal_Cuda(impl->h_array, length));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Compute x = alpha x on the host
//------------------------------------------------------------------------------
static int CeedHostScale_Cuda(CeedScalar *x_array, CeedScalar alpha, CeedSize length) {
for (CeedSize i = 0; i < length; i++) x_array[i] *= alpha;
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Compute x = alpha x on device (impl in .cu file)
//------------------------------------------------------------------------------
int CeedDeviceScale_Cuda(CeedScalar *x_array, CeedScalar alpha, CeedSize length);
//------------------------------------------------------------------------------
// Compute x = alpha x
//------------------------------------------------------------------------------
static int CeedVectorScale_Cuda(CeedVector x, CeedScalar alpha) {
CeedSize length;
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(x, &impl));
CeedCallBackend(CeedVectorGetLength(x, &length));
// Set value for synced device/host array
if (impl->d_array) {
#if (CUDA_VERSION >= 12000)
cublasHandle_t handle;
CeedCallBackend(CeedGetCublasHandle_Cuda(CeedVectorReturnCeed(x), &handle));
#if defined(CEED_SCALAR_IS_FP32)
CeedCallCublas(CeedVectorReturnCeed(x), cublasSscal_64(handle, (int64_t)length, &alpha, impl->d_array, 1));
#else /* CEED_SCALAR */
CeedCallCublas(CeedVectorReturnCeed(x), cublasDscal_64(handle, (int64_t)length, &alpha, impl->d_array, 1));
#endif /* CEED_SCALAR */
#else /* CUDA_VERSION */
CeedCallBackend(CeedDeviceScale_Cuda(impl->d_array, alpha, length));
#endif /* CUDA_VERSION */
impl->h_array = NULL;
} else if (impl->h_array) {
CeedCallBackend(CeedHostScale_Cuda(impl->h_array, alpha, length));
impl->d_array = NULL;
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Compute y = alpha x + y on the host
//------------------------------------------------------------------------------
static int CeedHostAXPY_Cuda(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length) {
for (CeedSize i = 0; i < length; i++) y_array[i] += alpha * x_array[i];
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Compute y = alpha x + y on device (impl in .cu file)
//------------------------------------------------------------------------------
int CeedDeviceAXPY_Cuda(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_array, CeedSize length);
//------------------------------------------------------------------------------
// Compute y = alpha x + y
//------------------------------------------------------------------------------
static int CeedVectorAXPY_Cuda(CeedVector y, CeedScalar alpha, CeedVector x) {
CeedSize length;
CeedVector_Cuda *y_impl, *x_impl;
CeedCallBackend(CeedVectorGetData(y, &y_impl));
CeedCallBackend(CeedVectorGetData(x, &x_impl));
CeedCallBackend(CeedVectorGetLength(y, &length));
// Set value for synced device/host array
if (y_impl->d_array) {
CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
#if (CUDA_VERSION >= 12000)
cublasHandle_t handle;
CeedCallBackend(CeedGetCublasHandle_Cuda(CeedVectorReturnCeed(y), &handle));
#if defined(CEED_SCALAR_IS_FP32)
CeedCallCublas(CeedVectorReturnCeed(y), cublasSaxpy_64(handle, (int64_t)length, &alpha, x_impl->d_array, 1, y_impl->d_array, 1));
#else /* CEED_SCALAR */
CeedCallCublas(CeedVectorReturnCeed(y), cublasDaxpy_64(handle, (int64_t)length, &alpha, x_impl->d_array, 1, y_impl->d_array, 1));
#endif /* CEED_SCALAR */
#else /* CUDA_VERSION */
CeedCallBackend(CeedDeviceAXPY_Cuda(y_impl->d_array, alpha, x_impl->d_array, length));
#endif /* CUDA_VERSION */
y_impl->h_array = NULL;
} else if (y_impl->h_array) {
CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
CeedCallBackend(CeedHostAXPY_Cuda(y_impl->h_array, alpha, x_impl->h_array, length));
y_impl->d_array = NULL;
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Compute y = alpha x + beta y on the host
//------------------------------------------------------------------------------
static int CeedHostAXPBY_Cuda(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length) {
for (CeedSize i = 0; i < length; i++) y_array[i] = alpha * x_array[i] + beta * y_array[i];
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Compute y = alpha x + beta y on device (impl in .cu file)
//------------------------------------------------------------------------------
int CeedDeviceAXPBY_Cuda(CeedScalar *y_array, CeedScalar alpha, CeedScalar beta, CeedScalar *x_array, CeedSize length);
//------------------------------------------------------------------------------
// Compute y = alpha x + beta y
//------------------------------------------------------------------------------
static int CeedVectorAXPBY_Cuda(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector x) {
CeedSize length;
CeedVector_Cuda *y_impl, *x_impl;
CeedCallBackend(CeedVectorGetData(y, &y_impl));
CeedCallBackend(CeedVectorGetData(x, &x_impl));
CeedCallBackend(CeedVectorGetLength(y, &length));
// Set value for synced device/host array
if (y_impl->d_array) {
CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
CeedCallBackend(CeedDeviceAXPBY_Cuda(y_impl->d_array, alpha, beta, x_impl->d_array, length));
}
if (y_impl->h_array) {
CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
CeedCallBackend(CeedHostAXPBY_Cuda(y_impl->h_array, alpha, beta, x_impl->h_array, length));
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Compute the pointwise multiplication w = x .* y on the host
//------------------------------------------------------------------------------
static int CeedHostPointwiseMult_Cuda(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length) {
for (CeedSize i = 0; i < length; i++) w_array[i] = x_array[i] * y_array[i];
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Compute the pointwise multiplication w = x .* y on device (impl in .cu file)
//------------------------------------------------------------------------------
int CeedDevicePointwiseMult_Cuda(CeedScalar *w_array, CeedScalar *x_array, CeedScalar *y_array, CeedSize length);
//------------------------------------------------------------------------------
// Compute the pointwise multiplication w = x .* y
//------------------------------------------------------------------------------
static int CeedVectorPointwiseMult_Cuda(CeedVector w, CeedVector x, CeedVector y) {
CeedSize length;
CeedVector_Cuda *w_impl, *x_impl, *y_impl;
CeedCallBackend(CeedVectorGetData(w, &w_impl));
CeedCallBackend(CeedVectorGetData(x, &x_impl));
CeedCallBackend(CeedVectorGetData(y, &y_impl));
CeedCallBackend(CeedVectorGetLength(w, &length));
// Set value for synced device/host array
if (!w_impl->d_array && !w_impl->h_array) {
CeedCallBackend(CeedVectorSetValue(w, 0.0));
}
if (w_impl->d_array) {
CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_DEVICE));
CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_DEVICE));
CeedCallBackend(CeedDevicePointwiseMult_Cuda(w_impl->d_array, x_impl->d_array, y_impl->d_array, length));
}
if (w_impl->h_array) {
CeedCallBackend(CeedVectorSyncArray(x, CEED_MEM_HOST));
CeedCallBackend(CeedVectorSyncArray(y, CEED_MEM_HOST));
CeedCallBackend(CeedHostPointwiseMult_Cuda(w_impl->h_array, x_impl->h_array, y_impl->h_array, length));
}
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Destroy the vector
//------------------------------------------------------------------------------
static int CeedVectorDestroy_Cuda(const CeedVector vec) {
CeedVector_Cuda *impl;
CeedCallBackend(CeedVectorGetData(vec, &impl));
CeedCallCuda(CeedVectorReturnCeed(vec), cudaFree(impl->d_array_owned));
CeedCallBackend(CeedFree(&impl->h_array_owned));
CeedCallBackend(CeedFree(&impl));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------
// Create a vector of the specified length (does not allocate memory)
//------------------------------------------------------------------------------
int CeedVectorCreate_Cuda(CeedSize n, CeedVector vec) {
CeedVector_Cuda *impl;
Ceed ceed;
CeedCallBackend(CeedVectorGetCeed(vec, &ceed));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetArray", CeedVectorSetArray_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray", CeedVectorTakeArray_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "CopyStrided", CeedVectorCopyStrided_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValue", CeedVectorSetValue_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SetValueStrided", CeedVectorSetValueStrided_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "SyncArray", CeedVectorSyncArray_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArray", CeedVectorGetArray_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead", CeedVectorGetArrayRead_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite", CeedVectorGetArrayWrite_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Norm", CeedVectorNorm_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Reciprocal", CeedVectorReciprocal_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Scale", CeedVectorScale_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPY", CeedVectorAXPY_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", CeedVectorAXPBY_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Cuda));
CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Cuda));
CeedCallBackend(CeedDestroy(&ceed));
CeedCallBackend(CeedCalloc(1, &impl));
CeedCallBackend(CeedVectorSetData(vec, impl));
return CEED_ERROR_SUCCESS;
}
//------------------------------------------------------------------------------