-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
184 lines (161 loc) · 7.08 KB
/
main.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
/*{{{
Copyright © 2014-2018 Matthias Kretz <kretz@kde.org>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the names of contributing organizations nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
}}}*/
#include <array>
#include <iostream>
#include <iomanip>
#include <memory>
#include <random>
#include <stdexcept>
#include "../tsc.h"
#include "point.h"
#include "linearsearch.h"
#include "kdtree.h"
int main() // {{{1
{
// settings {{{2
constexpr int SetSize = 20000; // required memory ~ SetSize * sizeof(Node)
// ~ SetSize * (sizeof(Point<T>) + 16)
// = SetSize * 32
constexpr int NumberOfSearches = 50000;
using T = float;
using Point = ::Point<T, 3>;
// random points {{{2
std::default_random_engine randomEngine(1);
typename std::conditional<std::is_floating_point<T>::value,
std::uniform_real_distribution<T>,
std::uniform_int_distribution<T>>::type uniform(-99, 99);
std::vector<Point> randomPoints;
for (int i = 0; i < SetSize; ++i) {
randomPoints.emplace_back(uniform(randomEngine), uniform(randomEngine), uniform(randomEngine));
}
TimeStampCounter tsc;
tsc.start(); // create KdTree {{{2
KdTree<Point> pointsTree;
for (const Point &p : randomPoints) {
pointsTree.insert(p);
}
tsc.stop();
const auto kdtree_inserts = tsc.cycles();
tsc.start(); // create KdTreeV {{{2
KdTreeV<Point> pointsTreeV;
for (const Point &p : randomPoints) {
pointsTreeV.insert(p);
}
tsc.stop();
const auto kdtreev_inserts = tsc.cycles();
//std::cout << pointsTreeV << '\n';
tsc.start(); // create LinearNeighborSearch {{{2
LinearNeighborSearch<Point> pointsVector(SetSize);
for (const Point &p : randomPoints) {
pointsVector.insert(p);
}
tsc.stop();
const auto linear_inserts = tsc.cycles();
tsc.start(); // create LinearNeighborSearchV {{{2
LinearNeighborSearchV<Point> linearSearchV(SetSize);
for (const Point &p : randomPoints) {
linearSearchV.insert(p);
}
tsc.stop();
const auto linearv_inserts = tsc.cycles();
// print insert timings {{{2
std::cout << " KdTree KdTreeV LinearNeighborSearch LinearNeighborSearchV KdTree/KdTreeV "
"Linear/KdTree Linear/LinearV\n";
std::cout << "inserts "
<< std::setw(11) << kdtree_inserts
<< std::setw(11) << kdtreev_inserts
<< std::setw(22) << linear_inserts
<< std::setw(23) << linearv_inserts
<< std::setw(16) << double(kdtree_inserts) / double(kdtreev_inserts) << std::endl;
// random search points {{{2
std::vector<Point> searchPoints;
searchPoints.reserve(NumberOfSearches);
for (int i = 0; i < NumberOfSearches; ++i) {
searchPoints.emplace_back(uniform(randomEngine), uniform(randomEngine), uniform(randomEngine));
}
tsc.start(); // KdTree searches {{{2
for (int i = 0; i < NumberOfSearches; ++i) {
const auto &p = searchPoints[i];
const auto &p2 = pointsTree.findNearest(p);
asm(""::"m"(p2));
//std::cout << "looking near " << p << ", found " << p2 << ", distance " << get_kdtree_distance(p, p2) << '\n';
}
tsc.stop();
const auto time_kdtree = tsc.cycles();
tsc.start(); // KdTreeV searches {{{2
for (int i = 0; i < NumberOfSearches; ++i) {
const auto &p = searchPoints[i];
const auto &p2 = pointsTreeV.findNearest(p);
asm(""::"m"(p2));
//std::cout << "looking near " << p << ", found " << p2 << ", distance " << get_kdtree_distance(p, p2) << '\n';
}
tsc.stop();
const auto time_kdtreev = tsc.cycles();
tsc.start(); // LinearNeighborSearch searches {{{2
for (int i = 0; i < NumberOfSearches; ++i) {
const auto &p = searchPoints[i];
const auto &p2 = pointsVector.findNearest(p);
asm(""::"m"(p2));
#if 0
const auto &p3 = pointsTree.findNearest(p);
const auto &p4 = pointsTreeV.findNearest(p);
if (get_kdtree_distance(p, p2) != get_kdtree_distance(p, p3)) {
std::cerr << p << " failed " << p2 << " (" << get_kdtree_distance(p, p2)
<< ") vs. KdTree " << p3 << " (" << get_kdtree_distance(p, p3) << ")\n";
}
if (get_kdtree_distance(p, p2) != get_kdtree_distance(p, p4)) {
std::cerr << p << " failed " << p2 << " (" << get_kdtree_distance(p, p2)
<< ") vs. KdTreeV " << p4 << " (" << get_kdtree_distance(p, p4) << ")\n";
}
#endif
}
tsc.stop();
const auto time_linear = tsc.cycles();
tsc.start(); // LinearNeighborSearchV searches {{{2
for (int i = 0; i < NumberOfSearches; ++i) {
const auto &p = searchPoints[i];
const auto &p2 = linearSearchV.findNearest(p);
asm(""::"m"(p2));
#if 0
const auto &p4 = pointsTreeV.findNearest(p);
if (get_kdtree_distance(p, p2) != get_kdtree_distance(p, p4)) {
std::cerr << p << " failed " << p2 << " (" << get_kdtree_distance(p, p2)
<< ") vs. KdTreeV " << p4 << " (" << get_kdtree_distance(p, p4) << ")\n";
}
#endif
}
tsc.stop();
const auto time_linearv = tsc.cycles();
// print search timings {{{2
std::cout << "searches"
<< std::setw(11) << time_kdtree
<< std::setw(11) << time_kdtreev
<< std::setw(22) << time_linear
<< std::setw(23) << time_linearv
<< std::setw(16) << double(time_kdtree) / double(time_kdtreev)
<< std::setw(15) << double(time_linear) / double(time_kdtree)
<< std::setw(16) << double(time_linear) / double(time_linearv) << '\n';
return 0;
} // }}}1
// vim: foldmethod=marker