-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgpuVector.cuh
2746 lines (2374 loc) · 91.3 KB
/
gpuVector.cuh
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
#ifndef __GPU_VECTOR_CUH
#define __GPU_VECTOR_CUH
#include"cuda_runtime.h"
#include"iostream"
#include <vector>
//#include"lib.cuh"
#include"type_traits"
#include <algorithm>
//#define GVECTOR_WITH_MATLAB
#if defined(GVECTOR_WITH_MATLAB)
extern void pass_matrix_to_matlab(const char* namestr, float* pdata, int nrows, int ncols, int wordpitch, bool rowmajor = false);
extern void pass_matrix_to_matlab(const char* namestr, double* pdata, int nrows, int ncols, int wordpitch, bool rowmajor = false);
extern void pass_matrix_to_matlab(const char* namestr, int* pdata, int nrows, int ncols, int wordpitch, bool rowmajor = false);
extern void pass_matrix_to_matlab(const char* namestr, bool* pdata, int nrows, int ncols, int wordpitch, bool rowmajor = false);
#endif
#define cuda_error_check do{ \
auto err = cudaGetLastError(); \
if (err != 0) { \
printf("\x1b[31mCUDA error occured at line %d in file %s, error type %s \x1b[0m\n", __LINE__,__FILE__, cudaGetErrorName(err));\
} \
}while(0)
/*
=========== define computation kernel ===========
*/
namespace gv {
template<int N>
__host__ __device__ int round(int n) {
if (n % N == 0) {
return n;
} else {
int rn = (n + (N - 1)) / N * N;
return rn;
}
};
__host__ __device__ int round(int n, int N) {
if (n % N == 0) {
return n;
} else {
int rn = (n + (N - 1)) / N * N;
return rn;
}
}
template<typename T>
__global__ void init_array_kernel(T* array, T value, int array_size) {
int tid = blockIdx.x*blockDim.x + threadIdx.x;
if (tid < array_size) {
array[tid] = value;
}
}
// define some kernel function
template <typename T, unsigned int blockSize>
__device__ void warpReduce(volatile T *sdata, unsigned int tid) {
if (blockSize >= 64) sdata[tid] += sdata[tid + 32];
if (blockSize >= 32) sdata[tid] += sdata[tid + 16];
if (blockSize >= 16) sdata[tid] += sdata[tid + 8];
if (blockSize >= 8) sdata[tid] += sdata[tid + 4];
if (blockSize >= 4) sdata[tid] += sdata[tid + 2];
if (blockSize >= 2) sdata[tid] += sdata[tid + 1];
}
template <typename T, unsigned int blockSize>
__device__ void warpMax(volatile T *sdata, unsigned int tid) {
if (blockSize >= 64) { T s = sdata[tid + 32]; if (sdata[tid] < s) sdata[tid] = s; };
if (blockSize >= 32) { T s = sdata[tid + 16]; if (sdata[tid] < s) sdata[tid] = s; };
if (blockSize >= 16) { T s = sdata[tid + 8]; if (sdata[tid] < s) sdata[tid] = s; };
if (blockSize >= 8) { T s = sdata[tid + 4]; if (sdata[tid] < s) sdata[tid] = s; };
if (blockSize >= 4) { T s = sdata[tid + 2]; if (sdata[tid] < s) sdata[tid] = s; };
if (blockSize >= 2) { T s = sdata[tid + 1]; if (sdata[tid] < s) sdata[tid] = s; };
}
template <typename T, unsigned int blockSize>
__device__ void warpMin(volatile T *sdata, unsigned int tid) {
if (blockSize >= 64) { T s = sdata[tid + 32]; if (sdata[tid] > s) sdata[tid] = s; };
if (blockSize >= 32) { T s = sdata[tid + 16]; if (sdata[tid] > s) sdata[tid] = s; };
if (blockSize >= 16) { T s = sdata[tid + 8]; if (sdata[tid] > s) sdata[tid] = s; };
if (blockSize >= 8) { T s = sdata[tid + 4]; if (sdata[tid] > s) sdata[tid] = s; };
if (blockSize >= 4) { T s = sdata[tid + 2]; if (sdata[tid] > s) sdata[tid] = s; };
if (blockSize >= 2) { T s = sdata[tid + 1]; if (sdata[tid] > s) sdata[tid] = s; };
}
template<typename T, unsigned int blockSize>
__device__ void blockReduce(volatile T * sdata) {
int len = blockSize;
while (len > 64) {
len /= 2;
if (threadIdx.x < len) {
sdata[threadIdx.x] += sdata[threadIdx.x + len];
}
__syncthreads();
}
if (threadIdx.x < 32) {
warpReduce<T, blockSize>(sdata, threadIdx.x);
}
}
template<typename T, typename Tout, typename Lam>
__global__ void map(T* g_data, Tout* dst, int n, Lam func) {
int tid = blockIdx.x*blockDim.x + threadIdx.x;
if (tid < n) {
dst[tid] = func(g_data[tid]);
}
}
template<typename T, typename Lam>
__global__ void map(T* dst, int n, Lam func) {
int tid = blockIdx.x*blockDim.x + threadIdx.x;
if (tid < n) {
dst[tid] = func(tid);
}
}
template<typename Lam>
__global__ void map(int n, Lam func) {
int tid = blockIdx.x*blockDim.x + threadIdx.x;
if (tid < n) {
func(tid);
}
}
template<typename Scalar, typename graph_t>
__global__ void compute_graph_kernel(Scalar* dst, int array_size, graph_t graph) {
int tid = blockDim.x*blockIdx.x + threadIdx.x;
if (tid >= array_size) return;
dst[tid] = graph.eval(tid);
}
template<typename Scalar, typename filter, typename replace_val>
__global__ void replace_filter_kernel(Scalar* dst, int array_size, filter f, replace_val v) {
int tid = blockDim.x*blockIdx.x + threadIdx.x;
if (tid >= array_size) return;
if (f.eval(tid)) {
dst[tid] = v.eval(tid);
}
}
template<typename Tin, int blockSize = 512, typename Tout = Tin>
__global__ void block_sum_kernel(const Tin* pdata, Tout* odata, size_t n) {
__shared__ Tout sdata[blockSize];
if (blockDim.x != blockSize) {
printf("error block size does not match at line %d ! \n", __LINE__);
}
int tid = threadIdx.x;
size_t element_id = threadIdx.x + blockDim.x*blockIdx.x;
Tout s = 0;
// load data to block
if (element_id < n) {
s = pdata[element_id];
}
sdata[tid] = s;
__syncthreads();
// block reduce sum
if (blockSize >= 512) { if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
// use warpReduce to sum last 64 component
if (tid < 32) { warpReduce<Tout, blockSize>(sdata, tid); }
if (tid == 0) odata[blockIdx.x] = sdata[0];
}
template<typename T, int blockSize = 512>
__global__ void block_max_kernel(const T* indata, T* odata, size_t n) {
__shared__ T sdata[blockSize];
if (blockDim.x != blockSize) {
printf("error block size does not match at line %d ! \n", __LINE__);
}
int tid = threadIdx.x;
size_t element_id = threadIdx.x + blockIdx.x*blockDim.x;
T s = -1e30;
if (element_id < n) {
s = indata[element_id];
}
sdata[tid] = s;
__syncthreads();
// block max
if (blockSize >= 512) { if (tid < 256) { T v = sdata[tid + 256]; if (sdata[tid] < v) sdata[tid] = v; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { T v = sdata[tid + 128]; if (sdata[tid] < v) sdata[tid] = v; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { T v = sdata[tid + 64]; if (sdata[tid] < v) sdata[tid] = v; } __syncthreads(); }
// use warpReduce to sum last 64 component
if (tid < 32) warpMax<T, blockSize>(sdata, tid);
if (tid == 0) odata[blockIdx.x] = sdata[0];
}
template<typename T, int blockSize = 512>
__global__ void block_maxabs_kernel(const T* indata, T* odata, size_t n) {
__shared__ T sdata[blockSize];
if (blockDim.x != blockSize) {
printf("error block size does not match at line %d ! \n", __LINE__);
}
int tid = threadIdx.x;
size_t element_id = threadIdx.x + blockIdx.x*blockDim.x;
T s = -1e30;
if (element_id < n) {
s = abs(indata[element_id]);
}
sdata[tid] = s;
__syncthreads();
// block max
if (blockSize >= 512) { if (tid < 256) { T v = sdata[tid + 256]; if (sdata[tid] < v) sdata[tid] = v; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { T v = sdata[tid + 128]; if (sdata[tid] < v) sdata[tid] = v; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { T v = sdata[tid + 64]; if (sdata[tid] < v) sdata[tid] = v; } __syncthreads(); }
// use warpReduce to sum last 64 component
if (tid < 32) warpMax<T, blockSize>(sdata, tid);
if (tid == 0) odata[blockIdx.x] = sdata[0];
}
template<typename T, int blockSize = 512>
__global__ void block_min_kernel(const T* indata, T* odata, size_t n) {
__shared__ T sdata[blockSize];
if (blockDim.x != blockSize) {
printf("error block size does not match at line %d ! \n", __LINE__);
}
int tid = threadIdx.x;
size_t element_id = threadIdx.x + blockIdx.x*blockDim.x;
T s = 1e30;
if (element_id < n) {
s = indata[element_id];
}
sdata[tid] = s;
__syncthreads();
// block max
if (blockSize >= 512) { if (tid < 256) { T v = sdata[tid + 256]; if (sdata[tid] > v) sdata[tid] = v; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { T v = sdata[tid + 128]; if (sdata[tid] > v) sdata[tid] = v; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { T v = sdata[tid + 64]; if (sdata[tid] > v) sdata[tid] = v; } __syncthreads(); }
// use warpReduce to sum last 64 component
if (tid < 32) warpMin<T, blockSize>(sdata, tid);
if (tid == 0) odata[blockIdx.x] = sdata[0];
}
template<typename T, int blockSize = 512>
__global__ void block_dot_kernel(const T* v1p, const T* v2p, T* odata, size_t n) {
__shared__ T sdata[blockSize];
if (blockDim.x != blockSize) {
printf("error block size does not match at line %d ! \n", __LINE__);
}
int tid = threadIdx.x;
size_t element_id = threadIdx.x + blockIdx.x*blockDim.x;
T s = 0.f;
// load data to block
if (element_id < n) {
s = v1p[element_id] * v2p[element_id];
}
sdata[tid] = s;
__syncthreads();
// block reduce sum
if (blockSize >= 512) { if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
// use warpReduce to sum last 64 component
if (tid < 32) warpReduce<T, blockSize>(sdata, tid);
if (tid == 0) odata[blockIdx.x] = sdata[0];
}
template<typename T, typename graph_t1, typename graph_t2, int blockSize = 512>
__global__ void dot_graph_kernel(T* dump, int array_size, graph_t1 g1, graph_t2 g2) {
__shared__ T sdata[blockSize];
if (blockDim.x != blockSize) {
printf("error block size does not match at line %d ! \n", __LINE__);
}
int tid = threadIdx.x;
size_t element_id = threadIdx.x + blockIdx.x*blockDim.x;
T s = 0.f;
// load data to block
if (element_id < array_size) {
s = g1.eval(element_id) * g2.eval(element_id);
}
sdata[tid] = s;
__syncthreads();
// block reduce sum
if (blockSize >= 512) { if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
// use warpReduce to sum last 64 component
if (tid < 32) warpReduce<T, blockSize>(sdata, tid);
if (tid == 0) dump[blockIdx.x] = sdata[0];
}
template<typename T, typename graph_t, int blockSize = 512>
__global__ void sum_graph_kernel(T* dump, int array_size, graph_t graph) {
__shared__ T sdata[blockSize];
if (blockDim.x != blockSize) {
printf("error block size does not match at line %d ! \n", __LINE__);
}
int tid = threadIdx.x;
size_t element_id = threadIdx.x + blockIdx.x*blockDim.x;
T s = 0.f;
// load data to block
if (element_id < array_size) {
s = graph.eval(element_id);
}
sdata[tid] = s;
__syncthreads();
// block reduce sum
if (blockSize >= 512) { if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
// use warpReduce to sum last 64 component
if (tid < 32) warpReduce<T, blockSize>(sdata, tid);
if (tid == 0) dump[blockIdx.x] = sdata[0];
}
template<typename T, typename graph_t, int blockSize = 512>
__global__ void sqrnorm_graph_kernel(T* dump, int array_size, graph_t graph) {
__shared__ T sdata[blockSize];
if (blockDim.x != blockSize) {
printf("error block size does not match at line %d ! \n", __LINE__);
}
int tid = threadIdx.x;
size_t element_id = threadIdx.x + blockIdx.x*blockDim.x;
T s = 0.f;
// load data to block
if (element_id < array_size) {
T val = graph.eval(element_id);
s = val * val;
}
sdata[tid] = s;
__syncthreads();
// block reduce sum
if (blockSize >= 512) { if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
// use warpReduce to sum last 64 component
if (tid < 32) warpReduce<T, blockSize>(sdata, tid);
if (tid == 0) dump[blockIdx.x] = sdata[0];
}
template<typename T, typename graph_t, int blockSize = 512>
__global__ void max_graph_kernel(T* odata, size_t n, graph_t graph) {
__shared__ T sdata[blockSize];
if (blockDim.x != blockSize) {
printf("error block size does not match at line %d ! \n", __LINE__);
}
int tid = threadIdx.x;
size_t element_id = threadIdx.x + blockIdx.x*blockDim.x;
T s = -1e30;
if (element_id < n) {
s = graph.eval(element_id);
}
sdata[tid] = s;
__syncthreads();
// block max
if (blockSize >= 512) { if (tid < 256) { T v = sdata[tid + 256]; if (sdata[tid] < v) sdata[tid] = v; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { T v = sdata[tid + 128]; if (sdata[tid] < v) sdata[tid] = v; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { T v = sdata[tid + 64]; if (sdata[tid] < v) sdata[tid] = v; } __syncthreads(); }
// use warpReduce to sum last 64 component
if (tid < 32) warpMax<T, blockSize>(sdata, tid);
if (tid == 0) odata[blockIdx.x] = sdata[0];
}
template<typename T, typename graph_t, int blockSize = 512>
__global__ void min_graph_kernel(T* odata, size_t n, graph_t graph) {
__shared__ T sdata[blockSize];
if (blockDim.x != blockSize) {
printf("error block size does not match at line %d ! \n", __LINE__);
}
int tid = threadIdx.x;
size_t element_id = threadIdx.x + blockIdx.x*blockDim.x;
T s = 1e30;
if (element_id < n) {
s = graph.eval(element_id);
}
sdata[tid] = s;
__syncthreads();
// block max
if (blockSize >= 512) { if (tid < 256) { T v = sdata[tid + 256]; if (sdata[tid] > v) sdata[tid] = v; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { T v = sdata[tid + 128]; if (sdata[tid] > v) sdata[tid] = v; } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { T v = sdata[tid + 64]; if (sdata[tid] > v) sdata[tid] = v; } __syncthreads(); }
// use warpReduce to sum last 64 component
if (tid < 32) warpMin<T, blockSize>(sdata, tid);
if (tid == 0) odata[blockIdx.x] = sdata[0];
}
__host__ void make_kernel_param(size_t* block_num, size_t* block_size, size_t num_tasks, size_t prefer_block_size) {
*block_size = prefer_block_size;
*block_num = (num_tasks + prefer_block_size - 1) / prefer_block_size;
}
// dump_array_sum makes original array dirty, make sure dump is large enough
template<typename T, int blockSize = 512>
T dump_array_sum(T* dump, size_t n) {
T sum;
if (n <= 1) {
cudaMemcpy(&sum, dump, sizeof(T), cudaMemcpyDeviceToHost);
return sum;
}
size_t grid_dim, block_dim;
T* block_dump2 = dump;
T* block_dump1 = dump + ((n + 63) / 2 / 32) * 32;
do {
#if 0
// input : dump1 output : dump2
std::swap(block_dump1, block_dump2);
make_kernel_param(&grid_dim, &block_dim, n, blockSize);
block_sum_kernel<T, blockSize> << <grid_dim, block_dim >> > (block_dump1, block_dump2, n);
// error may occurred because of the inideal parallel block, the block result will overwrite latter data
#else
make_kernel_param(&grid_dim, &block_dim, n, blockSize);
block_sum_kernel<T, blockSize> << <grid_dim, block_dim >> > (block_dump2, block_dump2, n);
// if the early block is excuted first, latter data will not be overwritten
#endif
} while ((n = (n + blockSize - 1) / blockSize) > 1);
cudaMemcpy(&sum, block_dump2, sizeof(T), cudaMemcpyDeviceToHost);
return sum;
}
template<typename T>
T dot(const T* indata1, const T* indata2, T* dump_buf, size_t n, T* dot_dst = nullptr) {
constexpr int blockSize = 512;
size_t grid_dim, block_dim;
make_kernel_param(&grid_dim, &block_dim, n, blockSize);
block_dot_kernel << <grid_dim, block_dim >> > (indata1, indata2, dump_buf, n);
if (n <= blockSize) {
T sum;
cudaMemcpy(&sum, dump_buf, sizeof(T), cudaMemcpyDeviceToHost);
if (dot_dst != nullptr)
cudaMemcpy(dot_dst, &sum, sizeof(T), cudaMemcpyHostToDevice);
//cuda_error_check;
return sum;
}
else {
T sum = dump_array_sum(dump_buf, (n + blockSize - 1) / blockSize);
if (dot_dst != nullptr)
cudaMemcpy(dot_dst, &sum, sizeof(T), cudaMemcpyHostToDevice);
//cuda_error_check;
return sum;
}
}
template<typename T, int blockSize = 512>
T parallel_max(const T* indata, T* dump, size_t array_size, T* max_dst = nullptr) {
//constexpr int blockSize = 512;
size_t grid_dim, block_dim;
make_kernel_param(&grid_dim, &block_dim, array_size, blockSize);
block_max_kernel << <grid_dim, block_dim >> > (indata, dump, array_size);
if (array_size <= blockSize) {
T max_num;
cudaMemcpy(&max_num, dump, sizeof(T), cudaMemcpyDeviceToHost);
if (max_dst != nullptr)
cudaMemcpy(max_dst, &max_num, sizeof(T), cudaMemcpyHostToDevice);
return max_num;
}
else {
T max_num = dump_max(dump, (array_size + blockSize - 1) / blockSize);
if (max_dst != nullptr)
cudaMemcpy(max_dst, &max_num, sizeof(T), cudaMemcpyHostToDevice);
return max_num;
}
}
template<typename T>
T parallel_maxabs(const T* indata, T* dump, size_t array_size, T* max_dst = nullptr) {
constexpr int blockSize = 512;
size_t grid_dim, block_dim;
make_kernel_param(&grid_dim, &block_dim, array_size, blockSize);
block_maxabs_kernel << <grid_dim, block_dim >> > (indata, dump, array_size);
if (array_size <= blockSize) {
T max_num;
cudaMemcpy(&max_num, dump, sizeof(T), cudaMemcpyDeviceToHost);
if (max_dst != nullptr)
cudaMemcpy(max_dst, &max_num, sizeof(T), cudaMemcpyHostToDevice);
return max_num;
}
else {
T max_num = dump_max(dump, (array_size + blockSize - 1) / blockSize);
if (max_dst != nullptr)
cudaMemcpy(max_dst, &max_num, sizeof(T), cudaMemcpyHostToDevice);
return max_num;
}
}
template<typename T>
T parallel_min(const T* indata, T* dump, size_t array_size, T* min_dst = nullptr) {
constexpr int blockSize = 512;
size_t grid_dim, block_dim;
make_kernel_param(&grid_dim, &block_dim, array_size, blockSize);
block_min_kernel << <grid_dim, block_dim >> > (indata, dump, array_size);
if (array_size <= blockSize) {
T min_num;
cudaMemcpy(&min_num, dump, sizeof(T), cudaMemcpyDeviceToHost);
if (min_dst != nullptr)
cudaMemcpy(min_dst, &min_num, sizeof(T), cudaMemcpyHostToDevice);
return min_num;
}
else {
T min_num = dump_min(dump, (array_size + blockSize - 1) / blockSize);
if (min_dst != nullptr)
cudaMemcpy(min_dst, &min_num, sizeof(T), cudaMemcpyHostToDevice);
return min_num;
}
}
template<typename T>
T parallel_sum(const T* indata, T* dump, size_t array_size, T* sum_dst = nullptr) {
constexpr int blockSize = 512;
size_t grid_dim, block_dim;
make_kernel_param(&grid_dim, &block_dim, array_size, blockSize);
block_sum_kernel << <grid_dim, block_dim >> > (indata, dump, array_size);
if (array_size <= blockSize) {
T array_sum;
cudaMemcpy(&array_sum, dump, sizeof(T), cudaMemcpyDeviceToHost);
if (sum_dst != nullptr) {
cudaMemcpy(sum_dst, &array_sum, sizeof(T), cudaMemcpyHostToDevice);
}
return array_sum;
}
else {
T array_sum = dump_array_sum(dump, (array_size + blockSize - 1) / blockSize);
if (sum_dst != nullptr) {
cudaMemcpy(sum_dst, &array_sum, sizeof(T), cudaMemcpyHostToDevice);
}
return array_sum;
}
}
template<typename T, int blockSize = 512>
T dump_max(T* dump, size_t n) {
T max_num = -1e30;
if (n <= 1) {
cudaMemcpy(&max_num, dump, sizeof(T), cudaMemcpyDeviceToHost);
return max_num;
}
size_t grid_dim, block_dim;
do {
make_kernel_param(&grid_dim, &block_dim, n, blockSize);
block_max_kernel<T, blockSize> << <grid_dim, block_dim >> > (dump, dump, n);
//cudaDeviceSynchronize();
//cudaMemcpy(&max_num, dump, sizeof(T), cudaMemcpyDeviceToHost);
//std::cout << "current max num " << max_num << std::endl;
} while ((n = (n + blockSize - 1) / blockSize) > 1);
cudaMemcpy(&max_num, dump, sizeof(T), cudaMemcpyDeviceToHost);
return max_num;
}
template<typename T, int blockSize = 512>
T dump_min(T* dump, size_t n) {
T min_num = 1e30;
if (n <= 1) {
cudaMemcpy(&min_num, dump, sizeof(T), cudaMemcpyDeviceToHost);
return min_num;
}
size_t grid_dim, block_dim;
do {
make_kernel_param(&grid_dim, &block_dim, n, blockSize);
block_min_kernel<T, blockSize> << <grid_dim, block_dim >> > (dump, dump, n);
//cudaDeviceSynchronize();
//cudaMemcpy(&max_num, dump, sizeof(T), cudaMemcpyDeviceToHost);
//std::cout << "current max num " << max_num << std::endl;
} while ((n = (n + blockSize - 1) / blockSize) > 1);
cudaMemcpy(&min_num, dump, sizeof(T), cudaMemcpyDeviceToHost);
return min_num;
}
template<typename T>
void init_array(T* dev_array, T value, int array_size) {
size_t grid_dim;
size_t block_dim;
make_kernel_param(&grid_dim, &block_dim, array_size, 512);
init_array_kernel << <grid_dim, block_dim >> > (dev_array, value, array_size);
cudaDeviceSynchronize();
cuda_error_check;
}
};
/*
=========== define vector and expression class ===========
*/
namespace gv {
template<typename Scalar> class gVectorMap;
template<typename Scalar> class gVector;
template<typename Scalar> class gElementProxy;
template<typename > struct is_expression;
template<typename, typename> struct min_exp_t;
template<typename, typename> struct max_exp_t;
template<typename, typename, typename> struct concat_exp_t;
template<typename> struct sqrt_exp_t;
template<typename Scalar, typename opExp_t, bool transpose = false> struct dup_exp_t;
template<typename > struct abs_exp_t;
template<typename Scalar, typename opExp_t> struct slice_exp_t;
template<typename, typename > struct map_exp_t;
template<typename T = float, typename std::enable_if<std::is_scalar<T>::value, int >::type = 0> struct scalar_t;
template<typename Scalar, typename T = gVector<Scalar>, typename std::enable_if<std::is_same<T, gVector<Scalar>>::value, int>::type = 0> struct var_t;
template<typename, typename >struct dot_exp_t;
template<typename T>
class gVector {
public:
typedef T Scalar;
static gVector buf_vector;
private:
Scalar* _data = nullptr;
size_t _size = 0;
void build(size_t dim);
template<typename>friend class gVectorMap;
template<typename Lambda>friend void apply_vector(gVector& v1, const gVector& v2, Lambda func);
protected:
gVector(Scalar* data_ptr, size_t size) :_data(data_ptr), _size(size) {}
protected:
auto& _Get_data(void) { return _data; }
auto& _Get_size(void) { return _size; }
public:
Scalar*& data() { return _data; }
const Scalar* data() const { return _data; }
size_t size() const { return _size; }
void move(Scalar* data_ptr, size_t size) { _data = data_ptr; _size = size; }
void clear(void);
bool empty(void) const { return _size == 0; }
void swap(gVector& v2);
void set(Scalar val) { init_array(data(), val, size()); }
void set(int* filter, Scalar val);
void set(const Scalar* host_ptr);
template<typename expr_t, typename expr_repl,
typename std::enable_if<is_expression<expr_t>::value, int>::type = 0,
typename std::enable_if<is_expression<expr_repl>::value, int>::type = 0
>
void set_filter_value(const expr_t& exprfilter, const expr_repl& repl_val) {
size_t expr_dim = exprfilter.size();
if (expr_dim != _size) {
throw std::string("unmatched vector size !");
}
size_t grid_size, block_size;
make_kernel_param(&grid_size, &block_size, expr_dim, 512);
Scalar* pdata = _data;
expr_t filter = exprfilter;
expr_repl replval = repl_val;
replace_filter_kernel << <grid_size, block_size >> > (pdata, expr_dim, filter, replval);
cudaDeviceSynchronize();
cuda_error_check;
}
void lambda_test(void) {
size_t grid_size = 10, block_size = 512;
auto kernel = [=] __device__(int eid) {
//if (filter.eval(eid)) {
//pdata[eid] = replval.eval(eid);
//}
return 0;
};
gv::map << <grid_size, block_size >> > (_size, kernel);
cudaDeviceSynchronize();
cuda_error_check;
}
template<typename expr_t, typename S,
typename std::enable_if<is_expression<expr_t>::value, int>::type = 0,
typename std::enable_if<std::is_scalar_v<S>, int>::type = 0
>
void set(const expr_t& exprfilter, S repl_val) {
set_filter_value(exprfilter, scalar_t<S>(repl_val));
}
template<typename expr_t, typename S,
typename std::enable_if<is_expression<expr_t>::value, int>::type = 0,
typename std::enable_if<std::is_scalar_v<S>, int>::type = 0
>
void set(const expr_t& exprfilter, const gv::gVector<S>& repl_val) {
set_filter_value(exprfilter, var_t<S>(repl_val));
}
template<bool transpose = false>
dup_exp_t<Scalar, var_t<Scalar>> dup(int ndup, int nwordvalid, int nwordpitch) const;
void get(Scalar *host_ptr, size_t len, size_t offset = 0) const;
gVector(void) :_size(0), _data(nullptr) {}
//gVector(Scalar* host_ptr, size_t size);
gVector(const gVector& v);
//gVector(gVector&& v) noexcept {
// _data = v.data(); _size = v._size;
// v._data = nullptr; v._size = 0;
// std::cout << "Move constructor called" << std::endl;
//}
explicit gVector(size_t dim, Scalar default_value = 0) {
_size = dim;
cudaMalloc(&_data, _size * sizeof(Scalar));
init_array(_data, default_value, _size);
cuda_error_check;
}
virtual ~gVector(void) {
//cuda_error_check;
if (_data != nullptr) { cudaFree(_data); }
cuda_error_check;
}
public:
void resize(size_t dim) { build(dim); }
// for init, this version will not free buf
void resize(size_t dim, int) {
cudaMalloc(&_data, dim * sizeof(Scalar));
_size = dim;
}
public:
const gVector& operator=(const gVector& v2);
void download(Scalar* host_ptr) const;
const gVector& operator+=(const gVector& v2);
const gVector& operator-=(const gVector& v2);
const gVector& operator*=(const gVector& v2);
const gVector& operator/=(const gVector& v2);
const gVector& operator/=(Scalar s);
const gVector& operator*=(Scalar s);
template<typename expr_t, typename std::enable_if<is_expression<expr_t>::value, int>::type = 0 >
const gVector& operator+=(const expr_t& expr) {
size_t expr_dim = expr.size();
if (expr_dim != _size) {
throw std::string("unmatched vector size !");
}
((*this) + expr).launch(_data, expr_dim);
return *this;
}
template<typename expr_t, typename std::enable_if<is_expression<expr_t>::value, int>::type = 0 >
const gVector& operator-=(const expr_t& expr) {
size_t expr_dim = expr.size();
if (expr_dim != _size) {
throw std::string("unmatched vector size !");
}
((*this) - expr).launch(_data, expr_dim);
return *this;
}
template<typename expr_t, typename std::enable_if<is_expression<expr_t>::value, int>::type = 0 >
const gVector& operator/=(const expr_t& expr) {
size_t expr_dim = expr.size();
if (expr_dim != _size) {
throw std::string("unmatched vector size !");
}
((*this) / expr).launch(_data, expr_dim);
return *this;
}
template<typename expr_t, typename std::enable_if<is_expression<expr_t>::value, int>::type = 0 >
const gVector& operator*=(const expr_t& expr) {
size_t expr_dim = expr.size();
if (expr_dim != _size) {
throw std::string("unmatched vector size !");
}
((*this) * expr).launch(_data, expr_dim);
return *this;
}
template<typename expr_t, typename std::enable_if<is_expression<expr_t>::value, int>::type = 0 >
const gVector& operator=(const expr_t& expr) {
size_t expr_dim = expr.size();
if (expr_dim != size()) {
clear();
build(expr_dim);
}
expr.launch(_data, expr_dim);
return *this;
}
template<typename expr_t, typename std::enable_if<is_expression<expr_t>::value, int>::type = 0>
gVector(const expr_t& expr) {
size_t expr_dim = expr.size();
resize(expr_dim, 0);
expr.launch(_data, expr_dim);
}
template<typename expr_t>
const gVector& operator*=(const typename std::enable_if<expr_t::is_exp, expr_t>::type& expr) {
auto new_expr = (*this)*expr;
size_t expr_dim = expr.size();
new_expr.launch(_data, expr_dim);
}
template<typename expr_t>
const gVector& operator/=(const typename std::enable_if<expr_t::is_exp, expr_t>::type& expr) {
auto new_expr = (*this) / expr;
size_t expr_dim = expr.size();
new_expr.launch(_data, expr_dim);
}
template<typename expr_t>
const gVector& operator+=(const typename std::enable_if<expr_t::is_exp, expr_t>::type& expr) {
auto new_expr = (*this) + expr;
size_t expr_dim = expr.size();
new_expr.launch(_data, expr_dim);
}
template<typename expr_t>
const gVector& operator-=(const typename std::enable_if<expr_t::is_exp, expr_t>::type& expr) {
auto new_expr = (*this) - expr;
size_t expr_dim = expr.size();
new_expr.launch(_data, expr_dim);
}
//Scalar operator[](int eid) const;
gElementProxy<Scalar> operator[](int eid);
void invInPlace(void);
void maximize(Scalar s);
void maximize(const gVector& v2);
void minimize(Scalar s);
void minimize(const gVector& v2);
template<typename opExp_t, typename std::enable_if<is_expression<opExp_t>::value, int>::type = 0, typename vec_t = gVector>
min_exp_t<var_t<Scalar>, opExp_t> min(const opExp_t& op2) const {
return min_exp_t<var_t<Scalar>, opExp_t>(var_t<Scalar>(*this), op2);
}
template<typename opExp_t, typename std::enable_if<is_expression<opExp_t>::value, int>::type = 0, typename vec_t = gVector>
max_exp_t<var_t<Scalar>, opExp_t> max(const opExp_t& op2) const {
return max_exp_t<var_t<Scalar>, opExp_t>(var_t<Scalar>(*this), op2);
}
template<typename vec_t = gVector, typename Scalar_type = Scalar,
typename std::enable_if<std::is_scalar<Scalar_type>::value, int>::type = 0>
min_exp_t<var_t<Scalar>, scalar_t<Scalar_type>> min(Scalar_type op2) const {
return min_exp_t<var_t<Scalar>, scalar_t<Scalar_type>>(var_t<Scalar>(*this), op2);
}
template<typename vec_t = gVector, typename Scalar_type = Scalar,
typename std::enable_if<std::is_scalar<Scalar_type>::value, int>::type = 0>
max_exp_t<var_t<Scalar>, scalar_t<Scalar_type>> max(Scalar_type op2)const {
return max_exp_t<var_t<Scalar>, scalar_t<Scalar_type>>(var_t<Scalar>(*this), op2);
}
template<typename Lambda, typename vec_t = gVector>
map_exp_t<var_t<Scalar>, Lambda> fmap(Lambda func) {
return map_exp_t<var_t<Scalar>, Lambda>(var_t<Scalar>(*this), func);
}
Scalar max(void) const;
Scalar min(void) const;
Scalar min_positive(void) const;
Scalar norm(void) const {
return sqrt(dot(*this));
}
Scalar infnorm(void) const {
Scalar* pbuf = gVector<Scalar>::get_dump_buf(sizeof(Scalar) * (size() / 300 + 1));
return parallel_maxabs(_data, pbuf, size());
}
Scalar sqrnorm(void) const { return dot(*this); }
void Sqrt(void);
template<typename Lambda, typename vec_t = gVector>
void mapInplace(Lambda func) {
auto expr = fmap<Lambda, vec_t>(func);
expr.launch(_data, _size);
}
void clamp(Scalar lower, Scalar upper);
void clamp(Scalar* lower, Scalar* upper);
void clamp(gVector& vl, gVector& vu) { clamp(vl.data(), vu.data()); }
slice_exp_t<Scalar, var_t<Scalar>> slice(int start, int end) const;
//gVector concated_one(const gVector& v2) const;
//gVector concated_one(Scalar val) const;
//void concate_one(const gVector& v2);
//void concate_one(Scalar val);
//template<typename Arg0, typename... Args>
//gVector concated(Arg0 arg0, Args... args) {
// return concated_one(arg0).concated(args...);
//}
//template<typename Arg0, typename... Args>
//void concate(const Arg0& arg0, Args... args) {
// concate_one(arg0);
// concate(args...);
//}
//void concate(void) { return; }
//gVector concated(void) const { return *this; }
template<typename S, typename std::enable_if<std::is_scalar<S>::value, int>::type = 0>
concat_exp_t<Scalar, var_t<Scalar>, var_t<S>> concat(const gVector<S>& v) {
return concat_exp_t<Scalar, var_t<Scalar>, var_t<S>>(var_t<Scalar>(*this), var_t<S>(v));
}
template<typename S, typename std::enable_if<std::is_scalar<S>::value, int>::type = 0>
concat_exp_t<Scalar, var_t<Scalar>, scalar_t<S>> concat(S v) {
return concat_exp_t<Scalar, var_t<Scalar>, scalar_t<S>>(var_t<Scalar>(*this), scalar_t<S>(v));
}
template<typename opExp_t, typename std::enable_if<is_expression<opExp_t>::value, int>::type = 0>
concat_exp_t<Scalar, var_t<Scalar>, opExp_t> concat(const opExp_t& op) {
return concat_exp_t<Scalar, var_t<Scalar>, opExp_t>(var_t<Scalar>(*this), op);
}
template<typename S0, typename... S, typename std::enable_if < (sizeof...(S) >= 1), int>::type = 0 >
auto concat(const S0& op0, const S&... opex) {
return concat(op0).concat(opex...);
}
abs_exp_t<var_t<Scalar>> abs(void) {
return abs_exp_t<var_t<Scalar>>(var_t<Scalar>(*this));
}
std::vector<Scalar> slice2host(int start, int end) const;
static void Init(size_t max_vec_size = 0) {
if (std::is_same<Scalar, double>::value) {
cudaDeviceSetSharedMemConfig(cudaSharedMemBankSizeEightByte);
std::cout << "[gv] use 8b bank" << std::endl;
} else if (std::is_same<Scalar, float>::value) {
cudaDeviceSetSharedMemConfig(cudaSharedMemBankSizeFourByte);
std::cout << "[gv] use 4b bank" << std::endl;
}
if (max_vec_size > buf_vector.size()) {
buf_vector.resize(max_vec_size);
}
}
static Scalar* get_dump_buf(size_t size_len) {
if (size_len < 1024) size_len = 1024;
if (size_len < buf_vector.size() * sizeof(Scalar)) {
return buf_vector.data();
} else {
buf_vector.resize(round<sizeof(Scalar)>(size_len) / sizeof(Scalar));
return buf_vector.data();
}
}