SimiLie
Loading...
Searching...
No Matches
chain.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include <ddc/ddc.hpp>
7
8#include <similie/misc/are_all_same.hpp>
9#include <similie/misc/portable_stl.hpp>
10#include <similie/misc/specialization.hpp>
11
12#include <Kokkos_StdAlgorithms.hpp>
13
14#include "simplex.hpp"
15
16namespace sil {
17
18namespace exterior {
19
21template <
22 class SimplexType,
23 class LayoutStridedPolicy = Kokkos::LayoutRight,
24 class MemorySpace = Kokkos::HostSpace>
25class Chain
26{
27public:
28 using memory_space = MemorySpace;
29
30 using simplex_type = SimplexType;
31 using simplices_type = Kokkos::View<SimplexType*, LayoutStridedPolicy, memory_space>;
32 using discrete_element_type = typename simplex_type::discrete_element_type;
33 using discrete_vector_type = typename simplex_type::discrete_vector_type;
34
35 using iterator_type = Kokkos::Experimental::Impl::RandomAccessIterator<simplices_type>;
36
37private:
38 static constexpr bool s_is_local = false;
39 static constexpr std::size_t s_k = simplex_type::dimension();
40 simplices_type m_simplices;
41 std::size_t
42 m_size; // Effective size, m_simplices elements between m_size and m_simplices.size() are undefined
43
44public:
45 KOKKOS_DEFAULTED_FUNCTION constexpr Chain() = default;
46
47 KOKKOS_DEFAULTED_FUNCTION constexpr Chain(Chain const&) = default;
48
49 KOKKOS_DEFAULTED_FUNCTION constexpr Chain(Chain&&) = default;
50
51 template <class... T>
52 requires(!std::is_convertible_v<T, std::size_t> && ...)
53 KOKKOS_FUNCTION constexpr explicit Chain(simplices_type allocation, T... simplex) noexcept
54 : m_simplices(std::move(allocation))
55 , m_size(sizeof...(T))
56 {
57 std::size_t i = 0;
58 ((m_simplices(i++) = simplex), ...);
59 assert(check() == 0 && "there are duplicate simplices in the chain");
60 }
61
62 KOKKOS_FUNCTION constexpr explicit Chain(simplices_type allocation, std::size_t size) noexcept
63 : m_simplices(std::move(allocation))
64 , m_size(size)
65 {
66 assert(check() == 0 && "there are duplicate simplices in the chain");
67 }
68
69 KOKKOS_DEFAULTED_FUNCTION ~Chain() = default;
70
71 KOKKOS_DEFAULTED_FUNCTION Chain& operator=(Chain const& other) = default;
72
73 KOKKOS_DEFAULTED_FUNCTION Chain& operator=(Chain&& other) = default;
74
75 static KOKKOS_FUNCTION constexpr bool is_local() noexcept
76 {
77 return s_is_local;
78 }
79
80 static KOKKOS_FUNCTION constexpr std::size_t dimension() noexcept
81 {
82 return s_k;
83 }
84
85 KOKKOS_FUNCTION simplices_type& allocation() noexcept
86 {
87 return m_simplices;
88 }
89
90 KOKKOS_FUNCTION std::size_t size() noexcept
91 {
92 return m_size;
93 }
94
95 KOKKOS_FUNCTION std::size_t size() const noexcept
96 {
97 return m_size;
98 }
99
100 KOKKOS_FUNCTION std::size_t allocation_size() noexcept
101 {
102 return m_simplices.size();
103 }
104
105 KOKKOS_FUNCTION std::size_t allocation_size() const noexcept
106 {
107 return m_simplices.size();
108 }
109
110 void resize()
111 {
112 Kokkos::resize(m_simplices, size());
113 }
114
115 void resize(std::size_t size)
116 {
117 Kokkos::resize(m_simplices, size);
118 }
119
120 KOKKOS_FUNCTION int check()
121 {
122 for (auto i = begin(); i < end() - 1; ++i) {
123 for (auto j = i + 1; j < end(); ++j) {
124 if (*i == *j) {
125 return -1;
126 }
127 }
128 }
129 return 0;
130 }
131
132 KOKKOS_FUNCTION void optimize()
133 {
134 auto i = begin();
135 auto stop = end();
136 while (i < stop - 1) {
137 auto k = i;
138 for (auto j = i + 1; k == i && j < stop; ++j) {
139 if (*i == -*j) {
140 k = j;
141 }
142 }
143 if (k != i) {
144 misc::detail::shift_left(k, stop, 1);
145 misc::detail::shift_left(i, stop, 1);
146 m_size -= 2;
147 stop = end();
148 } else {
149 i++;
150 }
151 }
152 assert(check() == 0 && "there are duplicate simplices in the chain");
153 }
154
155 KOKKOS_FUNCTION auto begin()
156 {
157 return Kokkos::Experimental::begin(m_simplices);
158 }
159
160 KOKKOS_FUNCTION auto begin() const
161 {
162 return Kokkos::Experimental::begin(m_simplices);
163 }
164
165 KOKKOS_FUNCTION auto end()
166 {
167 return Kokkos::Experimental::begin(m_simplices) + size();
168 }
169
170 KOKKOS_FUNCTION auto end() const
171 {
172 return Kokkos::Experimental::begin(m_simplices) + size();
173 }
174
175 KOKKOS_FUNCTION auto cbegin() const
176 {
177 return Kokkos::Experimental::begin(m_simplices);
178 }
179
180 KOKKOS_FUNCTION auto cend() const
181 {
182 return Kokkos::Experimental::begin(m_simplices) + size();
183 }
184
185 KOKKOS_FUNCTION simplex_type& operator[](std::size_t i) noexcept
186 {
187 assert(i < size());
188 return m_simplices(i);
189 }
190
191 KOKKOS_FUNCTION simplex_type const& operator[](std::size_t i) const noexcept
192 {
193 assert(i < size());
194 return m_simplices(i);
195 }
196
197 KOKKOS_FUNCTION Chain& operator++()
198 {
199 assert(size() < allocation_size());
200 m_size++;
201 return *this;
202 }
203
204 KOKKOS_FUNCTION Chain& operator+=(const std::size_t n)
205 {
206 assert(size() + n <= allocation_size());
207 m_size += n;
208 return *this;
209 }
210
211 KOKKOS_FUNCTION Chain& operator+=(const simplex_type& simplex)
212 {
213 assert(size() < allocation_size());
214 m_simplices(m_size) = simplex;
215 m_size++;
216 return *this;
217 }
218
219 KOKKOS_FUNCTION Chain& operator+=(const Chain& simplices_to_add)
220 {
221 assert(size() + simplices_to_add.size() <= allocation_size());
222 for (auto i = simplices_to_add.begin(); i < simplices_to_add.end(); ++i) {
223 m_simplices(m_size + Kokkos::Experimental::distance(simplices_to_add.begin(), i)) = *i;
224 }
225 m_size += simplices_to_add.size();
226 return *this;
227 }
228
229 KOKKOS_FUNCTION Chain operator+(simplex_type simplex)
230 {
231 Chain chain = *this;
232 chain += simplex;
233 return chain;
234 }
235
236 KOKKOS_FUNCTION Chain operator+(Chain simplices_to_add)
237 {
238 Chain chain = *this;
239 chain += simplices_to_add;
240 return chain;
241 }
242
243 KOKKOS_FUNCTION Chain& revert()
244 {
245 for (auto i = Kokkos::Experimental::begin(m_simplices);
246 i < Kokkos::Experimental::begin(m_simplices) + size();
247 ++i) {
248 *i = -*i;
249 }
250 return *this;
251 }
252
253 KOKKOS_FUNCTION Chain operator-()
254 {
255 Chain chain = *this;
256 chain.revert();
257 return chain;
258 }
259
260 template <class T>
261 KOKKOS_FUNCTION auto operator-(T t)
262 {
263 return *this + (-t);
264 }
265
266 template <class T>
267 KOKKOS_FUNCTION Chain& operator*=(T t)
268 {
269 if (t == 1) {
270 } else if (t == -1) {
271 revert();
272 } else {
273 assert(false && "chain must be multiplied by 1 or -1");
274 }
275 return *this;
276 }
277
278 template <class T>
279 KOKKOS_FUNCTION auto operator*(T t)
280 {
281 Chain chain = *this;
282 chain *= t;
283 return chain;
284 }
285
286 KOKKOS_FUNCTION bool operator==(Chain simplices)
287 {
288 for (auto i = simplices.begin(); i < simplices.end(); ++i) {
289 if (*i != m_simplices(Kokkos::Experimental::distance(simplices.begin(), i))) {
290 return false;
291 }
292 }
293 return true;
294 }
295};
296
297template <class Head, class... Tail>
298Chain(Head, Tail...) -> Chain<
299 typename Head::value_type,
300 typename Head::array_layout,
301 typename Head::memory_space>;
302
303template <misc::Specialization<Chain> ChainType>
304std::ostream& operator<<(std::ostream& out, ChainType const& chain)
305{
306 out << "[\n";
307 for (typename ChainType::simplex_type const& simplex : chain) {
308 out << " " << simplex << "\n";
309 }
310 out << "]";
311 return out;
312}
313
314} // namespace exterior
315
316} // namespace sil
Chain class.
Definition chain.hpp:26
void resize(std::size_t size)
Definition chain.hpp:115
KOKKOS_FUNCTION Chain & revert()
Definition chain.hpp:243
KOKKOS_DEFAULTED_FUNCTION constexpr Chain(Chain const &)=default
KOKKOS_FUNCTION std::size_t allocation_size() const noexcept
Definition chain.hpp:105
KOKKOS_FUNCTION auto cend() const
Definition chain.hpp:180
typename simplex_type::discrete_element_type discrete_element_type
Definition chain.hpp:32
KOKKOS_FUNCTION std::size_t allocation_size() noexcept
Definition chain.hpp:100
KOKKOS_FUNCTION bool operator==(Chain simplices)
Definition chain.hpp:286
KOKKOS_FUNCTION Chain operator+(simplex_type simplex)
Definition chain.hpp:229
static KOKKOS_FUNCTION constexpr std::size_t dimension() noexcept
Definition chain.hpp:80
KOKKOS_FUNCTION Chain & operator++()
Definition chain.hpp:197
MemorySpace memory_space
Definition chain.hpp:28
KOKKOS_FUNCTION int check()
Definition chain.hpp:120
KOKKOS_FUNCTION Chain & operator+=(const simplex_type &simplex)
Definition chain.hpp:211
KOKKOS_FUNCTION constexpr Chain(simplices_type allocation, std::size_t size) noexcept
Definition chain.hpp:62
KOKKOS_FUNCTION auto operator-(T t)
Definition chain.hpp:261
KOKKOS_FUNCTION void optimize()
Definition chain.hpp:132
KOKKOS_FUNCTION Chain & operator+=(const Chain &simplices_to_add)
Definition chain.hpp:219
KOKKOS_FUNCTION simplex_type const & operator[](std::size_t i) const noexcept
Definition chain.hpp:191
KOKKOS_FUNCTION Chain operator+(Chain simplices_to_add)
Definition chain.hpp:236
KOKKOS_FUNCTION Chain & operator*=(T t)
Definition chain.hpp:267
KOKKOS_DEFAULTED_FUNCTION constexpr Chain()=default
KOKKOS_FUNCTION Chain & operator+=(const std::size_t n)
Definition chain.hpp:204
KOKKOS_FUNCTION simplex_type & operator[](std::size_t i) noexcept
Definition chain.hpp:185
SimplexType simplex_type
Definition chain.hpp:30
KOKKOS_FUNCTION auto operator*(T t)
Definition chain.hpp:279
KOKKOS_FUNCTION auto cbegin() const
Definition chain.hpp:175
KOKKOS_FUNCTION auto begin() const
Definition chain.hpp:160
Kokkos::View< SimplexType *, LayoutStridedPolicy, memory_space > simplices_type
Definition chain.hpp:31
KOKKOS_DEFAULTED_FUNCTION Chain & operator=(Chain const &other)=default
KOKKOS_FUNCTION auto end()
Definition chain.hpp:165
static KOKKOS_FUNCTION constexpr bool is_local() noexcept
Definition chain.hpp:75
KOKKOS_FUNCTION auto end() const
Definition chain.hpp:170
typename simplex_type::discrete_vector_type discrete_vector_type
Definition chain.hpp:33
KOKKOS_DEFAULTED_FUNCTION ~Chain()=default
Kokkos::Experimental::Impl::RandomAccessIterator< simplices_type > iterator_type
Definition chain.hpp:35
KOKKOS_DEFAULTED_FUNCTION Chain & operator=(Chain &&other)=default
KOKKOS_FUNCTION std::size_t size() const noexcept
Definition chain.hpp:95
KOKKOS_FUNCTION std::size_t size() noexcept
Definition chain.hpp:90
KOKKOS_FUNCTION auto begin()
Definition chain.hpp:155
KOKKOS_FUNCTION Chain operator-()
Definition chain.hpp:253
KOKKOS_FUNCTION simplices_type & allocation() noexcept
Definition chain.hpp:85
KOKKOS_DEFAULTED_FUNCTION constexpr Chain(Chain &&)=default
std::ostream & operator<<(std::ostream &out, ChainType const &chain)
Definition chain.hpp:304
Chain(Head, Tail...) -> Chain< typename Head::value_type, typename Head::array_layout, typename Head::memory_space >
The top-level namespace of SimiLie.
Definition csr.hpp:14