-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmeans_three_stage_buffered.hpp
547 lines (484 loc) · 18.6 KB
/
kmeans_three_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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* 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) 2017-2018, Lutz, Clemens <[email protected]>
*/
#ifndef KMEANS_THREE_STAGE_BUFFERED_HPP
#define KMEANS_THREE_STAGE_BUFFERED_HPP
#include "abstract_kmeans.hpp"
#include "labeling_factory.hpp"
#include "mass_update_factory.hpp"
#include "centroid_update_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 <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 KmeansThreeStageBuffered :
public AbstractKmeans<PointT, LabelT, MassT, ColMajor>
{
public:
using LabelingFunction = typename LabelingFactory<PointT, LabelT, ColMajor>::LabelingFunction;
using MassUpdateFunction = typename MassUpdateFactory<LabelT, MassT>::MassUpdateFunction;
using CentroidUpdateFunction = typename CentroidUpdateFactory<PointT, LabelT, MassT, ColMajor>::CentroidUpdateFunction;
KmeansThreeStageBuffered() :
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_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 labeling_lambda = [
f_labeling = this->f_labeling,
num_features = this->num_features,
num_clusters = this->num_clusters,
&device_old_centroids = this->device_old_centroids
]
(
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_labeling(
queue,
num_features,
num_buffer_points,
num_clusters,
points_begin,
points_end,
device_old_centroids.begin(),
device_old_centroids.end(),
labels_begin,
labels_end,
datapoint,
wait_list
);
};
std::future<std::deque<boost::compute::event>> ll_future;
assert(true ==
scheduler.enqueue(
labeling_lambda,
points_handle,
labels_handle,
buffer_size,
buffer_size / this->num_features,
ll_future,
this->measurement->add_datapoint(iterations)
));
auto mass_update_lambda = [
f_mass_update = this->f_mass_update,
num_clusters = this->num_clusters,
&device_masses = this->device_masses
]
(
boost::compute::command_queue queue,
size_t /* cl_offset */,
size_t label_bytes,
boost::compute::buffer labels,
boost::compute::wait_list wait_list,
Measurement::DataPoint& datapoint
)
{
auto num_buffer_labels = label_bytes / sizeof(LabelT);
boost::compute::buffer_iterator<LabelT>
labels_begin(
labels,
0
),
labels_end(
labels,
label_bytes / sizeof(LabelT)
);
return f_mass_update(
queue,
num_buffer_labels,
num_clusters,
labels_begin,
labels_end,
device_masses.begin(),
device_masses.end(),
datapoint,
wait_list);
};
std::future<std::deque<boost::compute::event>> mu_future;
assert(true ==
scheduler.enqueue(
mass_update_lambda,
labels_handle,
buffer_size / this->num_features,
mu_future,
this->measurement->add_datapoint(iterations)
));
auto centroid_update_lambda = [
f_centroid_update = this->f_centroid_update,
num_features = this->num_features,
num_clusters = this->num_clusters,
&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_centroid_update(
queue,
num_features,
num_buffer_points,
num_clusters,
points_begin,
points_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>> cu_future;
assert(true ==
scheduler.enqueue(
centroid_update_lambda,
points_handle,
labels_handle,
buffer_size,
buffer_size / this->num_features,
cu_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_labeler(LabelingConfiguration config) {
LabelingFactory<PointT, LabelT, ColMajor> factory;
f_labeling = factory.create(
this->context,
config,
*this->measurement);
}
void set_mass_updater(MassUpdateConfiguration config) {
MassUpdateFactory<LabelT, MassT> factory;
f_mass_update = factory.create(
this->context,
config,
*this->measurement);
}
void set_centroid_updater(CentroidUpdateConfiguration config) {
CentroidUpdateFactory<PointT, LabelT, MassT, ColMajor> factory;
f_centroid_update = factory.create(
this->context,
config,
*this->measurement);
}
void set_labeling_context(boost::compute::context c) {
if (context == boost::compute::context()) {
context = c;
}
else {
assert(context == c);
}
}
void set_mass_update_context(boost::compute::context c) {
if (context == boost::compute::context()) {
context = c;
}
else {
assert(context == c);
}
}
void set_centroid_update_context(boost::compute::context c) {
if (context == boost::compute::context()) {
context = c;
}
else {
assert(context == c);
}
}
void set_labeling_queue(boost::compute::command_queue q) {
if (this->queue == boost::compute::command_queue()) {
this->queue = q;
}
else {
assert(this->queue == q);
}
auto device = q.get_device();
this->measurement->set_parameter(
"LabelingPlatform",
device.platform().name()
);
this->measurement->set_parameter(
"LabelingDevice",
device.name()
);
}
void set_mass_update_queue(boost::compute::command_queue q) {
if (this->queue == boost::compute::command_queue()) {
this->queue = q;
}
else {
assert(this->queue == q);
}
auto device = q.get_device();
this->measurement->set_parameter(
"MassUpdatePlatform",
device.platform().name()
);
this->measurement->set_parameter(
"MassUpdateDevice",
device.name()
);
}
void set_centroid_update_queue(boost::compute::command_queue q) {
if (this->queue == boost::compute::command_queue()) {
this->queue = q;
}
else {
assert(this->queue == q);
}
auto device = q.get_device();
this->measurement->set_parameter(
"CentroidUpdatePlatform",
device.platform().name()
);
this->measurement->set_parameter(
"CentroidUpdateDevice",
device.name()
);
}
private:
static constexpr size_t buffer_size = 16ul * 1024ul * 1024ul;
LabelingFunction f_labeling;
MassUpdateFunction f_mass_update;
CentroidUpdateFunction f_centroid_update;
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;
};
} // namespace Clustering
#endif /* KMEANS_THREE_STAGE_BUFFERED_HPP */