-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.cu
201 lines (163 loc) · 6.61 KB
/
functions.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
#include"kernels.cu"
#include"matrix.cpp"
#ifndef FUNCTIONS
#define FUNCTIONS
void cuda_matmul(matrix *mat1,matrix *mat2,matrix *mat3,bool update = false) {
if(mat1 -> width != mat2 -> height) {
printf("Multiply Dim mismatch %d %d\n",mat1 -> height,mat2 -> width);
return;
}
if(!mat1 -> cudaMat) mat1 -> storeCuda();
if(!mat2 -> cudaMat) mat2 -> storeCuda();
if(!mat3 -> mat) mat3 -> init(mat1 -> height,mat2 -> width);
if(!mat3 -> cudaMat) mat3 -> storeCuda();
gaxpy_kernel<<<mat1->height,mat2->width>>>(mat1->width,mat1->cudaMat,mat2->cudaMat,mat3->cudaMat);
if(update) mat3 -> updateCuda();
mat3 -> isUpdated = update;
}
void cuda_transpose(matrix *mat1,matrix *tr_mat,bool update = false) {
if(!mat1 -> cudaMat) mat1 -> storeCuda();
if(!tr_mat-> mat) tr_mat -> init(mat1 -> width,mat1 -> height);
if(!tr_mat -> cudaMat) tr_mat -> storeCuda();
transpose_kernel<<<mat1->height,mat1->width>>>(mat1 -> cudaMat,tr_mat -> cudaMat);
if(update) tr_mat -> updateCuda();
tr_mat -> isUpdated = update;
}
void cuda_hadamard(matrix *mat1,matrix *mat2,matrix *mat3,bool update = false) {
if(mat1 -> width != mat2 -> width || mat1->height != mat2->height) {
printf("Hadamard Dim mismatch %d %d %d %d\n",mat1 -> height,mat1 -> width,
mat2->height,mat2->width);
return;
}
if(!mat1 -> cudaMat) mat1 -> storeCuda();
if(!mat2 -> cudaMat) mat2 -> storeCuda();
if(!mat3 -> mat) mat3 -> init(mat1 -> height,mat2 -> width);
if(!mat3 -> cudaMat) mat3 -> storeCuda();
hadamard_kernel<<<mat1->height,mat1->width>>>
(mat1->cudaMat,mat2->cudaMat,mat3->cudaMat);
if(update) mat3 -> updateCuda();
mat3 -> isUpdated = update;
}
//matrix multiplication with a bias
void cuda_matmul(matrix *mat1,matrix *mat2,matrix *bias,matrix *mat3,bool update = false) {
if(mat1 -> width != mat2 -> height) {
printf("Multiply Dim mismatch %d %d\n",mat1 -> height,mat2 -> width);
return;
}
if(bias -> mat && (bias -> height == mat1 -> height && bias -> width == mat2 -> width) ||
(bias -> height == mat1 -> height && bias -> width == 1) ||
(bias -> width == mat2 -> width && bias -> height == 1)) {
if(!mat1 -> cudaMat) mat1 -> storeCuda();
if(!mat2 -> cudaMat) mat2 -> storeCuda();
if(!bias -> cudaMat) bias -> storeCuda();
if(!mat3 -> mat) mat3 -> init(mat1 -> height,mat2 -> width);
if(!mat3 -> cudaMat) mat3 -> storeCuda();
gaxpy_kernel<<<mat1->height,mat2->width>>>
(mat1->width,mat1->cudaMat,mat2->cudaMat,mat3->cudaMat,
bias->cudaMat,bias->height,bias->width);
if(update) mat3 -> updateCuda();
mat3 -> isUpdated = update;
}
else
printf("Dimension mismatch bias %d %d \n ",bias -> height,bias -> width);
}
void cuda_vecMSE(matrix *mat1,matrix *mat2,matrix *mat3,bool update = false) {
if(mat1->height != mat2 -> height && mat1 -> width != mat2 -> width) {
printf("MSE Dim mismatch %d %d %d %d\n",mat1 -> height,mat1 -> width,
mat2->height,mat2->width);
}
if(!mat1 -> cudaMat) mat1 -> storeCuda();
if(!mat2 -> cudaMat) mat2 -> storeCuda();
if(!mat3 -> mat) mat3 -> init(mat1 -> height,mat2 -> width);
if(!mat3 -> cudaMat) mat3 -> storeCuda();
saxpy_kernel<<<mat1->height,mat1->width>>>
(mat1->cudaMat,mat2->cudaMat,mat3->cudaMat,1);
hadamard_kernel<<<mat3->height,mat1->width>>>
(mat3->cudaMat,mat3->cudaMat,mat3->cudaMat);
if(update) mat3 -> updateCuda();
mat3 -> isUpdated = update;
}
void cuda_vecDiff(matrix *mat1,matrix *mat2,matrix *mat3,bool update = false) {
if(mat1->height != mat2 -> height && mat1 -> width != mat2 -> width) {
printf("MSE Dim mismatch %d %d %d %d\n",mat1 -> height,mat1 -> width,
mat2->height,mat2->width);
}
if(!mat1 -> cudaMat) mat1 -> storeCuda();
if(!mat2 -> cudaMat) mat2 -> storeCuda();
if(!mat3 -> mat) mat3 -> init(mat1 -> height,mat1 -> width);
if(!mat3 -> cudaMat) mat3 -> storeCuda();
operate_kernel<<<mat3->height,mat3->width>>>
(mat2->cudaMat,mat3->cudaMat,-1.0,OP_MUL);
saxpy_kernel<<<mat3->height,mat3->width>>>
(mat1->cudaMat,mat3->cudaMat,mat3->cudaMat,1);
if(update) mat3 -> updateCuda();
mat3 -> isUpdated = update;
}
void cuda_vecADD(matrix *mat1,matrix *mat2,matrix *mat3,bool update = false) {
if(mat1->height != mat2 -> height && mat1 -> width != mat2 -> width) {
printf("MSE Dim mismatch %d %d %d %d\n",mat1 -> height,mat1 -> width,
mat2->height,mat2->width);
}
if(!mat1 -> cudaMat) mat1 -> storeCuda();
if(!mat2 -> cudaMat) mat2 -> storeCuda();
if(!mat3 -> mat) mat3 -> init(mat1 -> height,mat1 -> width);
if(!mat3 -> cudaMat) mat3 -> storeCuda();
saxpy_kernel<<<mat3->height,mat3->width>>>
(mat1->cudaMat,mat2->cudaMat,mat3->cudaMat,1);
if(update) mat3 -> updateCuda();
mat3 -> isUpdated = update;
}
void cuda_function(matrix *mat1,matrix *mat2,int fn,bool update = false) {
if(!mat1 -> cudaMat) mat1 -> storeCuda();
if(!mat2 -> mat) mat2 -> init(mat1 -> height, mat1 -> width);
if(!mat2 -> cudaMat) mat2 -> storeCuda();
function_kernel<<<mat1 -> height,mat1 -> width>>>
(mat1 -> cudaMat,mat2 -> cudaMat,fn);
if(update) mat2 -> updateCuda();
mat2 -> isUpdated = update;
}
void cuda_operation(matrix *mat1,matrix *mat2,double a,int op,bool update = false) {
if(!mat1 -> cudaMat) mat1 -> storeCuda();
if(!mat2 -> mat) mat2 -> init(mat1 -> height, mat1 -> width);
if(!mat2 -> cudaMat) mat2 -> storeCuda();
operate_kernel<<<mat1 -> height,mat1 -> width>>>
(mat1 -> cudaMat,mat2 -> cudaMat,a,op);
if(update) mat2 -> updateCuda();
mat2 -> isUpdated = update;
}
void cuda_reduce(matrix *mat1,matrix *res,int op,int axis,bool update = false) {
if(!mat1 -> cudaMat) mat1 -> storeCuda();
if(axis == 3) {
if(!res -> mat) res -> init(1,1);
else if (res -> height != 1 || res -> width != 1) {
printf("Reduction result not match dimension %d %d",res -> height,res ->width);
return;
}
if(!res -> cudaMat) res -> storeCuda();
reduction_kernel<<<1,mat1->height,mat1->height>>>
(mat1 -> cudaMat,res -> cudaMat,op,mat1->width,axis);
}
else if(axis == 2) {
if(!res -> mat) res -> init(mat1 -> height,1);
else if(res -> height != mat1 -> height || res -> width != 1) {
printf("Reduction result not match dimension %d %d",res -> height,res ->width);
return;
}
if(!res -> cudaMat) res -> storeCuda();
reduction_kernel<<<1,mat1->height>>>
(mat1 -> cudaMat,res -> cudaMat,op,mat1->width,axis);
}
else if(axis == 1) {
if(!res -> mat) res -> init(1,mat1 -> width);
else if(res -> height != 1 || res -> width != mat1 -> width) {
printf("Reduction result not match dimension %d %d",res -> height,res ->width);
return;
}
if(!res -> cudaMat) res -> storeCuda();
reduction_kernel<<<1,mat1->width>>>
(mat1 -> cudaMat,res -> cudaMat,op,mat1->height,axis);
}
if(update) res -> updateCuda();
res -> isUpdated = update;
}
#endif