forked from pooyan-dadvand/SearchLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchLab.cpp
More file actions
299 lines (233 loc) · 9.63 KB
/
SearchLab.cpp
File metadata and controls
299 lines (233 loc) · 9.63 KB
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
//System includes
#include <fstream>
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
#include <iomanip>
// Point
#include "point.h"
// Ugly fixes
#include <assert.h>
#define KRATOS_ERROR std::cout
// Kratos Independent
#define KRATOS_INDEPENDENT
// Kratos includes
#include "spatial_containers/spatial_containers.h"
#include "points_bins.h"
#include "points_hash.h"
double GetCurrentTime() {
#ifndef _OPENMP
return std::clock() / static_cast<double>(CLOCKS_PER_SEC);
#else
return omp_get_wtime();
#endif
}
template< class T, std::size_t dim >
class PointDistance2 {
public:
double operator()(T const& p1, T const& p2) {
double dist = 0.0;
for (std::size_t i = 0; i < dim; i++) {
double tmp = p1[i] - p2[i];
dist += tmp*tmp;
}
return dist;
}
};
template<class SearchStructureType, class PointType, class IteratorType, class DistanceIterator>
void RunTestsOldInterface(char const Title[], IteratorType PBegin, IteratorType PEnd, IteratorType results0, DistanceIterator distances0, std::size_t MaxResults, PointType* allPoints, double radius0, std::size_t numsearch, std::size_t bucket_size) {
double t0, t1;
std::size_t n;
std::size_t npoints = PEnd - PBegin;
std::vector<std::size_t> numresArray(numsearch);
PointType& point0 = allPoints[0];
std::size_t numsearch_nearest = 10 * numsearch;
t0 = GetCurrentTime();
SearchStructureType nodes_tree(PBegin, PEnd);
t1 = GetCurrentTime();
std::cout << Title << "\t" << t1 - t0 << "\t";
t0 = GetCurrentTime();
#pragma omp parallel for
for (std::size_t i = 0; i < numsearch; i++) {
n = nodes_tree.SearchInRadius(point0, radius0, results0, distances0, MaxResults);
}
t1 = GetCurrentTime();
std::cout << t1 - t0 << "\t";
t0 = GetCurrentTime();
for (std::size_t i = 0; i < numsearch; i++) {
n = nodes_tree.SearchInRadius(point0, radius0, results0, distances0, MaxResults);
}
t1 = GetCurrentTime();
std::cout << t1 - t0 << "\t";
PointType** PNearestArray = new PointType*[numsearch];
std::vector<double> distancesArrayNearest(numsearch);
PointType* PNearest = PNearestArray[0];
t0 = GetCurrentTime();
for (std::size_t i = 0; i < 10; i++) {
nodes_tree.SearchNearestPoint(allPoints, numsearch, PNearestArray, distancesArrayNearest);
}
t1 = GetCurrentTime();
std::cout << t1 - t0 << "\t";
double distance = 0;
t0 = GetCurrentTime();
for (std::size_t i = 0; i < numsearch_nearest; i++) {
PNearest = nodes_tree.SearchNearestPoint(point0, distance);
}
t1 = GetCurrentTime();
std::cout << t1 - t0 << "\t";
std::cout << n << "\t" << *PNearestArray[0] << std::endl;
};
template<class BinsType>
void RunTestsNewInterface(char const Title[], std::vector<Point<3>> & points_vector, const Point<3> & search_point, double radius, std::size_t numsearch, std::size_t numsearch_nearest) {
double t0 = GetCurrentTime();
BinsType bins(points_vector.begin(), points_vector.end());
double t1 = GetCurrentTime();
std::cout << "Points Bin" << "\t" << t1 - t0 << "\t";
std::vector<PointsBins<Point<3>>::ResultType> results;
PointsBins<Point<3>>::ResultType nearest_point_result;
t0 = GetCurrentTime();
#pragma omp parallel for firstprivate(results)
for (std::size_t i = 0; i < numsearch; i++) {
results.clear();
bins.SearchInRadius(search_point, radius, results);
}
t1 = GetCurrentTime();
std::cout << t1 - t0 << "\t";
t0 = GetCurrentTime();
for (std::size_t i = 0; i < numsearch; i++) {
results.clear();
bins.SearchInRadius(search_point, radius, results);
}
t1 = GetCurrentTime();
std::cout << t1 - t0 << "\t";
t0 = GetCurrentTime();
#pragma omp parallel for firstprivate(nearest_point_result)
for (std::size_t i = 0; i < numsearch_nearest; i++) {
nearest_point_result = bins.SearchNearest(search_point);
}
t1 = GetCurrentTime();
std::cout << t1 - t0 << "\t";
t0 = GetCurrentTime();
for (std::size_t i = 0; i < numsearch_nearest; i++) {
nearest_point_result = bins.SearchNearest(search_point);
}
t1 = GetCurrentTime();
std::cout << t1 - t0 << "\t" << results.size() << "\t" << *nearest_point_result.Get();
std::cout << std::endl;
}
int RunPointSearchComparison(std::string Filename, double Radius) {
constexpr std::size_t Dim = 3;
typedef Point<Dim> PointType;
typedef PointType * PtrPointType;
typedef PtrPointType * PointVector;
typedef PtrPointType * PointIterator;
typedef double* DistanceVector;
typedef double* DistanceIterator;
typedef Kratos::Bucket<Dim, PointType, PointVector, PtrPointType, PointIterator, DistanceIterator, PointDistance2<PointType, Dim>> bucket_type; //Bucket;
typedef Kratos::Bins<Dim, PointType, PointVector, PtrPointType, PointIterator, DistanceIterator, PointDistance2<PointType, Dim>> StaticBinsType; //StaticBins;
typedef Kratos::BinsDynamic<Dim, PointType, PointVector, PtrPointType, PointIterator, DistanceIterator, PointDistance2<PointType, Dim>> DynamicBinsType; //DynamicBins;
typedef Kratos::Tree< Kratos::KDTreePartition<bucket_type>> kdtree_type; //Kdtree;
typedef Kratos::Tree< Kratos::KDTreePartitionAverageSplit<bucket_type>> kdtree_average_split_type; //Kdtree;
typedef Kratos::Tree< Kratos::KDTreePartitionMidPointSplit<bucket_type>> kdtree_midpoint_split_type; //Kdtree;
typedef Kratos::Tree< Kratos::OCTreePartition<bucket_type>> OctreeType; //Octree;
typedef Kratos::Tree< Kratos::KDTreePartitionMidPointSplit<StaticBinsType>> kdtree_StaticBinsType; //KdtreeBins;
typedef Kratos::Tree< Kratos::OCTreePartition<StaticBinsType>> octree_StaticBinsType; //OctreeBins;
typedef Kratos::Tree< Kratos::KDTreePartitionMidPointSplit<DynamicBinsType>> kdtree_DynamicBinsType; //KdtreeBins;
typedef Kratos::Tree< Kratos::OCTreePartition<DynamicBinsType>> octree_bins_type; //OctreeBins;
// Input data
std::cout << std::setprecision(4) << std::fixed;
PointVector points;
std::ifstream input;
input.open(Filename.c_str());
if (!input) {
std::cout << "Cannot open data file" << std::endl;
return 0;
}
std::cout << "Comparison for " << Filename << std::endl;
PointType point;
std::size_t npoints;
input >> npoints;
points = new PointType*[npoints];
std::size_t pid;
for (std::size_t i = 0; i < npoints; i++) {
input >> pid;
input >> point;
points[i] = new PointType(point);
points[i]->id = pid;
}
PointType min_point(*points[0]);
PointType max_point(*points[0]);
PointType mid_point;
min_point.id = 0;
max_point.id = 0;
mid_point.id = 0;
for (std::size_t i = 0; i < npoints; i++) {
for (std::size_t j = 0; j < 3; j++) {
if (min_point[j] > (*points[i])[j]) min_point[j] = (*points[i])[j];
if (max_point[j] < (*points[i])[j]) max_point[j] = (*points[i])[j];
}
}
for (std::size_t i = 0; i < Dim; i++) {
mid_point.coord[i] = (max_point[i] + min_point[i]) / 2.00;
}
// Output data Info
PointType & search_point = mid_point;
std::size_t numsearch = 100000;
std::size_t numsearch_nearest = numsearch * 10;
std::cout << " min point : " << min_point << std::endl;
std::cout << " max point : " << max_point << std::endl;
std::cout << " search_point : " << search_point << std::endl;
std::cout << " search radius : " << Radius << std::endl;
std::cout << std::endl;
std::cout << " Number of Points : " << npoints << std::endl;
std::cout << " Number of Repetitions : " << numsearch << std::endl;
std::cout << std::endl;
std::cout << "SS\t\tGEN\tSIROMP\tSIRSER\tSNPOMP\tSNPSER\tNOFR\tNP" << std::endl;
// Data Setup
PointType * allPoints;
allPoints = new PointType[numsearch];
std::size_t max_results = npoints;
for (std::size_t i = 0; i < 1; i++) {
allPoints[i] = search_point;
}
//Prepare the search point, search radius and resut arrays
DistanceIterator distances = new double[npoints];
PointIterator p_results = new PtrPointType[max_results];
// Point-Based Search Structures
std::vector<Point<3>> points_vector;
for (std::size_t i = 0; i < npoints; i++) {
points_vector.push_back(*(points[i]));
}
// New Interface
RunTestsNewInterface<PointsBins<Point<3>>>("PointBins", points_vector, search_point, Radius, numsearch, numsearch_nearest);
// Old Interface
RunTestsOldInterface<StaticBinsType>("StaticBins", points, points + npoints, p_results, distances, max_results, allPoints, Radius, numsearch, 1);
RunTestsOldInterface<DynamicBinsType>("DynamicBins", points, points + npoints, p_results, distances, max_results, allPoints, Radius, numsearch, 1);
RunTestsOldInterface<OctreeType>("OcTree\t", points, points + npoints, p_results, distances, max_results, allPoints, Radius, numsearch, 10);
//RunTestsOldInterface<kdtree_type>("KdTree\t", points, points + npoints, p_results, distances, max_results, allPoints, radius, numsearch, 10);
//RunTestsOldInterface<kdtree_average_split_type>("KdTreeAverage", points, points + npoints, resultsArray, distancesArray, max_results, allPoints, radiusArray, numsearch, 10);
//RunTestsOldInterface<kdtree_midpoint_split_type>("KdTreeMidpoint", points, points + npoints, resultsArray, distancesArray, max_results, allPoints, radiusArray, numsearch, 10);
return 0;
}
int main(int arg, char* argv[]) {
std::string filename;
double radius = 0.01;
if (arg > 1) {
std::cout << "Argument not founded " << std::endl;
filename = argv[1];
if (arg == 3) {
radius = atof(argv[2]) / 1000000;
}
return 0;
}
filename = "genericCube100x100x100.5051.pts";
RunPointSearchComparison(filename, radius);
filename = "offsetCube79x79x79.1603.pts";
RunPointSearchComparison(filename, radius);
filename = "clusterCube6x6x6X4913.490.pts";
RunPointSearchComparison(filename, radius);
filename = "line100000.5.pts";
RunPointSearchComparison(filename, radius);
return 0;
}