forked from dyzheng/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_kernel_op.cu
More file actions
1351 lines (1211 loc) · 59.2 KB
/
Copy pathmath_kernel_op.cu
File metadata and controls
1351 lines (1211 loc) · 59.2 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
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
#include "module_base/module_device/memory_op.h"
#include "module_hsolver/kernels/math_kernel_op.h"
#include "module_psi/psi.h"
#include "module_base/tool_quit.h"
#include <base/macros/macros.h>
#include <cuda_runtime.h>
#include <thrust/complex.h>
#include <thrust/execution_policy.h>
#include <thrust/inner_product.h>
namespace hsolver
{
const int warp_size = 32;
const unsigned int full_mask = 0xffffffff;
const int thread_per_block = 256;
}
template <>
struct GetTypeReal<thrust::complex<float>> {
using type = float; /**< The return type specialization for std::complex<double>. */
};
template <>
struct GetTypeReal<thrust::complex<double>> {
using type = double; /**< The return type specialization for std::complex<double>. */
};
namespace hsolver {
template <typename T>
struct GetTypeThrust {
using type = T;
};
template <>
struct GetTypeThrust<std::complex<float>> {
using type = thrust::complex<float>; /**< The return type specialization for std::complex<float>. */
};
template <>
struct GetTypeThrust<std::complex<double>> {
using type = thrust::complex<double>; /**< The return type specialization for std::complex<float>. */
};
static cublasHandle_t cublas_handle = nullptr;
static inline
void xdot_wrapper(const int &n, const float * x, const int &incx, const float * y, const int &incy, float &result) {
cublasErrcheck(cublasSdot(cublas_handle, n, x, incx, y, incy, &result));
}
static inline
void xdot_wrapper(const int &n, const double * x, const int &incx, const double * y, const int &incy, double &result) {
cublasErrcheck(cublasDdot(cublas_handle, n, x, incx, y, incy, &result));
}
void createGpuBlasHandle(){
if (cublas_handle == nullptr) {
cublasErrcheck(cublasCreate(&cublas_handle));
}
}
void destoryBLAShandle(){
if (cublas_handle != nullptr) {
cublasErrcheck(cublasDestroy(cublas_handle));
cublas_handle = nullptr;
}
}
template <typename FPTYPE>
__forceinline__ __device__ void warp_reduce(FPTYPE& val) {
for (int offset = 16; offset > 0; offset >>= 1)
val += __shfl_down_sync(full_mask, val, offset);
}
template <typename Real>
__global__ void line_minimize_with_block(
thrust::complex<Real>* grad,
thrust::complex<Real>* hgrad,
thrust::complex<Real>* psi,
thrust::complex<Real>* hpsi,
const int n_basis,
const int n_basis_max)
{
int band_idx = blockIdx.x; // band_idx
int tid = threadIdx.x; // basis_idx
int item = 0;
Real epsilo_0 = 0.0, epsilo_1 = 0.0, epsilo_2 = 0.0;
Real theta = 0.0, cos_theta = 0.0, sin_theta = 0.0;
__shared__ Real data[thread_per_block * 3];
data[tid] = 0;
for (int basis_idx = tid; basis_idx < n_basis; basis_idx += thread_per_block) {
item = band_idx * n_basis_max + basis_idx;
data[tid] += (grad[item] * thrust::conj(grad[item])).real();
}
__syncthreads();
// just do some parallel reduction in shared memory
for (int ii = thread_per_block >> 1; ii > warp_size; ii >>= 1) {
if (tid < ii) {
data[tid] += data[tid + ii];
}
__syncthreads();
}
// For threads in the same warp, it is better that they process the same work
// Also, __syncwarp() should be used instead of __syncthreads()
// Therefore we unroll the loop and ensure that the threads does the same work
if (tid < warp_size) {
data[tid] += data[tid + 32]; __syncwarp();
data[tid] += data[tid + 16]; __syncwarp();
data[tid] += data[tid + 8]; __syncwarp();
data[tid] += data[tid + 4]; __syncwarp();
data[tid] += data[tid + 2]; __syncwarp();
data[tid] += data[tid + 1]; __syncwarp();
}
__syncthreads();
Real norm = 1.0 / sqrt(data[0]);
__syncthreads();
data[tid] = 0;
data[thread_per_block + tid] = 0;
data[2 * thread_per_block + tid] = 0;
for (int basis_idx = tid; basis_idx < n_basis; basis_idx += thread_per_block) {
item = band_idx * n_basis_max + basis_idx;
grad[item] *= norm;
hgrad[item] *= norm;
data[tid] += (hpsi[item] * thrust::conj(psi[item])).real();
data[thread_per_block + tid] += (grad[item] * thrust::conj(hpsi[item])).real();
data[2 * thread_per_block + tid] += (grad[item] * thrust::conj(hgrad[item])).real();
}
__syncthreads();
// just do some parallel reduction in shared memory
for (int ii = thread_per_block >> 1; ii > warp_size; ii >>= 1) {
if (tid < ii) {
data[tid] += data[tid + ii];
data[thread_per_block + tid] += data[thread_per_block + tid + ii];
data[2 * thread_per_block + tid] += data[2 * thread_per_block + tid + ii];
}
__syncthreads();
}
if (tid < warp_size) {
data[tid] += data[tid + 32]; __syncwarp();
data[tid] += data[tid + 16]; __syncwarp();
data[tid] += data[tid + 8]; __syncwarp();
data[tid] += data[tid + 4]; __syncwarp();
data[tid] += data[tid + 2]; __syncwarp();
data[tid] += data[tid + 1]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 32]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 16]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 8]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 4]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 2]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 1]; __syncwarp();
data[2 * thread_per_block + tid] += data[2 * thread_per_block + tid + 32]; __syncwarp();
data[2 * thread_per_block + tid] += data[2 * thread_per_block + tid + 16]; __syncwarp();
data[2 * thread_per_block + tid] += data[2 * thread_per_block + tid + 8]; __syncwarp();
data[2 * thread_per_block + tid] += data[2 * thread_per_block + tid + 4]; __syncwarp();
data[2 * thread_per_block + tid] += data[2 * thread_per_block + tid + 2]; __syncwarp();
data[2 * thread_per_block + tid] += data[2 * thread_per_block + tid + 1]; __syncwarp();
}
__syncthreads();
epsilo_0 = data[0];
epsilo_1 = data[thread_per_block];
epsilo_2 = data[2 * thread_per_block];
theta = 0.5 * abs(atan(2 * epsilo_1/(epsilo_0 - epsilo_2)));
cos_theta = cos(theta);
sin_theta = sin(theta);
for (int basis_idx = tid; basis_idx < n_basis; basis_idx += thread_per_block) {
item = band_idx * n_basis_max + basis_idx;
psi [item] = psi [item] * cos_theta + grad [item] * sin_theta;
hpsi[item] = hpsi[item] * cos_theta + hgrad[item] * sin_theta;
}
}
template <typename Real>
__global__ void calc_grad_with_block(
const Real* prec,
Real* err,
Real* beta,
thrust::complex<Real>* psi,
thrust::complex<Real>* hpsi,
thrust::complex<Real>* grad,
thrust::complex<Real>* grad_old,
const int n_basis,
const int n_basis_max)
{
int band_idx = blockIdx.x; // band_idx
int tid = threadIdx.x; // basis_idx
int item = 0;
Real err_st = 0.0;
Real beta_st = 0.0;
Real epsilo = 0.0;
Real grad_2 = 0.0;
thrust::complex<Real> grad_1 = {0, 0};
__shared__ Real data[thread_per_block * 2];
// Init shared memory
data[tid] = 0;
for (int basis_idx = tid; basis_idx < n_basis; basis_idx += thread_per_block) {
item = band_idx * n_basis_max + basis_idx;
data[tid] += (psi[item] * thrust::conj(psi[item])).real();
}
__syncthreads();
// just do some parallel reduction in shared memory
for (int ii = thread_per_block >> 1; ii > warp_size; ii >>= 1) {
if (tid < ii) {
data[tid] += data[tid + ii];
}
__syncthreads();
}
if (tid < warp_size) {
data[tid] += data[tid + 32]; __syncwarp();
data[tid] += data[tid + 16]; __syncwarp();
data[tid] += data[tid + 8]; __syncwarp();
data[tid] += data[tid + 4]; __syncwarp();
data[tid] += data[tid + 2]; __syncwarp();
data[tid] += data[tid + 1]; __syncwarp();
}
__syncthreads();
Real norm = 1.0 / sqrt(data[0]);
__syncthreads();
data[tid] = 0;
for (int basis_idx = tid; basis_idx < n_basis; basis_idx += thread_per_block) {
item = band_idx * n_basis_max + basis_idx;
psi[item] *= norm;
hpsi[item] *= norm;
data[tid] += (hpsi[item] * thrust::conj(psi[item])).real();
}
__syncthreads();
// just do some parallel reduction in shared memory
for (int ii = thread_per_block >> 1; ii > warp_size; ii >>= 1) {
if (tid < ii) {
data[tid] += data[tid + ii];
}
__syncthreads();
}
if (tid < warp_size) {
data[tid] += data[tid + 32]; __syncwarp();
data[tid] += data[tid + 16]; __syncwarp();
data[tid] += data[tid + 8]; __syncwarp();
data[tid] += data[tid + 4]; __syncwarp();
data[tid] += data[tid + 2]; __syncwarp();
data[tid] += data[tid + 1]; __syncwarp();
}
__syncthreads();
epsilo = data[0];
__syncthreads();
data[tid] = 0;
data[thread_per_block + tid] = 0;
for (int basis_idx = tid; basis_idx < n_basis; basis_idx += thread_per_block) {
item = band_idx * n_basis_max + basis_idx;
grad_1 = hpsi[item] - epsilo * psi[item];
grad_2 = thrust::norm(grad_1);
data[tid] += grad_2;
data[thread_per_block + tid] += grad_2 / prec[basis_idx];
}
__syncthreads();
// just do some parallel reduction in shared memory
for (int ii = thread_per_block >> 1; ii > warp_size; ii >>= 1) {
if (tid < ii) {
data[tid] += data[tid + ii];
data[thread_per_block + tid] += data[thread_per_block + tid + ii];
}
__syncthreads();
}
if (tid < warp_size) {
data[tid] += data[tid + 32]; __syncwarp();
data[tid] += data[tid + 16]; __syncwarp();
data[tid] += data[tid + 8]; __syncwarp();
data[tid] += data[tid + 4]; __syncwarp();
data[tid] += data[tid + 2]; __syncwarp();
data[tid] += data[tid + 1]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 32]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 16]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 8]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 4]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 2]; __syncwarp();
data[thread_per_block + tid] += data[thread_per_block + tid + 1]; __syncwarp();
}
__syncthreads();
err_st = data[0];
beta_st = data[thread_per_block];
for (int basis_idx = tid; basis_idx < n_basis; basis_idx += thread_per_block) {
item = band_idx * n_basis_max + basis_idx;
grad_1 = hpsi[item] - epsilo * psi[item];
grad[item] = -grad_1 / prec[basis_idx] + beta_st / beta[band_idx] * grad_old[item];
}
__syncthreads();
if (tid == 0) {
beta[band_idx] = beta_st;
err[band_idx] = sqrt(err_st);
}
}
// Define the CUDA kernel:
template <typename T>
__global__ void vector_div_constant_kernel(
const int size,
T* result,
const T* vector,
const typename GetTypeReal<T>::type constant)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < size)
{
result[i] = vector[i] / constant;
}
}
template <typename T>
__global__ void vector_mul_vector_kernel(
const int size,
T* result,
const T* vector1,
const typename GetTypeReal<T>::type* vector2)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < size)
{
result[i] = vector1[i] * vector2[i];
}
}
template <typename T>
__global__ void vector_div_vector_kernel(
const int size,
T* result,
const T* vector1,
const typename GetTypeReal<T>::type* vector2)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < size)
{
result[i] = vector1[i] / vector2[i];
}
}
template <typename T, typename Real>
__global__ void constantvector_addORsub_constantVector_kernel(
const int size,
T* result,
const T* vector1,
const Real constant1,
const T* vector2,
const Real constant2)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < size)
{
result[i] = vector1[i] * constant1 + vector2[i] * constant2;
}
}
template <typename T>
__global__ void matrix_transpose_kernel(
const int row,
const int col,
const T* in,
T* out)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < row)
{
for (int j = 0; j < col; j++)
{
out[j * row + i] = in[i * col + j];
}
}
}
template <typename T>
__global__ void matrix_setTo_another_kernel(
const int n,
const int LDA,
const int LDB,
const T* matrix_A,
T* matrix_B)
{
int j = blockIdx.x * blockDim.x + threadIdx.x;
if (j < LDA && j < LDB)
{
for (int i = 0; i < n; i++)
{
matrix_B[i * LDB + j] = matrix_A[i * LDA + j];
}
}
}
template <typename T, typename Real>
__global__ void refresh_Hcc_Scc_Vcc_kernel(
const int n,
T *hcc,
T *scc,
T *vcc,
const int ldh,
const Real *eigenvalue,
const T one)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n)
{
hcc[i * ldh + i] = eigenvalue[i];
scc[i * ldh + i] = one;
vcc[i * ldh + i] = one;
}
}
template <typename T, typename Real>
__global__ void matrix_multiply_vector_kernel(const int m, const int n, T *a, const int lda, const Real *v, const Real alpha, T *c, const int ldc){
int row = blockIdx.x * blockDim.x + threadIdx.x;
int col = blockIdx.y * blockDim.y + threadIdx.y;
if (col >= n || row >= m) return;
c[col * ldc + row] = a[col * lda + row] * v[col] * alpha;
}
template <typename T, typename Real>
__global__ void upate_psi_by_precondition_kernel(const int m, const int n, T *psi, const int lda, const Real *precondition, const Real *eigenvalue){
int i = blockIdx.x * blockDim.x + threadIdx.x;
int j = blockIdx.y;
if (i >= m || j >= n) return;
Real x = std::abs(precondition[i] - eigenvalue[j]);
Real pre = 0.5 * (1.0 + x + sqrt(1 + (x - 1.0) * (x - 1.0)));
psi[j * lda + i] = psi[j * lda + i] / pre;
}
__device__ double complexAbsSquared(cuDoubleComplex z) {
return z.x * z.x + z.y * z.y;
}
template <typename Real>
__device__ Real warpReduceSum(Real val) {
for (int offset = 16; offset > 0; offset /= 2)
val += __shfl_down_sync(0xffffffff, val, offset);
return val;
}
template <typename Real>
__device__ Real blockReduceSum(Real val, volatile Real* shared) {
int lane = threadIdx.x % 32;
int wid = threadIdx.x / 32;
val = warpReduceSum(val);
if (lane == 0)
shared[wid] = val;
__syncthreads();
Real sum = 0.0;
if (wid == 0) {
sum = (threadIdx.x < blockDim.x / 32) ? shared[lane] : 0.0;
sum = warpReduceSum(sum);
if (lane == 0) shared[0] = sum;
}
__syncthreads();
return shared[0];
}
__device__ double norm_square(thrust::complex<double> val) {
double real = val.real();
double imag = val.imag();
return real * real + imag * imag;
}
__device__ float norm_square(thrust::complex<float> val) {
float real = val.real();
float imag = val.imag();
return real * real + imag * imag;
}
__device__ double norm_square(double val){
return val * val;
}
template <typename T, typename Real>
__global__ void normalize_matrix_column_kernel(int rows, int cols, T* matrix, int lda) {
int col = blockIdx.x;
if (col >= cols) return;
int tid = threadIdx.x;
int stride = blockDim.x;
extern __shared__ char s_char[];
Real* shared = reinterpret_cast<Real*>(s_char);
Real local_sum = 0.0;
for (int i = tid; i < rows; i += stride) {
T val = matrix[col * lda + i];
local_sum += norm_square(val);
}
Real l2_sq = blockReduceSum(local_sum, shared);
Real norm = sqrt(l2_sq + 1e-20);
for (int i = tid; i < rows; i += stride) {
matrix[col * lda + i] /= norm;
}
}
template <typename T>
void line_minimize_with_block_op<T, base_device::DEVICE_GPU>::operator()(T* grad_out,
T* hgrad_out,
T* psi_out,
T* hpsi_out,
const int& n_basis,
const int& n_basis_max,
const int& n_band)
{
auto A = reinterpret_cast<thrust::complex<Real>*>(grad_out);
auto B = reinterpret_cast<thrust::complex<Real>*>(hgrad_out);
auto C = reinterpret_cast<thrust::complex<Real>*>(psi_out);
auto D = reinterpret_cast<thrust::complex<Real>*>(hpsi_out);
line_minimize_with_block<Real><<<n_band, thread_per_block>>>(
A, B, C, D,
n_basis, n_basis_max);
cudaCheckOnDebug();
}
template <typename T>
void calc_grad_with_block_op<T, base_device::DEVICE_GPU>::operator()(const Real* prec_in,
Real* err_out,
Real* beta_out,
T* psi_out,
T* hpsi_out,
T* grad_out,
T* grad_old_out,
const int& n_basis,
const int& n_basis_max,
const int& n_band)
{
auto A = reinterpret_cast<thrust::complex<Real>*>(psi_out);
auto B = reinterpret_cast<thrust::complex<Real>*>(hpsi_out);
auto C = reinterpret_cast<thrust::complex<Real>*>(grad_out);
auto D = reinterpret_cast<thrust::complex<Real>*>(grad_old_out);
calc_grad_with_block<Real><<<n_band, thread_per_block>>>(
prec_in, err_out, beta_out,
A, B, C, D,
n_basis, n_basis_max);
cudaCheckOnDebug();
}
template <>
double dot_real_op<double, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& dim,
const double* psi_L,
const double* psi_R,
const bool reduce)
{
double result = 0.0;
xdot_wrapper(dim, psi_L, 1, psi_R, 1, result);
if (reduce) {
Parallel_Reduce::reduce_pool(result);
}
return result;
}
// for this implementation, please check
// https://thrust.github.io/doc/group__transformed__reductions_ga321192d85c5f510e52300ae762c7e995.html denghui modify
// 2022-10-03 Note that ddot_(2*dim,a,1,b,1) = REAL( zdotc_(dim,a,1,b,1) ) GPU specialization of actual computation.
template <typename FPTYPE>
inline FPTYPE dot_complex_wrapper(const base_device::DEVICE_GPU* d,
const int& dim,
const std::complex<FPTYPE>* psi_L,
const std::complex<FPTYPE>* psi_R,
const bool reduce)
{
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// denghui modify 2022-10-07
// Note that ddot_(2*dim,a,1,b,1) = REAL( zdotc_(dim,a,1,b,1) )
const FPTYPE* pL = reinterpret_cast<const FPTYPE*>(psi_L);
const FPTYPE* pR = reinterpret_cast<const FPTYPE*>(psi_R);
FPTYPE result = 0.0;
xdot_wrapper(dim * 2, pL, 1, pR, 1, result);
if (reduce) {
Parallel_Reduce::reduce_pool(result);
}
return result;
}
template <>
float dot_real_op<std::complex<float>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& dim,
const std::complex<float>* psi_L,
const std::complex<float>* psi_R,
const bool reduce)
{
return dot_complex_wrapper(d, dim, psi_L, psi_R, reduce);
}
template <>
double dot_real_op<std::complex<double>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& dim,
const std::complex<double>* psi_L,
const std::complex<double>* psi_R,
const bool reduce)
{
return dot_complex_wrapper(d, dim, psi_L, psi_R, reduce);
}
// vector operator: result[i] = vector[i] / constant
template <>
void vector_div_constant_op<double, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int dim,
double* result,
const double* vector,
const double constant)
{
// In small cases, 1024 threads per block will only utilize 17 blocks, much less than 40
int thread = thread_per_block;
int block = (dim + thread - 1) / thread;
vector_div_constant_kernel<double> <<<block, thread >>> (dim, result, vector, constant);
cudaCheckOnDebug();
}
// vector operator: result[i] = vector[i] / constant
template <typename FPTYPE>
inline void vector_div_constant_complex_wrapper(const base_device::DEVICE_GPU* d,
const int dim,
std::complex<FPTYPE>* result,
const std::complex<FPTYPE>* vector,
const FPTYPE constant)
{
thrust::complex<FPTYPE>* result_tmp = reinterpret_cast<thrust::complex<FPTYPE>*>(result);
const thrust::complex<FPTYPE>* vector_tmp = reinterpret_cast<const thrust::complex<FPTYPE>*>(vector);
int thread = thread_per_block;
int block = (dim + thread - 1) / thread;
vector_div_constant_kernel<thrust::complex<FPTYPE>> <<<block, thread >>> (dim, result_tmp, vector_tmp, constant);
cudaCheckOnDebug();
}
template <>
void vector_div_constant_op<std::complex<float>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int dim,
std::complex<float>* result,
const std::complex<float>* vector,
const float constant)
{
vector_div_constant_complex_wrapper(d, dim, result, vector, constant);
}
template <>
void vector_div_constant_op<std::complex<double>, base_device::DEVICE_GPU>::operator()(
const base_device::DEVICE_GPU* d,
const int dim,
std::complex<double>* result,
const std::complex<double>* vector,
const double constant)
{
vector_div_constant_complex_wrapper(d, dim, result, vector, constant);
}
// vector operator: result[i] = vector1[i](not complex) * vector2[i](not complex)
template <>
void vector_mul_vector_op<double, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& dim,
double* result,
const double* vector1,
const double* vector2)
{
int thread = thread_per_block;
int block = (dim + thread - 1) / thread;
vector_mul_vector_kernel<double> <<<block, thread >>> (dim, result, vector1, vector2);
cudaCheckOnDebug();
}
// vector operator: result[i] = vector1[i](complex) * vector2[i](not complex)
template <typename FPTYPE>
inline void vector_mul_vector_complex_wrapper(const base_device::DEVICE_GPU* d,
const int& dim,
std::complex<FPTYPE>* result,
const std::complex<FPTYPE>* vector1,
const FPTYPE* vector2)
{
thrust::complex<FPTYPE>* result_tmp = reinterpret_cast<thrust::complex<FPTYPE>*>(result);
const thrust::complex<FPTYPE>* vector1_tmp = reinterpret_cast<const thrust::complex<FPTYPE>*>(vector1);
int thread = thread_per_block;
int block = (dim + thread - 1) / thread;
vector_mul_vector_kernel<thrust::complex<FPTYPE>> <<<block, thread >>> (dim, result_tmp, vector1_tmp, vector2);
cudaCheckOnDebug();
}
template <>
void vector_mul_vector_op<std::complex<float>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& dim,
std::complex<float>* result,
const std::complex<float>* vector1,
const float* vector2)
{
vector_mul_vector_complex_wrapper(d, dim, result, vector1, vector2);
}
template <>
void vector_mul_vector_op<std::complex<double>, base_device::DEVICE_GPU>::operator()(
const base_device::DEVICE_GPU* d,
const int& dim,
std::complex<double>* result,
const std::complex<double>* vector1,
const double* vector2)
{
vector_mul_vector_complex_wrapper(d, dim, result, vector1, vector2);
}
// vector operator: result[i] = vector1[i](not complex) / vector2[i](not complex)
template <>
void vector_div_vector_op<double, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& dim,
double* result,
const double* vector1,
const double* vector2)
{
int thread = thread_per_block;
int block = (dim + thread - 1) / thread;
vector_div_vector_kernel<double> <<<block, thread >>> (dim, result, vector1, vector2);
cudaCheckOnDebug();
}
// vector operator: result[i] = vector1[i](complex) / vector2[i](not complex)
template <typename FPTYPE>
inline void vector_div_vector_complex_wrapper(const base_device::DEVICE_GPU* d,
const int& dim,
std::complex<FPTYPE>* result,
const std::complex<FPTYPE>* vector1,
const FPTYPE* vector2)
{
thrust::complex<FPTYPE>* result_tmp = reinterpret_cast<thrust::complex<FPTYPE>*>(result);
const thrust::complex<FPTYPE>* vector1_tmp = reinterpret_cast<const thrust::complex<FPTYPE>*>(vector1);
int thread = thread_per_block;
int block = (dim + thread - 1) / thread;
vector_div_vector_kernel<thrust::complex<FPTYPE>> <<<block, thread >>> (dim, result_tmp, vector1_tmp, vector2);
cudaCheckOnDebug();
}
template <>
void vector_div_vector_op<std::complex<float>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& dim,
std::complex<float>* result,
const std::complex<float>* vector1,
const float* vector2)
{
vector_div_vector_complex_wrapper(d, dim, result, vector1, vector2);
}
template <>
void vector_div_vector_op<std::complex<double>, base_device::DEVICE_GPU>::operator()(
const base_device::DEVICE_GPU* d,
const int& dim,
std::complex<double>* result,
const std::complex<double>* vector1,
const double* vector2)
{
vector_div_vector_complex_wrapper(d, dim, result, vector1, vector2);
}
// vector operator: result[i] = vector1[i] * constant1 + vector2[i] * constant2
template <typename T>
void constantvector_addORsub_constantVector_op<T, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& dim,
T* result,
const T* vector1,
const Real constant1,
const T* vector2,
const Real constant2)
{
using Type = typename GetTypeThrust<T>::type;
using Real = typename GetTypeReal<T>::type;
auto result_tmp = reinterpret_cast<Type*>(result);
auto vector1_tmp = reinterpret_cast<const Type*>(vector1);
auto vector2_tmp = reinterpret_cast<const Type*>(vector2);
int thread = thread_per_block;
int block = (dim + thread - 1) / thread;
constantvector_addORsub_constantVector_kernel<Type, Real> <<<block, thread >>>(dim, result_tmp, vector1_tmp, constant1, vector2_tmp, constant2);
cudaCheckOnDebug();
}
template <>
void axpy_op<double, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& N,
const double* alpha,
const double* X,
const int& incX,
double* Y,
const int& incY)
{
cublasErrcheck(cublasDaxpy(cublas_handle, N, alpha, X, incX, Y, incY));
}
template <>
void axpy_op<std::complex<float>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& N,
const std::complex<float>* alpha,
const std::complex<float>* X,
const int& incX,
std::complex<float>* Y,
const int& incY)
{
cublasErrcheck(cublasCaxpy(cublas_handle, N, (float2*)alpha, (float2*)X, incX, (float2*)Y, incY));
}
template <>
void axpy_op<std::complex<double>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& N,
const std::complex<double>* alpha,
const std::complex<double>* X,
const int& incX,
std::complex<double>* Y,
const int& incY)
{
cublasErrcheck(cublasZaxpy(cublas_handle, N, (double2*)alpha, (double2*)X, incX, (double2*)Y, incY));
}
cublasOperation_t judge_trans_op(bool is_complex, const char& trans, const char* name)
{
if (trans == 'N')
{
return CUBLAS_OP_N;
}
else if(trans == 'T')
{
return CUBLAS_OP_T;
}
else if(is_complex && trans == 'C')
{
return CUBLAS_OP_C;
}
else
{
ModuleBase::WARNING_QUIT(name, std::string("Unknown trans type ") + trans + std::string(" !"));
}
}
template <>
void gemv_op<double, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const char& trans,
const int& m,
const int& n,
const double* alpha,
const double* A,
const int& lda,
const double* X,
const int& incx,
const double* beta,
double* Y,
const int& incy)
{
cublasOperation_t cutrans = judge_trans_op(false, trans, "gemv_op");
cublasErrcheck(cublasDgemv(cublas_handle, cutrans, m, n, alpha, A, lda, X, incx, beta, Y, incx));
}
template <>
void gemv_op<std::complex<float>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const char& trans,
const int& m,
const int& n,
const std::complex<float>* alpha,
const std::complex<float>* A,
const int& lda,
const std::complex<float>* X,
const int& incx,
const std::complex<float>* beta,
std::complex<float>* Y,
const int& incy)
{
cublasOperation_t cutrans = judge_trans_op(true, trans, "gemv_op");
cublasErrcheck(cublasCgemv(cublas_handle, cutrans, m, n, (float2*)alpha, (float2*)A, lda, (float2*)X, incx, (float2*)beta, (float2*)Y, incx));
}
template <>
void gemv_op<std::complex<double>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const char& trans,
const int& m,
const int& n,
const std::complex<double>* alpha,
const std::complex<double>* A,
const int& lda,
const std::complex<double>* X,
const int& incx,
const std::complex<double>* beta,
std::complex<double>* Y,
const int& incy)
{
cublasOperation_t cutrans = judge_trans_op(true, trans, "gemv_op");
cublasErrcheck(cublasZgemv(cublas_handle, cutrans, m, n, (double2*)alpha, (double2*)A, lda, (double2*)X, incx, (double2*)beta, (double2*)Y, incx));
}
template <>
void scal_op<float, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& N,
const std::complex<float>* alpha,
std::complex<float>* X,
const int& incx)
{
cublasErrcheck(cublasCscal(cublas_handle, N, (float2*)alpha, (float2*)X, incx));
}
template <>
void scal_op<double, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& N,
const std::complex<double>* alpha,
std::complex<double>* X,
const int& incx)
{
cublasErrcheck(cublasZscal(cublas_handle, N, (double2*)alpha, (double2*)X, incx));
}
template <>
void gemm_op<double, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const char& transa,
const char& transb,
const int& m,
const int& n,
const int& k,
const double* alpha,
const double* a,
const int& lda,
const double* b,
const int& ldb,
const double* beta,
double* c,
const int& ldc)
{
cublasOperation_t cutransA = judge_trans_op(false, transa, "gemm_op");
cublasOperation_t cutransB = judge_trans_op(false, transb, "gemm_op");
cublasErrcheck(cublasDgemm(cublas_handle, cutransA, cutransB, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc));
}
template <>
void gemm_op<std::complex<float>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const char& transa,
const char& transb,
const int& m,
const int& n,
const int& k,
const std::complex<float>* alpha,
const std::complex<float>* a,
const int& lda,
const std::complex<float>* b,
const int& ldb,
const std::complex<float>* beta,
std::complex<float>* c,
const int& ldc)
{
cublasOperation_t cutransA = judge_trans_op(true, transa, "gemm_op");
cublasOperation_t cutransB = judge_trans_op(true, transb, "gemm_op");
cublasErrcheck(cublasCgemm(cublas_handle, cutransA, cutransB, m, n ,k, (float2*)alpha, (float2*)a , lda, (float2*)b, ldb, (float2*)beta, (float2*)c, ldc));
}
template <>
void gemm_op<std::complex<double>, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const char& transa,
const char& transb,
const int& m,
const int& n,
const int& k,
const std::complex<double>* alpha,
const std::complex<double>* a,
const int& lda,
const std::complex<double>* b,
const int& ldb,
const std::complex<double>* beta,
std::complex<double>* c,
const int& ldc)
{
cublasOperation_t cutransA = judge_trans_op(true, transa, "gemm_op");
cublasOperation_t cutransB = judge_trans_op(true, transb, "gemm_op");
cublasErrcheck(cublasZgemm(cublas_handle, cutransA, cutransB, m, n ,k, (double2*)alpha, (double2*)a , lda, (double2*)b, ldb, (double2*)beta, (double2*)c, ldc));
}
template <>
void matrixTranspose_op<double, base_device::DEVICE_GPU>::operator()(const base_device::DEVICE_GPU* d,
const int& row,
const int& col,
const double* input_matrix,
double* output_matrix)
{
double* device_temp = nullptr;
base_device::memory::resize_memory_op<double, base_device::DEVICE_GPU>()(d, device_temp, row * col);
if (row == col)
{