-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp-test.cc
204 lines (173 loc) · 5.72 KB
/
app-test.cc
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
#include <iostream>
#include <iostream>
#include <random>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <sstream>
#include <signal.h>
#include "timer.h"
/* all generated sources are pure C99 */
extern "C"
{
/* vital runtime functions */
#include <aurora_runtime.h>
/**
* kernel file XXX.cve generates a header XXX_offload.h which we need
* to include here
*/
#include <gema_offload.h>
#include <gemm_offload.h>
}
/* ************************************************************************** */
template<typename T>
void
random_matrix(
T * mat,
const int m,
const int n,
std::mt19937& rnd_gen)
{
std::uniform_real_distribution<T> dist(
static_cast<T>(-1.0),
static_cast<T>(1.0));
for(unsigned int i = 0; i < m * n; ++i)
mat[i] = dist(rnd_gen);
}
/* ************************************************************************** */
int
main(
int argc,
const char * argv[])
{
using scalar_t = double;
const int ve_dev_id = 1;
const int batch = 1000;
/* initialize the Aurora device */
std::cout << "Initializing the Aurora..." << std::endl;
if(ve_init(ve_dev_id) != VEO_COMMAND_OK)
{
std::cout << "Failed to initialize the Aurora device, exiting..." <<
std::endl;
}
std::cout << "Done!" << std::endl;
/* create random matrices for the GEMM */
const long long int seed = 384562397463248l;
const int m = 256;
std::vector<scalar_t> As(m * m * batch, 1.0);
std::vector<scalar_t> Bs(m * m * batch, 1.0);
std::vector<scalar_t> GEMA_Cs(m * m * batch, 0.0);
std::vector<scalar_t> GEMM_Cs(m * m * batch, 0.0);
START_TIMER("GenData");
std::cout << "Generating data..." << std::endl;
#pragma omp parallel for
for(int s = 0; s < batch; ++s)
{
std::mt19937 rnd_gen(seed + s);
scalar_t * A = As.data() + s * m * m;
scalar_t * B = Bs.data() + s * m * m;
scalar_t * GEMA_C = GEMA_Cs.data() + s * m * m;
scalar_t * GEMM_C = GEMM_Cs.data() + s * m * m;
random_matrix(A, m, m, rnd_gen);
random_matrix(B, m, m, rnd_gen);
/* create GEMA result for comparison */
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < m; ++j)
{
/* interpret B as row major */
GEMA_C[i * m + j] = A[i * m + j] + B[i * m + j];
}
}
/* create GEMM result for comparison */
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < m; ++j)
{
for(int l = 0; l < m; ++l)
{
/* interpret B as col major */
GEMM_C[i * m + j] += A[i * m + l] * B[l * m + j];
}
}
}
}
std::cout << "Done!" << std::endl;
STOP_TIMER("GenData");
PRINT_TIMER("GenData");
/* allocate storage on device */
uint64_t dev_As, dev_Bs, dev_GEMA_Cs, dev_GEMM_Cs;
ve_malloc(&dev_As, m * m * batch * sizeof(scalar_t));
ve_malloc(&dev_Bs, m * m * batch * sizeof(scalar_t));
ve_malloc(&dev_GEMA_Cs, m * m * batch * sizeof(scalar_t));
ve_malloc(&dev_GEMM_Cs, m * m * batch * sizeof(scalar_t));
/* schedule all memcopies - 'commit' on the last call */
ve_memcpy_h2d(dev_As, As.data(), m * m * batch * sizeof(scalar_t), 0);
ve_memcpy_h2d(dev_Bs, Bs.data(), m * m * batch * sizeof(scalar_t), 1);
/* device function calls */
START_TIMER("Batched GEMA");
gema_op_d__offload__(dev_As, dev_Bs, dev_GEMA_Cs, batch);
STOP_TIMER("Batched GEMA");
PRINT_TIMER("Batched GEMA");
PRINT_PERF("Batched GEMA", batch*(double)m*(double)m);
START_TIMER("Batched GEMM");
gemm_op_d__offload__(dev_As, dev_Bs, dev_GEMM_Cs, batch);
STOP_TIMER("Batched GEMM");
PRINT_TIMER("Batched GEMM");
PRINT_PERF("Batched GEMM", batch*(double)m*(double)m*(double)m*2);
/* transfer the results back */
std::vector<scalar_t> d_GEMA_Cs(batch * m * m * sizeof(scalar_t));
std::vector<scalar_t> d_GEMM_Cs(batch * m * m * sizeof(scalar_t));
ve_memcpy_d2h(d_GEMA_Cs.data(), dev_GEMA_Cs, m * m * batch *
sizeof(scalar_t), 1);
ve_memcpy_d2h(d_GEMM_Cs.data(), dev_GEMM_Cs, m * m * batch *
sizeof(scalar_t), 1);
/* free device memory */
ve_free(dev_GEMM_Cs);
ve_free(dev_GEMA_Cs);
ve_free(dev_Bs);
ve_free(dev_As);
/* check results */
std::cout << "Checking GEMA results..." << std::endl;
for(int s = 0; s < batch; ++s)
{
const scalar_t * ref_C = GEMA_Cs.data() + s * m * m;
const scalar_t * d_C = d_GEMA_Cs.data() + s * m * m;
scalar_t max_err = 0.0;
for(int i = 0; i < m * m; ++i)
{
const scalar_t i_err = std::abs(ref_C[i] - d_C[i]);
if(i_err > max_err)
{
max_err = i_err;
}
}
if(max_err > 1e-3)
{
std::cout << "GEMA: Max err " << max_err << " at item " << s << "..." << std::endl;
}
}
std::cout << "Checking GEMM results..." << std::endl;
for(int s = 0; s < batch; ++s)
{
const scalar_t * ref_C = GEMM_Cs.data() + s * m * m;
const scalar_t * d_C = d_GEMM_Cs.data() + s * m * m;
scalar_t max_err = 0.0;
for(int i = 0; i < m * m; ++i)
{
const scalar_t i_err = std::abs(ref_C[i] - d_C[i]);
if(i_err > max_err)
{
max_err = i_err;
}
}
if(max_err > 1e-3)
{
std::cout << "GEMM: Max err " << max_err << " at item " << s << "..." << std::endl;
}
}
std::cout << "Done!" << std::endl;
/* shutdown the device connection */
ve_finish();
return EXIT_SUCCESS;
}