Debian Codegraph @

include/deal.II/arborx/distributed_tree.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2022 - 2024 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_arborx_distributed_tree_h
16 #define dealii_arborx_distributed_tree_h
17
18 #include <deal.II/base/config.h>
19
20 #if defined(DEAL_II_ARBORX_WITH_MPI) && defined(DEAL_II_WITH_MPI)
21 # include <deal.II/arborx/access_traits.h>
22
23 # include <deal.II/base/mpi.h>
24
25 # include <ArborX_DistributedTree.hpp>
26 # include <Kokkos_Core.hpp>
27
28 DEAL_II_NAMESPACE_OPEN
29
30 namespace ArborXWrappers
31 {
32 /**
33 * This class implements a wrapper around ArborX::DistributedTree, the
34 * distributed version of ArborX::BVH.
35 */
36 # if ARBORX_VERSION_MAJOR < 2
37 class DistributedTree
38 {
39 public:
40 /**
41 * Constructor. Use a vector of BoundingBox @p bounding_boxes as primitives.
42 * The BoundingBox objects in @p bounding_boxes are local to the MPI process.
43 */
44 template <int dim, typename Number>
45 DistributedTree(
46 const MPI_Comm comm,
47 const std::vector<BoundingBox<dim, Number>> &bounding_boxes);
48
49 /**
50 * Constructor. Use a vector of @p points as primitives. The Point objects
51 * in @p points are local to the MPI process.
52 */
53 template <int dim, typename Number>
54 DistributedTree(const MPI_Comm comm,
55 const std::vector<Point<dim, Number>> &points);
56
57 /**
58 * Return the indices and the MPI ranks of those BoundingBox objects that
59 * satisfy the @p queries.
60 * Because @p queries can contain multiple queries, the function returns a pair
61 * of indices and ranks and the associated offsets.
62 *
63 * Valid `QueryType` classes are
64 * ArborXWrappers::BoundingBoxIntersectPredicate,
65 * ArborXWrappers::BoundingBoxNearestPredicate,
66 * ArborXWrappers::PointIntersectPredicate,
67 * ArborXWrappers::PointNearestPredicate,
68 * ArborXWrappers::SphereIntersectPredicate, and
69 * ArborXWrappers::SphereNearestPredicate.
70 */
71 template <typename QueryType>
72 std::pair<std::vector<std::pair<int, int>>, std::vector<int>>
73 query(const QueryType &queries);
74
75 private:
76 /**
77 * Underlying ArborX object.
78 */
79 ArborX::DistributedTree<Kokkos::HostSpace> distributed_tree;
80 };
81
82
83
84 template <int dim, typename Number>
85 DistributedTree::DistributedTree(
86 const MPI_Comm comm,
87 const std::vector<BoundingBox<dim, Number>> &bounding_boxes)
88 : distributed_tree(comm,
89 Kokkos::DefaultHostExecutionSpace{},
90 bounding_boxes)
91 {}
92
93
94
95 template <int dim, typename Number>
96 DistributedTree::DistributedTree(
97 const MPI_Comm comm,
98 const std::vector<Point<dim, Number>> &points)
99 : distributed_tree(comm, Kokkos::DefaultHostExecutionSpace{}, points)
100 {}
101
102
103
104 template <typename QueryType>
105 std::pair<std::vector<std::pair<int, int>>, std::vector<int>>
106 DistributedTree::query(const QueryType &queries)
107 {
108 Kokkos::View<int *, Kokkos::HostSpace> offsets("offsets", 0);
109 Kokkos::View<Kokkos::pair<int, int> *, Kokkos::HostSpace> indices_ranks(
110 "indices_ranks", 0);
111 distributed_tree.query(Kokkos::DefaultHostExecutionSpace{},
112 queries,
113 indices_ranks,
114 offsets);
115
116 std::vector<std::pair<int, int>> indices_ranks_vector;
117 for (unsigned int i = 0; i < indices_ranks.extent(0); ++i)
118 {
119 indices_ranks_vector.emplace_back(indices_ranks(i).first,
120 indices_ranks(i).second);
121 }
122
123 std::vector<int> offsets_vector;
124 offsets_vector.insert(offsets_vector.begin(),
125 offsets.data(),
126 offsets.data() + offsets.extent(0));
127
128 return {indices_ranks_vector, offsets_vector};
129 }
130 # else
131 template <typename Value>
132 class DistributedTree
133 {
134 public:
135 /**
136 * Constructor. Use a vector of Point, BoundingBox, or sphere
137 * (std::pair<Point,Number>) @p values as primitives. The objects in @p values
138 * are local to the MPI process.
139 */
140 DistributedTree(const MPI_Comm comm, const std::vector<Value> &values);
141
142 /**
143 * Return the indices and the MPI ranks of those BoundingBox objects that
144 * satisfy the @p queries.
145 * Because @p queries can contain multiple queries, the function returns a pair
146 * of indices and ranks and the associated offsets.
147 *
148 * Valid `QueryType` classes are
149 * ArborXWrappers::BoundingBoxIntersectPredicate,
150 * ArborXWrappers::BoundingBoxNearestPredicate,
151 * ArborXWrappers::PointIntersectPredicate,
152 * ArborXWrappers::PointNearestPredicate,
153 * ArborXWrappers::SphereIntersectPredicate, and
154 * ArborXWrappers::SphereNearestPredicate.
155 */
156 template <typename QueryType>
157 std::pair<std::vector<std::pair<int, int>>, std::vector<int>>
158 query(const QueryType &queries);
159
160 private:
161 /**
162 * Underlying ArborX object.
163 */
164 ArborX::DistributedTree<Kokkos::HostSpace,
165 ArborX::PairValueIndex<Value, unsigned int>,
166 internal::IndexableGetter>
167 distributed_tree;
168 /**
169 * Callback to extract the index and the rank of each primitive that
170 * satisfies a query.
171 */
172 internal::ExtractIndexRank callback;
173 };
174
175
176
177 template <typename Value>
178 DistributedTree<Value>::DistributedTree(const MPI_Comm comm,
179 const std::vector<Value> &values)
180 : distributed_tree(comm,
181 Kokkos::DefaultHostExecutionSpace{},
182 ArborX::Experimental::attach_indices(values),
183 internal::IndexableGetter{})
184 , callback{Utilities::MPI::this_mpi_process(comm)}
185 {}
186
187 template <typename Value>
188 template <typename QueryType>
189 std::pair<std::vector<std::pair<int, int>>, std::vector<int>>
190 DistributedTree<Value>::query(const QueryType &queries)
191 {
192 Kokkos::View<int *, Kokkos::HostSpace> offsets("offsets", 0);
193 Kokkos::View<Kokkos::pair<unsigned int, unsigned int> *, Kokkos::HostSpace>
194 indices_ranks("indices_ranks", 0);
195 if constexpr (QueryType::is_nearest)
196 {
197 distributed_tree.query(
198 Kokkos::DefaultHostExecutionSpace{},
199 queries,
200 ArborX::Experimental::declare_callback_constrained(callback),
201 indices_ranks,
202 offsets);
203 }
204 else
205 {
206 distributed_tree.query(Kokkos::DefaultHostExecutionSpace{},
207 queries,
208 callback,
209 indices_ranks,
210 offsets);
211 }
212
213 std::vector<std::pair<int, int>> indices_ranks_vector;
214 for (unsigned int i = 0; i < indices_ranks.extent(0); ++i)
215 {
216 indices_ranks_vector.emplace_back(indices_ranks(i).first,
217 indices_ranks(i).second);
218 }
219
220 std::vector<int> offsets_vector;
221 offsets_vector.insert(offsets_vector.begin(),
222 offsets.data(),
223 offsets.data() + offsets.extent(0));
224
225 return {indices_ranks_vector, offsets_vector};
226 }
227
228 # endif
229 } // namespace ArborXWrappers
230
231 DEAL_II_NAMESPACE_CLOSE
232
233 #else
234
235 // Make sure the scripts that create the C++20 module input files have
236 // something to latch on if the preprocessor #ifdef above would
237 // otherwise lead to an empty content of the file.
238 DEAL_II_NAMESPACE_OPEN
239 DEAL_II_NAMESPACE_CLOSE
240
241 #endif
242 #endif