-
Notifications
You must be signed in to change notification settings - Fork 0
/
matricesFloat.cu
274 lines (228 loc) · 7.52 KB
/
matricesFloat.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
// Unary operations
extern "C"
__global__ void transpose(int width, int height,
float* in, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
out[x + y * width] = in[y + x * height];
}
}
extern "C"
__global__ void flipX(int width, int height,
float* in, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
out[x + y * width] = in[(width - 1 - x) + y * width];
}
}
extern "C"
__global__ void flipY(int width, int height,
float* in, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
out[x + y * width] = in[x + (height - 1 - y) * width];
}
}
extern "C"
__global__ void rotate90(int width, int height,
float* in, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
out[x + y * width] = in[(height - 1 - y) + x * height];
}
}
extern "C"
__global__ void rotate180(int width, int height,
float* in, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
out[x + y * width] = in[(width - 1 - x) + (height - 1 - y) * width];
}
}
extern "C"
__global__ void rotate270(int width, int height,
float* in, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
out[x + y * width] = in[y + (width - 1 - x) * height];
}
}
extern "C"
__global__ void set(int width, int height, float value,
float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
out[x + y * width] = value;
}
}
extern "C"
__global__ void sum(int size, float* mtx) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
if (x < size) {
mtx[x] += mtx[x + size];
mtx[x + size] = 0;
}
}
// Binary operations
extern "C"
__global__ void tensorAdd(int width, int height,
float* in1, float* in2, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
int pos = x + y * width;
out[pos] = in1[pos] + in2[pos];
}
}
extern "C"
__global__ void tensorSub(int width, int height,
float* in1, float* in2, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
int pos = x + y * width;
out[pos] = in1[pos] - in2[pos];
}
}
extern "C"
__global__ void concatenateVertical(int width, int height1, int height2,
float* in1, float* in2, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height1 + height2) {
int pos = x + y * width;
if (y < height1) out[pos] = in1[pos];
else {
y -= height1;
out[pos] = in2[x + y * width];
}
}
}
extern "C"
__global__ void concatenateHorizontal(int width1, int width2, int height,
float* in1, float* in2, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width1 + width2 && y < height) {
int pos = x + y * (width1 + width2);
if (x < width1) out[pos] = in1[x + y * width1];
else {
x -= width1;
out[pos] = in2[x + y * width2];
}
}
}
extern "C"
__global__ void mul(int width, int height, float value,
float* in, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
int pos = x + y * width;
out[pos] = in[pos] * value;
}
}
extern "C"
__global__ void tensorMul(int width, int height,
float* in1, float* in2, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
int pos = x + y * width;
out[pos] = in1[pos] * in2[pos];
}
}
extern "C"
__global__ void matrixMul(int w1h2, int height1, int width2,
float* in1, float* in2, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width2 && y < height1) {
float sum = 0.0;
for (int i = 0; i < w1h2; i++)
sum += in1[i + y * w1h2] * in2[x + i * width2];
out[x + y * width2] = sum;
}
}
extern "C"
__global__ void tensorDiv(int width, int height,
float* in1, float* in2, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
int pos = x + y * width;
out[pos] = in1[pos] / in2[pos];
}
}
extern "C"
__global__ void matrixConvEmptyBorder(int width, int height, int mx, int my,
float* in, float* matrix, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
int hx = mx >> 1;
int hy = my >> 1;
float sum = 0;
for (int dy = 0; dy < mx; dy++) {
int py = y + dy - hy;
if (py < 0) continue;
if (py >= height) continue;
for (int dx = 0; dx < mx; dx++) {
int px = x + dx - hx;
if (px < 0) continue;
if (px >= width) continue;
sum += in[px + py * width] * matrix[dx + dy * mx];
}
}
out[x + y * width] = sum;
}
}
extern "C"
__global__ void matrixConvExtendBorder(int width, int height, int mx, int my,
float* in, float* matrix, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
int hx = mx >> 1;
int hy = my >> 1;
float sum = 0;
for (int dy = 0; dy < mx; dy++) {
int py = max(0, min(height - 1, y + dy - hy));
for (int dx = 0; dx < mx; dx++) {
int px = max(0, min(width - 1, x + dx - hx));
sum += in[px + py * width] * matrix[dx + dy * mx];
}
}
out[x + y * width] = sum;
}
}
extern "C"
__global__ void matrixConvRepeatBorder(int width, int height, int mx, int my,
float* in, float* matrix, float* out) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
int hx = mx >> 1;
int hy = my >> 1;
float sum = 0;
for (int dy = 0; dy < mx; dy++) {
int py = y + dy - hy;
if (py < 0) py += height;
if (py >= height) py -= height;
for (int dx = 0; dx < mx; dx++) {
int px = x + dx - hx;
if (px < 0) px += width;
if (px >= width) px -= width;
sum += in[px + py * width] * matrix[dx + dy * mx];
}
}
out[x + y * width] = sum;
}
}