PACMAN 0.1.0
Portable Algorithms for Coupling, Mapping, and Adaptive iNterpolation
Loading...
Searching...
No Matches
callbacks.hpp
Go to the documentation of this file.
1//
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4//
5
6#pragma once
7
8#include <ArborX.hpp>
9#include <ArborX_Point.hpp>
10#include <ArborX_Sphere.hpp>
11
12#include "common/concepts.hpp"
13#include "common/types.hpp"
14#include "utils/operators.hpp"
15
16namespace PACMAN {
17namespace RbfPum {
18
21template <KokkosViewRank<1> ViewType> struct DistanceToKNearest {
22 const int_t k;
23 ViewType samples;
24
25 DistanceToKNearest(int_t k_, ViewType samples_) : k(k_), samples(samples_) {}
26};
27
31 template <typename Predicate, typename Value, typename OutputFunctor>
32 KOKKOS_FUNCTION void operator()(Predicate predicate, Value const &value,
33 OutputFunctor const &out) const {
34 out(SquaredDifference(ArborX::getData(predicate), value.value));
35 }
36};
37
40template <KokkosViewRank<1> ViewType> struct Projection {
41 ViewType centers;
42
43 explicit Projection(ViewType centers_) : centers(centers_) {}
44};
45
49 template <typename Predicate, typename Value, typename OutputFunctor>
50 KOKKOS_FUNCTION void operator()(Predicate predicate, Value const &value,
51 OutputFunctor const &out) const {
52 // <center, projection>
53 out(Kokkos::make_pair(ArborX::getData(predicate), value.value));
54 }
55};
56
59template <KokkosViewRank<1> ViewType> struct TagEmptyCenters {
61
62 explicit TagEmptyCenters(ViewType centers_candidates)
63 : centersCandidates(centers_candidates) {}
64};
65
69template <KokkosViewRank<1> ViewType> struct TagEmptyCentersCallback {
72
73 TagEmptyCentersCallback(ViewType centers_candidates, fp_t threshold_)
74 : centersCandidates(centers_candidates), threshold(threshold_) {}
75
76 template <typename Predicate, typename Value>
77 KOKKOS_FUNCTION void operator()(Predicate predicate,
78 Value const &value) const {
79 const int center_index = ArborX::getData(predicate);
80 // The center should be removed
81 if (SquaredDifference(centersCandidates(center_index), value.value) >
82 threshold) {
83 // Only one match per center, so centersCandidates(center_index) is
84 // accessed only once
85 // No need of atomic operation here
86 centersCandidates(center_index)[0] = NAN;
87 }
88 }
89};
90
93template <KokkosViewRank<1> ViewType> struct TransformToNearest {
95
96 explicit TransformToNearest(ViewType centers_candidates)
97 : centersCandidates(centers_candidates) {}
98};
99
103template <KokkosViewRank<1> ViewType> struct TransformToNearestCallback {
105
106 explicit TransformToNearestCallback(ViewType centers_candidates)
107 : centersCandidates(centers_candidates) {}
108
109 template <typename Predicate, typename Value>
110 KOKKOS_FUNCTION void operator()(Predicate predicate,
111 Value const &value) const {
112 // The center must be projected to the nearest point (value)
113 centersCandidates(ArborX::getData(predicate)) = value.value;
114 }
115};
116
119template <KokkosViewRank<1> ViewType> struct GetClustersPoints {
120 ViewType centers;
122
123 GetClustersPoints(ViewType centers_, coordinates_t radius_)
124 : centers(centers_), radius(radius_) {}
125};
126
127template <KokkosViewRank<1> ViewType>
129
130template <KokkosViewRank<1> ViewType>
132
133template <KokkosViewRank<1> ViewType>
135
136template <KokkosViewRank<1> ViewType>
138
139template <KokkosViewRank<1> ViewType>
141
142template <KokkosViewRank<1> ViewType>
144
145template <KokkosViewRank<1> ViewType>
147
152 template <typename Predicate, typename Value, typename OutputFunctor>
153 KOKKOS_FUNCTION void operator()(Predicate /* predicate */, Value const &value,
154 OutputFunctor const &out) const {
155 out(value.index);
156 }
157};
158
159} // namespace RbfPum
160} // namespace PACMAN
161
162namespace ArborX {
163
167template <PACMAN::KokkosViewRank<1> ViewType>
168struct AccessTraits<PACMAN::RbfPum::DistanceToKNearest<ViewType>> {
169 using memory_space = typename ViewType::memory_space;
171 static KOKKOS_FUNCTION size_t size(const Self &self) {
172 return self.samples.extent(0);
173 }
174 static KOKKOS_FUNCTION auto get(const Self &self, size_t i) {
175 return attach(nearest(self.samples(i), self.k), self.samples(i));
176 }
177};
178
182template <PACMAN::KokkosViewRank<1> ViewType>
183struct AccessTraits<PACMAN::RbfPum::Projection<ViewType>> {
184 using memory_space = typename ViewType::memory_space;
186 static KOKKOS_FUNCTION size_t size(const Self &self) {
187 return self.centers.extent(0);
188 }
189 static KOKKOS_FUNCTION auto get(const Self &self, size_t i) {
190 return attach(nearest(self.centers(i), 1), self.centers(i));
191 }
192};
193
197template <PACMAN::KokkosViewRank<1> ViewType>
198struct AccessTraits<PACMAN::RbfPum::TagEmptyCenters<ViewType>> {
199 using memory_space = typename ViewType::memory_space;
201 using Point = typename ViewType::non_const_value_type;
202 static KOKKOS_FUNCTION size_t size(const Self &self) {
203 return self.centersCandidates.extent(0);
204 }
205 static KOKKOS_FUNCTION auto get(const Self &self, size_t i) {
206 // isNaN
207 if (self.centersCandidates(i)[0] != self.centersCandidates(i)[0]) {
208 return attach(nearest(Point{}, 0), -1);
209 }
210 return attach(nearest(self.centersCandidates(i), 1), (int)i);
211 }
212};
213
217template <PACMAN::KokkosViewRank<1> ViewType>
218struct AccessTraits<PACMAN::RbfPum::TransformToNearest<ViewType>> {
219 using memory_space = typename ViewType::memory_space;
221 using Point = typename ViewType::non_const_value_type;
222 static KOKKOS_FUNCTION size_t size(const Self &self) {
223 return self.centersCandidates.extent(0);
224 }
225 static KOKKOS_FUNCTION auto get(const Self &self, size_t i) {
226 // isNaN
227 if (self.centersCandidates(i)[0] != self.centersCandidates(i)[0]) {
228 return attach(nearest(Point{}, 0), -1);
229 }
230 return attach(nearest(self.centersCandidates(i), 1), (int)i);
231 }
232};
233
237template <PACMAN::KokkosViewRank<1> ViewType>
238struct AccessTraits<PACMAN::RbfPum::GetClustersPoints<ViewType>> {
239 using memory_space = typename ViewType::memory_space;
241 static KOKKOS_FUNCTION size_t size(const Self &self) {
242 return self.centers.extent(0);
243 }
244 static KOKKOS_FUNCTION auto get(const Self &self, size_t i) {
245 return intersects(Sphere(self.centers(i), self.radius));
246 }
247};
248} // namespace ArborX
Projection(ViewType) -> Projection< ViewType >
GetClustersPoints(ViewType, coordinates_t) -> GetClustersPoints< ViewType >
TransformToNearestCallback(ViewType) -> TransformToNearestCallback< ViewType >
TransformToNearest(ViewType) -> TransformToNearest< ViewType >
KOKKOS_INLINE_FUNCTION fp_t SquaredDifference(const ::ArborX::Point< Dim, coordinates_t > &lhs, const ::ArborX::Point< Dim, coordinates_t > &rhs) noexcept
Returns the squared distance between two points, and check for NaN values.
TagEmptyCenters(ViewType) -> TagEmptyCenters< ViewType >
DistanceToKNearest(int_t, ViewType) -> DistanceToKNearest< ViewType >
TagEmptyCentersCallback(ViewType, fp_t) -> TagEmptyCentersCallback< ViewType >
double fp_t
Definition types.hpp:15
double coordinates_t
Definition types.hpp:16
int32_t int_t
Definition types.hpp:18
static KOKKOS_FUNCTION size_t size(const Self &self)
static KOKKOS_FUNCTION auto get(const Self &self, size_t i)
PACMAN::RbfPum::DistanceToKNearest< ViewType > Self
static KOKKOS_FUNCTION size_t size(const Self &self)
static KOKKOS_FUNCTION auto get(const Self &self, size_t i)
static KOKKOS_FUNCTION auto get(const Self &self, size_t i)
static KOKKOS_FUNCTION size_t size(const Self &self)
static KOKKOS_FUNCTION size_t size(const Self &self)
static KOKKOS_FUNCTION auto get(const Self &self, size_t i)
PACMAN::RbfPum::TransformToNearest< ViewType > Self
static KOKKOS_FUNCTION size_t size(const Self &self)
static KOKKOS_FUNCTION auto get(const Self &self, size_t i)
Custom ArborX callback which returns the squared distance between a cluster center and its nearest po...
Definition callbacks.hpp:30
KOKKOS_FUNCTION void operator()(Predicate predicate, Value const &value, OutputFunctor const &out) const
Definition callbacks.hpp:32
Support struct for an ArborX predicate.
Definition callbacks.hpp:21
DistanceToKNearest(int_t k_, ViewType samples_)
Definition callbacks.hpp:25
A custon ArborX callback which returns the indices of the points inside of each region (according to ...
KOKKOS_FUNCTION void operator()(Predicate, Value const &value, OutputFunctor const &out) const
Support struct for an ArborX predicate.
GetClustersPoints(ViewType centers_, coordinates_t radius_)
A custom ArborX callback which returns the nearest mesh point of a given value (the nearest projectio...
Definition callbacks.hpp:48
KOKKOS_FUNCTION void operator()(Predicate predicate, Value const &value, OutputFunctor const &out) const
Definition callbacks.hpp:50
Support struct for an ArborX predicate.
Definition callbacks.hpp:40
Projection(ViewType centers_)
Definition callbacks.hpp:43
A custon ArborX callback which tags empty regions centers, by setting the x axis value of the center ...
Definition callbacks.hpp:69
TagEmptyCentersCallback(ViewType centers_candidates, fp_t threshold_)
Definition callbacks.hpp:73
KOKKOS_FUNCTION void operator()(Predicate predicate, Value const &value) const
Definition callbacks.hpp:77
Support struct for an ArborX predicate.
Definition callbacks.hpp:59
TagEmptyCenters(ViewType centers_candidates)
Definition callbacks.hpp:62
A custom ArborX callback which performs a nearest point projection for all the ArborX::Point in the g...
KOKKOS_FUNCTION void operator()(Predicate predicate, Value const &value) const
TransformToNearestCallback(ViewType centers_candidates)
Support struct for an ArborX predicate.
Definition callbacks.hpp:93
TransformToNearest(ViewType centers_candidates)
Definition callbacks.hpp:96