-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmeans_single_stage_buffered.hpp
352 lines (306 loc) · 12 KB
/
kmeans_single_stage_buffered.hpp
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
343
344
345
346
347
348
349
350
351
352
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
*
* Copyright (c) 2018, Lutz, Clemens <lutzcle@cml.li>
*/
#ifndef KMEANS_SINGLE_STAGE_BUFFERED_HPP
#define KMEANS_SINGLE_STAGE_BUFFERED_HPP
#include "abstract_kmeans.hpp"
#include "fused_factory.hpp"
#include "simple_buffer_cache.hpp"
#include "single_device_scheduler.hpp"
#include "buffer_helper.hpp"
#include "cl_kernels/matrix_binary_op.hpp"
#include "measurement/measurement.hpp"
#include "timer.hpp"
#include <functional>
#include <algorithm>
#include <vector>
#include <memory>
#include <boost/compute/core.hpp>
#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/algorithm/fill.hpp>
#include <boost/compute/async/wait.hpp>
#include <boost/compute/container/vector.hpp>
namespace Clustering {
template <typename PointT, typename LabelT, typename MassT, bool ColMajor = true>
class KmeansSingleStageBuffered :
public AbstractKmeans<PointT, LabelT, MassT, ColMajor>
{
public:
using FusedFunction = typename FusedFactory<PointT, LabelT, MassT, ColMajor>::FusedFunction;
KmeansSingleStageBuffered() :
AbstractKmeans<PointT, LabelT, MassT, ColMajor>()
{
}
void run() {
buffer_cache = std::make_shared<SimpleBufferCache>(
size_t(buffer_size)
);
this->scheduler.add_buffer_cache(buffer_cache);
this->matrix_divide.prepare(
this->context,
matrix_divide.Divide
);
this->host_points_partitioned.resize(this->host_points->size());
BufferHelper::partition_matrix(
this->host_points->data(),
&this->host_points_partitioned[0],
this->host_points->size() * sizeof(PointT),
this->num_features,
this->buffer_cache->buffer_size()
);
device_old_centroids = decltype(device_old_centroids)(
this->num_clusters * this->num_features,
this->queue.get_context()
);
device_new_centroids = decltype(device_new_centroids)(
this->num_clusters * this->num_features,
this->queue.get_context()
);
boost::compute::event centroids_h2d_event =
boost::compute::copy_async(
this->host_centroids->begin(),
this->host_centroids->begin()
+ this->num_features * this->num_clusters,
device_old_centroids.begin(),
this->queue).get_event();
device_masses = decltype(device_masses)(
this->num_clusters,
this->queue.get_context()
);
assert(true ==
this->scheduler.add_device(
this->context,
this->queue.get_device()
));
assert(true ==
this->buffer_cache->add_device(
this->context,
this->queue.get_device(),
// TODO: remove this temporary fix
// Underlaying problem is that we try allocate too
// much pinned memory on host in SimpleBufferCache.
// Instead, need to multiplex each pinned buffer among
// multiple device buffers
//
// this->queue.get_device().global_memory_size()
// - 64 * 1024 * 1024
128 * 1024 * 1024
));
auto points_handle = this->buffer_cache->add_object(
(void*)this->host_points_partitioned.data(),
this->host_points->size() * sizeof(PointT),
ObjectMode::ReadOnly
);
auto labels_handle = this->buffer_cache->add_object(
this->host_labels->data(),
this->host_labels->size() * sizeof(LabelT),
ObjectMode::ReadWrite
);
// If centroids initializer function is callable, then call
if (this->centroids_initializer) {
this->centroids_initializer(
device_old_centroids, // TODO: should be points vector
device_old_centroids
);
}
// Wait for all preprocessing steps to finish before
// starting timer
this->queue.finish();
Timer::Timer total_timer;
total_timer.start();
uint32_t iterations = 0;
while (iterations < this->max_iterations) {
boost::compute::event fill_masses_event =
boost::compute::fill_async(
device_masses.begin(),
device_masses.end(),
0,
this->queue
)
.get_event();
boost::compute::event fill_centroids_event =
boost::compute::fill_async(
device_new_centroids.begin(),
device_new_centroids.end(),
0,
this->queue
)
.get_event();
auto lambda = [
f_fused = this->f_fused,
num_features = this->num_features,
num_clusters = this->num_clusters,
&device_old_centroids = this->device_old_centroids,
&device_new_centroids = this->device_new_centroids,
&device_masses = this->device_masses
]
(
boost::compute::command_queue queue,
size_t /* cl_offset */,
size_t point_bytes,
size_t label_bytes,
boost::compute::buffer points,
boost::compute::buffer labels,
boost::compute::wait_list wait_list,
Measurement::DataPoint& datapoint
)
{
auto num_buffer_points = label_bytes / sizeof(LabelT);
boost::compute::buffer_iterator<PointT>
points_begin(
points,
0
),
points_end(
points,
point_bytes / sizeof(PointT)
);
boost::compute::buffer_iterator<LabelT>
labels_begin(
labels,
0
),
labels_end(
labels,
label_bytes / sizeof(LabelT)
);
return f_fused(
queue,
num_features,
num_buffer_points,
num_clusters,
points_begin,
points_end,
device_old_centroids.begin(),
device_old_centroids.end(),
device_new_centroids.begin(),
device_new_centroids.end(),
labels_begin,
labels_end,
device_masses.begin(),
device_masses.end(),
datapoint,
wait_list
);
};
std::future<std::deque<boost::compute::event>> fu_future;
assert(true ==
scheduler.enqueue(
lambda,
points_handle,
labels_handle,
buffer_size,
buffer_size / this->num_features,
fu_future,
this->measurement->add_datapoint(iterations)
));
assert(true == scheduler.run());
boost::compute::wait_list division_wait_list;
matrix_divide.row(
this->queue,
this->num_features,
this->num_clusters,
device_new_centroids.begin(),
device_new_centroids.end(),
device_masses.begin(),
device_masses.end(),
this->measurement->add_datapoint(iterations),
division_wait_list
);
std::swap(device_old_centroids, device_new_centroids);
++iterations;
}
// Wait for last queue to finish processing
this->queue.finish();
uint64_t total_time = total_timer
.stop<std::chrono::nanoseconds>();
this->measurement->add_datapoint()
.set_name("TotalTime")
.add_value() = total_time;
boost::compute::event centroids_copy_event = boost::compute::copy_async(
this->device_old_centroids.begin(),
this->device_old_centroids.begin() + this->num_features * this->num_clusters,
this->host_centroids->begin(),
this->queue
).get_event();
centroids_copy_event.wait();
boost::compute::event masses_copy_event = boost::compute::copy_async(
this->device_masses.begin(),
this->device_masses.begin() + this->num_clusters,
this->host_masses->begin(),
this->queue
).get_event();
masses_copy_event.wait();
{
char *begin, *iter, *end;
size_t labels_content_size = buffer_size / this->num_features;
for (
begin = (char*) this->host_labels->data(),
end = begin + this->host_labels->size() * sizeof(LabelT),
iter = begin;
iter < end;
iter += labels_content_size
)
{
boost::compute::event labels_read_event;
boost::compute::wait_list labels_read_wait_list;
auto iter_step = (iter + labels_content_size > end)
? end
: iter + labels_content_size
;
assert(true ==
buffer_cache->read(
this->queue,
labels_handle,
iter,
iter_step,
labels_read_event,
labels_read_wait_list,
this->measurement->add_datapoint()
));
}
}
this->queue.finish();
}
void set_fused(FusedConfiguration config) {
FusedFactory<PointT, LabelT, MassT, ColMajor> factory;
f_fused = factory.create(
this->context,
config,
*this->measurement);
}
void set_context(boost::compute::context c) {
context = c;
}
void set_queue(boost::compute::command_queue q) {
queue = q;
auto device = q.get_device();
this->measurement->set_parameter(
"FusedPlatform",
device.platform().name()
);
this->measurement->set_parameter(
"FusedDevice",
device.name()
);
}
private:
static constexpr size_t buffer_size = 16ul * 1024ul * 1024ul;
FusedFunction f_fused;
boost::compute::context context;
boost::compute::command_queue queue;
typename AbstractKmeans<PointT, LabelT, MassT, ColMajor>::template HostVector<PointT> host_points_partitioned;
std::shared_ptr<SimpleBufferCache> buffer_cache;
SingleDeviceScheduler scheduler;
MatrixBinaryOp<PointT, MassT> matrix_divide;
boost::compute::vector<PointT> device_old_centroids;
boost::compute::vector<PointT> device_new_centroids;
boost::compute::vector<MassT> device_masses;
};
}
#endif /* KMEANS_SINGLE_STAGE_BUFFERED_HPP */