-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_defined_kernels_fp32.cu
976 lines (758 loc) · 37.5 KB
/
user_defined_kernels_fp32.cu
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
// System includes
#include <stdio.h>
#include <assert.h>
#include <math.h>
// CUDA runtime
#include <cuda_runtime.h>
// Helper functions and utilities to work with CUDA
#include <helper_functions.h>
#include <helper_cuda.h>
// CUDA's Complex number support header
#include <cuComplex.h>
// Thrust includes
#include <thrust/sort.h>
#include <thrust/device_vector.h>
void sort_on_device_fp32(float* h_vec, int width) {
// wrap raw pointer with a device_ptr
thrust::device_ptr<float> d_vec = thrust::device_pointer_cast(h_vec);
// sort data on the device
thrust::sort(d_vec, d_vec + width);
}
/*void sort_on_device(thrust::host_vector<int>& h_vec)
{
// transfer data to the device
thrust::device_vector<int> d_vec = h_vec;
// sort data on the device
thrust::sort(d_vec.begin(), d_vec.end());
// transfer data back to host
thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());
}*/
/*
This kernel locates the index of all the non-zero elements and returns their count.
*/
__global__ void countNonZeroElements_fp32(float *idata, int *count, int width, int height)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
if (idata[global_1D_index] != 0.0) { atomicAdd(count, 1); }
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel extracts the indices of all the non-zero elements.
*/
__global__ void filter_k_fp32(float *dst, int *nres, float *src, int width, int height) {
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height && src[global_1D_index] != 0.0) {
dst[atomicAdd(nres, 1)] = global_1D_index;
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel maps indices with non-zero elements to the output matrix and the rest as -1.
*/
__global__ void mapNonZeroIndices_fp32(float *idata, float *odata, int width, int height)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
int isZero = (idata[global_1D_index] == 0.0); // Converting two-way if-block to one liner.
odata[global_1D_index] = global_1D_index * (!isZero) // If the value at this index is non-zero, the index will get mapped.
+ (-1) * isZero; // Else, the mapped index will be -1, which indicates its invalidity.
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel handles complex matrix construction from given float matrices.
*/
__global__ void complexMatrixConstruction_fp32(float *idata1, float *idata2, cuComplex *odata, int width, int height)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
odata[global_1D_index] = make_cuComplex(idata1[global_1D_index], idata2[global_1D_index]);
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel handles matrix element-wise ASMD (Add, Subtract, Multiply or Divide) operations.
0 = Addition
1 = Subtraction
2 = Multiplication
3 = Division
*/
__global__ void matrixElementWiseASMD_fp32(float const *idata1, float const *idata2, float *odata, int width, int height,
int operationChoice)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Depending on the operation choice, it either adds or subtracts
if (operationChoice == 0)
odata[global_1D_index] = idata1[global_1D_index] + idata2[global_1D_index];
else if (operationChoice == 1)
odata[global_1D_index] = idata1[global_1D_index] - idata2[global_1D_index];
else if (operationChoice == 2)
odata[global_1D_index] = idata1[global_1D_index] * idata2[global_1D_index];
else if (operationChoice == 3)
odata[global_1D_index] = idata1[global_1D_index] / idata2[global_1D_index];
}
}
/*********************************************************************************************************************/
/*====================================================================================================================*/
/*
This kernel handles matrix element-wise ASMD (Add, Subtract, Multiply or Divide) operations when the latter of the
matrices is a single row matrix.
0 = Addition
1 = Subtraction
2 = Multiplication
3 = Division
*/
__global__ void matrixElementWiseASMDForSingleRow_fp32(float const *idata1, float const *idata2, float *odata, int width, int height,
int operationChoice)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Depending on the operation choice, it either adds or subtracts
// The latter of the matrices only has a single row. Hence, (global_2D_y * width) = 0
if (operationChoice == 0)
odata[global_1D_index] = idata1[global_1D_index] + idata2[global_2D_x];
else if (operationChoice == 1)
odata[global_1D_index] = idata1[global_1D_index] - idata2[global_2D_x];
else if (operationChoice == 2)
odata[global_1D_index] = idata1[global_1D_index] * idata2[global_2D_x];
else if (operationChoice == 3)
odata[global_1D_index] = idata1[global_1D_index] / idata2[global_2D_x];
}
}
/*********************************************************************************************************************/
/*====================================================================================================================*/
/*
This kernel handles COMPLEX matrix element-wise ASMD (Add, Subtract, Multiply or Divide) operations.
0 = Addition
1 = Subtraction
2 = Multiplication
3 = Division
*/
__global__ void matrixComplexElementWiseASMD_fp32(cuComplex const *idata1, cuComplex const *idata2, cuComplex *odata,
int width, int height, int operationChoice)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Depending on the operation choice, it either adds or subtracts
if (operationChoice == 0)
odata[global_1D_index] = cuCaddf(idata1[global_1D_index], idata2[global_1D_index]);
else if (operationChoice == 1)
odata[global_1D_index] = cuCsubf(idata1[global_1D_index], idata2[global_1D_index]);
else if (operationChoice == 2)
odata[global_1D_index] = cuCmulf(idata1[global_1D_index], idata2[global_1D_index]);
else if (operationChoice == 3)
odata[global_1D_index] = cuCdivf(idata1[global_1D_index], idata2[global_1D_index]);
}
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*
This kernel handles matrix element-wise scalar ASMD (Add, Subtract, Multiply or Divide) operations.
For example, adding a constant to all the elements of a matrix.
0 = Addition
1 = Subtraction
2 = Multiplication
3 = Division
Addition and Multiplication are commutative operations.
For Subtraction and Division in reverse order, commutative property does not hold.
Hence, the kernel provides for an order_of_operation argument to choose which value comes first.
For order_of_operation = 0, the corresponding matrix element comes first followed by the constant/scalar.
For order_of_operation = 1, the constant/scalar comes first followed by the matrix element.
*/
__global__ void matrixElementWiseScalarASMD_fp32(float *idata, float constant, float *odata, int width, int height,
int choice_of_operation, int order_of_operation)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Depending on the operation choice, it either adds or subtracts.
// In case I wonder later, this won't lead to thread divergence because all the threads
// will go to a single branch of the following control flow block during runtime.
if (choice_of_operation == 0)
odata[global_1D_index] = idata[global_1D_index] + constant;
else if (choice_of_operation == 1 && order_of_operation == 0) // Subtraction in regular order
odata[global_1D_index] = idata[global_1D_index] - constant;
else if (choice_of_operation == 1 && order_of_operation == 1) // Subtraction in reversed order
odata[global_1D_index] = constant - idata[global_1D_index];
else if (choice_of_operation == 2)
odata[global_1D_index] = idata[global_1D_index] * constant;
else if (choice_of_operation == 3 && order_of_operation == 0) // Division in regular order
odata[global_1D_index] = idata[global_1D_index] / constant;
else if (choice_of_operation == 3 && order_of_operation == 1) // Division in reversed order
odata[global_1D_index] = constant / idata[global_1D_index];
}
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*
This kernel handles complex matrix element-wise scalar ASMD (Add, Subtract, Multiply or Divide) operations.
For example, adding a float scalar or complex scalar to all the elements of a matrix.
0 = Addition
1 = Subtraction
2 = Multiplication
3 = Division
Addition and Multiplication are commutative operations.
For Subtraction and Division in reverse order, commutative property does not hold.
Hence, the kernel provides for an order_of_operation argument to choose which value comes first.
For order_of_operation = 0, the corresponding matrix element comes first followed by the scalar/scalar.
For order_of_operation = 1, the scalar comes first followed by the matrix element.
*/
__global__ void matrixComplexElementWiseScalarASMD_fp32(cuComplex *idata, float scalar, cuComplex *odata,
int width, int height, int choice_of_operation, int order_of_operation)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Depending on the operation choice, it either adds or subtracts.
// will go to a single branch of the following control flow block during runtime.
if (choice_of_operation == 0)
odata[global_1D_index] = cuCaddf(idata[global_1D_index], make_cuComplex(scalar, 0));
else if (choice_of_operation == 1 && order_of_operation == 0) // Subtraction in regular order
odata[global_1D_index] = cuCsubf(idata[global_1D_index], make_cuComplex(scalar, 0));
else if (choice_of_operation == 1 && order_of_operation == 1) // Subtraction in reversed order
odata[global_1D_index] = cuCsubf(make_cuComplex(scalar, 0), idata[global_1D_index]);
else if (choice_of_operation == 2)
odata[global_1D_index] = cuCmulf(idata[global_1D_index], make_cuComplex(scalar, 0));
else if (choice_of_operation == 3 && order_of_operation == 0) // Division in regular order
odata[global_1D_index] = cuCdivf(idata[global_1D_index], make_cuComplex(scalar, 0));
else if (choice_of_operation == 3 && order_of_operation == 1) // Division in reversed order
odata[global_1D_index] = cuCdivf(make_cuComplex(scalar, 0), idata[global_1D_index]);
}
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*
This kernel handles matrix element-wise operations such as Numpy's sin and cos.
0 = Real part extraction
1 = Imaginary part extraction
2 = Absolute value for the given element
*/
__global__ void matrixElementWiseSinOrCosOrAbs_fp32(float *idata, float *odata,
int width, int height, int operationChoice, int use_intrinsics)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Depending on the operation choice, it either adds or subtracts
if (operationChoice == 0) // Sin operation
odata[global_1D_index] = sin(idata[global_1D_index]);
else if (operationChoice == 1) // Cos operation
odata[global_1D_index] = cos(idata[global_1D_index]);
else if (operationChoice == 2)
odata[global_1D_index] = fabs(idata[global_1D_index]);
}
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*
This kernel computes angles from given eK and fK matrices.
*/
__global__ void matrixElementWiseAngles_fp32(float *eK, float *fK, float *odata,
int width, int height)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Depending on the operation choice, it either adds or subtracts
odata[global_1D_index] = atan2(fK[global_1D_index], eK[global_1D_index]) * 180 / 3.1415926536;
}
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*
This kernel handles COMPLEX matrix to float32 element-wise operations (Numpy's real, imag, abs).
0 = Real part extraction
1 = Imaginary part extraction
2 = Absolute value for each element
3 = Sign of the Absolute Value for each element
*/
__global__ void matrixComplexElementWiseExtractions_fp32(cuComplex *idata1, float *odata,
int width, int height, int operationChoice)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Depending on the operation choice, it either adds or subtracts
if (operationChoice == 0)
odata[global_1D_index] = cuCrealf(idata1[global_1D_index]);
else if (operationChoice == 1)
odata[global_1D_index] = cuCimagf(idata1[global_1D_index]);
else if (operationChoice == 2)
odata[global_1D_index] = cuCabsf(idata1[global_1D_index]);
else if (operationChoice == 3)
odata[global_1D_index] = 1 * (cuCabsf(idata1[global_1D_index]) > 0);
}
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*
This kernel handles COMPLEX matrix conjugate operations.
*/
__global__ void matrixComplexConjugate_fp32(cuComplex *idata1, cuComplex *odata,
int width, int height)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
odata[global_1D_index] = cuConjf(idata1[global_1D_index]);
}
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*
This kernel handles on-device matrix typecasting from float to complex!
*/
__global__ void floatToComplex(float *idata, cuComplex *odata, int width, int height)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
odata[global_1D_index] = make_cuComplex(idata[global_1D_index], 0);
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
// General purpose slicing kernel.
__global__ void slice_fp32(float *input,
float *output,
const int row_start,
const int row_end,
const int column_start,
const int column_end,
const int input_matrix_width,
const int output_matrix_width)
{
int global_index_X = blockIdx.x * blockDim.x + threadIdx.x;
int global_index_Y = blockIdx.y * blockDim.y + threadIdx.y;
int row = global_index_Y;
int column = global_index_X;
if (row >= row_start && row < row_end && column >= column_start && column < column_end) {
output[(global_index_X - column_start) + (global_index_Y - row_start) * output_matrix_width]
= input[global_index_X + global_index_Y * input_matrix_width];
}
}
// General purpose complex64 slicing kernel.
__global__ void sliceComplex64(cuComplex *input,
cuComplex *output,
const int row_start,
const int row_end,
const int column_start,
const int column_end,
const int input_matrix_width,
const int output_matrix_width)
{
int global_index_X = blockIdx.x * blockDim.x + threadIdx.x;
int global_index_Y = blockIdx.y * blockDim.y + threadIdx.y;
int row = global_index_Y;
int column = global_index_X;
if (row >= row_start && row < row_end && column >= column_start && column < column_end) {
output[(global_index_X - column_start) + (global_index_Y - row_start) * output_matrix_width]
= input[global_index_X + global_index_Y * input_matrix_width];
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
// This kernel can be used to perform slicing of matrices with the help of given 1D indices matrix.
__global__ void slice_with_indices_fp32(float *input,
float *output,
float *indices,
const int input_matrix_height,
const int input_matrix_width,
const int indices_matrix_length)
{
int global_index_X = blockIdx.x * blockDim.x + threadIdx.x;
int global_index_Y = blockIdx.y * blockDim.y + threadIdx.y;
if (global_index_X < indices_matrix_length && global_index_Y < 1) {
int value_at_current_index_in_indices = (int)indices[global_index_X];
output[global_index_X] = input[value_at_current_index_in_indices];
/*for (int i = 0; i < input_matrix_width; i++) {
float value_at_current_index_in_input = input[i + value_at_current_index_in_indices * input_matrix_width];
output[i + global_index_X * input_matrix_width] = value_at_current_index_in_input;
}*/
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel performs special cutting of the R matrix.
*/
__global__ void specialSlicingOnR_kernel_fp32(float *input,
float *output,
float *indices,
int input_matrix_height,
int input_matrix_width,
int indices_matrix_length)
{
int global_index_X = blockIdx.x * blockDim.x + threadIdx.x;
int global_index_Y = blockIdx.y * blockDim.y + threadIdx.y;
if (global_index_X < indices_matrix_length && global_index_Y < 1) {
int value_at_current_index_in_indices = (int)indices[global_index_X];
float value_at_current_index_in_input = pow(input[value_at_current_index_in_indices], 2);
output[global_index_X] = value_at_current_index_in_input;
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel performs special cutting of the H matrix.
*/
__global__ void specialSlicingOnH_kernel_fp32(float *input,
float *output,
float *indices,
int input_matrix_height,
int input_matrix_width,
int indices_matrix_length,
int output_matrix_width)
{
int global_index_X = blockIdx.x * blockDim.x + threadIdx.x;
int global_index_Y = blockIdx.y * blockDim.y + threadIdx.y;
if (global_index_X < indices_matrix_length && global_index_Y < 1) {
int value_at_current_index_in_indices = (int)indices[global_index_X];
for (int i = 0; i < input_matrix_width; i++) {
float value_at_current_index_in_input = input[i + value_at_current_index_in_indices * input_matrix_width];
output[i + global_index_X * input_matrix_width] = value_at_current_index_in_input;
}
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
__global__ void matrix_insert_fp32(float *input,
float *matrix_to_insert,
const int row_start,
const int row_end,
const int column_start,
const int column_end,
const int input_matrix_height,
const int input_matrix_width)
{
int global_index_X = blockIdx.x * blockDim.x + threadIdx.x;
int global_index_Y = blockIdx.y * blockDim.y + threadIdx.y;
int row = global_index_Y;
int column = global_index_X;
int matrix_to_insert_width = column_end - column_start;
// Attempting replacement of IF-condition
/*int value_to_insert = matrix_to_insert[(global_index_X - row_start) + (global_index_Y - column_start) * matrix_to_insert_width];
int original_value = input[global_index_X + global_index_Y * input_matrix_width];
input[global_index_X + global_index_Y * input_matrix_width] = (value_to_insert * insert_or_not) + (original_value * (!insert_or_not));*/
if (row >= row_start && row < row_end && column >= column_start && column < column_end) {
float value_to_insert = matrix_to_insert[(global_index_X - column_start) + (global_index_Y - row_start) * matrix_to_insert_width];
input[global_index_X + global_index_Y * input_matrix_width] = value_to_insert;
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
__global__ void transposeNaive_fp32(float *odata, float* idata, int width, int height)
{
int xIndex = blockIdx.x * blockDim.x + threadIdx.x;
int yIndex = blockIdx.y * blockDim.y + threadIdx.y;
if (xIndex >= width || yIndex >= height)
return;
int index_in = xIndex + width * yIndex;
int index_out = yIndex + height * xIndex;
odata[index_out] = idata[index_in];
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel handles matrix concatenation (horizontal or vertical) operations.
operationChoice:
0 = Horizontal concatenation
1 = Vertical concatenation
*/
__global__ void matrixConcatenate_fp32(float *idata1, float *idata2, float *odata, int width, int height,
int width1, int height1, int width2, int operationChoice)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the target matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
if (operationChoice == 0) {
if (global_2D_x < width1) {
// Computing 1D index for the first matrix
int idata1_1D_index = global_2D_x + global_2D_y * width1;
// Storing the first matrix's elements into the concatenated matrix
odata[global_1D_index] = idata1[idata1_1D_index];
}
else {
// Calculating global 1D index for accessing the second matrix in row-major fashion
int idata2_1D_index = (global_2D_x - width1) + global_2D_y * width2;
// Storing the first matrix's elements into the concatenated matrix
odata[global_1D_index] = idata2[idata2_1D_index];
}
}
else {
if (global_2D_y < height1) {
// Computing 1D index for the first matrix
int idata1_1D_index = global_2D_x + global_2D_y * width1;
// Storing the first matrix's elements into the concatenated matrix
odata[global_1D_index] = idata1[idata1_1D_index];
}
else {
// Calculating global 1D index for accessing the second matrix in row-major fashion
int idata2_1D_index = global_2D_x + (global_2D_y - height1) * width2;
// Storing the first matrix's elements into the concatenated matrix
odata[global_1D_index] = idata2[idata2_1D_index];
}
}
}
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*
This kernel handles matrix concatenation (horizontal or vertical) operations.
operationChoice:
0 = Horizontal concatenation
1 = Vertical concatenation
*/
__global__ void matrixConcatenateComplex_fp32(cuComplex *idata1, cuComplex *idata2, cuComplex *odata,
int width, int height, int width1, int height1, int width2, int operationChoice)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the target matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
if (operationChoice == 0) {
if (global_2D_x < width1) {
// Computing 1D index for the first matrix
int idata1_1D_index = global_2D_x + global_2D_y * width1;
// Storing the first matrix's elements into the concatenated matrix
odata[global_1D_index] = idata1[idata1_1D_index];
}
else {
// Calculating global 1D index for accessing the second matrix in row-major fashion
int idata2_1D_index = (global_2D_x - width1) + global_2D_y * width2;
// Storing the first matrix's elements into the concatenated matrix
odata[global_1D_index] = idata2[idata2_1D_index];
}
}
else {
if (global_2D_y < height1) {
// Computing 1D index for the first matrix
int idata1_1D_index = global_2D_x + global_2D_y * width1;
// Storing the first matrix's elements into the concatenated matrix
odata[global_1D_index] = idata1[idata1_1D_index];
}
else {
// Calculating global 1D index for accessing the second matrix in row-major fashion
int idata2_1D_index = global_2D_x + (global_2D_y - height1) * width2;
// Storing the first matrix's elements into the concatenated matrix
odata[global_1D_index] = idata2[idata2_1D_index];
}
}
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel handles converting a empty matrix(with only zeros) to a
diagonal-flattened matrix using given input matrix.
*/
__global__ void matrixDiagflat_fp32(float *idata, float *odata, int width, int height)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Insert elements from input matrix at diagonal positions
odata[global_1D_index] = idata[global_2D_x] * (global_2D_x == global_2D_y);
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
__global__ void matrixDiagflatWithPower_fp32(float *idata, float *odata, int power, int width, int height)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Insert elements from input matrix at diagonal positions
odata[global_1D_index] = pow(idata[global_2D_x], power) * (global_2D_x == global_2D_y);
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
__global__ void matrixDiagflatComplex_fp32(cuComplex *idata, cuComplex *odata, int width, int height)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Insert elements from input matrix at diagonal positions
if (global_2D_x == global_2D_y)
odata[global_1D_index] = idata[global_2D_x];
else
odata[global_1D_index] = make_cuComplex(0.0f, 0.0f); // Not setting this will lead to undefined behavior
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel handles matrix element-wise sign operation, identical to Numpy.sign()
*/
__global__ void matrixElementWiseSign_fp32(float *idata, float *odata, int width, int height)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Depending on the operation choice, it either adds or subtracts.
// In case I wonder later, this will certainly lead to branch divergence. Better logic awaited!
if (idata[global_1D_index] == 0)
odata[global_1D_index] = 0;
else if (idata[global_1D_index] > 0)
odata[global_1D_index] = 1;
else if (idata[global_1D_index] < 0)
odata[global_1D_index] = -1;
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel handles creating identify, ones or zeros matrix (like numpy.eye, numpy.ones, numpy.zeros).
0 = Identity Matrix
1 = Ones
2 = Zeros
*/
__global__ void matrixEyeOrOnesOrZeros_fp32(float *odata, int width, int height, int operationChoice)
{
// Calculating global 2D indices
int global_2D_x = blockDim.x * blockIdx.x + threadIdx.x;
int global_2D_y = blockDim.y * blockIdx.y + threadIdx.y;
// Processing only those threads which are within matrix dimensions
if (global_2D_x < width && global_2D_y < height) {
// Calculating global 1D index for accessing the matrix in row-major fashion
int global_1D_index = global_2D_x + global_2D_y * width;
// Depending on the operation choice, it either adds or subtracts
if (operationChoice == 0)
odata[global_1D_index] = (global_2D_x == global_2D_y); // <-- Gives 1 if i=j, else 0
else if (operationChoice == 1)
odata[global_1D_index] = 1;
else if (operationChoice == 2)
odata[global_1D_index] = 0; // <-- Leaving this step would work too as the elements are automatically initialized to 0.
// Actually about the comment above, NO! It turns out setting the memory location values is left to the programmer and no values are assigned by default.
}
}
/*====================================================================================================================*/
/*====================================================================================================================*/
/*
This kernel is used to find the maximum/minimum value in a given matrix using shared memory.
The reduction is done in two stages.
Once, all the maximum/minimum values from each block is gathered into a single block.
Then, this single block obtained above is processed to find the maximum/minimum.
Setting is_max decides the max/min configuration.
Our implementation is limited to finding the maximum only.
*/
__global__ void shmem_min_max_reduce_kernel_fp32(float * d_out, const float * const d_in, int elements)
{
int globalX = threadIdx.x + blockDim.x * blockIdx.x;
int tid = threadIdx.x;
// sdata is allocated in the kernel call: 3rd arg to <<<b, t, shmem>>>
extern __shared__ float sdata[];
sdata[tid] = 1E-37; // 1
// load shared mem from global mem
if (globalX < elements)
sdata[tid] = d_in[globalX];
__syncthreads(); // make sure entire block is loaded!
// do reduction in shared mem
for (unsigned int s = blockDim.x / 2; s > 0; s >>= 1)
{
if (tid < s && globalX < elements)
{
sdata[tid] = max(sdata[tid], sdata[tid + s]);
}
__syncthreads(); // make sure all adds at one stage are done!
}
// only thread 0 writes result for this block back to global mem
if (tid == 0)
{
d_out[blockIdx.x] = sdata[0];
}
}