-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix-multiply.cpp
342 lines (290 loc) · 11.9 KB
/
matrix-multiply.cpp
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
#include <algorithm>
#include <iostream>
#include <random>
#include <sycl/sycl.hpp>
#include "util.hpp"
using namespace sycl;
constexpr size_t N = 512;
constexpr size_t B = 16;
void matrix_multiply_naive(const std::vector<float> &a, const std::vector<float> &b, std::vector<float> &c) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
float sum = 0;
for (int k = 0; k < N; ++k) {
sum += a[i * N + k] * b[k * N + j];
}
c[i * N + j] = sum;
}
}
}
void matrix_multiply(
queue &q,
buffer<float, 2> &a_buf, buffer<float, 2> &b_buf, buffer<float, 2> &c_buf) {
// Submit the kernel to the queue
q.submit([&](handler &h) {
accessor a{a_buf, h, read_only};
accessor b{b_buf, h, read_only};
accessor c{c_buf, h, write_only, no_init};
// BEGIN CODE SNIP
h.parallel_for(range{N, N}, [=](id<2> idx) {
int i = idx[0];
int j = idx[1];
float sum = 0;
for (int k = 0; k < N; ++k) {
sum += a[i][k] * b[k][j];
}
c[i][j] = sum;
});
// END CODE SNIP
});
}
void matrix_multiply_nd_range(
queue &q,
buffer<float, 2> &a_buf, buffer<float, 2> &b_buf, buffer<float, 2> &c_buf) {
// Submit the kernel to the queue
q.submit([&](handler &h) {
accessor a{a_buf, h, read_only};
accessor b{b_buf, h, read_only};
accessor c{c_buf, h, write_only, no_init};
// BEGIN CODE SNIP
range global{N, N};
range local{B, B};
h.parallel_for(nd_range{global, local}, [=](nd_item<2> it) {
int i = it.get_global_id(0);
int j = it.get_global_id(1);
float sum = 0;
for (int k = 0; k < N; ++k) {
sum += a[i][k] * b[k][j];
}
c[i][j] = sum;
});
// END CODE SNIP
});
}
void matrix_multiply_nd_range_group_local_mem(
queue &q,
buffer<float, 2> &a_buf, buffer<float, 2> &b_buf, buffer<float, 2> &c_buf) {
// Submit the kernel to the queue
q.submit([&](handler &h) {
accessor a{a_buf, h, read_only};
accessor b{b_buf, h, read_only};
accessor c{c_buf, h, write_only, no_init};
auto tile = local_accessor<float, 2>({B, B}, h);
// BEGIN CODE SNIP
range global{N, N};
range local{B, B};
h.parallel_for(nd_range{global, local}, [=](nd_item<2> it) {
int m = it.get_global_id(0);
int n = it.get_global_id(1);
int i = it.get_local_id(0);
int j = it.get_local_id(1);
float sum = 0;
for (int t = 0; t < N; t += B) {
// load the matrix tile from matrix A
// each work-item read one element
// synchronize to wait for other work-item
tile[i][j] = a[m][t + j];
group_barrier(it.get_group());
// Perform computation using the local memory
// tile, and matrix B in global memory.
for (int k = 0; k < B; k++) {
sum += tile[i][k] * b[t + k][n];
}
// synchronize to wait for other work-item
group_barrier(it.get_group());
}
c[m][n] = sum;
});
// END CODE SNIP
});
}
void matrix_multiply_nd_range_group_broadcast(
queue &q,
buffer<float, 2> &a_buf, buffer<float, 2> &b_buf, buffer<float, 2> &c_buf) {
// Submit the kernel to the queue
q.submit([&](handler &h) {
accessor a{a_buf, h, read_only};
accessor b{b_buf, h, read_only};
accessor c{c_buf, h, write_only, no_init};
size_t tile_size = B;
// BEGIN CODE SNIP
range global{N, N};
range local{1, tile_size};
h.parallel_for(nd_range{global, local}, [=](nd_item<2> it) {
// Indices in the global index space:
int m = it.get_global_id()[0];
int n = it.get_global_id()[1];
// Index in the local index space:
int i = it.get_local_id()[1];
float sum = 0;
for (int t = 0; t < N; t += tile_size) {
// Load the matrix tile from matrix A.
float tileI = a[m][t + i];
// Perform computation by broadcasting from
// the matrix tile and loading from matrix B
// in global memory. The loop variable k
// describes which work-item in the work-group
// to broadcast data from.
for (int k = 0; k < tile_size; k++) {
sum += group_broadcast(it.get_group(), tileI, k) * b[t + k][n];
}
}
// Write the final result to global memory.
c[m][n] = sum;
});
// END CODE SNIP
});
}
void matrix_multiply_nd_range_sub_group_local_mem(
queue &q,
buffer<float, 2> &a_buf, buffer<float, 2> &b_buf, buffer<float, 2> &c_buf) {
// Submit the kernel to the queue
q.submit([&](handler &h) {
accessor a{a_buf, h, read_only};
accessor b{b_buf, h, read_only};
accessor c{c_buf, h, write_only, no_init};
size_t tile_size = B;
auto tile = local_accessor<float, 2>({tile_size, tile_size}, h);
// BEGIN CODE SNIP
range global{N, N};
range local{B, B};
h.parallel_for(nd_range{global, local}, [=](nd_item<2> it) [[sycl::reqd_sub_group_size(B)]] {
int m = it.get_global_id(0);
int n = it.get_global_id(1);
int i = it.get_local_id(0);
int j = it.get_local_id(1);
float sum = 0;
for (int t = 0; t < N; t += tile_size) {
// load the matrix tile from matrix A
// each work-item read one element
// synchronize to wait for other work-item
tile[i][j] = a[m][t + j];
group_barrier(it.get_sub_group());
// Perform computation using the local memory
// tile, and matrix B in global memory.
for (int k = 0; k < tile_size; k++) {
sum += tile[i][k] * b[t + k][n];
}
// synchronize to wait for other work-item
group_barrier(it.get_sub_group());
}
c[m][n] = sum;
});
// END CODE SNIP
});
}
void matrix_multiply_nd_range_sub_group_broadcast(
queue &q,
buffer<float, 2> &a_buf, buffer<float, 2> &b_buf, buffer<float, 2> &c_buf) {
// Submit the kernel to the queue
q.submit([&](handler &h) {
accessor a{a_buf, h, read_only};
accessor b{b_buf, h, read_only};
accessor c{c_buf, h, write_only, no_init};
size_t tile_size = B;
// BEGIN CODE SNIP
range global{N, N};
range local{B, B};
h.parallel_for(nd_range{global, local}, [=](nd_item<2> it) [[sycl::reqd_sub_group_size(B)]] {
// Indices in the global index space:
int m = it.get_global_id()[0];
int n = it.get_global_id()[1];
// Index in the local index space:
int i = it.get_local_id()[1];
float sum = 0;
for (int t = 0; t < N; t += tile_size) {
// Load the matrix tile from matrix A.
float tileI = a[m][t + i];
// Perform computation by broadcasting from
// the matrix tile and loading from matrix B
// in global memory. The loop variable k
// describes which work-item in the sub-group
// to broadcast data from.
for (int k = 0; k < tile_size; k++) {
sum += group_broadcast(it.get_sub_group(), tileI, k) * b[t + k][n];
}
}
// Write the final result to global memory.
c[m][n] = sum;
});
// END CODE SNIP
});
}
void test_perfomance() {
std::vector<float> a(N * N), b(N * N), c(N * N);
std::cout << "CPU single core: ";
benchmark_func([&] { matrix_multiply_naive(a, b, c); });
queue cpu_q{cpu_selector_v};
queue gpu_q{gpu_selector_by_cu};
std::vector<std::tuple<
std::string,
std::function<void(queue &, buffer<float, 2> &, buffer<float, 2> &, buffer<float, 2> &)>,
queue> > tests = {
{"CPU SYCL", matrix_multiply, cpu_q},
{"CPU SYCL ND-range", matrix_multiply_nd_range, cpu_q},
{"CPU SYCL ND-range Group Local Memory", matrix_multiply_nd_range_group_local_mem, cpu_q},
{"CPU SYCL ND-range Group Broadcast", matrix_multiply_nd_range_group_broadcast, cpu_q},
{"CPU SYCL ND-range Sub-group Local Memory", matrix_multiply_nd_range_sub_group_local_mem, cpu_q},
{"CPU SYCL ND-range Sub-group Broadcast", matrix_multiply_nd_range_sub_group_broadcast, cpu_q},
{"GPU SYCL", matrix_multiply, gpu_q},
{"GPU SYCL ND-range", matrix_multiply_nd_range, gpu_q},
{"GPU SYCL ND-range Group Local Memory", matrix_multiply_nd_range_group_local_mem, gpu_q},
{"GPU SYCL ND-range Group Broadcast", matrix_multiply_nd_range_group_broadcast, gpu_q},
{"GPU SYCL ND-range Sub-group Local Memory", matrix_multiply_nd_range_sub_group_local_mem, gpu_q},
{"GPU SYCL ND-range Sub-group Broadcast", matrix_multiply_nd_range_sub_group_broadcast, gpu_q},
};
for (auto &[name,kernel, q]: tests) {
{
buffer<float, 2> a_buf(a.data(), range<2>(N, N)),
b_buf(b.data(), range<2>(N, N)),
c_buf(c.data(), range<2>(N, N));
std::cout << name << ": ";
benchmark_sycl_kernel([&](queue &q) { kernel(q, a_buf, b_buf, c_buf); }, q);
}
}
}
void test_acc() {
// Initialize input and output memory on the host
std::vector<float> a(N * N), b(N * N), c(N * N), gt(N * N);
std::default_random_engine gen(42);
std::uniform_real_distribution<float> dist(0.0, 1.0);
auto rng = [&]() { return dist(gen); };
std::generate(a.begin(), a.end(), rng);
std::generate(b.begin(), b.end(), rng);
// calculate groud truth
matrix_multiply_naive(a, b, gt);
queue cpu_q{cpu_selector_v};
queue gpu_q{gpu_selector_by_cu};
std::vector<std::tuple<
std::string,
std::function<void(queue &, buffer<float, 2> &, buffer<float, 2> &, buffer<float, 2> &)>,
queue> > tests = {
{"CPU SYCL", matrix_multiply, cpu_q},
{"CPU SYCL ND-range", matrix_multiply_nd_range, cpu_q},
{"CPU SYCL ND-range Group Local Memory", matrix_multiply_nd_range_group_local_mem, cpu_q},
{"CPU SYCL ND-range Group Broadcast", matrix_multiply_nd_range_group_broadcast, cpu_q},
{"CPU SYCL ND-range Sub-group Local Memory", matrix_multiply_nd_range_sub_group_local_mem, cpu_q},
{"CPU SYCL ND-range Sub-group Broadcast", matrix_multiply_nd_range_sub_group_broadcast, cpu_q},
{"GPU SYCL", matrix_multiply, gpu_q},
{"GPU SYCL ND-range", matrix_multiply_nd_range, gpu_q},
{"GPU SYCL ND-range Group Local Memory", matrix_multiply_nd_range_group_local_mem, gpu_q},
{"GPU SYCL ND-range Group Broadcast", matrix_multiply_nd_range_group_broadcast, gpu_q},
{"GPU SYCL ND-range Sub-group Local Memory", matrix_multiply_nd_range_sub_group_local_mem, gpu_q},
{"GPU SYCL ND-range Sub-group Broadcast", matrix_multiply_nd_range_sub_group_broadcast, gpu_q},
};
for (auto &[name,kernel, q]: tests) {
{
std::fill(c.begin(), c.end(), 0);
buffer<float, 2> a_buf(a.data(), range<2>(N, N)),
b_buf(b.data(), range<2>(N, N)),
c_buf(c.data(), range<2>(N, N));
kernel(q, a_buf, b_buf, c_buf);
}
auto success = floatVectorEquals(c, gt);
std::cout << name << ": " << (success ? "SUCCESS" : "FAILURE") << std::endl;
}
}
int main() {
test_acc();
test_perfomance();
}