forked from slava77/mkfit-hackathon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgplex_mul.cu
205 lines (175 loc) · 5.29 KB
/
gplex_mul.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
#include "gplex_mul.h"
#include "GPlex.h"
template <typename GPlex>
__global__ void set_mem(GPlex a, float val, size_t N) {
for (int n = threadIdx.x + blockIdx.x * blockDim.x;
n < N;
n += blockDim.x * gridDim.x) {
for (int i = 0; i < GPlex::kSize; ++i) {
a(n, 0, i) = val;
}
}
}
template <typename GPlexNM, typename GPlexMP, typename GPlexNP>
__global__ void naive_mult_kn(const RESTRICT GPlexNM a, const RESTRICT GPlexMP b, GPlexNP c, const int N)
{
for (int n = threadIdx.x + blockIdx.x * blockDim.x;
n < N;
n += blockDim.x * gridDim.x) {
for (int i = 0; i < GPlexNM::kRows; ++i) {
for (int j = 0; j < GPlexMP::kCols; ++j) {
for (int k = 0; k < GPlexNM::kCols; ++k) {
c(n, i, j) += a(n, i, k) * b(n, k, j);
}
}
}
}
}
template <typename GPlexNM, typename GPlexMP, typename GPlexNP>
__global__ void reg_c_mult_kn(const RESTRICT GPlexNM a, const RESTRICT GPlexMP b, GPlexNP c, const int N)
{
for (int n = threadIdx.x + blockIdx.x * blockDim.x;
n < N;
n += blockDim.x * gridDim.x) {
for (int i = 0; i < GPlexNM::kRows; ++i) {
for (int j = 0; j < GPlexMP::kCols; ++j) {
float c_tmp = 0;
for (int k = 0; k < GPlexNM::kCols; ++k) {
c_tmp += a(n, i, k) * b(n, k, j);
}
c(n, i, j) = c_tmp;
}
}
}
}
template <typename GPlexNM, typename GPlexMP, typename GPlexNP>
__global__ void shared_mult_kn(const RESTRICT GPlexNM a, const RESTRICT GPlexMP b, GPlexNP c, const int N)
{
for (int n = threadIdx.x + blockIdx.x * blockDim.x;
n < N;
n += blockDim.x * gridDim.x) {
int tix = threadIdx.x;
__shared__ float sh_a[GPlexNM::kSize][block_size];
__shared__ float sh_b[GPlexMP::kSize][block_size];
for (int i = 0; i < GPlexNM::kSize; ++i) {
sh_a[i][tix] = a(n, 0, i);
}
for (int i = 0; i < GPlexNM::kSize; ++i) {
sh_b[i][tix] = b(n, 0, i);
}
__syncthreads();
for (int i = 0; i < GPlexNM::kRows; ++i) {
for (int j = 0; j < GPlexMP::kCols; ++j) {
float c_tmp = 0;
for (int k = 0; k < GPlexNM::kCols; ++k) {
/*c_tmp += a(n, i, k) * b(n, k, j);*/
c_tmp += sh_a[k + GPlexNM::kCols * i][tix]
* sh_b[j + GPlexMP::kCols * k][tix];
/*c_tmp += sh_a[0][tix] ;*/
/** sh_b[j + GPlexMP::kCols * k][tix];*/
}
c(n, i, j) = c_tmp;
}
}
}
}
template <typename GPlexNM, typename GPlexMP, typename GPlexNP>
__global__ void reg_mult_kn(const RESTRICT GPlexNM a, const RESTRICT GPlexMP b, GPlexNP c, const int N)
{
for (int n = threadIdx.x + blockIdx.x * blockDim.x;
n < N;
n += blockDim.x * gridDim.x) {
GPlexRegLL reg_a;
GPlexRegLL reg_b;
for (int i = 0; i < GPlexNM::kSize; ++i) {
reg_a[i] = a(n, 0, i);
}
for (int i = 0; i < GPlexMP::kSize; ++i) {
reg_b[i] = b(n, 0, i);
}
for (int i = 0; i < GPlexNM::kRows; ++i) {
for (int j = 0; j < GPlexMP::kCols; ++j) {
float c_tmp = 0;
for (int k = 0; k < GPlexNM::kCols; ++k) {
c_tmp += reg_a(n, i, k) * reg_b(n, k, j);
}
c(n, i, j) = c_tmp;
}
}
}
}
bool check(int N, GPlexLL c, bool managed)
{
const float eps = 1e-30;
float c0, c36;
if (managed) {
c0 = c(0,0,0);
c36 = c(1,0,0);
int device = -1;
cudaGetDevice(&device);
cudaMemPrefetchAsync(c.Ptr(), N*sizeof(float)*GPlexLL::kSize, device, NULL);
} else {
GPlexBLL h[(N+NN-1)/NN];
c.copyToHost(h[0]);
c0 = h[0].At(0,0,0);
c36 = h[1].At(1,0,0);
}
bool pass = (std::abs(c0 - c36) < eps) && (std::abs(c0 - 6.0f) < eps);
if (!pass) {
std::cout << "Fail check c[0]=" << c0 << " c[36]=" << c36 << std::endl;
}
return pass;
}
void run_naive_mul(int iter, bool managed)
{
constexpr int N = Nwidth;
GPlexLL a, b, c;
constexpr int sz = N*sizeof(float)*GPlexLL::kSize;
if (managed) {
a.allocateManaged(N);
b.allocateManaged(N);
c.allocateManaged(N);
int device = -1;
cudaGetDevice(&device);
cudaMemPrefetchAsync(a.Ptr(), sz, device, NULL);
cudaMemPrefetchAsync(b.Ptr(), sz, device, NULL);
cudaMemPrefetchAsync(c.Ptr(), sz, device, NULL);
} else {
a.allocate(N);
b.allocate(N);
c.allocate(N);
}
cudaCheckError();
dim3 grid (((N-1)/block_size + 1), 1, 1);
dim3 block (block_size, 1, 1);
set_mem <<< grid, block >>> (a, 1.f , N);
set_mem <<< grid, block >>> (b, 1.f, N);
set_mem <<< grid, block >>> (c, 0.f, N);
if (managed) {
cudaMemAdvise(a.Ptr(), sz, cudaMemAdviseSetReadMostly, 0);
cudaMemAdvise(b.Ptr(), sz, cudaMemAdviseSetReadMostly, 0);
}
cudaCheckErrorSync();
for (int i = 0; i < iter; ++i) {
set_mem <<< grid, block >>> (c, 0.f, N);
naive_mult_kn <<< grid, block >>> (a, b, c, N);
}
cudaCheckErrorSync();
assert(check(N, c, managed));
for (int i = 0; i < iter; ++i)
reg_c_mult_kn <<< grid, block >>> (a, b, c, N);
cudaCheckErrorSync();
assert(check(N, c, managed));
for (int i = 0; i < iter; ++i)
shared_mult_kn <<< grid, block >>> (a, b, c, N);
cudaCheckErrorSync();
assert(check(N, c, managed));
for (int i = 0; i < iter; ++i)
reg_mult_kn <<< grid, block >>> (a, b, c, N);
cudaCheckErrorSync();
assert(check(N, c, managed));
a.free();
b.free();
c.free();
cudaCheckErrorSync();
}