-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathotm_search.cpp
265 lines (220 loc) · 9.13 KB
/
otm_search.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
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
#include <hpc_execution.hpp>
#include <hpc_functional.hpp>
#include <hpc_macros.hpp>
#include <hpc_range.hpp>
#include <hpc_range_sum.hpp>
#include <hpc_vector.hpp>
#include <lgr_mesh_indices.hpp>
#include <lgr_state.hpp>
#include <limits>
#include <otm_meshing.hpp>
#include <otm_search.hpp>
#include <otm_search_util.hpp>
using namespace lgr::search_util;
#ifdef LGR_ENABLE_SEARCH
#include <otm_arborx_search_impl.hpp>
#endif
namespace lgr {
namespace search {
#ifdef LGR_ENABLE_SEARCH
void
initialize_otm_search()
{
arborx::initialize();
}
void
finalize_otm_search()
{
arborx::finalize();
}
namespace {
template <typename QueryViewType, typename IndexType>
HPC_NOINLINE void
do_search_and_fill_counts(
const arborx::device_point_view& search_points,
const QueryViewType& queries,
const hpc::counting_range<IndexType>& search_indices,
arborx::device_int_view& offsets,
arborx::device_int_view& indices,
hpc::device_vector<int, IndexType>& search_counts)
{
arborx::do_search(search_points, queries, indices, offsets);
auto counts = search_counts.begin();
auto count_func = [=] HPC_DEVICE(IndexType point) {
auto point_begin = offsets(hpc::weaken(point));
auto point_end = offsets(hpc::weaken(point) + 1);
counts[point] = int(point_end - point_begin);
};
hpc::for_each(hpc::device_policy(), search_indices, count_func);
}
template <typename Index1, typename Index2, typename Index1to2Ordinal>
HPC_NOINLINE void
size_and_fill_lgr_data_structures(
const hpc::counting_range<Index1>& search_indices,
const arborx::device_int_view& indices,
const hpc::device_vector<int, Index1>& search_counts,
hpc::device_range_sum<Index1to2Ordinal, Index1>& search_result_ranges,
hpc::device_vector<Index2, Index1to2Ordinal>& search_results)
{
search_result_ranges.assign_sizes(search_counts);
auto new_results_size = hpc::reduce(hpc::device_policy(), search_counts, 0);
search_results.resize(new_results_size);
auto points_results_ranges = search_result_ranges.cbegin();
auto points_to_results = search_results.begin();
auto fill_func = [=] HPC_DEVICE(Index1 point) {
auto const point_results_range = points_results_ranges[point];
for (auto result : point_results_range) {
points_to_results[result] = Index2(indices(hpc::weaken(result)));
}
};
hpc::for_each(hpc::device_policy(), search_indices, fill_func);
}
template <typename IndexType, typename IndexOrdinalType>
HPC_NOINLINE void
size_and_fill_lgr_data_structures_from_symmetric_search(
const hpc::counting_range<IndexType>& search_indices,
const arborx::device_int_view& indices,
const arborx::device_int_view& offsets,
hpc::device_vector<int, IndexType>& search_counts,
hpc::device_range_sum<IndexOrdinalType, IndexType>& search_result_ranges,
hpc::device_vector<IndexType, IndexOrdinalType>& search_results)
{
// this version filters search indices from the results where index == result,
// which implies that a node or point found itself in the search.
auto counts = search_counts.begin();
auto count_func = [=] HPC_DEVICE(IndexType point) { counts[point] -= 1; };
hpc::for_each(hpc::device_policy(), search_indices, count_func);
search_result_ranges.assign_sizes(search_counts);
auto new_results_size = hpc::reduce(hpc::device_policy(), search_counts, 0);
search_results.resize(new_results_size);
auto points_results_ranges = search_result_ranges.cbegin();
auto points_to_results = search_results.begin();
auto fill_func = [=] HPC_DEVICE(IndexType point) {
auto const point_results_range = points_results_ranges[point];
auto non_self_result_index = offsets(hpc::weaken(point));
for (auto result : point_results_range) {
if (indices(non_self_result_index) == point) ++non_self_result_index;
points_to_results[result] = IndexType(indices(non_self_result_index));
++non_self_result_index;
}
};
hpc::for_each(hpc::device_policy(), search_indices, fill_func);
}
template <typename QueryViewType, typename Index1, typename Index2, typename Index1to2Ordinal>
HPC_NOINLINE void
do_search_and_fill_lgr_data_structures(
const arborx::device_point_view& search_points,
const QueryViewType& queries,
const hpc::counting_range<Index1>& search_indices,
hpc::device_range_sum<Index1to2Ordinal, Index1>& search_result_ranges,
hpc::device_vector<Index2, Index1to2Ordinal>& search_results)
{
arborx::device_int_view offsets("offsets", 0);
arborx::device_int_view indices("indices", 0);
hpc::device_vector<int, Index1> index_counts(search_indices.size());
do_search_and_fill_counts(search_points, queries, search_indices, offsets, indices, index_counts);
size_and_fill_lgr_data_structures(search_indices, indices, index_counts, search_result_ranges, search_results);
}
} // namespace
HPC_NOINLINE void
do_otm_point_nearest_node_search(lgr::state& s, int max_support_nodes_per_point)
{
auto search_nodes = arborx::create_arborx_nodes(s);
auto search_points = arborx::create_arborx_points(s);
auto queries = arborx::make_nearest_node_queries(search_points, max_support_nodes_per_point);
do_search_and_fill_lgr_data_structures(
search_nodes, queries, s.points, s.points_to_point_nodes, s.point_nodes_to_nodes);
invert_otm_point_node_relations(s);
}
namespace {
template <typename T, typename Range, typename Policy>
HPC_NOINLINE inline T
reduce_min(Policy p, Range& r, T init)
{
return hpc::transform_reduce(p, r, init, hpc::minimum<T>(), hpc::identity<T>());
}
} // namespace
void
do_otm_iterative_point_support_search(lgr::state& s, int min_support_nodes_per_point)
{
auto search_nodes = arborx::create_arborx_nodes(s);
auto search_spheres = arborx::create_arborx_point_spheres(s);
auto queries = arborx::make_intersect_sphere_queries(search_spheres);
hpc::device_vector<int, point_index> counts(s.points.size());
arborx::device_int_view offsets("offsets", 0);
arborx::device_int_view indices("indices", 0);
int min_nodes_in_support_over_all_points = 0;
const double inflation_factor = 1.2;
while (min_nodes_in_support_over_all_points < min_support_nodes_per_point) {
arborx::inflate_sphere_query_radii(queries, inflation_factor);
do_search_and_fill_counts(search_nodes, queries, s.points, offsets, indices, counts);
min_nodes_in_support_over_all_points = reduce_min(hpc::device_policy(), counts, hpc::numeric_limits<int>::max());
}
size_and_fill_lgr_data_structures(s.points, indices, counts, s.points_to_point_nodes, s.point_nodes_to_nodes);
invert_otm_point_node_relations(s);
}
HPC_NOINLINE void
do_otm_node_nearest_node_search(const lgr::state& s, nearest_neighbors<node_index>& n, int max_nodes_per_node)
{
auto search_points = arborx::create_arborx_nodes(s);
auto queries = arborx::make_nearest_node_queries(search_points, max_nodes_per_node + 1);
hpc::device_vector<int, point_index> counts(s.nodes.size());
arborx::device_int_view offsets("offsets", 0);
arborx::device_int_view indices("indices", 0);
do_search_and_fill_counts(search_points, queries, s.nodes, offsets, indices, counts);
size_and_fill_lgr_data_structures_from_symmetric_search(
s.nodes, indices, offsets, counts, n.entities_to_neighbor_ordinals, n.entities_to_neighbors);
}
HPC_NOINLINE void
do_otm_point_nearest_point_search(const lgr::state& s, nearest_neighbors<point_index>& n, int max_points_per_point)
{
auto search_points = arborx::create_arborx_points(s);
auto queries = arborx::make_nearest_node_queries(search_points, max_points_per_point + 1);
hpc::device_vector<int, point_index> counts(s.points.size());
arborx::device_int_view offsets("offsets", 0);
arborx::device_int_view indices("indices", 0);
do_search_and_fill_counts(search_points, queries, s.points, offsets, indices, counts);
size_and_fill_lgr_data_structures_from_symmetric_search(
s.points, indices, offsets, counts, n.entities_to_neighbor_ordinals, n.entities_to_neighbors);
}
#else // ! LGR_ENABLE_SEARCH
namespace {
HPC_NOINLINE void
search_not_enabled_error()
{
throw std::runtime_error("ArborX search not enabled! Rebuild with LGR_ENABLE_SEARCH=ON.");
}
} // namespace
void
initialize_otm_search()
{
search_not_enabled_error();
}
void
finalize_otm_search()
{
search_not_enabled_error();
}
HPC_NOINLINE void
do_otm_point_nearest_node_search(lgr::state&, int)
{
search_not_enabled_error();
}
void
do_otm_iterative_point_support_search(lgr::state&, int)
{
search_not_enabled_error();
}
HPC_NOINLINE void
do_otm_node_nearest_node_search(const lgr::state&, nearest_neighbors<node_index>&, int)
{
search_not_enabled_error();
}
HPC_NOINLINE void
do_otm_point_nearest_point_search(const lgr::state&, nearest_neighbors<point_index>&, int)
{
search_not_enabled_error();
}
#endif
} // namespace search
} // namespace lgr