-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmeans_gpu_assisted.hpp
94 lines (76 loc) · 3.05 KB
/
kmeans_gpu_assisted.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
/*
* 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) 2016, Lutz, Clemens <lutzcle@cml.li>
*/
#ifndef KMEANS_GPU_ASSISTED_HPP
#define KMEANS_GPU_ASSISTED_HPP
#include "cl_kernels/lloyd_labeling_api.hpp"
#include "cl_kernels/lloyd_labeling_vp_clc_api.hpp"
#include "cl_kernels/lloyd_labeling_vp_clcp_api.hpp"
#include "kmeans_common.hpp"
#include "matrix.hpp"
#include "measurement/measurement.hpp"
#include <cstdint>
#include <type_traits>
#include <memory>
#ifdef MAC
#include <OpenCL/cl.hpp>
#else
#include <CL/cl.hpp>
#endif
namespace cle {
template <typename FP, typename INT, typename AllocFP, typename AllocINT>
class KmeansGPUAssisted {
public:
KmeansGPUAssisted(
cl::Context const& context,
cl::CommandQueue const& queue
);
enum class LabelingStrategy {
Plain,
VpClc,
VpClcp
};
char const* name() const;
int initialize();
int finalize();
int operator() (
uint32_t const max_iterations,
cle::Matrix<FP, AllocFP, INT, true> const& points,
cle::Matrix<FP, AllocFP, INT, true>& centroids,
std::vector<INT, AllocINT>& cluster_size,
std::vector<INT, AllocINT>& memberships,
Measurement::Measurement& stats
);
private:
using CL_FP = typename std::conditional<
std::is_same<FP, float>::value, cl_float, cl_double>::type;
using CL_INT = typename std::conditional<
std::is_same<INT, uint32_t>::value, cl_uint, cl_ulong>::type;
cle::LloydLabelingAPI<CL_FP, CL_INT> labeling_kernel_;
cle::LloydLabelingVpClcAPI<CL_FP, CL_INT> labeling_vp_clc_kernel_;
cle::LloydLabelingVpClcpAPI<CL_FP, CL_INT> labeling_vp_clcp_kernel_;
cl::Context context_;
cl::CommandQueue queue_;
cl::Device device_;
LabelingStrategy labeling_strategy_ = LabelingStrategy::VpClcp;
cl_uint warp_size_;
};
using KmeansGPUAssisted32 = KmeansGPUAssisted<float, uint32_t, std::allocator<float>, std::allocator<uint32_t>>;
using KmeansGPUAssisted64 = KmeansGPUAssisted<double, uint64_t, std::allocator<double>, std::allocator<uint64_t>>;
#ifdef USE_ALIGNED_ALLOCATOR
using KmeansGPUAssisted32Aligned = KmeansGPUAssisted<float, uint32_t, AlignedAllocatorFP32, AlignedAllocatorINT32>;
using KmeansGPUAssisted64Aligned = KmeansGPUAssisted<double, uint64_t, AlignedAllocatorFP64, AlignedAllocatorINT64>;
#endif
}
extern template class cle::KmeansGPUAssisted<float, uint32_t, std::allocator<float>, std::allocator<uint32_t>>;
extern template class cle::KmeansGPUAssisted<double, uint64_t, std::allocator<double>, std::allocator<uint64_t>>;
#ifdef USE_ALIGNED_ALLOCATOR
extern template class cle::KmeansGPUAssisted<float, uint32_t, cle::AlignedAllocatorFP32, cle::AlignedAllocatorINT32>;
extern template class cle::KmeansGPUAssisted<double, uint64_t, cle::AlignedAllocatorFP64, cle::AlignedAllocatorINT64>;
#endif
#endif /* KMEANS_GPU_ASSISTED_HPP */