-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathcusolver_batch.cpp
2006 lines (1697 loc) · 104 KB
/
cusolver_batch.cpp
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (C) Codeplay Software Limited
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* For your convenience, a copy of the License has been included in this
* repository.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/
#include "cublas_helper.hpp"
#include "cusolver_helper.hpp"
#include "cusolver_task.hpp"
#include "oneapi/math/exceptions.hpp"
#include "oneapi/math/lapack/detail/cusolver/onemath_lapack_cusolver.hpp"
namespace oneapi {
namespace math {
namespace lapack {
namespace cusolver {
// BATCH BUFFER API
template <typename Func, typename T>
inline void geqrf_batch(const char* func_name, Func func, sycl::queue& queue, std::int64_t m,
std::int64_t n, sycl::buffer<T>& a, std::int64_t lda, std::int64_t stride_a,
sycl::buffer<T>& tau, std::int64_t stride_tau, std::int64_t batch_size,
sycl::buffer<T>& scratchpad, std::int64_t scratchpad_size) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(m, n, lda, stride_a, stride_tau, batch_size, scratchpad_size);
queue.submit([&](sycl::handler& cgh) {
auto a_acc = a.template get_access<sycl::access::mode::read_write>(cgh);
auto tau_acc = tau.template get_access<sycl::access::mode::write>(cgh);
auto scratch_acc = scratchpad.template get_access<sycl::access::mode::read_write>(cgh);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
auto a_ = sc.get_mem<cuDataType*>(a_acc);
auto tau_ = sc.get_mem<cuDataType*>(tau_acc);
auto scratch_ = sc.get_mem<cuDataType*>(scratch_acc);
cusolverStatus_t err;
// Uses scratch so sync between each cuSolver call
for (int64_t i = 0; i < batch_size; ++i) {
cusolver_native_named_func(func_name, func, err, handle, m, n, a_ + stride_a * i,
lda, tau_ + stride_tau * i, scratch_, scratchpad_size,
nullptr);
}
});
});
}
#define GEQRF_STRIDED_BATCH_LAUNCHER(TYPE, CUSOLVER_ROUTINE) \
void geqrf_batch(sycl::queue& queue, std::int64_t m, std::int64_t n, sycl::buffer<TYPE>& a, \
std::int64_t lda, std::int64_t stride_a, sycl::buffer<TYPE>& tau, \
std::int64_t stride_tau, std::int64_t batch_size, \
sycl::buffer<TYPE>& scratchpad, std::int64_t scratchpad_size) { \
return geqrf_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, m, n, a, lda, stride_a, \
tau, stride_tau, batch_size, scratchpad, scratchpad_size); \
}
GEQRF_STRIDED_BATCH_LAUNCHER(float, cusolverDnSgeqrf)
GEQRF_STRIDED_BATCH_LAUNCHER(double, cusolverDnDgeqrf)
GEQRF_STRIDED_BATCH_LAUNCHER(std::complex<float>, cusolverDnCgeqrf)
GEQRF_STRIDED_BATCH_LAUNCHER(std::complex<double>, cusolverDnZgeqrf)
#undef GEQRF_STRIDED_BATCH_LAUNCHER
template <typename Func, typename T>
inline void getri_batch(const char* func_name, Func func, sycl::queue& queue, std::int64_t n,
sycl::buffer<T>& a, std::int64_t lda, std::int64_t stride_a,
sycl::buffer<std::int64_t>& ipiv, std::int64_t stride_ipiv,
std::int64_t batch_size, sycl::buffer<T>& scratchpad,
std::int64_t scratchpad_size) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(n, lda, stride_a, stride_ipiv, batch_size, scratchpad_size);
std::uint64_t ipiv32_size = n * batch_size;
sycl::buffer<int> ipiv32(sycl::range<1>{ ipiv32_size });
sycl::buffer<int> devInfo{ batch_size };
queue.submit([&](sycl::handler& cgh) {
auto ipiv_acc = sycl::accessor{ ipiv, cgh, sycl::read_only };
auto ipiv32_acc = sycl::accessor{ ipiv32, cgh, sycl::write_only };
cgh.parallel_for(sycl::range<1>{ ipiv32_size }, [=](sycl::id<1> index) {
ipiv32_acc[index] = static_cast<int>(ipiv_acc[(index / n) * stride_ipiv + index % n]);
});
});
// getri_batched is contained within cublas, not cusolver. For this reason
// we need to use cublas types instead of cusolver types (as is needed for
// other lapack routines)
queue.submit([&](sycl::handler& cgh) {
using blas::cublas::cublas_error;
sycl::accessor a_acc{ a, cgh, sycl::read_only };
sycl::accessor scratch_acc{ scratchpad, cgh, sycl::write_only };
sycl::accessor ipiv32_acc{ ipiv32, cgh };
sycl::accessor devInfo_acc{ devInfo, cgh, sycl::write_only };
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
cublasStatus_t err;
CUresult cuda_result;
cublasHandle_t cublas_handle;
CUBLAS_ERROR_FUNC(cublasCreate, err, &cublas_handle);
CUstream cu_stream = sycl::get_native<sycl::backend::ext_oneapi_cuda>(queue);
CUBLAS_ERROR_FUNC(cublasSetStream, err, cublas_handle, cu_stream);
auto a_ = sc.get_mem<cuDataType*>(a_acc);
auto scratch_ = sc.get_mem<cuDataType*>(scratch_acc);
auto ipiv32_ = sc.get_mem<int*>(ipiv32_acc);
auto info_ = sc.get_mem<int*>(devInfo_acc);
CUdeviceptr a_dev;
cuDataType** a_batched = create_ptr_list_from_stride(a_, stride_a, batch_size);
CUDA_ERROR_FUNC(cuMemAlloc, cuda_result, &a_dev, sizeof(T*) * batch_size);
CUDA_ERROR_FUNC(cuMemcpyHtoD, cuda_result, a_dev, a_batched, sizeof(T*) * batch_size);
auto** a_dev_ = reinterpret_cast<cuDataType**>(a_dev);
CUdeviceptr scratch_dev;
cuDataType** scratch_batched =
create_ptr_list_from_stride(scratch_, stride_a, batch_size);
CUDA_ERROR_FUNC(cuMemAlloc, cuda_result, &scratch_dev, sizeof(T*) * batch_size);
CUDA_ERROR_FUNC(cuMemcpyHtoD, cuda_result, scratch_dev, scratch_batched,
sizeof(T*) * batch_size);
auto** scratch_dev_ = reinterpret_cast<cuDataType**>(scratch_dev);
blas::cublas::cublas_native_named_func(func_name, func, err, cublas_handle, n, a_dev_,
lda, ipiv32_, scratch_dev_, lda, info_,
batch_size);
free(a_batched);
free(scratch_batched);
cuMemFree(a_dev);
cuMemFree(scratch_dev);
});
});
// The inverted matrices stored in scratch_ need to be stored in a_
queue.submit([&](sycl::handler& cgh) {
sycl::accessor a_acc{ a, cgh, sycl::write_only };
sycl::accessor scratch_acc{ scratchpad, cgh, sycl::read_only };
cgh.parallel_for(sycl::range<1>{ static_cast<size_t>(
sycl::max(stride_a * batch_size, lda * n * batch_size)) },
[=](sycl::id<1> index) { a_acc[index] = scratch_acc[index]; });
});
queue.submit([&](sycl::handler& cgh) {
sycl::accessor ipiv32_acc{ ipiv32, cgh, sycl::read_only };
sycl::accessor ipiv_acc{ ipiv, cgh, sycl::write_only };
cgh.parallel_for(sycl::range<1>{ static_cast<size_t>(ipiv32_size) },
[=](sycl::id<1> index) {
ipiv_acc[(index / n) * stride_ipiv + index % n] =
static_cast<int64_t>(ipiv32_acc[index]);
});
});
}
#define GETRI_STRIDED_BATCH_LAUNCHER(TYPE, CUSOLVER_ROUTINE) \
void getri_batch(sycl::queue& queue, std::int64_t n, sycl::buffer<TYPE>& a, std::int64_t lda, \
std::int64_t stride_a, sycl::buffer<std::int64_t>& ipiv, \
std::int64_t stride_ipiv, std::int64_t batch_size, \
sycl::buffer<TYPE>& scratchpad, std::int64_t scratchpad_size) { \
return getri_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, n, a, lda, stride_a, ipiv, \
stride_ipiv, batch_size, scratchpad, scratchpad_size); \
}
GETRI_STRIDED_BATCH_LAUNCHER(float, cublasSgetriBatched)
GETRI_STRIDED_BATCH_LAUNCHER(double, cublasDgetriBatched)
GETRI_STRIDED_BATCH_LAUNCHER(std::complex<float>, cublasCgetriBatched)
GETRI_STRIDED_BATCH_LAUNCHER(std::complex<double>, cublasZgetriBatched)
#undef GETRI_STRIDED_BATCH_LAUNCHER
template <typename Func, typename T>
inline void getrs_batch(const char* func_name, Func func, sycl::queue& queue,
oneapi::math::transpose trans, std::int64_t n, std::int64_t nrhs,
sycl::buffer<T>& a, std::int64_t lda, std::int64_t stride_a,
sycl::buffer<std::int64_t>& ipiv, std::int64_t stride_ipiv,
sycl::buffer<T>& b, std::int64_t ldb, std::int64_t stride_b,
std::int64_t batch_size, sycl::buffer<T>& scratchpad,
std::int64_t scratchpad_size) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(n, nrhs, lda, ldb, stride_ipiv, stride_b, batch_size, scratchpad_size);
// cuSolver legacy api does not accept 64-bit ints.
// To get around the limitation.
// Create new buffer and convert 64-bit values.
std::uint64_t ipiv_size = stride_ipiv * batch_size;
sycl::buffer<int> ipiv32(sycl::range<1>{ ipiv_size });
queue.submit([&](sycl::handler& cgh) {
auto ipiv32_acc = ipiv32.template get_access<sycl::access::mode::write>(cgh);
auto ipiv_acc = ipiv.template get_access<sycl::access::mode::read>(cgh);
cgh.parallel_for(sycl::range<1>{ ipiv_size }, [=](sycl::id<1> index) {
ipiv32_acc[index] = static_cast<std::int32_t>(ipiv_acc[index]);
});
});
queue.submit([&](sycl::handler& cgh) {
auto a_acc = a.template get_access<sycl::access::mode::read>(cgh);
auto ipiv_acc = ipiv32.template get_access<sycl::access::mode::read>(cgh);
auto b_acc = b.template get_access<sycl::access::mode::write>(cgh);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
auto a_ = sc.get_mem<cuDataType*>(a_acc);
auto ipiv_ = sc.get_mem<std::int32_t*>(ipiv_acc);
auto b_ = sc.get_mem<cuDataType*>(b_acc);
cusolverStatus_t err;
// Does not use scratch so call cuSolver asynchronously and sync at end
for (int64_t i = 0; i < batch_size; ++i) {
CUSOLVER_ERROR_FUNC_T(func_name, func, err, handle, get_cublas_operation(trans), n,
nrhs, a_ + stride_a * i, lda, ipiv_ + stride_ipiv * i,
b_ + stride_b * i, ldb, nullptr);
}
#ifndef SYCL_EXT_ONEAPI_ENQUEUE_NATIVE_COMMAND
CUSOLVER_SYNC(err, handle)
#endif
});
});
}
#define GETRS_STRIDED_BATCH_LAUNCHER(TYPE, CUSOLVER_ROUTINE) \
void getrs_batch(sycl::queue& queue, oneapi::math::transpose trans, std::int64_t n, \
std::int64_t nrhs, sycl::buffer<TYPE>& a, std::int64_t lda, \
std::int64_t stride_a, sycl::buffer<std::int64_t>& ipiv, \
std::int64_t stride_ipiv, sycl::buffer<TYPE>& b, std::int64_t ldb, \
std::int64_t stride_b, std::int64_t batch_size, \
sycl::buffer<TYPE>& scratchpad, std::int64_t scratchpad_size) { \
return getrs_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, trans, n, nrhs, a, lda, \
stride_a, ipiv, stride_ipiv, b, ldb, stride_b, batch_size, scratchpad, \
scratchpad_size); \
}
GETRS_STRIDED_BATCH_LAUNCHER(float, cusolverDnSgetrs)
GETRS_STRIDED_BATCH_LAUNCHER(double, cusolverDnDgetrs)
GETRS_STRIDED_BATCH_LAUNCHER(std::complex<float>, cusolverDnCgetrs)
GETRS_STRIDED_BATCH_LAUNCHER(std::complex<double>, cusolverDnZgetrs)
#undef GETRS_STRIDED_BATCH_LAUNCHER
template <typename Func, typename T>
inline void getrf_batch(const char* func_name, Func func, sycl::queue& queue, std::int64_t m,
std::int64_t n, sycl::buffer<T>& a, std::int64_t lda, std::int64_t stride_a,
sycl::buffer<std::int64_t>& ipiv, std::int64_t stride_ipiv,
std::int64_t batch_size, sycl::buffer<T>& scratchpad,
std::int64_t scratchpad_size) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(m, n, lda, stride_a, stride_ipiv, batch_size, scratchpad_size);
// cuSolver legacy api does not accept 64-bit ints.
// To get around the limitation.
// Create new buffer with 32-bit ints then copy over results
std::uint64_t ipiv_size = stride_ipiv * batch_size;
sycl::buffer<int> ipiv32(sycl::range<1>{ ipiv_size });
sycl::buffer<int> devInfo{ batch_size };
queue.submit([&](sycl::handler& cgh) {
auto a_acc = a.template get_access<sycl::access::mode::read_write>(cgh);
auto ipiv32_acc = ipiv32.template get_access<sycl::access::mode::write>(cgh);
auto devInfo_acc = devInfo.template get_access<sycl::access::mode::write>(cgh);
auto scratch_acc = scratchpad.template get_access<sycl::access::mode::write>(cgh);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
auto a_ = sc.get_mem<cuDataType*>(a_acc);
auto ipiv_ = sc.get_mem<int*>(ipiv32_acc);
auto devInfo_ = sc.get_mem<int*>(devInfo_acc);
auto scratch_ = sc.get_mem<cuDataType*>(scratch_acc);
cusolverStatus_t err;
// Uses scratch so sync between each cuSolver call
for (std::int64_t i = 0; i < batch_size; ++i) {
cusolver_native_named_func(func_name, func, err, handle, m, n, a_ + stride_a * i,
lda, scratch_, ipiv_ + stride_ipiv * i, devInfo_ + i);
}
});
});
// Copy from 32-bit USM to 64-bit
queue.submit([&](sycl::handler& cgh) {
auto ipiv32_acc = ipiv32.template get_access<sycl::access::mode::read>(cgh);
auto ipiv_acc = ipiv.template get_access<sycl::access::mode::write>(cgh);
cgh.parallel_for(sycl::range<1>{ ipiv_size },
[=](sycl::id<1> index) { ipiv_acc[index] = ipiv32_acc[index]; });
});
lapack_info_check(queue, devInfo, __func__, func_name, batch_size);
}
#define GETRF_STRIDED_BATCH_LAUNCHER(TYPE, CUSOLVER_ROUTINE) \
void getrf_batch(sycl::queue& queue, std::int64_t m, std::int64_t n, sycl::buffer<TYPE>& a, \
std::int64_t lda, std::int64_t stride_a, sycl::buffer<std::int64_t>& ipiv, \
std::int64_t stride_ipiv, std::int64_t batch_size, \
sycl::buffer<TYPE>& scratchpad, std::int64_t scratchpad_size) { \
return getrf_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, m, n, a, lda, stride_a, \
ipiv, stride_ipiv, batch_size, scratchpad, scratchpad_size); \
}
GETRF_STRIDED_BATCH_LAUNCHER(float, cusolverDnSgetrf)
GETRF_STRIDED_BATCH_LAUNCHER(double, cusolverDnDgetrf)
GETRF_STRIDED_BATCH_LAUNCHER(std::complex<float>, cusolverDnCgetrf)
GETRF_STRIDED_BATCH_LAUNCHER(std::complex<double>, cusolverDnZgetrf)
#undef GETRF_STRIDED_BATCH_LAUNCHER
template <typename Func, typename T>
inline void orgqr_batch(const char* func_name, Func func, sycl::queue& queue, std::int64_t m,
std::int64_t n, std::int64_t k, sycl::buffer<T>& a, std::int64_t lda,
std::int64_t stride_a, sycl::buffer<T>& tau, std::int64_t stride_tau,
std::int64_t batch_size, sycl::buffer<T>& scratchpad,
std::int64_t scratchpad_size) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(m, n, k, lda, stride_a, stride_tau, batch_size, scratchpad_size);
queue.submit([&](sycl::handler& cgh) {
auto a_acc = a.template get_access<sycl::access::mode::read_write>(cgh);
auto tau_acc = tau.template get_access<sycl::access::mode::write>(cgh);
auto scratch_acc = scratchpad.template get_access<sycl::access::mode::read_write>(cgh);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
auto a_ = sc.get_mem<cuDataType*>(a_acc);
auto tau_ = sc.get_mem<cuDataType*>(tau_acc);
auto scratch_ = sc.get_mem<cuDataType*>(scratch_acc);
cusolverStatus_t err;
// Uses scratch so sync between each cuSolver call
for (int64_t i = 0; i < batch_size; ++i) {
cusolver_native_named_func(func_name, func, err, handle, m, n, k, a_ + stride_a * i,
lda, tau_ + stride_tau * i, scratch_, scratchpad_size,
nullptr);
}
});
});
}
#define ORGQR_STRIDED_BATCH_LAUNCHER(TYPE, CUSOLVER_ROUTINE) \
void orgqr_batch(sycl::queue& queue, std::int64_t m, std::int64_t n, std::int64_t k, \
sycl::buffer<TYPE>& a, std::int64_t lda, std::int64_t stride_a, \
sycl::buffer<TYPE>& tau, std::int64_t stride_tau, std::int64_t batch_size, \
sycl::buffer<TYPE>& scratchpad, std::int64_t scratchpad_size) { \
return orgqr_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, m, n, k, a, lda, stride_a, \
tau, stride_tau, batch_size, scratchpad, scratchpad_size); \
}
ORGQR_STRIDED_BATCH_LAUNCHER(float, cusolverDnSorgqr)
ORGQR_STRIDED_BATCH_LAUNCHER(double, cusolverDnDorgqr)
#undef ORGQR_STRIDED_BATCH_LAUNCHER
template <typename Func, typename T>
inline void potrf_batch(const char* func_name, Func func, sycl::queue& queue,
oneapi::math::uplo uplo, std::int64_t n, sycl::buffer<T>& a,
std::int64_t lda, std::int64_t stride_a, std::int64_t batch_size,
sycl::buffer<T>& scratchpad, std::int64_t scratchpad_size) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(n, lda, stride_a, batch_size, scratchpad_size);
queue.submit([&](sycl::handler& cgh) {
auto a_acc = a.template get_access<sycl::access::mode::read_write>(cgh);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
CUdeviceptr a_dev;
CUresult cuda_result;
cusolverStatus_t err;
auto a_ = sc.get_mem<cuDataType*>(a_acc);
// Transform ptr and stride to list of ptr's
cuDataType** a_batched = create_ptr_list_from_stride(a_, stride_a, batch_size);
CUDA_ERROR_FUNC(cuMemAlloc, cuda_result, &a_dev, sizeof(T*) * batch_size);
CUDA_ERROR_FUNC(cuMemcpyHtoD, cuda_result, a_dev, a_batched, sizeof(T*) * batch_size);
auto** a_dev_ = reinterpret_cast<cuDataType**>(a_dev);
cusolver_native_named_func(func_name, func, err, handle, get_cublas_fill_mode(uplo),
(int)n, a_dev_, (int)lda, nullptr, (int)batch_size);
free(a_batched);
cuMemFree(a_dev);
});
});
}
// Scratchpad memory not needed as parts of buffer a is used as workspace memory
#define POTRF_STRIDED_BATCH_LAUNCHER(TYPE, CUSOLVER_ROUTINE) \
void potrf_batch(sycl::queue& queue, oneapi::math::uplo uplo, std::int64_t n, \
sycl::buffer<TYPE>& a, std::int64_t lda, std::int64_t stride_a, \
std::int64_t batch_size, sycl::buffer<TYPE>& scratchpad, \
std::int64_t scratchpad_size) { \
return potrf_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, uplo, n, a, lda, stride_a, \
batch_size, scratchpad, scratchpad_size); \
}
POTRF_STRIDED_BATCH_LAUNCHER(float, cusolverDnSpotrfBatched)
POTRF_STRIDED_BATCH_LAUNCHER(double, cusolverDnDpotrfBatched)
POTRF_STRIDED_BATCH_LAUNCHER(std::complex<float>, cusolverDnCpotrfBatched)
POTRF_STRIDED_BATCH_LAUNCHER(std::complex<double>, cusolverDnZpotrfBatched)
#undef POTRF_STRIDED_BATCH_LAUNCHER
template <typename Func, typename T>
inline void potrs_batch(const char* func_name, Func func, sycl::queue& queue,
oneapi::math::uplo uplo, std::int64_t n, std::int64_t nrhs,
sycl::buffer<T>& a, std::int64_t lda, std::int64_t stride_a,
sycl::buffer<T>& b, std::int64_t ldb, std::int64_t stride_b,
std::int64_t batch_size, sycl::buffer<T>& scratchpad,
std::int64_t scratchpad_size) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(n, nrhs, lda, ldb, stride_a, stride_b, batch_size, scratchpad_size);
// cuSolver function only supports nrhs = 1
if (nrhs != 1)
throw unimplemented("lapack", "potrs_batch", "cusolver potrs_batch only supports nrhs = 1");
queue.submit([&](sycl::handler& cgh) {
auto a_acc = a.template get_access<sycl::access::mode::read_write>(cgh);
auto b_acc = b.template get_access<sycl::access::mode::read_write>(cgh);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
CUdeviceptr a_dev, b_dev;
cusolverStatus_t err;
CUresult cuda_result;
auto a_ = sc.get_mem<cuDataType*>(a_acc);
auto b_ = sc.get_mem<cuDataType*>(b_acc);
// Transform ptr and stride to list of ptr's
cuDataType** a_batched = create_ptr_list_from_stride(a_, stride_a, batch_size);
cuDataType** b_batched = create_ptr_list_from_stride(b_, stride_b, batch_size);
CUDA_ERROR_FUNC(cuMemAlloc, cuda_result, &a_dev, sizeof(T*) * batch_size);
CUDA_ERROR_FUNC(cuMemcpyHtoD, cuda_result, a_dev, a_batched, sizeof(T*) * batch_size);
CUDA_ERROR_FUNC(cuMemAlloc, cuda_result, &b_dev, sizeof(T*) * batch_size);
CUDA_ERROR_FUNC(cuMemcpyHtoD, cuda_result, b_dev, b_batched, sizeof(T*) * batch_size);
auto** a_dev_ = reinterpret_cast<cuDataType**>(a_dev);
auto** b_dev_ = reinterpret_cast<cuDataType**>(b_dev);
cusolver_native_named_func(func_name, func, err, handle, get_cublas_fill_mode(uplo),
(int)n, (int)nrhs, a_dev_, (int)lda, b_dev_, ldb, nullptr,
(int)batch_size);
free(a_batched);
free(b_batched);
cuMemFree(a_dev);
cuMemFree(b_dev);
});
});
}
// Scratchpad memory not needed as parts of buffer a is used as workspace memory
#define POTRS_STRIDED_BATCH_LAUNCHER(TYPE, CUSOLVER_ROUTINE) \
void potrs_batch(sycl::queue& queue, oneapi::math::uplo uplo, std::int64_t n, \
std::int64_t nrhs, sycl::buffer<TYPE>& a, std::int64_t lda, \
std::int64_t stride_a, sycl::buffer<TYPE>& b, std::int64_t ldb, \
std::int64_t stride_b, std::int64_t batch_size, \
sycl::buffer<TYPE>& scratchpad, std::int64_t scratchpad_size) { \
return potrs_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, uplo, n, nrhs, a, lda, \
stride_a, b, ldb, stride_b, batch_size, scratchpad, scratchpad_size); \
}
POTRS_STRIDED_BATCH_LAUNCHER(float, cusolverDnSpotrsBatched)
POTRS_STRIDED_BATCH_LAUNCHER(double, cusolverDnDpotrsBatched)
POTRS_STRIDED_BATCH_LAUNCHER(std::complex<float>, cusolverDnCpotrsBatched)
POTRS_STRIDED_BATCH_LAUNCHER(std::complex<double>, cusolverDnZpotrsBatched)
#undef POTRS_STRIDED_BATCH_LAUNCHER
template <typename Func, typename T>
inline void ungqr_batch(const char* func_name, Func func, sycl::queue& queue, std::int64_t m,
std::int64_t n, std::int64_t k, sycl::buffer<T>& a, std::int64_t lda,
std::int64_t stride_a, sycl::buffer<T>& tau, std::int64_t stride_tau,
std::int64_t batch_size, sycl::buffer<T>& scratchpad,
std::int64_t scratchpad_size) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(m, n, k, lda, stride_a, stride_tau, batch_size, scratchpad_size);
queue.submit([&](sycl::handler& cgh) {
auto a_acc = a.template get_access<sycl::access::mode::read_write>(cgh);
auto tau_acc = tau.template get_access<sycl::access::mode::write>(cgh);
auto scratch_acc = scratchpad.template get_access<sycl::access::mode::read_write>(cgh);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
auto a_ = sc.get_mem<cuDataType*>(a_acc);
auto tau_ = sc.get_mem<cuDataType*>(tau_acc);
auto scratch_ = sc.get_mem<cuDataType*>(scratch_acc);
cusolverStatus_t err;
// Uses scratch so sync between each cuSolver call
for (int64_t i = 0; i < batch_size; ++i) {
cusolver_native_named_func(func_name, func, err, handle, m, n, k, a_ + stride_a * i,
lda, tau_ + stride_tau * i, scratch_, scratchpad_size,
nullptr);
}
});
});
}
#define UNGQR_STRIDED_BATCH_LAUNCHER(TYPE, CUSOLVER_ROUTINE) \
void ungqr_batch(sycl::queue& queue, std::int64_t m, std::int64_t n, std::int64_t k, \
sycl::buffer<TYPE>& a, std::int64_t lda, std::int64_t stride_a, \
sycl::buffer<TYPE>& tau, std::int64_t stride_tau, std::int64_t batch_size, \
sycl::buffer<TYPE>& scratchpad, std::int64_t scratchpad_size) { \
return ungqr_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, m, n, k, a, lda, stride_a, \
tau, stride_tau, batch_size, scratchpad, scratchpad_size); \
}
UNGQR_STRIDED_BATCH_LAUNCHER(std::complex<float>, cusolverDnCungqr)
UNGQR_STRIDED_BATCH_LAUNCHER(std::complex<double>, cusolverDnZungqr)
#undef UNGQR_STRIDED_BATCH_LAUNCHER
// BATCH USM API
template <typename Func, typename T>
inline sycl::event geqrf_batch(const char* func_name, Func func, sycl::queue& queue, std::int64_t m,
std::int64_t n, T* a, std::int64_t lda, std::int64_t stride_a,
T* tau, std::int64_t stride_tau, std::int64_t batch_size,
T* scratchpad, std::int64_t scratchpad_size,
const std::vector<sycl::event>& dependencies) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(m, n, lda, stride_a, stride_tau, batch_size, scratchpad_size);
auto done = queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(dependencies);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
auto a_ = reinterpret_cast<cuDataType*>(a);
auto tau_ = reinterpret_cast<cuDataType*>(tau);
auto scratch_ = reinterpret_cast<cuDataType*>(scratchpad);
cusolverStatus_t err;
// Uses scratch so sync between each cuSolver call
for (int64_t i = 0; i < batch_size; ++i) {
cusolver_native_named_func(func_name, func, err, handle, m, n, a_ + stride_a * i,
lda, tau_ + stride_tau * i, scratch_, scratchpad_size,
nullptr);
}
});
});
return done;
}
#define GEQRF_STRIDED_BATCH_LAUNCHER_USM(TYPE, CUSOLVER_ROUTINE) \
sycl::event geqrf_batch(sycl::queue& queue, std::int64_t m, std::int64_t n, TYPE* a, \
std::int64_t lda, std::int64_t stride_a, TYPE* tau, \
std::int64_t stride_tau, std::int64_t batch_size, TYPE* scratchpad, \
std::int64_t scratchpad_size, \
const std::vector<sycl::event>& dependencies) { \
return geqrf_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, m, n, a, lda, stride_a, \
tau, stride_tau, batch_size, scratchpad, scratchpad_size, \
dependencies); \
}
GEQRF_STRIDED_BATCH_LAUNCHER_USM(float, cusolverDnSgeqrf)
GEQRF_STRIDED_BATCH_LAUNCHER_USM(double, cusolverDnDgeqrf)
GEQRF_STRIDED_BATCH_LAUNCHER_USM(std::complex<float>, cusolverDnCgeqrf)
GEQRF_STRIDED_BATCH_LAUNCHER_USM(std::complex<double>, cusolverDnZgeqrf)
#undef GEQRF_STRIDED_BATCH_LAUNCHER_USM
template <typename Func, typename T>
inline sycl::event geqrf_batch(const char* func_name, Func func, sycl::queue& queue,
std::int64_t* m, std::int64_t* n, T** a, std::int64_t* lda, T** tau,
std::int64_t group_count, std::int64_t* group_sizes, T* scratchpad,
std::int64_t scratchpad_size,
const std::vector<sycl::event>& dependencies) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(group_count, scratchpad_size);
for (int64_t i = 0; i < group_count; ++i)
overflow_check(m[i], n[i], lda[i], group_sizes[i]);
auto done = queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(dependencies);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
auto a_ = reinterpret_cast<cuDataType**>(a);
auto tau_ = reinterpret_cast<cuDataType**>(tau);
auto scratch_ = reinterpret_cast<cuDataType*>(scratchpad);
int64_t global_id = 0;
cusolverStatus_t err;
// Uses scratch so sync between each cuSolver call
for (int64_t group_id = 0; group_id < group_count; ++group_id) {
for (int64_t local_id = 0; local_id < group_sizes[group_id];
++local_id, ++global_id) {
cusolver_native_named_func(func_name, func, err, handle, m[group_id],
n[group_id], a_[global_id], lda[group_id],
tau_[global_id], scratch_, scratchpad_size, nullptr);
}
}
});
});
return done;
}
#define GEQRF_BATCH_LAUNCHER_USM(TYPE, CUSOLVER_ROUTINE) \
sycl::event geqrf_batch( \
sycl::queue& queue, std::int64_t* m, std::int64_t* n, TYPE** a, std::int64_t* lda, \
TYPE** tau, std::int64_t group_count, std::int64_t* group_sizes, TYPE* scratchpad, \
std::int64_t scratchpad_size, const std::vector<sycl::event>& dependencies) { \
return geqrf_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, m, n, a, lda, tau, \
group_count, group_sizes, scratchpad, scratchpad_size, dependencies); \
}
GEQRF_BATCH_LAUNCHER_USM(float, cusolverDnSgeqrf)
GEQRF_BATCH_LAUNCHER_USM(double, cusolverDnDgeqrf)
GEQRF_BATCH_LAUNCHER_USM(std::complex<float>, cusolverDnCgeqrf)
GEQRF_BATCH_LAUNCHER_USM(std::complex<double>, cusolverDnZgeqrf)
#undef GEQRF_BATCH_LAUNCHER_USM
template <typename Func, typename T>
inline sycl::event getrf_batch(const char* func_name, Func func, sycl::queue& queue, std::int64_t m,
std::int64_t n, T* a, std::int64_t lda, std::int64_t stride_a,
std::int64_t* ipiv, std::int64_t stride_ipiv,
std::int64_t batch_size, T* scratchpad, std::int64_t scratchpad_size,
const std::vector<sycl::event>& dependencies) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(m, n, lda, stride_a, stride_ipiv, batch_size, scratchpad_size);
// cuSolver legacy api does not accept 64-bit ints.
// To get around the limitation.
// Allocate memory with 32-bit ints then copy over results
std::uint64_t ipiv_size = stride_ipiv * batch_size;
int* ipiv32 = (int*)malloc_device(sizeof(int) * ipiv_size, queue);
int* devInfo = (int*)malloc_device(sizeof(int) * batch_size, queue);
auto done = queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(dependencies);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
auto a_ = reinterpret_cast<cuDataType*>(a);
auto devInfo_ = reinterpret_cast<int*>(devInfo);
auto scratchpad_ = reinterpret_cast<cuDataType*>(scratchpad);
auto ipiv_ = reinterpret_cast<int*>(ipiv32);
cusolverStatus_t err;
// Uses scratch so sync between each cuSolver call
for (int64_t i = 0; i < batch_size; ++i) {
cusolver_native_named_func(func_name, func, err, handle, m, n, a_ + stride_a * i,
lda, scratchpad_, ipiv_ + stride_ipiv * i, devInfo_ + i);
}
});
});
// Copy from 32-bit USM to 64-bit
sycl::event done_casting = queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(done);
cgh.parallel_for(sycl::range<1>{ ipiv_size },
[=](sycl::id<1> index) { ipiv[index] = ipiv32[index]; });
});
// Enqueue free memory, don't return event as not-neccessary for user to wait for ipiv32 being released
queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(done_casting);
cgh.host_task([=](sycl::interop_handle ih) { sycl::free(ipiv32, queue); });
});
// lapack_info_check calls queue.wait()
lapack_info_check(queue, devInfo, __func__, func_name, batch_size);
sycl::free(devInfo, queue);
return done_casting;
}
#define GETRF_STRIDED_BATCH_LAUNCHER_USM(TYPE, CUSOLVER_ROUTINE) \
sycl::event getrf_batch(sycl::queue& queue, std::int64_t m, std::int64_t n, TYPE* a, \
std::int64_t lda, std::int64_t stride_a, std::int64_t* ipiv, \
std::int64_t stride_ipiv, std::int64_t batch_size, TYPE* scratchpad, \
std::int64_t scratchpad_size, \
const std::vector<sycl::event>& dependencies) { \
return getrf_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, m, n, a, lda, stride_a, \
ipiv, stride_ipiv, batch_size, scratchpad, scratchpad_size, \
dependencies); \
}
GETRF_STRIDED_BATCH_LAUNCHER_USM(float, cusolverDnSgetrf)
GETRF_STRIDED_BATCH_LAUNCHER_USM(double, cusolverDnDgetrf)
GETRF_STRIDED_BATCH_LAUNCHER_USM(std::complex<float>, cusolverDnCgetrf)
GETRF_STRIDED_BATCH_LAUNCHER_USM(std::complex<double>, cusolverDnZgetrf)
#undef GETRF_STRIDED_BATCH_LAUNCHER_USM
template <typename Func, typename T>
inline sycl::event getrf_batch(const char* func_name, Func func, sycl::queue& queue,
std::int64_t* m, std::int64_t* n, T** a, std::int64_t* lda,
std::int64_t** ipiv, std::int64_t group_count,
std::int64_t* group_sizes, T* scratchpad,
std::int64_t scratchpad_size,
const std::vector<sycl::event>& dependencies) {
using cuDataType = typename CudaEquivalentType<T>::Type;
int64_t batch_size = 0;
overflow_check(group_count, scratchpad_size);
for (int64_t i = 0; i < group_count; ++i) {
overflow_check(m[i], n[i], lda[i], group_sizes[i]);
batch_size += group_sizes[i];
}
// cuSolver legacy api does not accept 64-bit ints.
// To get around the limitation.
// Allocate memory with 32-bit ints then copy over results
int** ipiv32 = (int**)malloc(sizeof(int*) * batch_size);
int64_t global_id = 0;
for (int64_t group_id = 0; group_id < group_count; ++group_id)
for (int64_t local_id = 0; local_id < group_sizes[group_id]; ++local_id, ++global_id)
ipiv32[global_id] = (int*)malloc_device(sizeof(int) * n[group_id], queue);
int* devInfo = (int*)malloc_device(sizeof(int) * batch_size, queue);
auto done = queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(dependencies);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
auto a_ = reinterpret_cast<cuDataType**>(a);
auto scratch_ = reinterpret_cast<cuDataType*>(scratchpad);
int64_t global_id = 0;
cusolverStatus_t err;
// Uses scratch so sync between each cuSolver call
for (int64_t group_id = 0; group_id < group_count; ++group_id) {
for (int64_t local_id = 0; local_id < group_sizes[group_id];
++local_id, ++global_id) {
cusolver_native_named_func(func_name, func, err, handle, m[group_id],
n[group_id], a_[global_id], lda[group_id], scratch_,
ipiv32[global_id], devInfo + global_id);
}
}
});
});
// Copy from 32-bit USM to 64-bit
std::vector<sycl::event> casting_dependencies(group_count);
for (int64_t group_id = 0, global_id = 0; group_id < group_count; ++group_id) {
uint64_t ipiv_size = n[group_id];
for (int64_t local_id = 0; local_id < group_sizes[group_id]; ++local_id, ++global_id) {
int64_t* d_ipiv = ipiv[global_id];
int* d_ipiv32 = ipiv32[global_id];
sycl::event e = queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(done);
cgh.parallel_for(sycl::range<1>{ ipiv_size },
[=](sycl::id<1> index) { d_ipiv[index] = d_ipiv32[index]; });
});
casting_dependencies[group_id] = e;
}
}
// Enqueue free memory
sycl::event done_freeing = queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(casting_dependencies);
cgh.host_task([=](sycl::interop_handle ih) {
for (int64_t global_id = 0; global_id < batch_size; ++global_id)
sycl::free(ipiv32[global_id], queue);
free(ipiv32);
});
});
// lapack_info_check calls queue.wait()
lapack_info_check(queue, devInfo, __func__, func_name, batch_size);
sycl::free(devInfo, queue);
return done_freeing;
}
#define GETRF_BATCH_LAUNCHER_USM(TYPE, CUSOLVER_ROUTINE) \
sycl::event getrf_batch(sycl::queue& queue, std::int64_t* m, std::int64_t* n, TYPE** a, \
std::int64_t* lda, std::int64_t** ipiv, std::int64_t group_count, \
std::int64_t* group_sizes, TYPE* scratchpad, \
std::int64_t scratchpad_size, \
const std::vector<sycl::event>& dependencies) { \
return getrf_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, m, n, a, lda, ipiv, \
group_count, group_sizes, scratchpad, scratchpad_size, dependencies); \
}
GETRF_BATCH_LAUNCHER_USM(float, cusolverDnSgetrf)
GETRF_BATCH_LAUNCHER_USM(double, cusolverDnDgetrf)
GETRF_BATCH_LAUNCHER_USM(std::complex<float>, cusolverDnCgetrf)
GETRF_BATCH_LAUNCHER_USM(std::complex<double>, cusolverDnZgetrf)
#undef GETRS_BATCH_LAUNCHER_USM
template <typename Func, typename T>
sycl::event getri_batch(const char* func_name, Func func, sycl::queue& queue, std::int64_t n, T* a,
std::int64_t lda, std::int64_t stride_a, std::int64_t* ipiv,
std::int64_t stride_ipiv, std::int64_t batch_size, T* scratchpad,
std::int64_t scratchpad_size,
const std::vector<sycl::event>& dependencies) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(n, lda, stride_a, stride_ipiv, batch_size, scratchpad_size);
std::uint64_t ipiv32_size = n * batch_size;
int* ipiv32 = sycl::malloc_device<int>(ipiv32_size, queue);
int* devInfo = sycl::malloc_device<int>(batch_size, queue);
sycl::event done_casting = queue.submit([&](sycl::handler& cgh) {
cgh.parallel_for(
sycl::range<1>{ static_cast<size_t>(ipiv32_size) }, [=](sycl::id<1> index) {
ipiv32[index] = static_cast<int>(ipiv[(index / n) * stride_ipiv + index % n]);
});
});
// getri_batched is contained within cublas, not cusolver. For this reason
// we need to use cublas types instead of cusolver types (as is needed for
// other lapack routines)
auto done = queue.submit([&](sycl::handler& cgh) {
using blas::cublas::cublas_error;
cgh.depends_on(done_casting);
cgh.depends_on(dependencies);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
cublasStatus_t err;
CUresult cuda_result;
cublasHandle_t cublas_handle;
CUBLAS_ERROR_FUNC(cublasCreate, err, &cublas_handle);
CUstream cu_stream = sycl::get_native<sycl::backend::ext_oneapi_cuda>(queue);
CUBLAS_ERROR_FUNC(cublasSetStream, err, cublas_handle, cu_stream);
CUdeviceptr a_dev;
auto* a_ = reinterpret_cast<cuDataType*>(a);
cuDataType** a_batched = create_ptr_list_from_stride(a_, stride_a, batch_size);
CUDA_ERROR_FUNC(cuMemAlloc, cuda_result, &a_dev, sizeof(T*) * batch_size);
CUDA_ERROR_FUNC(cuMemcpyHtoD, cuda_result, a_dev, a_batched, sizeof(T*) * batch_size);
auto** a_dev_ = reinterpret_cast<cuDataType**>(a_dev);
CUdeviceptr scratch_dev;
auto* scratch_ = reinterpret_cast<cuDataType*>(scratchpad);
cuDataType** scratch_batched =
create_ptr_list_from_stride(scratch_, stride_a, batch_size);
CUDA_ERROR_FUNC(cuMemAlloc, cuda_result, &scratch_dev, sizeof(T*) * batch_size);
CUDA_ERROR_FUNC(cuMemcpyHtoD, cuda_result, scratch_dev, scratch_batched,
sizeof(T*) * batch_size);
auto** scratch_dev_ = reinterpret_cast<cuDataType**>(scratch_dev);
blas::cublas::cublas_native_named_func(func_name, func, err, cublas_handle, n, a_dev_,
lda, ipiv32, scratch_dev_, lda, devInfo,
batch_size);
free(a_batched);
free(scratch_batched);
cuMemFree(a_dev);
cuMemFree(scratch_dev);
});
});
// The inverted matrices stored in scratch_ need to be stored in a_
auto copy1 = queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(done);
cgh.parallel_for(
sycl::range<1>{ static_cast<size_t>(stride_a * (batch_size - 1) + lda * n) },
[=](sycl::id<1> index) { a[index] = scratchpad[index]; });
});
auto copy2 = queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(done);
cgh.parallel_for(
sycl::range<1>{ static_cast<size_t>(ipiv32_size) }, [=](sycl::id<1> index) {
ipiv[(index / n) * stride_ipiv + index % n] = static_cast<int64_t>(ipiv32[index]);
});
});
copy1.wait();
copy2.wait();
sycl::free(ipiv32, queue);
sycl::free(devInfo, queue);
return done;
}
#define GETRI_BATCH_LAUNCHER_USM(TYPE, CUSOLVER_ROUTINE) \
sycl::event getri_batch( \
sycl::queue& queue, std::int64_t n, TYPE* a, std::int64_t lda, std::int64_t stride_a, \
std::int64_t* ipiv, std::int64_t stride_ipiv, std::int64_t batch_size, TYPE* scratchpad, \
std::int64_t scratchpad_size, const std::vector<sycl::event>& dependencies) { \
return getri_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, n, a, lda, stride_a, ipiv, \
stride_ipiv, batch_size, scratchpad, scratchpad_size, dependencies); \
}
GETRI_BATCH_LAUNCHER_USM(float, cublasSgetriBatched)
GETRI_BATCH_LAUNCHER_USM(double, cublasDgetriBatched)
GETRI_BATCH_LAUNCHER_USM(std::complex<float>, cublasCgetriBatched)
GETRI_BATCH_LAUNCHER_USM(std::complex<double>, cublasZgetriBatched)
#undef GETRI_BATCH_LAUNCHER_USM
sycl::event getri_batch(sycl::queue& queue, std::int64_t* n, float** a, std::int64_t* lda,
std::int64_t** ipiv, std::int64_t group_count, std::int64_t* group_sizes,
float* scratchpad, std::int64_t scratchpad_size,
const std::vector<sycl::event>& dependencies) {
throw unimplemented("lapack", "getri_batch");
}
sycl::event getri_batch(sycl::queue& queue, std::int64_t* n, double** a, std::int64_t* lda,
std::int64_t** ipiv, std::int64_t group_count, std::int64_t* group_sizes,
double* scratchpad, std::int64_t scratchpad_size,
const std::vector<sycl::event>& dependencies) {
throw unimplemented("lapack", "getri_batch");
}
sycl::event getri_batch(sycl::queue& queue, std::int64_t* n, std::complex<float>** a,
std::int64_t* lda, std::int64_t** ipiv, std::int64_t group_count,
std::int64_t* group_sizes, std::complex<float>* scratchpad,
std::int64_t scratchpad_size,
const std::vector<sycl::event>& dependencies) {
throw unimplemented("lapack", "getri_batch");
}
sycl::event getri_batch(sycl::queue& queue, std::int64_t* n, std::complex<double>** a,
std::int64_t* lda, std::int64_t** ipiv, std::int64_t group_count,
std::int64_t* group_sizes, std::complex<double>* scratchpad,
std::int64_t scratchpad_size,
const std::vector<sycl::event>& dependencies) {
throw unimplemented("lapack", "getri_batch");
}
template <typename Func, typename T>
inline sycl::event getrs_batch(const char* func_name, Func func, sycl::queue& queue,
oneapi::math::transpose trans, std::int64_t n, std::int64_t nrhs,
T* a, std::int64_t lda, std::int64_t stride_a, std::int64_t* ipiv,
std::int64_t stride_ipiv, T* b, std::int64_t ldb,
std::int64_t stride_b, std::int64_t batch_size, T* scratchpad,
std::int64_t scratchpad_size,
const std::vector<sycl::event>& dependencies) {
using cuDataType = typename CudaEquivalentType<T>::Type;
overflow_check(n, nrhs, lda, ldb, stride_ipiv, stride_b, batch_size, scratchpad_size);
// cuSolver legacy api does not accept 64-bit ints.
// To get around the limitation.
// Create new memory and convert 64-bit values.
std::uint64_t ipiv_size = stride_ipiv * batch_size;
int* ipiv32 = (int*)malloc_device(sizeof(int) * ipiv_size, queue);
auto done_casting = queue.submit([&](sycl::handler& cgh) {
cgh.parallel_for(sycl::range<1>{ ipiv_size }, [=](sycl::id<1> index) {
ipiv32[index] = static_cast<std::int32_t>(ipiv[index]);
});
});
auto done = queue.submit([&](sycl::handler& cgh) {
cgh.depends_on(dependencies);
cgh.depends_on(done_casting);
onemath_cusolver_host_task(cgh, queue, [=](CusolverScopedContextHandler& sc) {
auto handle = sc.get_handle(queue);
auto a_ = reinterpret_cast<cuDataType*>(a);
auto ipiv_ = reinterpret_cast<int*>(ipiv32);
auto b_ = reinterpret_cast<cuDataType*>(b);
cusolverStatus_t err;
// Does not use scratch so call cuSolver asynchronously and sync at end
for (int64_t i = 0; i < batch_size; ++i) {
CUSOLVER_ERROR_FUNC_T(func_name, func, err, handle, get_cublas_operation(trans), n,
nrhs, a_ + stride_a * i, lda, ipiv_ + stride_ipiv * i,
b_ + stride_b * i, ldb, nullptr);
}
#ifndef SYCL_EXT_ONEAPI_ENQUEUE_NATIVE_COMMAND
CUSOLVER_SYNC(err, handle)
#endif
sycl::free(ipiv32, queue);
});
});
return done;
}
#define GETRS_STRIDED_BATCH_LAUNCHER_USM(TYPE, CUSOLVER_ROUTINE) \
sycl::event getrs_batch(sycl::queue& queue, oneapi::math::transpose trans, std::int64_t n, \
std::int64_t nrhs, TYPE* a, std::int64_t lda, std::int64_t stride_a, \
std::int64_t* ipiv, std::int64_t stride_ipiv, TYPE* b, \
std::int64_t ldb, std::int64_t stride_b, std::int64_t batch_size, \
TYPE* scratchpad, std::int64_t scratchpad_size, \
const std::vector<sycl::event>& dependencies) { \
return getrs_batch(#CUSOLVER_ROUTINE, CUSOLVER_ROUTINE, queue, trans, n, nrhs, a, lda, \
stride_a, ipiv, stride_ipiv, b, ldb, stride_b, batch_size, scratchpad, \
scratchpad_size, dependencies); \
}