DDC 0.0.0

a discrete domain computation library

non_uniform_point_sampling.hpp
1 // SPDX-License-Identifier: MIT
2 
3 #pragma once
4 
5 #include <cassert>
6 #include <type_traits>
7 #include <vector>
8 
9 #include <Kokkos_Core.hpp>
10 
11 #include "ddc/coordinate.hpp"
12 #include "ddc/detail/macros.hpp"
13 #include "ddc/discrete_element.hpp"
14 #include "ddc/discrete_space.hpp"
15 #include "ddc/discrete_vector.hpp"
16 
17 namespace ddc {
18 
20 template <class CDim>
22 {
23 public:
25 
27 
28 
30 
32 
34 
36 
37 public:
38  template <class MemorySpace>
39  class Impl
40  {
41  template <class OMemorySpace>
42  friend class Impl;
43 
44  Kokkos::View<continuous_element_type*, MemorySpace> m_points;
45 
46  public:
48 
49  Impl() = default;
50 
52  explicit Impl(std::initializer_list<continuous_element_type> points)
53  {
54  std::vector<continuous_element_type> host_points(points.begin(), points.end());
55  Kokkos::View<continuous_element_type*, Kokkos::HostSpace>
56  host(host_points.data(), host_points.size());
57  Kokkos::resize(m_points, host.extent(0));
58  Kokkos::deep_copy(m_points, host);
59  }
60 
62  template <class InputRange>
63  explicit inline constexpr Impl(InputRange const& points)
64  {
65  if constexpr (Kokkos::is_view<InputRange>::value) {
66  Kokkos::deep_copy(m_points, points);
67  } else {
68  std::vector<continuous_element_type> host_points(points.begin(), points.end());
69  Kokkos::View<continuous_element_type*, Kokkos::HostSpace>
70  host(host_points.data(), host_points.size());
71  Kokkos::resize(m_points, host.extent(0));
72  Kokkos::deep_copy(m_points, host);
73  }
74  }
75 
77  template <class InputIt>
78  inline constexpr Impl(InputIt points_begin, InputIt points_end)
79  {
80  std::vector<continuous_element_type> host_points(points_begin, points_end);
81  Kokkos::View<continuous_element_type*, Kokkos::HostSpace>
82  host(host_points.data(), host_points.size());
83  Kokkos::resize(m_points, host.extent(0));
84  Kokkos::deep_copy(m_points, host);
85  }
86 
87  template <class OriginMemorySpace>
88  explicit Impl(Impl<OriginMemorySpace> const& impl)
89  : m_points(Kokkos::create_mirror_view_and_copy(MemorySpace(), impl.m_points))
90  {
91  }
92 
93  Impl(Impl const& x) = delete;
94 
95  Impl(Impl&& x) = default;
96 
97  ~Impl() = default;
98 
99  constexpr std::size_t size() const
100  {
101  return m_points.size();
102  }
103 
106  discrete_element_type const& icoord) const noexcept
107  {
108  return m_points(icoord.uid());
109  }
110  };
111 };
112 
113 template <class>
114 struct is_non_uniform_sampling : public std::false_type
115 {
116 };
117 
118 template <class CDim>
119 struct is_non_uniform_sampling<NonUniformPointSampling<CDim>> : public std::true_type
120 {
121 };
122 
123 template <class DDim>
125 
126 template <
127  class DDimImpl,
128  std::enable_if_t<
129  is_non_uniform_sampling_v<typename DDimImpl::discrete_dimension_type>,
130  int> = 0>
131 std::ostream& operator<<(std::ostream& out, DDimImpl const& mesh)
132 {
133  return out << "NonUniformPointSampling(" << mesh.size() << ")";
134 }
135 
136 template <class CDim>
137 DDC_INLINE_FUNCTION Coordinate<CDim> coordinate(
139 {
140  return discrete_space<NonUniformPointSampling<CDim>>().coordinate(c);
141 }
142 
143 template <class CDim>
144 DDC_INLINE_FUNCTION Coordinate<CDim> distance_at_left(
146 {
147  return coordinate(i) - coordinate(i - 1);
148 }
149 
150 template <class CDim>
153 {
154  return coordinate(i + 1) - coordinate(i);
155 }
156 
157 template <class CDim>
159 {
160  return coordinate(d.front());
161 }
162 
163 template <class CDim>
165 {
166  return coordinate(d.back());
167 }
168 
169 template <class CDim>
171 {
172  return rmax(d) - rmin(d);
173 }
174 
175 } // namespace ddc
Definition: discrete_domain.hpp:24
A DiscreteElement identifies an element of the discrete dimension.
Definition: discrete_element.hpp:124
A DiscreteVector is a vector in the discrete dimension.
Definition: discrete_vector.hpp:208
Definition: non_uniform_point_sampling.hpp:40
Impl(std::initializer_list< continuous_element_type > points)
Construct a NonUniformPointSampling using a brace-list, i.e. NonUniformPointSampling mesh({0....
Definition: non_uniform_point_sampling.hpp:52
constexpr std::size_t size() const
Definition: non_uniform_point_sampling.hpp:99
Impl(Impl< OriginMemorySpace > const &impl)
Definition: non_uniform_point_sampling.hpp:88
constexpr continuous_element_type coordinate(discrete_element_type const &icoord) const noexcept
Convert a mesh index into a position in CDim
Definition: non_uniform_point_sampling.hpp:105
constexpr Impl(InputRange const &points)
Construct a NonUniformPointSampling using a C++20 "common range".
Definition: non_uniform_point_sampling.hpp:63
constexpr Impl(InputIt points_begin, InputIt points_end)
Construct a NonUniformPointSampling using a pair of iterators.
Definition: non_uniform_point_sampling.hpp:78
Definition: aligned_allocator.hpp:9
ddc::Coordinate< QueryDDims... > rmin(DiscreteDomain< DDims... > const &domain) noexcept
Definition: discrete_domain.hpp:270
ddc::Coordinate< QueryDDims... > coordinate(DiscreteDomain< DDims... > const &domain, DiscreteElement< QueryDDims... > const &icoord) noexcept
Definition: discrete_domain.hpp:261
std::ostream & operator<<(std::ostream &out, DiscreteElement<> const &)
Definition: discrete_element.hpp:300
CDim continuous_dimension_type
Definition: non_uniform_point_sampling.hpp:24
DDC_INLINE_FUNCTION Coordinate< CDim > distance_at_left(DiscreteElement< NonUniformPointSampling< CDim >> i)
Definition: non_uniform_point_sampling.hpp:144
Coordinate< CDim > continuous_element_type
Definition: non_uniform_point_sampling.hpp:26
constexpr bool is_non_uniform_sampling_v
Definition: non_uniform_point_sampling.hpp:124
DDC_INLINE_FUNCTION Coordinate< CDim > distance_at_right(DiscreteElement< NonUniformPointSampling< CDim >> i)
Definition: non_uniform_point_sampling.hpp:151
ddc_detail::TaggedVector< CoordinateElement, CDims... > Coordinate
A Coordinate represents a coordinate in the continuous space.
Definition: coordinate.hpp:19
DDC_INLINE_FUNCTION Coordinate< CDim > rlength(DiscreteDomain< NonUniformPointSampling< CDim >> const &d)
Definition: non_uniform_point_sampling.hpp:170
ddc::Coordinate< QueryDDims... > rmax(DiscreteDomain< DDims... > const &domain) noexcept
Definition: discrete_domain.hpp:276
NonUniformPointSampling models a non-uniform discretization of the CDim segment .
Definition: non_uniform_point_sampling.hpp:22
Definition: non_uniform_point_sampling.hpp:115