DDC 0.9.0
Loading...
Searching...
No Matches
discrete_vector.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <array>
8#include <cstddef>
9#include <ostream>
10#include <type_traits>
11#include <utility>
12
13#include <Kokkos_Macros.hpp>
14
15#include "detail/macros.hpp"
16#include "detail/type_seq.hpp"
17#include "detail/utils.hpp"
18
19namespace ddc {
20
21template <class...>
22class DiscreteVector;
23
24template <class T>
25struct is_discrete_vector : std::false_type
26{
27};
28
29template <class... Tags>
30struct is_discrete_vector<DiscreteVector<Tags...>> : std::true_type
31{
32};
33
34template <class T>
35inline constexpr bool is_discrete_vector_v = is_discrete_vector<T>::value;
36
37
38namespace detail {
39
40template <class... Tags>
41struct ToTypeSeq<DiscreteVector<Tags...>>
42{
43 using type = TypeSeq<Tags...>;
44};
45
46} // namespace detail
47
48/** A DiscreteVectorElement is a scalar that represents the difference between two coordinates.
49 */
50using DiscreteVectorElement = std::ptrdiff_t;
51
52template <class QueryTag, class... Tags>
53KOKKOS_FUNCTION constexpr DiscreteVectorElement const& get(
54 DiscreteVector<Tags...> const& tuple) noexcept
55{
56 return tuple.template get<QueryTag>();
57}
58
59template <class QueryTag, class... Tags>
60KOKKOS_FUNCTION constexpr DiscreteVectorElement& get(DiscreteVector<Tags...>& tuple) noexcept
61{
62 return tuple.template get<QueryTag>();
63}
64
65template <class QueryTag, class... Tags>
66KOKKOS_FUNCTION constexpr DiscreteVectorElement const& get_or(
67 DiscreteVector<Tags...> const& tuple,
68 DiscreteVectorElement const& default_value) noexcept
69{
70 return tuple.template get_or<QueryTag>(default_value);
71}
72
73template <class QueryTag, class... Tags>
74KOKKOS_FUNCTION constexpr DiscreteVector<QueryTag> select_or(
75 DiscreteVector<Tags...> const& arr,
76 DiscreteVector<QueryTag> const& default_value) noexcept
77{
78 if constexpr (in_tags_v<QueryTag, detail::TypeSeq<Tags...>>) {
79 return DiscreteVector<QueryTag>(arr);
80 } else {
81 return default_value;
82 }
83}
84
85/// Unary operators: +, -
86
87template <class... Tags>
88KOKKOS_FUNCTION constexpr DiscreteVector<Tags...> operator+(DiscreteVector<Tags...> const& x)
89{
90 return x;
91}
92
93template <class... Tags>
94KOKKOS_FUNCTION constexpr DiscreteVector<Tags...> operator-(DiscreteVector<Tags...> const& x)
95{
96 return DiscreteVector<Tags...>((-get<Tags>(x))...);
97}
98
99/// Internal binary operators: +, -
100
101template <class... Tags, class... OTags>
102KOKKOS_FUNCTION constexpr auto operator+(
103 DiscreteVector<Tags...> const& lhs,
104 DiscreteVector<OTags...> const& rhs)
105{
106 using detail::TypeSeq;
107 if constexpr (sizeof...(Tags) >= sizeof...(OTags)) {
108 static_assert(((type_seq_contains_v<TypeSeq<OTags>, TypeSeq<Tags...>>) && ...));
109 DiscreteVector<Tags...> result(lhs);
110 result += rhs;
111 return result;
112 } else {
113 static_assert(((type_seq_contains_v<TypeSeq<Tags>, TypeSeq<OTags...>>) && ...));
114 DiscreteVector<OTags...> result(rhs);
115 result += lhs;
116 return result;
117 }
118}
119
120template <class... Tags, class... OTags>
121KOKKOS_FUNCTION constexpr auto operator-(
122 DiscreteVector<Tags...> const& lhs,
123 DiscreteVector<OTags...> const& rhs)
124{
125 using detail::TypeSeq;
126 if constexpr (sizeof...(Tags) >= sizeof...(OTags)) {
127 static_assert(((type_seq_contains_v<TypeSeq<OTags>, TypeSeq<Tags...>>) && ...));
128 DiscreteVector<Tags...> result(lhs);
129 result -= rhs;
130 return result;
131 } else {
132 static_assert(((type_seq_contains_v<TypeSeq<Tags>, TypeSeq<OTags...>>) && ...));
133 DiscreteVector<OTags...> result(-rhs);
134 result += lhs;
135 return result;
136 }
137}
138
139template <class Tag, class IntegralType, class = std::enable_if_t<std::is_integral_v<IntegralType>>>
141 DiscreteVector<Tag> const& lhs,
142 IntegralType const& rhs)
143{
144 return DiscreteVector<Tag>(get<Tag>(lhs) + rhs);
145}
146
147template <class IntegralType, class Tag, class = std::enable_if_t<std::is_integral_v<IntegralType>>>
149 IntegralType const& lhs,
150 DiscreteVector<Tag> const& rhs)
151{
152 return DiscreteVector<Tag>(lhs + get<Tag>(rhs));
153}
154
155template <class Tag, class IntegralType, class = std::enable_if_t<std::is_integral_v<IntegralType>>>
157 DiscreteVector<Tag> const& lhs,
158 IntegralType const& rhs)
159{
160 return DiscreteVector<Tag>(get<Tag>(lhs) - rhs);
161}
162
163template <class IntegralType, class Tag, class = std::enable_if_t<std::is_integral_v<IntegralType>>>
165 IntegralType const& lhs,
166 DiscreteVector<Tag> const& rhs)
167{
168 return DiscreteVector<Tag>(lhs - get<Tag>(rhs));
169}
170
171/// external left binary operator: *
172
173template <
174 class IntegralType,
175 class... Tags,
176 class = std::enable_if_t<std::is_integral_v<IntegralType>>>
177KOKKOS_FUNCTION constexpr auto operator*(
178 IntegralType const& lhs,
179 DiscreteVector<Tags...> const& rhs)
180{
181 return DiscreteVector<Tags...>((lhs * get<Tags>(rhs))...);
182}
183
184template <class... QueryTags, class... Tags>
185KOKKOS_FUNCTION constexpr DiscreteVector<QueryTags...> select(
186 DiscreteVector<Tags...> const& arr) noexcept
187{
188 return DiscreteVector<QueryTags...>(arr);
189}
190
191template <class... QueryTags, class... Tags>
192KOKKOS_FUNCTION constexpr DiscreteVector<QueryTags...> select(
193 DiscreteVector<Tags...>&& arr) noexcept
194{
195 return DiscreteVector<QueryTags...>(std::move(arr));
196}
197
198/// Returns a reference towards the DiscreteVector that contains the QueryTag
199template <
200 class QueryTag,
201 class HeadDVect,
202 class... TailDVects,
203 std::enable_if_t<
204 is_discrete_vector_v<HeadDVect> && (is_discrete_vector_v<TailDVects> && ...),
205 int>
206 = 1>
207KOKKOS_FUNCTION constexpr auto const& take(HeadDVect const& head, TailDVects const&... tail)
208{
209 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
210 if constexpr (type_seq_contains_v<detail::TypeSeq<QueryTag>, to_type_seq_t<HeadDVect>>) {
211 static_assert(
212 (!type_seq_contains_v<detail::TypeSeq<QueryTag>, to_type_seq_t<TailDVects>> && ...),
213 "ERROR: tag redundant");
214 return head;
215 } else {
216 static_assert(sizeof...(TailDVects) > 0, "ERROR: tag not found");
217 return take<QueryTag>(tail...);
218 }
219 DDC_IF_NVCC_THEN_POP
220}
221
222namespace detail {
223
224template <class T>
225class DiscreteVectorConversionOperators
226{
227};
228
229template <class Tag>
230class DiscreteVectorConversionOperators<DiscreteVector<Tag>>
231{
232public:
233 // NOLINTBEGIN(google-explicit-constructor)
234 KOKKOS_FUNCTION constexpr operator DiscreteVectorElement const&() const noexcept
235 {
236 return static_cast<DiscreteVector<Tag> const*>(this)->m_values[0];
237 }
238
239 KOKKOS_FUNCTION constexpr operator DiscreteVectorElement&() noexcept
240 {
241 return static_cast<DiscreteVector<Tag>*>(this)->m_values[0];
242 }
243 // NOLINTEND(google-explicit-constructor)
244};
245
246/// Returns a reference to the underlying `std::array`
247template <class... Tags>
248KOKKOS_FUNCTION constexpr std::array<DiscreteVectorElement, sizeof...(Tags)>& array(
249 DiscreteVector<Tags...>& v) noexcept
250{
251 return v.m_values;
252}
253
254/// Returns a reference to the underlying `std::array`
255template <class... Tags>
256KOKKOS_FUNCTION constexpr std::array<DiscreteVectorElement, sizeof...(Tags)> const& array(
257 DiscreteVector<Tags...> const& v) noexcept
258{
259 return v.m_values;
260}
261
262} // namespace detail
263
264/** A DiscreteVector is a vector in the discrete dimension
265 *
266 * Each is tagged by its associated dimensions.
267 */
268template <class... Tags>
269class DiscreteVector : public detail::DiscreteVectorConversionOperators<DiscreteVector<Tags...>>
270{
271 friend class detail::DiscreteVectorConversionOperators<DiscreteVector<Tags...>>;
272
273 friend KOKKOS_FUNCTION constexpr std::array<DiscreteVectorElement, sizeof...(Tags)>& detail::
274 array<Tags...>(DiscreteVector<Tags...>& v) noexcept;
275 friend KOKKOS_FUNCTION constexpr std::array<DiscreteVectorElement, sizeof...(Tags)> const&
276 detail::array<Tags...>(DiscreteVector<Tags...> const& v) noexcept;
277
278 using tags_seq = detail::TypeSeq<Tags...>;
279
280 static_assert(
281 type_seq_is_unique_v<tags_seq>,
282 "The dimensions of a DiscreteVector must be unique");
283
284private:
285 std::array<DiscreteVectorElement, sizeof...(Tags)> m_values;
286
287public:
288 using value_type = DiscreteVectorElement;
289
290 static KOKKOS_FUNCTION constexpr std::size_t size() noexcept
291 {
292 return sizeof...(Tags);
293 }
294
295public:
296 KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector() = default;
297
298 KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector(DiscreteVector const&) = default;
299
301
302 template <class... DVects, class = std::enable_if_t<(is_discrete_vector_v<DVects> && ...)>>
303 KOKKOS_FUNCTION constexpr explicit DiscreteVector(DVects const&... delems) noexcept
304 : m_values {take<Tags>(delems...).template get<Tags>()...}
305 {
306 }
307
308 template <
309 class IntegerType,
310 class = std::enable_if_t<std::is_convertible_v<IntegerType, DiscreteVectorElement>>>
311 KOKKOS_FUNCTION constexpr explicit DiscreteVector(
312 std::array<IntegerType, sizeof...(Tags)> const& values) noexcept
313 : m_values(
314 detail::convert_array_to<DiscreteVectorElement>(
315 values,
316 std::make_index_sequence<sizeof...(Tags)>()))
317 {
318 }
319
320 template <
321 class... Params,
322 class = std::enable_if_t<(!is_discrete_vector_v<Params> && ...)>,
323 class = std::enable_if_t<(std::is_convertible_v<Params, DiscreteVectorElement> && ...)>,
324 class = std::enable_if_t<sizeof...(Params) == sizeof...(Tags)>>
325 KOKKOS_FUNCTION constexpr explicit DiscreteVector(Params const&... params) noexcept
326 : m_values {static_cast<DiscreteVectorElement>(params)...}
327 {
328 }
329
331
333 = default;
334
336
337 template <class... OTags>
338 KOKKOS_FUNCTION constexpr bool operator==(DiscreteVector<OTags...> const& rhs) const noexcept
339 {
340 return ((m_values[type_seq_rank_v<Tags, tags_seq>] == rhs.template get<Tags>()) && ...);
341 }
342
343#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
344 // In C++20, `a!=b` shall be automatically translated by the compiler to `!(a==b)`
345 template <class... OTags>
346 KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector<OTags...> const& rhs) const noexcept
347 {
348 return !(*this == rhs);
349 }
350#endif
351
352 template <class QueryTag>
353 KOKKOS_FUNCTION constexpr DiscreteVectorElement& get() noexcept
354 {
355 static_assert(in_tags_v<QueryTag, tags_seq>, "requested Tag absent from DiscreteVector");
356 return m_values[type_seq_rank_v<QueryTag, tags_seq>];
357 }
358
359 template <class QueryTag>
360 KOKKOS_FUNCTION constexpr DiscreteVectorElement const& get() const noexcept
361 {
362 static_assert(in_tags_v<QueryTag, tags_seq>, "requested Tag absent from DiscreteVector");
363 return m_values[type_seq_rank_v<QueryTag, tags_seq>];
364 }
365
366 template <class QueryTag>
367 KOKKOS_FUNCTION constexpr DiscreteVectorElement const& get_or(
368 DiscreteVectorElement const& default_value) const&
369 {
370 DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
371 if constexpr (in_tags_v<QueryTag, tags_seq>) {
372 return m_values[type_seq_rank_v<QueryTag, tags_seq>];
373 } else {
374 return default_value;
375 }
376 DDC_IF_NVCC_THEN_POP
377 }
378
379 template <std::size_t N = sizeof...(Tags)>
380 KOKKOS_FUNCTION constexpr std::enable_if_t<N == 1, DiscreteVectorElement const&> value()
381 const noexcept
382 {
383 return m_values[0];
384 }
385
386 template <std::size_t N = sizeof...(Tags), class = std::enable_if_t<N == 1>>
388 {
389 ++m_values[0];
390 return *this;
391 }
392
393 template <std::size_t N = sizeof...(Tags), class = std::enable_if_t<N == 1>>
395 {
396 DiscreteVector const tmp = *this;
397 ++m_values[0];
398 return tmp;
399 }
400
401 template <std::size_t N = sizeof...(Tags), class = std::enable_if_t<N == 1>>
403 {
404 --m_values[0];
405 return *this;
406 }
407
408 template <std::size_t N = sizeof...(Tags), class = std::enable_if_t<N == 1>>
410 {
411 DiscreteVector const tmp = *this;
412 --m_values[0];
413 return tmp;
414 }
415
416 template <class... OTags>
417 KOKKOS_FUNCTION constexpr DiscreteVector& operator+=(DiscreteVector<OTags...> const& rhs)
418 {
419 static_assert(((type_seq_contains_v<detail::TypeSeq<OTags>, tags_seq>) && ...));
420 ((m_values[type_seq_rank_v<OTags, tags_seq>] += rhs.template get<OTags>()), ...);
421 return *this;
422 }
423
424 template <
425 class IntegralType,
426 std::size_t N = sizeof...(Tags),
427 class = std::enable_if_t<N == 1>,
428 class = std::enable_if_t<std::is_integral_v<IntegralType>>>
429 KOKKOS_FUNCTION constexpr DiscreteVector& operator+=(IntegralType const& rhs)
430 {
431 m_values[0] += rhs;
432 return *this;
433 }
434
435 template <class... OTags>
436 KOKKOS_FUNCTION constexpr DiscreteVector& operator-=(DiscreteVector<OTags...> const& rhs)
437 {
438 static_assert(((type_seq_contains_v<detail::TypeSeq<OTags>, tags_seq>) && ...));
439 ((m_values[type_seq_rank_v<OTags, tags_seq>] -= rhs.template get<OTags>()), ...);
440 return *this;
441 }
442
443 template <
444 class IntegralType,
445 std::size_t N = sizeof...(Tags),
446 class = std::enable_if_t<N == 1>,
447 class = std::enable_if_t<std::is_integral_v<IntegralType>>>
448 KOKKOS_FUNCTION constexpr DiscreteVector& operator-=(IntegralType const& rhs)
449 {
450 m_values[0] -= rhs;
451 return *this;
452 }
453
454 template <class... OTags>
455 KOKKOS_FUNCTION constexpr DiscreteVector& operator*=(DiscreteVector<OTags...> const& rhs)
456 {
457 static_assert(((type_seq_contains_v<detail::TypeSeq<OTags>, tags_seq>) && ...));
458 ((m_values[type_seq_rank_v<OTags, tags_seq>] *= rhs.template get<OTags>()), ...);
459 return *this;
460 }
461};
462
463template <class Tag>
464KOKKOS_FUNCTION constexpr bool operator<(
465 DiscreteVector<Tag> const& lhs,
466 DiscreteVector<Tag> const& rhs)
467{
468 return lhs.value() < rhs.value();
469}
470
471template <class Tag, class IntegralType>
472KOKKOS_FUNCTION constexpr bool operator<(DiscreteVector<Tag> const& lhs, IntegralType const& rhs)
473{
474 return lhs.value() < rhs;
475}
476
477inline std::ostream& operator<<(std::ostream& out, DiscreteVector<> const&)
478{
479 out << "()";
480 return out;
481}
482
483template <class Head, class... Tags>
484std::ostream& operator<<(std::ostream& out, DiscreteVector<Head, Tags...> const& arr)
485{
486 out << "(";
487 out << get<Head>(arr);
488 ((out << ", " << get<Tags>(arr)), ...);
489 out << ")";
490 return out;
491}
492
493} // namespace ddc
KOKKOS_FUNCTION constexpr DiscreteVector & operator--()
KOKKOS_FUNCTION constexpr DiscreteVectorElement const & get_or(DiscreteVectorElement const &default_value) const &
KOKKOS_FUNCTION constexpr std::enable_if_t< N==1, DiscreteVectorElement const & > value() const noexcept
KOKKOS_FUNCTION constexpr DiscreteVector & operator+=(IntegralType const &rhs)
KOKKOS_DEFAULTED_FUNCTION ~DiscreteVector()=default
KOKKOS_FUNCTION constexpr DiscreteVectorElement const & get() const noexcept
KOKKOS_FUNCTION constexpr DiscreteVector operator--(int)
KOKKOS_FUNCTION constexpr DiscreteVector & operator-=(DiscreteVector< OTags... > const &rhs)
KOKKOS_FUNCTION constexpr DiscreteVector & operator-=(IntegralType const &rhs)
KOKKOS_FUNCTION constexpr DiscreteVector operator++(int)
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector & operator=(DiscreteVector &&other)=default
KOKKOS_FUNCTION constexpr DiscreteVector & operator++()
static KOKKOS_FUNCTION constexpr std::size_t size() noexcept
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector()=default
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector(DiscreteVector const &)=default
KOKKOS_FUNCTION constexpr DiscreteVector(std::array< IntegerType, sizeof...(Tags)> const &values) noexcept
KOKKOS_FUNCTION constexpr DiscreteVector(DVects const &... delems) noexcept
KOKKOS_FUNCTION constexpr DiscreteVectorElement & get() noexcept
KOKKOS_FUNCTION constexpr bool operator==(DiscreteVector< OTags... > const &rhs) const noexcept
KOKKOS_FUNCTION constexpr DiscreteVector & operator*=(DiscreteVector< OTags... > const &rhs)
KOKKOS_FUNCTION constexpr DiscreteVector(Params const &... params) noexcept
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector(DiscreteVector &&)=default
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector & operator=(DiscreteVector const &other)=default
KOKKOS_FUNCTION constexpr DiscreteVector & operator+=(DiscreteVector< OTags... > const &rhs)
The top-level namespace of DDC.
KOKKOS_FUNCTION constexpr DiscreteVector< Tags... > operator+(DiscreteVector< Tags... > const &x)
Unary operators: +, -.
KOKKOS_FUNCTION constexpr auto operator*(IntegralType const &lhs, DiscreteVector< Tags... > const &rhs)
external left binary operator: *
KOKKOS_FUNCTION constexpr bool operator<(DiscreteVector< Tag > const &lhs, IntegralType const &rhs)
KOKKOS_FUNCTION constexpr DiscreteVectorElement const & get_or(DiscreteVector< Tags... > const &tuple, DiscreteVectorElement const &default_value) noexcept
KOKKOS_FUNCTION constexpr auto operator-(DiscreteVector< Tags... > const &lhs, DiscreteVector< OTags... > const &rhs)
KOKKOS_FUNCTION constexpr DiscreteVectorElement & get(DiscreteVector< Tags... > &tuple) noexcept
KOKKOS_FUNCTION constexpr DiscreteVector< Tag > operator-(DiscreteVector< Tag > const &lhs, IntegralType const &rhs)
constexpr bool is_discrete_vector_v
KOKKOS_FUNCTION constexpr DiscreteVectorElement const & get(DiscreteVector< Tags... > const &tuple) noexcept
KOKKOS_FUNCTION constexpr DiscreteVector< Tags... > operator-(DiscreteVector< Tags... > const &x)
KOKKOS_FUNCTION constexpr DiscreteVector< QueryTags... > select(DiscreteVector< Tags... > const &arr) noexcept
KOKKOS_FUNCTION constexpr DiscreteVector< Tag > operator+(IntegralType const &lhs, DiscreteVector< Tag > const &rhs)
KOKKOS_FUNCTION constexpr auto operator+(DiscreteVector< Tags... > const &lhs, DiscreteVector< OTags... > const &rhs)
Internal binary operators: +, -.
KOKKOS_FUNCTION constexpr auto const & take(HeadDVect const &head, TailDVects const &... tail)
Returns a reference towards the DiscreteVector that contains the QueryTag.
KOKKOS_FUNCTION constexpr bool operator<(DiscreteVector< Tag > const &lhs, DiscreteVector< Tag > const &rhs)
KOKKOS_FUNCTION constexpr DiscreteVector< Tag > operator-(IntegralType const &lhs, DiscreteVector< Tag > const &rhs)
KOKKOS_FUNCTION constexpr DiscreteVector< Tag > operator+(DiscreteVector< Tag > const &lhs, IntegralType const &rhs)
KOKKOS_FUNCTION constexpr DiscreteVector< QueryTag > select_or(DiscreteVector< Tags... > const &arr, DiscreteVector< QueryTag > const &default_value) noexcept
KOKKOS_FUNCTION constexpr DiscreteVector< QueryTags... > select(DiscreteVector< Tags... > &&arr) noexcept