forked from UoB-HPC/BabelStream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SYCLStream.cpp
328 lines (284 loc) · 7.96 KB
/
SYCLStream.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
// Copyright (c) 2015-16 Tom Deakin, Simon McIntosh-Smith,
// University of Bristol HPC
//
// For full license terms please see the LICENSE file distributed with this
// source code
#include "SYCLStream.h"
#include <iostream>
using namespace cl::sycl;
// Cache list of devices
bool cached = false;
std::vector<device> devices;
void getDeviceList(void);
program * p;
template <class T>
SYCLStream<T>::SYCLStream(const unsigned int ARRAY_SIZE, const int device_index)
{
if (!cached)
getDeviceList();
array_size = ARRAY_SIZE;
if (device_index >= devices.size())
throw std::runtime_error("Invalid device index");
device dev = devices[device_index];
// Determine sensible dot kernel NDRange configuration
if (dev.is_cpu())
{
dot_num_groups = dev.get_info<info::device::max_compute_units>();
dot_wgsize = dev.get_info<info::device::native_vector_width_double>() * 2;
}
else
{
dot_num_groups = dev.get_info<info::device::max_compute_units>() * 4;
dot_wgsize = dev.get_info<info::device::max_work_group_size>();
}
// Print out device information
std::cout << "Using SYCL device " << getDeviceName(device_index) << std::endl;
std::cout << "Driver: " << getDeviceDriver(device_index) << std::endl;
std::cout << "Reduction kernel config: " << dot_num_groups << " groups of size " << dot_wgsize << std::endl;
queue = new cl::sycl::queue(dev, [&](cl::sycl::exception_list l)
{
bool error = false;
for(auto e: l)
{
try
{
std::rethrow_exception(e);
}
catch (cl::sycl::exception e)
{
std::cout << e.what();
error = true;
}
}
if(error)
{
throw std::runtime_error("SYCL errors detected");
}
});
/* Pre-build the kernels */
p = new program(queue->get_context());
p->build_with_kernel_type<init_kernel>();
p->build_with_kernel_type<copy_kernel>();
p->build_with_kernel_type<mul_kernel>();
p->build_with_kernel_type<add_kernel>();
p->build_with_kernel_type<triad_kernel>();
p->build_with_kernel_type<dot_kernel>();
// Create buffers
d_a = new buffer<T>(array_size);
d_b = new buffer<T>(array_size);
d_c = new buffer<T>(array_size);
d_sum = new buffer<T>(dot_num_groups);
}
template <class T>
SYCLStream<T>::~SYCLStream()
{
delete d_a;
delete d_b;
delete d_c;
delete d_sum;
delete p;
delete queue;
devices.clear();
}
template <class T>
void SYCLStream<T>::copy()
{
queue->submit([&](handler &cgh)
{
auto ka = d_a->template get_access<access::mode::read>(cgh);
auto kc = d_c->template get_access<access::mode::write>(cgh);
cgh.parallel_for<copy_kernel>(p->get_kernel<copy_kernel>(),
range<1>{array_size}, [=](item<1> item)
{
auto id = item.get_id(0);
kc[id] = ka[id];
});
});
queue->wait();
}
template <class T>
void SYCLStream<T>::mul()
{
const T scalar = startScalar;
queue->submit([&](handler &cgh)
{
auto kb = d_b->template get_access<access::mode::write>(cgh);
auto kc = d_c->template get_access<access::mode::read>(cgh);
cgh.parallel_for<mul_kernel>(p->get_kernel<mul_kernel>(),
range<1>{array_size}, [=](item<1> item)
{
auto id = item.get_id(0);
kb[id] = scalar * kc[id];
});
});
queue->wait();
}
template <class T>
void SYCLStream<T>::add()
{
queue->submit([&](handler &cgh)
{
auto ka = d_a->template get_access<access::mode::read>(cgh);
auto kb = d_b->template get_access<access::mode::read>(cgh);
auto kc = d_c->template get_access<access::mode::write>(cgh);
cgh.parallel_for<add_kernel>(p->get_kernel<add_kernel>(),
range<1>{array_size}, [=](item<1> item)
{
auto id = item.get_id(0);
kc[id] = ka[id] + kb[id];
});
});
queue->wait();
}
template <class T>
void SYCLStream<T>::triad()
{
const T scalar = startScalar;
queue->submit([&](handler &cgh)
{
auto ka = d_a->template get_access<access::mode::write>(cgh);
auto kb = d_b->template get_access<access::mode::read>(cgh);
auto kc = d_c->template get_access<access::mode::read>(cgh);
cgh.parallel_for<triad_kernel>(p->get_kernel<triad_kernel>(),
range<1>{array_size}, [=](item<1> item)
{
auto id = item.get_id(0);
ka[id] = kb[id] + scalar * kc[id];
});
});
queue->wait();
}
template <class T>
T SYCLStream<T>::dot()
{
queue->submit([&](handler &cgh)
{
auto ka = d_a->template get_access<access::mode::read>(cgh);
auto kb = d_b->template get_access<access::mode::read>(cgh);
auto ksum = d_sum->template get_access<access::mode::write>(cgh);
auto wg_sum = accessor<T, 1, access::mode::read_write, access::target::local>(range<1>(dot_wgsize), cgh);
size_t N = array_size;
cgh.parallel_for<dot_kernel>(p->get_kernel<dot_kernel>(),
nd_range<1>(dot_num_groups*dot_wgsize, dot_wgsize), [=](nd_item<1> item)
{
size_t i = item.get_global(0);
size_t li = item.get_local(0);
size_t global_size = item.get_global_range()[0];
wg_sum[li] = 0.0;
for (; i < N; i += global_size)
wg_sum[li] += ka[i] * kb[i];
size_t local_size = item.get_local_range()[0];
for (int offset = local_size / 2; offset > 0; offset /= 2)
{
item.barrier(cl::sycl::access::fence_space::local_space);
if (li < offset)
wg_sum[li] += wg_sum[li + offset];
}
if (li == 0)
ksum[item.get_group(0)] = wg_sum[0];
});
});
T sum = 0.0;
auto h_sum = d_sum->template get_access<access::mode::read>();
for (int i = 0; i < dot_num_groups; i++)
{
sum += h_sum[i];
}
return sum;
}
template <class T>
void SYCLStream<T>::init_arrays(T initA, T initB, T initC)
{
queue->submit([&](handler &cgh)
{
auto ka = d_a->template get_access<access::mode::write>(cgh);
auto kb = d_b->template get_access<access::mode::write>(cgh);
auto kc = d_c->template get_access<access::mode::write>(cgh);
cgh.parallel_for<init_kernel>(p->get_kernel<init_kernel>(),
range<1>{array_size}, [=](item<1> item)
{
auto id = item.get_id(0);
ka[id] = initA;
kb[id] = initB;
kc[id] = initC;
});
});
queue->wait();
}
template <class T>
void SYCLStream<T>::read_arrays(std::vector<T>& a, std::vector<T>& b, std::vector<T>& c)
{
auto _a = d_a->template get_access<access::mode::read>();
auto _b = d_b->template get_access<access::mode::read>();
auto _c = d_c->template get_access<access::mode::read>();
for (int i = 0; i < array_size; i++)
{
a[i] = _a[i];
b[i] = _b[i];
c[i] = _c[i];
}
}
void getDeviceList(void)
{
// Get list of platforms
std::vector<platform> platforms = platform::get_platforms();
// Enumerate devices
for (unsigned i = 0; i < platforms.size(); i++)
{
std::vector<device> plat_devices = platforms[i].get_devices();
devices.insert(devices.end(), plat_devices.begin(), plat_devices.end());
}
cached = true;
}
void listDevices(void)
{
getDeviceList();
// Print device names
if (devices.size() == 0)
{
std::cerr << "No devices found." << std::endl;
}
else
{
std::cout << std::endl;
std::cout << "Devices:" << std::endl;
for (int i = 0; i < devices.size(); i++)
{
std::cout << i << ": " << getDeviceName(i) << std::endl;
}
std::cout << std::endl;
}
}
std::string getDeviceName(const int device)
{
if (!cached)
getDeviceList();
std::string name;
if (device < devices.size())
{
name = devices[device].get_info<info::device::name>();
}
else
{
throw std::runtime_error("Error asking for name for non-existant device");
}
return name;
}
std::string getDeviceDriver(const int device)
{
if (!cached)
getDeviceList();
std::string driver;
if (device < devices.size())
{
driver = devices[device].get_info<info::device::driver_version>();
}
else
{
throw std::runtime_error("Error asking for driver for non-existant device");
}
return driver;
}
// TODO: Fix kernel names to allow multiple template specializations
template class SYCLStream<float>;
template class SYCLStream<double>;