/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/physics/interpolation/linear_interpolator_1d.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/physics/interpolation/linear_interpolator_1d.hh Source File
DiFfRG
Discretization Framework for functional Renormalization Group flows
linear_interpolator_1d.hh
Go to the documentation of this file.
1#pragma once
2
3// DiFfRG
7
8// std
9#include <cstring>
10
11namespace DiFfRG
12{
35 template <typename NT, typename Coordinates> class LinearInterpolator1D
36 {
37 static_assert(Coordinates::dim == 1, "LinearInterpolator1D requires 1D coordinates");
38
40
41 using ViewType = Kokkos::View<NT *, GPU_memory, Kokkos::MemoryTraits<Kokkos::RandomAccess>>;
42 using HostViewType = typename ViewType::host_mirror_type;
43
44 // Without a distinct device space create_mirror_view aliases the source: the two members are
45 // then one allocation, and the upload must not be compiled at all so a host-only build pays no
46 // fence.
47 static constexpr bool has_separate_device =
48 !std::is_same_v<typename ViewType::memory_space, typename HostViewType::memory_space>;
49
50 public:
51 using ctype = typename Coordinates::ctype;
52 using value_type = NT;
53 static constexpr size_t dim = 1;
54
62 device_data("LinearInterpolator1D_data", coordinates.size()),
63 host_data(Kokkos::create_mirror_view(device_data))
64 {
65 }
66
75 KOKKOS_DEFAULTED_FUNCTION LinearInterpolator1D(const LinearInterpolator1D &) = default;
76
95 template <typename NT2> void update(const NT2 *in_data)
96 {
97 if constexpr (std::is_same_v<NT, NT2> && std::is_trivially_copyable_v<NT>)
98 std::memcpy(host_data.data(), in_data, size * sizeof(NT));
99 else
100 for (size_t i = 0; i < size; ++i)
101 host_data(i) = in_data[i];
102
103 if constexpr (has_separate_device) {
104 typename ViewType::execution_space exec;
105 Kokkos::deep_copy(exec, device_data, host_data);
106 exec.fence();
107 }
108 }
109
113 NT operator[](size_t i) const { return host_data(i); }
114
126 typename Coordinates::ctype KOKKOS_FUNCTION index(const typename Coordinates::ctype x) const
127 {
128 return coordinates.backward(x);
129 }
130
134 NT KOKKOS_FUNCTION at(const typename Coordinates::ctype idx) const
135 {
136 // Resolve the stencil: clamped [i, i+1] for a bounded axis, wrapping [i, (i+1) % size] for a periodic one
137 const auto stencil = make_interpolation_stencil<periodic>(idx, size);
138 const auto t = stencil.t;
139 const auto lower = value(stencil.lower);
140 const auto upper = value(stencil.upper);
141 if constexpr (std::is_arithmetic_v<NT>)
142 return Kokkos::fma(t, upper, Kokkos::fma(-t, lower, lower));
143 else
144 return t * upper + (1 - t) * lower;
145 }
146
150 NT KOKKOS_FUNCTION operator()(const typename Coordinates::ctype x) const { return at(index(x)); }
151
157 const Coordinates &get_coordinates() const { return coordinates; }
158
165 const NT *data() const { return host_data.data(); }
166
167 private:
171 KOKKOS_FORCEINLINE_FUNCTION NT value(const size_t i) const
172 {
173 KOKKOS_IF_ON_DEVICE((return device_data(i);))
174 KOKKOS_IF_ON_HOST((return host_data(i);))
175 }
176
177 const Coordinates coordinates;
178 const size_t size;
179
182 };
183} // namespace DiFfRG
A linear interpolator for 1D data, callable from host AND device code.
Definition linear_interpolator_1d.hh:36
ViewType device_data
Definition linear_interpolator_1d.hh:180
static constexpr size_t dim
Definition linear_interpolator_1d.hh:53
NT KOKKOS_FUNCTION at(const typename Coordinates::ctype idx) const
Interpolate at a grid index previously obtained from index().
Definition linear_interpolator_1d.hh:134
HostViewType host_data
Definition linear_interpolator_1d.hh:181
KOKKOS_DEFAULTED_FUNCTION LinearInterpolator1D(const LinearInterpolator1D &)=default
Shallow copy of BOTH views, valid in host and in device code.
NT value_type
Definition linear_interpolator_1d.hh:52
const Coordinates coordinates
Definition linear_interpolator_1d.hh:177
NT KOKKOS_FUNCTION operator()(const typename Coordinates::ctype x) const
Interpolate the data at a given point.
Definition linear_interpolator_1d.hh:150
const size_t size
Definition linear_interpolator_1d.hh:178
KOKKOS_FORCEINLINE_FUNCTION NT value(const size_t i) const
Read one element from whichever buffer belongs to the executing side.
Definition linear_interpolator_1d.hh:171
typename ViewType::host_mirror_type HostViewType
Definition linear_interpolator_1d.hh:42
Coordinates::ctype KOKKOS_FUNCTION index(const typename Coordinates::ctype x) const
Map a physical coordinate onto the grid index.
Definition linear_interpolator_1d.hh:126
NT operator[](size_t i) const
Host-side element access. Always valid, including on a copy.
Definition linear_interpolator_1d.hh:113
void update(const NT2 *in_data)
Replace the data, leaving host AND device current. The only mutator.
Definition linear_interpolator_1d.hh:95
LinearInterpolator1D(const Coordinates &coordinates)
Construct a LinearInterpolator1D with internal, zeroed data and a coordinate system.
Definition linear_interpolator_1d.hh:60
const NT * data() const
Read-only handle to the host values.
Definition linear_interpolator_1d.hh:165
static constexpr bool has_separate_device
Definition linear_interpolator_1d.hh:47
typename Coordinates::ctype ctype
Definition linear_interpolator_1d.hh:51
static constexpr bool periodic
Definition linear_interpolator_1d.hh:39
const Coordinates & get_coordinates() const
Get the coordinate system of the data.
Definition linear_interpolator_1d.hh:157
Kokkos::View< NT *, GPU_memory, Kokkos::MemoryTraits< Kokkos::RandomAccess > > ViewType
Definition linear_interpolator_1d.hh:41
Definition complex_math.hh:10
KOKKOS_FORCEINLINE_FUNCTION InterpolationStencil< CT > make_interpolation_stencil(CT idx, const size_t n)
Resolve a fractional grid index into the linear-interpolation stencil along one axis.
Definition interpolation_stencil.hh:34
constexpr bool is_periodic_coordinate_v
Whether a 1D coordinate class describes a periodic axis, i.e. one where the last grid point is follow...
Definition coordinates.hh:43
Definition kokkos.hh:538