-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cu
431 lines (355 loc) · 14.8 KB
/
matrix.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
#include "matrix.cuh"
#include <stdexcept>
#define THREAD_X 8
#define THREAD_Y 8
/**
* @brief The cuda kernel to add two 2D matrices.
*
* @tparam T, the type of value to retrieve.
*
* @param[in] width, the width of the two matrices.
* @param[in] height, the height of the two matrices.
* @param[in] m, the first matrix.
* @param[in] n, the second matrix.
* @param[out] output, 1D array containing the add result.
*
*/
template <typename T>
__global__ void addKernel(const int _width, const int _height, const T* _m, const T* _n, T* _output)
{
const auto col = threadIdx.x + blockIdx.x * blockDim.x;
const auto row = threadIdx.y + blockIdx.y * blockDim.y;
if (col >= _width || row >= _height) return;
const auto index = row * _width + col;
_output[index] = _m[index] + _n[index];
}
/**
* @brief Launch the cuda kernel to add two 2D matrices.
*
* @param[in] width, the width of the two matrices.
* @param[in] height, the height of the two matrices.
* @param[in] h_matrix1, the first matrix.
* @param[in] h_matrix2, the second matrix.
*
* @returns the result of the add inside 1D host vector.
*/
const thrust::host_vector<int> launchAdd(const int _width, const int _height, const int _width2, const int _height2, const thrust::host_vector<int>& h_matrix1, const thrust::host_vector<int>& h_matrix2)
{
if (_width != _width2 || _height != _height2) {
throw std::length_error("matrix1 width height != matrix2 width height");
}
else {
const dim3 blocks(_width / THREAD_X + 1, _height / THREAD_Y + 1);
const dim3 threads(THREAD_X, THREAD_Y);
const thrust::device_vector<int> d_matrix1 = h_matrix1;
const int* matrix1BufferArray = thrust::raw_pointer_cast(&d_matrix1[0]);
const thrust::device_vector<int> d_matrix2 = h_matrix2;
const int* matrix2BufferArray = thrust::raw_pointer_cast(&d_matrix2[0]);
thrust::device_vector<int> d_result(_width * _height);
int* resultBufferArray = thrust::raw_pointer_cast(&d_result[0]);
addKernel << <blocks, threads >> > (_width, _height, matrix1BufferArray, matrix2BufferArray, resultBufferArray);
cudaDeviceSynchronize();
const thrust::host_vector<int> h_result = d_result;
return h_result;
}
}
/**
* @brief The cuda kernel to minus two 2D matrices.
*
* @tparam T, the type of value to retrieve.
*
* @param[in] width, the width of the two matrices.
* @param[in] height, the height of the two matrices.
* @param[in] m, the first matrix.
* @param[in] n, the second matrix.
* @param[out] output, 1D array containing the minus result.
*
*/
template <typename T>
__global__ void minusKernel(const int _width, const int _height, const T* _m, const T* _n, T* _output)
{
const auto col = threadIdx.x + blockIdx.x * blockDim.x;
const auto row = threadIdx.y + blockIdx.y * blockDim.y;
if (col >= _width || row >= _height) return;
const auto index = row * _width + col;
_output[index] = _m[index] - _n[index];
}
/**
* @brief Launch the cuda kernel to minus two 2D matrices.
*
* @param[in] width, the width of the two matrices.
* @param[in] height, the height of the two matrices.
* @param[in] h_matrix1, the first matrix.
* @param[in] h_matrix2, the second matrix.
*
* @returns the result of the add inside 1D host vector.
*/
const thrust::host_vector<int> launchMinus(const int _width, const int _height, const int _width2, const int _height2, const thrust::host_vector<int>& h_matrix1, const thrust::host_vector<int>& h_matrix2)
{
if (_width != _width2 || _height != _height2) {
throw std::length_error("matrix1 width height != matrix2 width height");
}
else {
const dim3 blocks(_width / THREAD_X + 1, _height / THREAD_Y + 1);
const dim3 threads(THREAD_X, THREAD_Y);
const thrust::device_vector<int> d_matrix1 = h_matrix1;
const int* matrix1BufferArray = thrust::raw_pointer_cast(&d_matrix1[0]);
const thrust::device_vector<int> d_matrix2 = h_matrix2;
const int* matrix2BufferArray = thrust::raw_pointer_cast(&d_matrix2[0]);
thrust::device_vector<int> d_result(_width * _height);
int* resultBufferArray = thrust::raw_pointer_cast(&d_result[0]);
minusKernel << <blocks, threads >> > (_width, _height, matrix1BufferArray, matrix2BufferArray, resultBufferArray);
cudaDeviceSynchronize();
const thrust::host_vector<int> h_result = d_result;
return h_result;
}
}
/**
* @brief The cuda kernel to apply Hadamard operation to two 2D matrices.
*
* @tparam T, the type of value to retrieve.
*
* @param[in] width, the width of the two matrices.
* @param[in] height, the height of the two matrices.
* @param[in] m, the first matrix.
* @param[in] n, the second matrix.
* @param[out] output, 1D array containing the Hadamard result.
*
*/
template <typename T>
__global__ void hadamardKernel(const int _width, const int _height, const T* _m, const T* _n, T* _output)
{
const auto col = threadIdx.x + blockIdx.x * blockDim.x;
const auto row = threadIdx.y + blockIdx.y * blockDim.y;
if (col >= _width || row >= _height) return;
const auto index = row * _width + col;
_output[index] = _m[index] * _n[index];
}
/**
* @brief Launch the cuda kernel to apply the Hadamard product to two 2D matrices.
*
* @param[in] width, the width of the two matrices.
* @param[in] height, the height of the two matrices.
* @param[in] h_matrix1, the first matrix.
* @param[in] h_matrix2, the second matrix.
*
* @returns the result of the Hadamard product inside 1D host vector.
*/
const thrust::host_vector<int> launchHadamard(const int _width, const int _height, const int _width2, const int _height2, const thrust::host_vector<int>& h_matrix1, const thrust::host_vector<int>& h_matrix2)
{
if (_width != _width2 || _height != _height2) {
throw std::length_error("matrix1 width height != matrix2 width height");
}
else {
const dim3 blocks(_width / THREAD_X + 1, _height / THREAD_Y + 1);
const dim3 threads(THREAD_X, THREAD_Y);
const thrust::device_vector<int> d_matrix1 = h_matrix1;
const int* matrix1BufferArray = thrust::raw_pointer_cast(&d_matrix1[0]);
const thrust::device_vector<int> d_matrix2 = h_matrix2;
const int* matrix2BufferArray = thrust::raw_pointer_cast(&d_matrix2[0]);
thrust::device_vector<int> d_result(_width * _height);
int* resultBufferArray = thrust::raw_pointer_cast(&d_result[0]);
minusKernel << <blocks, threads >> > (_width, _height, matrix1BufferArray, matrix2BufferArray, resultBufferArray);
cudaDeviceSynchronize();
const thrust::host_vector<int> h_result = d_result;
return h_result;
}
}
/**
* @brief The cuda kernel to apply the multiplication of two matrices.
*
* @tparam T, the type of value to retrieve.
*
* @param[in] width, the width of the m matrix.
* @param[in] height, the height of the n matrix.
* @param[in] n, the first matrix.
* @param[in] m, the second matrix.
* @param[out] output, 1D array containing the multiplication result.
*
*/
template <typename T>
__global__ void multKernel(const int _width, const int _height, const T* _m, const T* _n, T* _output)
{
const auto col = threadIdx.x + blockIdx.x * blockDim.x;
const auto row = threadIdx.y + blockIdx.y * blockDim.y;
if (col >= _width || row >= _height) return;
T tmpSum = 0;
for (auto i = 0; i < _width; ++i) {
tmpSum += _m[row * _width + i] * _n[i * _width + col];
}
_output[row * _width + col] = tmpSum;
}
/**
* @brief Launch the cuda kernel to apply the multiplication of two matrices.
*
* @param[in] width, the width of the h_matrix1.
* @param[in] height, the height of the h_matrix1.
* @param[in] width2, the width of the h_matrix2.
* @param[in] height2, the height of the h_matrix2.
* @param[in] h_matrix1, the first matrix.
* @param[in] h_matrix2, the second matrix.
*
* @returns width, height and the result of the Hadamard product inside 1D host vector.
*/
const std::tuple<int, int, thrust::host_vector<int>> launchMultiplication(const int _width, const int _height, const int _width2, const int _height2, const thrust::host_vector<int>& h_matrix1, const thrust::host_vector<int>& h_matrix2)
{
if (_height != _width2) {
throw std::length_error("matrix1 height != matrix2 width");
}
else {
const dim3 blocks(_width2 / THREAD_X + 1, _height / THREAD_Y + 1);
const dim3 threads(THREAD_X, THREAD_Y);
const thrust::device_vector<int> d_matrix1 = h_matrix1;
const int* matrix1BufferArray = thrust::raw_pointer_cast(&d_matrix1[0]);
const thrust::device_vector<int> d_matrix2 = h_matrix2;
const int* matrix2BufferArray = thrust::raw_pointer_cast(&d_matrix2[0]);
thrust::device_vector<int> d_result(_width2 * _height);
int* resultBufferArray = thrust::raw_pointer_cast(&d_result[0]);
minusKernel << <blocks, threads >> > (_width2, _height, matrix1BufferArray, matrix2BufferArray, resultBufferArray);
cudaDeviceSynchronize();
const thrust::host_vector<int> h_result = d_result;
return std::make_tuple(_width2, _height, h_result);
}
}
/**
* @brief The cuda kernel to transpose the matrix.
*
* @tparam T, the type of value to retrieve.
*
* @param[in] width, the width of the m matrix.
* @param[in] height, the height of the m matrix.
* @param[in] m, the matrix.
* @param[out] output, 1D array containing the result.
*
*/
template <typename T>
__global__ void transposeKernel(const int _width, const int _height, const T* _m, T* _output)
{
const auto col = threadIdx.x + blockIdx.x * blockDim.x;
const auto row = threadIdx.y + blockIdx.y * blockDim.y;
if (col >= _width || row >= _height) return;
_output[row + _height * col] = _m[col + _width * row];
}
/**
* @brief Launch the cuda kernel to transpose the matrix.
*
* @param[in] width, the width of the m matrix.
* @param[in] height, the height of the m matrix.
* @param[in] m, the matrix.
*
* @returns the result of the transposed matrix inside a 1D host vector.
*/
const std::tuple<int, int, thrust::host_vector<int>> launchTransposition(const int _width, const int _height, const thrust::host_vector<int>& h_matrix)
{
const dim3 blocks(_width / THREAD_X + 1, _height / THREAD_Y + 1);
const dim3 threads(THREAD_X, THREAD_Y);
const thrust::device_vector<int> d_matrix1 = h_matrix;
const int* matrixBufferArray = thrust::raw_pointer_cast(&d_matrix1[0]);
thrust::device_vector<int> d_result(_width * _height);
int* resultBufferArray = thrust::raw_pointer_cast(&d_result[0]);
transposeKernel << <blocks, threads >> > (_width, _height, matrixBufferArray, resultBufferArray);
cudaDeviceSynchronize();
const thrust::host_vector<int> h_result = d_result;
return std::make_tuple(_height, _width, h_result);;
}
/**
* @brief The cuda kernel to rotate at 90° the matrix.
*
* @tparam T, the type of value to retrieve.
*
* @param[in] size, the width of the m matrix.
* @param[in] m, the matrix.
* @param[out] output, 1D array containing the result.
*
*/
template <typename T>
__global__ void rotate90MatrixKernel(const int _size, const T* _m, T* _output)
{
const auto layer = threadIdx.x + blockIdx.x * blockDim.x;
const auto index = threadIdx.y + blockIdx.y * blockDim.y;
const auto first = layer;
const auto last = _size - 1 - layer;
if (layer >= _size / 2 || index >= last) return;
const auto offset = index - first;
//Left -> top
_output[first * _size + index] = _m[(last - offset) * _size + first];
//Bottom -> left
_output[(last - offset) * _size + first] = _m[last * _size + last - offset];
//Right -> bottom
_output[last * _size + last - offset] = _m[index * _size + last];
//Top -> right
_output[index * _size + last] = _m[first * _size + index];
}
/**
* @brief Launch the cuda kernel to rotate the matrix.
*
* @param[in] width, the width of the m matrix.
* @param[in] height, the height of the m matrix.
* @param[in] m, the matrix.
*
* @returns the result of the rotated matrix inside a 1D host vector.
*/
const thrust::host_vector<int> launchRotation90Dgr(const int _width, const int _height, const thrust::host_vector<int>& h_matrix)
{
if (_width != _height){
throw std::length_error(" _width != _height");
}
else
{
const dim3 blocks(_width / THREAD_X + 1, _height / THREAD_Y + 1);
const dim3 threads(THREAD_X, THREAD_Y);
const thrust::device_vector<int> d_matrix1 = h_matrix;
const int* matrixBufferArray = thrust::raw_pointer_cast(&d_matrix1[0]);
thrust::device_vector<int> d_result(_width * _height);
int* resultBufferArray = thrust::raw_pointer_cast(&d_result[0]);
rotate90MatrixKernel << <blocks, threads >> > (_width, matrixBufferArray, resultBufferArray);
cudaDeviceSynchronize();
const thrust::host_vector<int> h_result = d_result;
return h_result;
}
}
/**
* @brief The cuda kernel to upper triangular the matrix.
*
* @tparam T, the type of value to retrieve.
*
* @param[in] size, the width of the m matrix.
* @param[out] m, the matrix result.
*
*/
template <typename T>
__global__ void upperTriangularMatrixKernel(const int _size, T* _m)
{
const auto col = threadIdx.x + blockIdx.x * blockDim.x;
const auto row = threadIdx.y + blockIdx.y * blockDim.y;
if (col >= _size - 1 || row <= row + 1 || row >= _size) return;
T temp = _m[row * _size + col] / _m[col * _size + col];
for (auto k = 0; k < _size + 1; ++k) {
_m[row * _size + k] -= (_m[col * _size + k] * temp);
}
}
/**
* @brief Launch the cuda kernel to upper triangular the matrix.
*
* @param[in] width, the width of the m matrix.
* @param[in] height, the height of the m matrix.
* @param[out] h_matrix, the matrix.
*
* @returns the result of the rotated matrix inside a 1D host vector.
*/
const thrust::host_vector<int> launchUpperTriangular(const int _width, const int _height, const thrust::host_vector<int>& h_matrix)
{
if (_width != _height){
throw std::length_error(" _width != _height");
}
else {
const dim3 blocks(_width / THREAD_X + 1, _height / THREAD_Y + 1);
const dim3 threads(THREAD_X, THREAD_Y);
thrust::device_vector<int> d_matrix1 = h_matrix;
int* matrixBufferArray = thrust::raw_pointer_cast(&d_matrix1[0]);
upperTriangularMatrixKernel << <blocks, threads >> > (_width, matrixBufferArray);
cudaDeviceSynchronize();
const thrust::host_vector<int> h_result = d_matrix1;
return h_result;
}
}