Repository navigation
Expand file tree
/
Copy pathSystemML.cu
More file actions
845 lines (767 loc) · 29.9 KB
/
Copy pathSystemML.cu
File metadata and controls
845 lines (767 loc) · 29.9 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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**********************************
When updating a kernel or adding a new one,
please compile the ptx file and commit it:
nvcc -ptx -arch=sm_30 SystemML.cu
***********************************/
#include <cfloat>
/**
* Does a copy of upper to lower triangle of the given matrix
* @param ret the input and output array allocated on the GPU
* @param dim the number of rows of the square matrix ret
* @param N total number of elements of the matrix
*/
extern "C"
__global__ void copy_u2l_dense(double* ret, int dim, int N) {
int ix = blockIdx.x * blockDim.x + threadIdx.x;
int iy = blockIdx.y * blockDim.y + threadIdx.y;
int id_dest = iy * dim + ix;
if(iy > ix && id_dest < N) {
// TODO: Potential to reduce the number of threads by half
int id_src = ix * dim + iy;
ret[id_dest] = ret[id_src];
}
}
extern "C"
__forceinline__ __device__ double getBoolean(int val) {
if(val == 0)
return 0.0;
else
return 1.0;
}
// op = {0=plus, 1=minus, 2=multiply, 3=divide, 4=power,
// 5=less, 6=lessequal, 7=greater, 8=greaterequal, 9=equal, 10=notequal,
// 11=min, 12=max, 13=and, 14=or, 15=log}
extern "C"
__forceinline__ __device__ double binaryOp(double x, double y, int op) {
switch(op) {
case 0 : return x + y;
case 1 : return x - y;
case 2 : return x * y;
case 3 : return x / y;
case 4 : return pow(x, y);
case 5 : return getBoolean(x < y);
case 6 : return getBoolean(x <= y);
case 7 : return getBoolean(x > y);
case 8 : return getBoolean(x >= y);
case 9 : return getBoolean(x == y);
case 10 : return getBoolean(x != y);
case 11 : return min(x, y);
case 12 : return max(x, y);
default : return DBL_MAX;
}
}
extern "C"
__global__ void relu(double* A, double* ret, int rlen, int clen) {
int ix = blockIdx.x * blockDim.x + threadIdx.x;
int iy = blockIdx.y * blockDim.y + threadIdx.y;
if(ix < rlen && iy < clen) {
int index = ix * clen + iy;
ret[index] = max(0.0, A[index]);
}
}
// This method computes the backpropagation errors for previous layer of relu operation
extern "C"
__global__ void relu_backward(double* X, double* dout, double* ret, int rlen, int clen) {
int ix = blockIdx.x * blockDim.x + threadIdx.x;
int iy = blockIdx.y * blockDim.y + threadIdx.y;
if(ix < rlen && iy < clen) {
int index = ix * clen + iy;
ret[index] = X[index] > 0 ? dout[index] : 0;
}
}
// Performs the operation corresponding to the DML script:
// ones = matrix(1, rows=1, cols=Hout*Wout)
// output = input + matrix(bias %*% ones, rows=1, cols=F*Hout*Wout)
// This operation is often followed by conv2d and hence we have introduced bias_add(input, bias) built-in function
extern "C"
__global__ void bias_add(double* input, double* bias, double* ret, int rlen, int clen, int PQ) {
int ix = blockIdx.x * blockDim.x + threadIdx.x;
int iy = blockIdx.y * blockDim.y + threadIdx.y;
if(ix < rlen && iy < clen) {
int index = ix * clen + iy;
int biasIndex = iy / PQ;
ret[index] = input[index] + bias[biasIndex];
}
}
// Performs the operation "ret <- A + alpha*B", where B is a vector
extern "C"
__global__ void daxpy_matrix_vector(double* A, double* B, double alpha, double* ret, int rlenA, int clenA, int rlenB, int clenB) {
int ix = blockIdx.x * blockDim.x + threadIdx.x;
int iy = blockIdx.y * blockDim.y + threadIdx.y;
if(ix < rlenA && iy < clenA) {
int index = ix * clenA + iy;
if(rlenB == 1) {
ret[index] = A[index] + alpha*B[iy];
}
else {
ret[index] = A[index] + alpha*B[ix];
}
}
}
// Performs similar operation as bias_add except elementwise multiplication instead of add
extern "C"
__global__ void bias_multiply(double* input, double* bias, double* ret, int rlen, int clen, int PQ) {
int ix = blockIdx.x * blockDim.x + threadIdx.x;
int iy = blockIdx.y * blockDim.y + threadIdx.y;
if(ix < rlen && iy < clen) {
int index = ix * clen + iy;
int biasIndex = iy / PQ;
ret[index] = input[index] * bias[biasIndex];
}
}
// Compares the value and set
extern "C"
__global__ void compare_and_set(double* A, double* ret, int rlen, int clen, double compareVal, double tol, double ifEqualsVal, double ifLessThanVal, double ifGreaterThanVal) {
int ix = blockIdx.x * blockDim.x + threadIdx.x;
int iy = blockIdx.y * blockDim.y + threadIdx.y;
int index = ix * clen + iy;
if(ix < rlen && iy < clen) {
if(abs(A[index]-compareVal) < tol)
ret[index] = ifEqualsVal;
else if(A[index] < compareVal)
ret[index] = ifLessThanVal;
else
ret[index] = ifGreaterThanVal;
}
}
/**
* Performs a binary cellwise arithmetic operation on 2 matrices.
* Either both matrices are of equal size or one of them is a vector or both are.
* @param A first input matrix allocated on GPU
* @param B second input matrix allocated on GPU
* @param C output allocated on GPU
* @param maxRlen maximum of the row lengths of A and B
* @param maxClen maximum of the column lengths of A and B
* @param vectorAStatus if A is a row vector, column vector or neither
* @param vectorBStatus if B is a row vector, column vector or neither
* @param op the numeric code of the arithmetic operation to perform
*
*/
extern "C"
__global__ void matrix_matrix_cellwise_op(double* A, double* B, double* C,
int maxRlen, int maxClen, int vectorAStatus, int vectorBStatus, int op) {
int ix = blockIdx.x * blockDim.x + threadIdx.x;
int iy = blockIdx.y * blockDim.y + threadIdx.y;
if(ix < maxRlen && iy < maxClen) {
int outIndex = ix * maxClen + iy;
int aIndex = outIndex;
int bIndex = outIndex;
if(vectorAStatus == 1)
aIndex = ix; // clen == 1
else if(vectorAStatus == 2)
aIndex = iy; // rlen == 1
if(vectorBStatus == 1)
bIndex = ix; // clen == 1
else if(vectorBStatus == 2)
bIndex = iy; // rlen == 1
C[outIndex] = binaryOp(A[aIndex], B[bIndex], op);
//printf("C[%d] = A[%d](%f) B[%d](%f) (%d %d)\n", outIndex, aIndex, A[aIndex], bIndex, B[bIndex], (ix+1), (iy+1));
__syncthreads();
}
}
/**
* Performs an arithmetic operation between a matrix and a scalar.
* C = s op A or C = A op s (where A is the matrix, s is the scalar and op is the operation)
* @param A input matrix allocated on GPU
* @param scalar scalar input
* @param C output matrix allocated on GPU
* @param size number of elements in matrix A
* @param op number code of the arithmetic operation to perform
* @param isLeftScalar whether the scalar is on the left side
*/
extern "C"
__global__ void matrix_scalar_op(double* A, double scalar, double* C, int size, int op, int isLeftScalar) {
int index = blockIdx.x *blockDim.x + threadIdx.x;
if(index < size) {
if(isLeftScalar) {
C[index] = binaryOp(scalar, A[index], op);
} else {
C[index] = binaryOp(A[index], scalar, op);
}
}
__syncthreads();
}
/**
* Sets all elements (fills) of a double array of given length with a given scalar value
* @param A array to be filled
* @param scalar value to fill array with
* @param lenA length of array A
*/
extern "C"
__global__ void fill(double* A, double scalar, int lenA) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < lenA){
A[index] = scalar;
}
}
/**
* Does a reduce operation over all elements of the array.
* This method has been adapted from the Reduction sample in the NVIDIA CUDA Samples (v8.0)
* and the Reduction example available through jcuda.org
* When invoked initially, all blocks partly compute the reduction operation over the entire array
* and writes it to the output/temporary array. A second invokation needs to happen to get the
* reduced value.
* The number of threads, blocks and amount of shared memory is calculated in a specific way.
* Please refer to the NVIDIA CUDA Sample or the SystemML code that invokes this method to see
* how its done.
* The template-ized version of this function is similar to what is found in NVIDIA CUB
*
* @param ReductionOp Type of the functor object that implements the reduction operation
*/
template <typename ReductionOp>
__device__ void reduce(
double *g_idata, ///< input data stored in device memory (of size n)
double *g_odata, ///< output/temporary array stored in device memory (of size n)
unsigned int n, ///< size of the input and temporary/output arrays
ReductionOp reduction_op, ///< Reduction operation to perform (functor object)
double initialValue) ///< initial value for the reduction variable
{
extern __shared__ double sdata[];
// perform first level of reduction,
// reading from global memory, writing to shared memory
unsigned int tid = threadIdx.x;
unsigned int i = blockIdx.x*blockDim.x*2 + threadIdx.x;
unsigned int gridSize = blockDim.x*2*gridDim.x;
double v = initialValue;
// we reduce multiple elements per thread. The number is determined by the
// number of active thread blocks (via gridDim). More blocks will result
// in a larger gridSize and therefore fewer elements per thread
while (i < n)
{
v = reduction_op(v, g_idata[i]);
// ensure we don't read out of bounds
if (i + blockDim.x < n)
v = reduction_op(v, g_idata[i+blockDim.x]);
i += gridSize;
}
// each thread puts its local sum into shared memory
sdata[tid] = v;
__syncthreads();
// do reduction in shared mem
if (blockDim.x >= 1024){ if (tid < 512) { sdata[tid] = v = reduction_op(v, sdata[tid + 512]); } __syncthreads(); }
if (blockDim.x >= 512) { if (tid < 256) { sdata[tid] = v = reduction_op(v, sdata[tid + 256]); } __syncthreads(); }
if (blockDim.x >= 256) { if (tid < 128) { sdata[tid] = v = reduction_op(v, sdata[tid + 128]); } __syncthreads(); }
if (blockDim.x >= 128) { if (tid < 64) { sdata[tid] = v = reduction_op(v, sdata[tid + 64]); } __syncthreads(); }
if (tid < 32)
{
// now that we are using warp-synchronous programming (below)
// we need to declare our shared memory volatile so that the compiler
// doesn't reorder stores to it and induce incorrect behavior.
volatile double* smem = sdata;
if (blockDim.x >= 64) { smem[tid] = v = reduction_op(v, smem[tid + 32]); }
if (blockDim.x >= 32) { smem[tid] = v = reduction_op(v, smem[tid + 16]); }
if (blockDim.x >= 16) { smem[tid] = v = reduction_op(v, smem[tid + 8]); }
if (blockDim.x >= 8) { smem[tid] = v = reduction_op(v, smem[tid + 4]); }
if (blockDim.x >= 4) { smem[tid] = v = reduction_op(v, smem[tid + 2]); }
if (blockDim.x >= 2) { smem[tid] = v = reduction_op(v, smem[tid + 1]); }
}
// write result for this block to global mem
if (tid == 0)
g_odata[blockIdx.x] = sdata[0];
}
/**
* Does a reduce (sum) over each row of the array.
* This kernel must be launched with as many blocks as there are rows.
* The intuition for this kernel is that each block does a reduction over a single row.
* The maximum number of blocks that can launched (as of compute capability 3.0) is 2^31 - 1
* This works out fine for SystemML, since the maximum elements in a Java array can be 2^31 - c (some small constant)
* If the matrix is "fat" and "short", i.e. there are small number of rows and a large number of columns,
* there could be under-utilization of the hardware.
* The template-ized version of this function is similar to what is found in NVIDIA CUB
* @param ReductionOp Type of the functor object that implements the reduction operation
* @param AssignmentOp Type of the functor object that is used to modify the value before writing it to its final location in global memory for each row
*/
template <typename ReductionOp,
typename AssignmentOp>
__device__ void reduce_row(
double *g_idata, ///< input data stored in device memory (of size rows*cols)
double *g_odata, ///< output/temporary array store in device memory (of size rows*cols)
unsigned int rows, ///< rows in input and temporary/output arrays
unsigned int cols, ///< columns in input and temporary/output arrays
ReductionOp reduction_op, ///< Reduction operation to perform (functor object)
AssignmentOp assignment_op, ///< Operation to perform before assigning this to its final location in global memory for each row
double initialValue){ ///< initial value for the reduction variable
extern __shared__ double sdata[];
// one block per row
if (blockIdx.x >= rows) {
return;
}
unsigned int block = blockIdx.x;
unsigned int tid = threadIdx.x;
unsigned int i = tid;
unsigned int block_offset = block * cols;
double v = initialValue;
while (i < cols){
v = reduction_op(v, g_idata[block_offset + i]);
i += blockDim.x;
}
// each thread puts its local sum into shared memory
sdata[tid] = v;
__syncthreads();
// do reduction in shared mem
if (blockDim.x >= 1024){ if (tid < 512) { sdata[tid] = v = reduction_op(v, sdata[tid + 512]); } __syncthreads(); }
if (blockDim.x >= 512) { if (tid < 256) { sdata[tid] = v = reduction_op(v, sdata[tid + 256]); } __syncthreads(); }
if (blockDim.x >= 256) { if (tid < 128) { sdata[tid] = v = reduction_op(v, sdata[tid + 128]); } __syncthreads(); }
if (blockDim.x >= 128) { if (tid < 64) { sdata[tid] = v = reduction_op(v, sdata[tid + 64]); } __syncthreads(); }
if (tid < 32)
{
// now that we are using warp-synchronous programming (below)
// we need to declare our shared memory volatile so that the compiler
// doesn't reorder stores to it and induce incorrect behavior.
volatile double* smem = sdata;
if (blockDim.x >= 64) { smem[tid] = v = reduction_op(v, smem[tid + 32]); }
if (blockDim.x >= 32) { smem[tid] = v = reduction_op(v, smem[tid + 16]); }
if (blockDim.x >= 16) { smem[tid] = v = reduction_op(v, smem[tid + 8]); }
if (blockDim.x >= 8) { smem[tid] = v = reduction_op(v, smem[tid + 4]); }
if (blockDim.x >= 4) { smem[tid] = v = reduction_op(v, smem[tid + 2]); }
if (blockDim.x >= 2) { smem[tid] = v = reduction_op(v, smem[tid + 1]); }
}
// write result for this block to global mem, modify it with assignment op
if (tid == 0)
g_odata[block] = assignment_op(sdata[0]);
}
/**
* Does a column wise reduction.
* The intuition is that there are as many global threads as there are columns
* Each global thread is responsible for a single element in the output vector
* This of course leads to a under-utilization of the GPU resources.
* For cases, where the number of columns is small, there can be unused SMs
*
* The template-ized version of this function is similar to what is found in NVIDIA CUB
* @param ReductionOp Type of the functor object that implements the reduction operation
* @param AssignmentOp Type of the functor object that is used to modify the value before writing it to its final location in global memory for each column
*/
template <typename ReductionOp,
typename AssignmentOp>
__device__ void reduce_col(
double *g_idata, ///< input data stored in device memory (of size rows*cols)
double *g_odata, ///< output/temporary array store in device memory (of size rows*cols)
unsigned int rows, ///< rows in input and temporary/output arrays
unsigned int cols, ///< columns in input and temporary/output arrays
ReductionOp reduction_op, ///< Reduction operation to perform (functor object)
AssignmentOp assignment_op, ///< Operation to perform before assigning this to its final location in global memory for each column
double initialValue) ///< initial value for the reduction variable
{
unsigned int global_tid = blockIdx.x * blockDim.x + threadIdx.x;
if (global_tid >= cols) {
return;
}
unsigned int i = global_tid;
unsigned int grid_size = cols;
double val = initialValue;
while (i < rows * cols) {
val = reduction_op(val, g_idata[i]);
i += grid_size;
}
g_odata[global_tid] = assignment_op(val);
}
/**
* Functor op for assignment op. This is a dummy/identity op.
*/
typedef struct {
__device__ __forceinline__
double operator()(double a) const {
return a;
}
} IdentityOp;
/**
* Functor op for summation operation
*/
typedef struct {
__device__ __forceinline__
double operator()(double a, double b) const {
return a + b;
}
} SumOp;
/**
* Do a summation over all elements of an array/matrix
* @param g_idata input data stored in device memory (of size n)
* @param g_odata output/temporary array stored in device memory (of size n)
* @param n size of the input and temporary/output arrays
*/
extern "C"
__global__ void reduce_sum(double *g_idata, double *g_odata, unsigned int n){
SumOp op;
reduce<SumOp>(g_idata, g_odata, n, op, 0.0);
}
/**
* Do a summation over all rows of a matrix
* @param g_idata input matrix stored in device memory (of size rows * cols)
* @param g_odata output vector stored in device memory (of size rows)
* @param rows number of rows in input matrix
* @param cols number of columns in input matrix
*/
extern "C"
__global__ void reduce_row_sum(double *g_idata, double *g_odata, unsigned int rows, unsigned int cols){
SumOp op;
IdentityOp aop;
reduce_row<SumOp, IdentityOp>(g_idata, g_odata, rows, cols, op, aop, 0.0);
}
/**
* Do a summation over all columns of a matrix
* @param g_idata input matrix stored in device memory (of size rows * cols)
* @param g_odata output vector stored in device memory (of size cols)
* @param rows number of rows in input matrix
* @param cols number of columns in input matrix
*/
extern "C"
__global__ void reduce_col_sum(double *g_idata, double *g_odata, unsigned int rows, unsigned int cols){
SumOp op;
IdentityOp aop;
reduce_col<SumOp, IdentityOp>(g_idata, g_odata, rows, cols, op, aop, 0.0);
}
/**
* Functor op for max operation
*/
typedef struct {
__device__ __forceinline__
double operator()(double a, double b) const {
return fmax(a, b);
}
} MaxOp;
/**
* Do a max over all elements of an array/matrix
* @param g_idata input data stored in device memory (of size n)
* @param g_odata output/temporary array stode in device memory (of size n)
* @param n size of the input and temporary/output arrays
*/
extern "C"
__global__ void reduce_max(double *g_idata, double *g_odata, unsigned int n){
MaxOp op;
reduce<MaxOp>(g_idata, g_odata, n, op, -DBL_MAX);
}
/**
* Do a max over all rows of a matrix
* @param g_idata input matrix stored in device memory (of size rows * cols)
* @param g_odata output vector stored in device memory (of size rows)
* @param rows number of rows in input matrix
* @param cols number of columns in input matrix
*/
extern "C"
__global__ void reduce_row_max(double *g_idata, double *g_odata, unsigned int rows, unsigned int cols){
MaxOp op;
IdentityOp aop;
reduce_row<MaxOp, IdentityOp>(g_idata, g_odata, rows, cols, op, aop, -DBL_MAX);
}
/**
* Do a max over all columns of a matrix
* @param g_idata input matrix stored in device memory (of size rows * cols)
* @param g_odata output vector stored in device memory (of size cols)
* @param rows number of rows in input matrix
* @param cols number of columns in input matrix
*/
extern "C"
__global__ void reduce_col_max(double *g_idata, double *g_odata, unsigned int rows, unsigned int cols){
MaxOp op;
IdentityOp aop;
reduce_col<MaxOp, IdentityOp>(g_idata, g_odata, rows, cols, op, aop, -DBL_MAX);
}
/**
* Functor op for min operation
*/
typedef struct {
__device__ __forceinline__
double operator()(double a, double b) const {
return fmin(a, b);
}
} MinOp;
/**
* Do a min over all elements of an array/matrix
* @param g_idata input data stored in device memory (of size n)
* @param g_odata output/temporary array stode in device memory (of size n)
* @param n size of the input and temporary/output arrays
*/
extern "C"
__global__ void reduce_min(double *g_idata, double *g_odata, unsigned int n){
MinOp op;
reduce<MinOp>(g_idata, g_odata, n, op, DBL_MAX);
}
/**
* Do a min over all rows of a matrix
* @param g_idata input matrix stored in device memory (of size rows * cols)
* @param g_odata output vector stored in device memory (of size rows)
* @param rows number of rows in input matrix
* @param cols number of columns in input matrix
*/
extern "C"
__global__ void reduce_row_min(double *g_idata, double *g_odata, unsigned int rows, unsigned int cols){
MinOp op;
IdentityOp aop;
reduce_row<MinOp, IdentityOp>(g_idata, g_odata, rows, cols, op, aop, DBL_MAX);
}
/**
* Do a min over all columns of a matrix
* @param g_idata input matrix stored in device memory (of size rows * cols)
* @param g_odata output vector stored in device memory (of size cols)
* @param rows number of rows in input matrix
* @param cols number of columns in input matrix
*/
extern "C"
__global__ void reduce_col_min(double *g_idata, double *g_odata, unsigned int rows, unsigned int cols){
MinOp op;
IdentityOp aop;
reduce_col<MinOp>(g_idata, g_odata, rows, cols, op, aop, DBL_MAX);
}
/**
* Functor op for product operation
*/
typedef struct {
__device__ __forceinline__
double operator()(double a, double b) const {
return a * b;
}
} ProductOp;
/**
* Do a product over all elements of an array/matrix
* @param g_idata input data stored in device memory (of size n)
* @param g_odata output/temporary array stode in device memory (of size n)
* @param n size of the input and temporary/output arrays
*/
extern "C"
__global__ void reduce_prod(double *g_idata, double *g_odata, unsigned int n){
ProductOp op;
reduce<ProductOp>(g_idata, g_odata, n, op, 1.0);
}
/**
* Functor op for mean operation
*/
struct MeanOp {
const long _size; ///< Number of elements by which to divide to calculate mean
__device__ __forceinline__
MeanOp(long size): _size(size) {}
__device__ __forceinline__
double operator()(double total) const {
return total / _size;
}
};
/**
* Do a mean over all rows of a matrix
* @param g_idata input matrix stored in device memory (of size rows * cols)
* @param g_odata output vector stored in device memory (of size rows)
* @param rows number of rows in input matrix
* @param cols number of columns in input matrix
*/
extern "C"
__global__ void reduce_row_mean(double *g_idata, double *g_odata, unsigned int rows, unsigned int cols){
SumOp op;
MeanOp aop(cols);
reduce_row<SumOp, MeanOp>(g_idata, g_odata, rows, cols, op, aop, 0.0);
}
/**
* Do a mean over all columns of a matrix
* @param g_idata input matrix stored in device memory (of size rows * cols)
* @param g_odata output vector stored in device memory (of size cols)
* @param rows number of rows in input matrix
* @param cols number of columns in input matrix
*/
extern "C"
__global__ void reduce_col_mean(double *g_idata, double *g_odata, unsigned int rows, unsigned int cols){
SumOp op;
MeanOp aop(rows);
reduce_col<SumOp, MeanOp>(g_idata, g_odata, rows, cols, op, aop, 0.0);
}
/**
* Do an exp over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_exp(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = exp(A[index]);
}
}
/**
* Do an sqrt over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_sqrt(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = sqrt(A[index]);
}
}
/**
* Do an round over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_round(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = (double)llround(A[index]);
}
}
/**
* Do an abs over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_abs(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = (double)fabs(A[index]);
}
}
/**
* Do an log over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_log(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = log(A[index]);
}
}
/**
* Do an floor over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_floor(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = floor(A[index]);
}
}
/**
* Do an ceil over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_ceil(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = ceil(A[index]);
}
}
/**
* Do an sin over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_sin(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = sin(A[index]);
}
}
/**
* Do an cos over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_cos(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = cos(A[index]);
}
}
/**
* Do an tan over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_tan(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = tan(A[index]);
}
}
/**
* Do an asin over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_asin(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = asin(A[index]);
}
}
/**
* Do an acos over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_acos(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = acos(A[index]);
}
}
/**
* Do an atan over all the elements of a matrix
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_atan(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
C[index] = atan(A[index]);
}
}
/**
* Do an sign over all the elements of a matrix
* Assign -1, 0 or 1 depending on the element being negative, 0 or positive
* @param A the input matrix (of length = size)
* @param C the pre-allocated output matrix (of length = size)
* @param siz the length of the input and output matrices
*/
extern "C"
__global__ void matrix_sign(double *A, double *C, unsigned int size) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size){
if (A[index] == 0.0) {
C[index] = 0.0;
} else {
C[index] = copysign(1.0, A[index]);
}
}
}