-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.h
115 lines (100 loc) · 1.95 KB
/
matrix.h
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
#ifndef __CUDA_TASK_MATRIX_H__
#define __CUDA_TASK_MATRIX_H__
#include <random>
/*
class MatrixIn;
class MatrixOut;
typedef CUDATask<MatrixIn, MatrixOut> CudaMMTask;
*/
template<typename T>
class MatrixIn
{
public:
MatrixIn()
{
this->a = nullptr;
this->b = nullptr;
}
~MatrixIn() { }
void init(int row, int col)
{
this->row = row;
this->col = col;
}
T *a, *b;
int row, col;
};
template<typename T>
class MatrixOut
{
public:
MatrixOut()
{
this->c = nullptr;
}
~MatrixOut() { }
void init(int row, int col)
{
this->row = row;
this->col = col;
}
T *c;
int row, col;
};
void matrix_mul_cpu(int *a, int* b, int* c, int width)
{
for (int i = 0; i < width; i++)
{
for (int j = 0; j < width; j++)
{
int sum = 0;
for (int k = 0; k < width; k++)
{
int tmp_a = a[i * width + k];
int tmp_b = b[k * width + j];
sum += tmp_a * tmp_b;
}
c[i * width + j] = sum;
}
}
}
inline void matrix_check(MatrixIn<int> *in, MatrixOut<int> *out)
{
bool correct = true;
int col = in->col;
int row = in->row;
int size = sizeof(int) * row * col;
int *a = in->a;
int *b = in->b;
int *c = out->c;
int *cref = (int *)malloc(size);
fprintf(stderr, "checking the first element, please wait...\n");
fprintf(stderr, "c[0]=%d\n", c[0]);
matrix_mul_cpu(a, b, cref, col);
fprintf(stderr, "cref[0]=%d\n", cref[0]);
fprintf(stderr, "checking all matrix, please wait...\n");
for (int i = 0; i < row * col; i++)
{
if (c[i] != cref[i])
{
correct = false;
fprintf(stderr, "c[%d]=%d but cref[%d]=%d\n",
i, c[i], i, cref[i]);
break;
}
}
fprintf(stderr, "%s result!\n", correct ? "Correct": "Wrong");
}
inline void init_random(MatrixIn<int> *in)
{
std::default_random_engine random_engine;
std::uniform_int_distribution<int> urandom_gen(0, 1000);
int col = in->col;
int row = in->row;
for (int i = 0; i < row * col; i++)
{
in->a[i] = urandom_gen(random_engine);
in->b[i] = urandom_gen(random_engine);
}
}
#endif