DDC 0.17.0
Loading...
Searching...
No Matches
discrete_domain.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 <cstddef>
8#include <iosfwd>
9#include <iterator>
10#include <span>
11#include <tuple>
12#include <type_traits>
13#include <utility>
14
15#include <Kokkos_Assert.hpp>
16#include <Kokkos_Macros.hpp>
17
21#include "type_seq.hpp"
22
23namespace ddc {
24
25template <concepts::discrete_dimension DDim>
27
28template <concepts::discrete_dimension... DDims>
29class DiscreteDomain;
30
31template <class T>
32struct is_discrete_domain : std::false_type
33{
34};
35
36template <class... Tags>
37struct is_discrete_domain<DiscreteDomain<Tags...>> : std::true_type
38{
39};
40
41template <class T>
42inline constexpr bool is_discrete_domain_v = is_discrete_domain<T>::value;
43
44namespace concepts {
45
46template <class T>
47concept discrete_domain = is_discrete_domain_v<T>;
48
49}
50
51namespace detail {
52
53template <class... Tags>
54struct ToTypeSeq<DiscreteDomain<Tags...>>
55{
56 using type = ddc::TypeSeq<Tags...>;
57};
58
59template <class... DDims, class... ODDims>
60struct Rebind<DiscreteDomain<DDims...>, ddc::TypeSeq<ODDims...>>
61{
62 using type = DiscreteDomain<ODDims...>;
63};
64
65} // namespace detail
66
67template <concepts::discrete_dimension... DDims>
69{
70 template <concepts::discrete_dimension...>
71 friend class DiscreteDomain;
72
73 static_assert(
74 type_seq_is_unique_v<TypeSeq<DDims...>>,
75 "The dimensions of a DiscreteDomain must be unique");
76
77public:
78 using discrete_element_type = DiscreteElement<DDims...>;
79
80 using discrete_vector_type = DiscreteVector<DDims...>;
81
82private:
83 DiscreteElement<DDims...> m_element_begin;
84
85 DiscreteElement<DDims...> m_element_end;
86
87public:
88 static KOKKOS_FUNCTION constexpr std::size_t rank()
89 {
90 return sizeof...(DDims);
91 }
92
93 KOKKOS_DEFAULTED_FUNCTION DiscreteDomain() = default;
94
95 /// Construct a DiscreteDomain by copies and merge of domains
96 template <concepts::discrete_domain... DDoms>
97 KOKKOS_FUNCTION constexpr explicit DiscreteDomain(DDoms const&... domains)
98 : m_element_begin(domains.front()...)
99 , m_element_end((domains.front() + domains.extents())...)
100 {
101 }
102
103 /** Construct a DiscreteDomain starting from element_begin with size points.
104 * @param element_begin the lower bound in each direction
105 * @param size the number of points in each direction
106 */
107 KOKKOS_FUNCTION constexpr DiscreteDomain(
108 discrete_element_type const& element_begin,
109 discrete_vector_type const& size)
110 : m_element_begin(element_begin)
111 , m_element_end(element_begin + size)
112 {
113 }
114
115 KOKKOS_DEFAULTED_FUNCTION DiscreteDomain(DiscreteDomain const& x) = default;
116
117 KOKKOS_DEFAULTED_FUNCTION DiscreteDomain(DiscreteDomain&& x) = default;
118
119 KOKKOS_DEFAULTED_FUNCTION ~DiscreteDomain() = default;
120
121 KOKKOS_DEFAULTED_FUNCTION DiscreteDomain& operator=(DiscreteDomain const& x) = default;
122
123 KOKKOS_DEFAULTED_FUNCTION DiscreteDomain& operator=(DiscreteDomain&& x) = default;
124
125 template <concepts::discrete_dimension... ODDims>
126 KOKKOS_FUNCTION constexpr bool operator==(DiscreteDomain<ODDims...> const& other) const
127 {
128 if (empty() && other.empty()) {
129 return true;
130 }
131 return m_element_begin == other.m_element_begin && m_element_end == other.m_element_end;
132 }
133
134#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
135 // In C++20, `a!=b` shall be automatically translated by the compiler to `!(a==b)`
136 template <concepts::discrete_dimension... ODDims>
137 KOKKOS_FUNCTION constexpr bool operator!=(DiscreteDomain<ODDims...> const& other) const
138 {
139 return !(*this == other);
140 }
141#endif
142
143 KOKKOS_FUNCTION constexpr std::size_t size() const
144 {
145 return (1UL * ... * extent<DDims>().value());
146 }
147
148 KOKKOS_FUNCTION constexpr discrete_vector_type extents() const noexcept
149 {
150 return m_element_end - m_element_begin;
151 }
152
153 template <concepts::discrete_dimension QueryDDim>
154 KOKKOS_FUNCTION constexpr DiscreteVector<QueryDDim> extent() const noexcept
155 {
156 return DiscreteElement<QueryDDim>(m_element_end)
157 - DiscreteElement<QueryDDim>(m_element_begin);
158 }
159
160 KOKKOS_FUNCTION constexpr discrete_element_type front() const noexcept
161 {
162 return m_element_begin;
163 }
164
165 KOKKOS_FUNCTION constexpr discrete_element_type back() const noexcept
166 {
167 return discrete_element_type((DiscreteElement<DDims>(m_element_end) - 1)...);
168 }
169
170 KOKKOS_FUNCTION constexpr DiscreteDomain take_first(discrete_vector_type n) const
171 {
172 return DiscreteDomain(front(), n);
173 }
174
175 KOKKOS_FUNCTION constexpr DiscreteDomain take_last(discrete_vector_type n) const
176 {
177 return DiscreteDomain(front() + (extents() - n), n);
178 }
179
180 KOKKOS_FUNCTION constexpr DiscreteDomain remove_first(discrete_vector_type n) const
181 {
182 return DiscreteDomain(front() + n, extents() - n);
183 }
184
185 KOKKOS_FUNCTION constexpr DiscreteDomain remove_last(discrete_vector_type n) const
186 {
187 return DiscreteDomain(front(), extents() - n);
188 }
189
190 KOKKOS_FUNCTION constexpr DiscreteDomain remove(
191 discrete_vector_type n1,
192 discrete_vector_type n2) const
193 {
194 return DiscreteDomain(front() + n1, extents() - n1 - n2);
195 }
196
197 KOKKOS_FUNCTION constexpr DiscreteElement<DDims...> operator()(
198 DiscreteVector<DDims...> const& dvect) const noexcept
199 {
200 return m_element_begin + dvect;
201 }
202
203 template <concepts::discrete_dimension... ODDims>
204 KOKKOS_FUNCTION constexpr auto restrict_with(DiscreteDomain<ODDims...> const& odomain) const
205 {
206 KOKKOS_ASSERT(
207 ((DiscreteElement<ODDims>(m_element_begin)
208 <= DiscreteElement<ODDims>(odomain.m_element_begin))
209 && ...))
210 KOKKOS_ASSERT(
211 ((DiscreteElement<ODDims>(m_element_end)
212 >= DiscreteElement<ODDims>(odomain.m_element_end))
213 && ...))
214 DiscreteVector<DDims...> const myextents = extents();
215 DiscreteVector<ODDims...> const oextents = odomain.extents();
216 return DiscreteDomain(
217 DiscreteElement<DDims...>((select_or<DDims>(
218 odomain.m_element_begin,
219 DiscreteElement<DDims>(m_element_begin)))...),
220 DiscreteVector<DDims...>(
221 (select_or<DDims>(oextents, DiscreteVector<DDims>(myextents)))...));
222 }
223
224 template <concepts::discrete_element... DElems>
225 KOKKOS_FUNCTION bool contains(DElems const&... delems) const noexcept
226 {
227 static_assert(
228 sizeof...(DDims) == (0 + ... + DElems::size()),
229 "Invalid number of dimensions");
230 static_assert((is_discrete_element_v<DElems> && ...), "Expected DiscreteElements");
231 DiscreteElement<DDims...> const delem(delems...);
232 for (std::size_t i = 0; i < rank(); ++i) {
233 if ((detail::array(delem)[i] < detail::array(m_element_begin)[i])
234 || (detail::array(delem)[i] >= detail::array(m_element_end)[i])) {
235 return false;
236 }
237 }
238 return true;
239 }
240
241 template <concepts::discrete_element... DElems>
242 KOKKOS_FUNCTION DiscreteVector<DDims...> distance_from_front(
243 DElems const&... delems) const noexcept
244 {
245 static_assert(
246 sizeof...(DDims) == (0 + ... + DElems::size()),
247 "Invalid number of dimensions");
248 static_assert((is_discrete_element_v<DElems> && ...), "Expected DiscreteElements");
249 return DiscreteVector<DDims...>(
250 (DiscreteElement<DDims>(take<DDims>(delems...))
251 - DiscreteElement<DDims>(m_element_begin))...);
252 }
253
254 KOKKOS_FUNCTION constexpr bool empty() const noexcept
255 {
256 return size() == 0;
257 }
258
259 KOKKOS_FUNCTION constexpr explicit operator bool()
260 {
261 return !empty();
262 }
263
264 KOKKOS_FUNCTION auto begin() const
265 requires(sizeof...(DDims) == 1)
266 {
267 return DiscreteDomainIterator<std::tuple_element_t<0, std::tuple<DDims...>>>(front());
268 }
269
270 KOKKOS_FUNCTION auto end() const
271 requires(sizeof...(DDims) == 1)
272 {
273 return DiscreteDomainIterator<std::tuple_element_t<0, std::tuple<DDims...>>>(m_element_end);
274 }
275
276 KOKKOS_FUNCTION auto cbegin() const
277 requires(sizeof...(DDims) == 1)
278 {
279 return DiscreteDomainIterator<std::tuple_element_t<0, std::tuple<DDims...>>>(front());
280 }
281
282 KOKKOS_FUNCTION auto cend() const
283 requires(sizeof...(DDims) == 1)
284 {
285 return DiscreteDomainIterator<std::tuple_element_t<0, std::tuple<DDims...>>>(m_element_end);
286 }
287
288 KOKKOS_FUNCTION constexpr decltype(auto) operator[](std::size_t n)
289 requires(sizeof...(DDims) == 1)
290 {
291 return begin()[n];
292 }
293
294 KOKKOS_FUNCTION constexpr decltype(auto) operator[](std::size_t n) const
295 requires(sizeof...(DDims) == 1)
296 {
297 return begin()[n];
298 }
299};
300
301template <>
302class DiscreteDomain<>
303{
304 template <concepts::discrete_dimension...>
305 friend class DiscreteDomain;
306
307public:
308 using discrete_element_type = DiscreteElement<>;
309
310 using discrete_vector_type = DiscreteVector<>;
311
312 static KOKKOS_FUNCTION constexpr std::size_t rank()
313 {
314 return 0;
315 }
316
317 KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteDomain() = default;
318
319 /// Construct a DiscreteDomain by copies and merge of domains
320 template <class... DDoms>
321 KOKKOS_FUNCTION constexpr explicit DiscreteDomain(DDoms const&... /*domains*/)
322 requires(is_discrete_domain_v<DDoms> && ...)
323 {
324 }
325
326 /** Construct a DiscreteDomain starting from element_begin with size points.
327 * @param element_begin the lower bound in each direction
328 * @param size the number of points in each direction
329 */
330 KOKKOS_FUNCTION constexpr DiscreteDomain(
331 [[maybe_unused]] discrete_element_type const& element_begin,
332 [[maybe_unused]] discrete_vector_type const& size)
333 {
334 // Parameters `element_begin` and `size` are always unused, but needed for Doxygen
335 }
336
337 KOKKOS_DEFAULTED_FUNCTION DiscreteDomain(DiscreteDomain const& x) = default;
338
339 KOKKOS_DEFAULTED_FUNCTION DiscreteDomain(DiscreteDomain&& x) = default;
340
341 KOKKOS_DEFAULTED_FUNCTION ~DiscreteDomain() = default;
342
343 KOKKOS_DEFAULTED_FUNCTION DiscreteDomain& operator=(DiscreteDomain const& x) = default;
344
345 KOKKOS_DEFAULTED_FUNCTION DiscreteDomain& operator=(DiscreteDomain&& x) = default;
346
347 KOKKOS_FUNCTION constexpr bool operator==(DiscreteDomain const& /*other*/) const
348 {
349 return true;
350 }
351
352#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
353 // In C++20, `a!=b` shall be automatically translated by the compiler to `!(a==b)`
354 KOKKOS_FUNCTION constexpr bool operator!=(DiscreteDomain const& other) const
355 {
356 return !(*this == other);
357 }
358#endif
359
360 static KOKKOS_FUNCTION constexpr std::size_t size()
361 {
362 return 1;
363 }
364
365 static KOKKOS_FUNCTION constexpr discrete_vector_type extents() noexcept
366 {
367 return {};
368 }
369
370 static KOKKOS_FUNCTION constexpr discrete_element_type front() noexcept
371 {
372 return {};
373 }
374
375 static KOKKOS_FUNCTION constexpr discrete_element_type back() noexcept
376 {
377 return {};
378 }
379
380 KOKKOS_FUNCTION constexpr DiscreteDomain take_first(discrete_vector_type /*n*/) const
381 {
382 return *this;
383 }
384
385 KOKKOS_FUNCTION constexpr DiscreteDomain take_last(discrete_vector_type /*n*/) const
386 {
387 return *this;
388 }
389
390 KOKKOS_FUNCTION constexpr DiscreteDomain remove_first(discrete_vector_type /*n*/) const
391 {
392 return *this;
393 }
394
395 KOKKOS_FUNCTION constexpr DiscreteDomain remove_last(discrete_vector_type /*n*/) const
396 {
397 return *this;
398 }
399
400 KOKKOS_FUNCTION constexpr DiscreteDomain remove(
401 discrete_vector_type /*n1*/,
402 discrete_vector_type /*n2*/) const
403 {
404 return *this;
405 }
406
407 KOKKOS_FUNCTION constexpr DiscreteElement<> operator()(
408 DiscreteVector<> const& /*dvect*/) const noexcept
409 {
410 return {};
411 }
412
413 template <concepts::discrete_dimension... ODDims>
414 KOKKOS_FUNCTION constexpr DiscreteDomain restrict_with(
415 DiscreteDomain<ODDims...> const& /*odomain*/) const
416 {
417 return *this;
418 }
419
420 static KOKKOS_FUNCTION bool contains() noexcept
421 {
422 return true;
423 }
424
425 static KOKKOS_FUNCTION bool contains(DiscreteElement<>) noexcept
426 {
427 return true;
428 }
429
430 static KOKKOS_FUNCTION DiscreteVector<> distance_from_front() noexcept
431 {
432 return {};
433 }
434
435 static KOKKOS_FUNCTION DiscreteVector<> distance_from_front(DiscreteElement<>) noexcept
436 {
437 return {};
438 }
439
440 static KOKKOS_FUNCTION constexpr bool empty() noexcept
441 {
442 return false;
443 }
444
445 KOKKOS_FUNCTION constexpr explicit operator bool()
446 {
447 return true;
448 }
449};
450
451template <concepts::discrete_dimension... QueryDDims, concepts::discrete_dimension... DDims>
452KOKKOS_FUNCTION constexpr DiscreteDomain<QueryDDims...> select(
453 DiscreteDomain<DDims...> const& domain)
454{
455 return DiscreteDomain<QueryDDims...>(domain);
456}
457
458namespace detail {
459
460template <ddc::concepts::type_seq T>
461struct ConvertTypeSeqToDiscreteDomain
462{
463};
464
465template <ddc::concepts::discrete_dimension... DDims>
466struct ConvertTypeSeqToDiscreteDomain<ddc::TypeSeq<DDims...>>
467{
468 using type = DiscreteDomain<DDims...>;
469};
470
471template <ddc::concepts::type_seq T>
472using convert_type_seq_to_discrete_domain_t = ConvertTypeSeqToDiscreteDomain<T>::type;
473
474} // namespace detail
475
476// Computes the subtraction DDom_a - DDom_b in the sense of linear spaces(retained dimensions are those in DDom_a which are not in DDom_b)
477template <concepts::discrete_dimension... DDimsA, concepts::discrete_dimension... DDimsB>
478KOKKOS_FUNCTION constexpr auto remove_dims_of(
479 DiscreteDomain<DDimsA...> const& DDom_a,
480 DiscreteDomain<DDimsB...> const& /*DDom_b*/) noexcept
481{
482 using TagSeqA = TypeSeq<DDimsA...>;
483 using TagSeqB = TypeSeq<DDimsB...>;
484
485 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
486 return detail::convert_type_seq_to_discrete_domain_t<type_seq_r>(DDom_a);
487}
488
489//! Remove the dimensions DDimsB from DDom_a
490//! @param[in] DDom_a The discrete domain on which to remove dimensions
491//! @return The discrete domain without DDimsB dimensions
492template <concepts::discrete_dimension... DDimsB, concepts::discrete_dimension... DDimsA>
493KOKKOS_FUNCTION constexpr auto remove_dims_of(DiscreteDomain<DDimsA...> const& DDom_a) noexcept
494{
495 using TagSeqA = TypeSeq<DDimsA...>;
496 using TagSeqB = TypeSeq<DDimsB...>;
497
498 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
499 return detail::convert_type_seq_to_discrete_domain_t<type_seq_r>(DDom_a);
500}
501
502//! Type alias for the discrete domain obtained by removing dimensions DDims from DDom.
503//! @tparam DDom The discrete domain type from which to remove dimensions.
504//! @tparam DDims The discrete dimensions to remove.
505//! @return The discrete domain type without the DDims dimensions.
506template <typename DDom, concepts::discrete_dimension... DDims>
507using remove_dims_of_t = decltype(remove_dims_of<DDims...>(std::declval<DDom>()));
508
509namespace detail {
510
511// Checks if dimension of DDom_a is DDim1. If not, returns restriction to DDim2 of DDom_b. May not be useful in its own, it helps for replace_dim_of
512template <
513 ddc::concepts::discrete_dimension DDim1,
514 ddc::concepts::discrete_dimension DDim2,
515 ddc::concepts::discrete_dimension DDimA,
516 ddc::concepts::discrete_dimension... DDimsB>
517KOKKOS_FUNCTION constexpr std::conditional_t<
518 std::is_same_v<DDimA, DDim1>,
519 ddc::DiscreteDomain<DDim2>,
520 ddc::DiscreteDomain<DDimA>>
521replace_dim_of_1d(
522 [[maybe_unused]] DiscreteDomain<DDimA> const& DDom_a,
523 [[maybe_unused]] DiscreteDomain<DDimsB...> const& DDom_b) noexcept
524{
525 if constexpr (std::is_same_v<DDimA, DDim1>) {
526 return ddc::DiscreteDomain<DDim2>(DDom_b);
527 } else {
528 return DDom_a;
529 }
530}
531
532} // namespace detail
533
534// Replace in DDom_a the dimension Dim1 by the dimension Dim2 of DDom_b
535template <
536 concepts::discrete_dimension DDim1,
537 concepts::discrete_dimension DDim2,
538 concepts::discrete_dimension... DDimsA,
539 concepts::discrete_dimension... DDimsB>
540KOKKOS_FUNCTION constexpr auto replace_dim_of(
541 DiscreteDomain<DDimsA...> const& DDom_a,
542 [[maybe_unused]] DiscreteDomain<DDimsB...> const& DDom_b) noexcept
543{
544 // TODO : static_asserts
545 using TagSeqA = TypeSeq<DDimsA...>;
546 using TagSeqB = TypeSeq<DDim1>;
547 using TagSeqC = TypeSeq<DDim2>;
548
549 using type_seq_r = ddc::type_seq_replace_t<TagSeqA, TagSeqB, TagSeqC>;
550 return ddc::detail::convert_type_seq_to_discrete_domain_t<type_seq_r>(
551 detail::replace_dim_of_1d<
552 DDim1,
553 DDim2,
554 DDimsA,
555 DDimsB...>(ddc::DiscreteDomain<DDimsA>(DDom_a), DDom_b)...);
556}
557
558// Replace dimensions from a domain type
559template <typename DDom, concepts::discrete_dimension DDim1, concepts::discrete_dimension DDim2>
560using replace_dim_of_t = decltype(replace_dim_of<DDim1, DDim2>(
561 std::declval<DDom>(),
562 std::declval<detail::rebind_t<DDom, TypeSeq<DDim2>>>()));
563
564
565template <concepts::discrete_dimension... QueryDDims, concepts::discrete_dimension... DDims>
566KOKKOS_FUNCTION constexpr DiscreteVector<QueryDDims...> extents(
567 DiscreteDomain<DDims...> const& domain) noexcept
568{
569 return DiscreteVector<QueryDDims...>(DiscreteDomain<QueryDDims>(domain).size()...);
570}
571
572template <concepts::discrete_dimension... QueryDDims, concepts::discrete_dimension... DDims>
573KOKKOS_FUNCTION constexpr DiscreteElement<QueryDDims...> front(
574 DiscreteDomain<DDims...> const& domain) noexcept
575{
576 return DiscreteElement<QueryDDims...>(DiscreteDomain<QueryDDims>(domain).front()...);
577}
578
579template <concepts::discrete_dimension... QueryDDims, concepts::discrete_dimension... DDims>
580KOKKOS_FUNCTION constexpr DiscreteElement<QueryDDims...> back(
581 DiscreteDomain<DDims...> const& domain) noexcept
582{
583 return DiscreteElement<QueryDDims...>(DiscreteDomain<QueryDDims>(domain).back()...);
584}
585
586template <concepts::discrete_dimension DDim>
588{
589private:
590 DiscreteElement<DDim> m_value = DiscreteElement<DDim>();
591
592public:
593 using iterator_category = std::random_access_iterator_tag;
594
595 using value_type = DiscreteElement<DDim>;
596
597 using difference_type = std::ptrdiff_t;
598
599 KOKKOS_DEFAULTED_FUNCTION DiscreteDomainIterator() = default;
600
601 KOKKOS_FUNCTION constexpr explicit DiscreteDomainIterator(DiscreteElement<DDim> value)
602 : m_value(value)
603 {
604 }
605
606 KOKKOS_FUNCTION constexpr DiscreteElement<DDim> operator*() const noexcept
607 {
608 return m_value;
609 }
610
611 KOKKOS_FUNCTION constexpr DiscreteDomainIterator& operator++()
612 {
613 ++m_value;
614 return *this;
615 }
616
617 KOKKOS_FUNCTION constexpr DiscreteDomainIterator operator++(int)
618 {
619 auto tmp = *this;
620 ++*this;
621 return tmp;
622 }
623
624 KOKKOS_FUNCTION constexpr DiscreteDomainIterator& operator--()
625 {
626 --m_value;
627 return *this;
628 }
629
630 KOKKOS_FUNCTION constexpr DiscreteDomainIterator operator--(int)
631 {
632 auto tmp = *this;
633 --*this;
634 return tmp;
635 }
636
637 KOKKOS_FUNCTION constexpr DiscreteDomainIterator& operator+=(difference_type n)
638 {
639 if (n >= static_cast<difference_type>(0)) {
640 m_value += static_cast<DiscreteElementType>(n);
641 } else {
642 m_value -= static_cast<DiscreteElementType>(-n);
643 }
644 return *this;
645 }
646
647 KOKKOS_FUNCTION constexpr DiscreteDomainIterator& operator-=(difference_type n)
648 {
649 if (n >= static_cast<difference_type>(0)) {
650 m_value -= static_cast<DiscreteElementType>(n);
651 } else {
652 m_value += static_cast<DiscreteElementType>(-n);
653 }
654 return *this;
655 }
656
657 KOKKOS_FUNCTION constexpr DiscreteElement<DDim> operator[](difference_type n) const
658 {
659 return m_value + n;
660 }
661
662 friend KOKKOS_FUNCTION constexpr bool operator==(
663 DiscreteDomainIterator const& xx,
664 DiscreteDomainIterator const& yy)
665 {
666 return xx.m_value == yy.m_value;
667 }
668
669#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
670 // In C++20, `a!=b` shall be automatically translated by the compiler to `!(a==b)`
671 friend KOKKOS_FUNCTION constexpr bool operator!=(
674 {
675 return xx.m_value != yy.m_value;
676 }
677#endif
678
679 friend KOKKOS_FUNCTION constexpr bool operator<(
680 DiscreteDomainIterator const& xx,
681 DiscreteDomainIterator const& yy)
682 {
683 return xx.m_value < yy.m_value;
684 }
685
686 friend KOKKOS_FUNCTION constexpr bool operator>(
687 DiscreteDomainIterator const& xx,
688 DiscreteDomainIterator const& yy)
689 {
690 return yy < xx;
691 }
692
693 friend KOKKOS_FUNCTION constexpr bool operator<=(
694 DiscreteDomainIterator const& xx,
695 DiscreteDomainIterator const& yy)
696 {
697 return !(yy < xx);
698 }
699
700 friend KOKKOS_FUNCTION constexpr bool operator>=(
701 DiscreteDomainIterator const& xx,
702 DiscreteDomainIterator const& yy)
703 {
704 return !(xx < yy);
705 }
706
707 friend KOKKOS_FUNCTION constexpr DiscreteDomainIterator operator+(
709 difference_type n)
710 {
711 return i += n;
712 }
713
714 friend KOKKOS_FUNCTION constexpr DiscreteDomainIterator operator+(
715 difference_type n,
717 {
718 return i += n;
719 }
720
721 friend KOKKOS_FUNCTION constexpr DiscreteDomainIterator operator-(
723 difference_type n)
724 {
725 return i -= n;
726 }
727
728 friend KOKKOS_FUNCTION constexpr difference_type operator-(
729 DiscreteDomainIterator const& xx,
730 DiscreteDomainIterator const& yy)
731 {
732 return (yy.m_value > xx.m_value) ? (-static_cast<difference_type>(yy.m_value - xx.m_value))
733 : (xx.m_value - yy.m_value);
734 }
735};
736
737namespace detail {
738
739void print_discrete_domain(
740 std::ostream& os,
741 bool empty,
742 std::span<DiscreteElementType const> front,
743 std::span<DiscreteElementType const> back);
744
745} // namespace detail
746
747/**
748 * Print a DiscreteDomain. Only the front and back elements are displayed.
749 *
750 * For example `DiscreteDomain(front=(0, 1), back=(9, 19))`, or
751 * `DiscreteDomain(empty)` if the domain contains no element.
752 */
753template <concepts::discrete_dimension... DDims>
754std::ostream& operator<<(std::ostream& os, DiscreteDomain<DDims...> const& domain)
755{
756 detail::print_discrete_domain(
757 os,
758 domain.empty(),
759 detail::array(domain.front()),
760 detail::array(domain.back()));
761 return os;
762}
763
764} // namespace ddc
friend class ChunkSpan
KOKKOS_FUNCTION constexpr ChunkCommon(ElementType *ptr, SupportType const &domain)
Constructs a new ChunkCommon from scratch.
KOKKOS_FUNCTION constexpr bool is_strided() const noexcept
KOKKOS_DEFAULTED_FUNCTION ~ChunkCommon() noexcept=default
KOKKOS_FUNCTION constexpr ElementType * data_handle() const
Access to the underlying allocation pointer.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon()=default
Empty ChunkCommon.
allocation_mdspan_type m_allocation_mdspan
The raw view of the data.
KOKKOS_FUNCTION constexpr SupportType domain() const noexcept
Provide access to the domain on which this chunk is defined.
static KOKKOS_FUNCTION constexpr bool is_always_strided() noexcept
KOKKOS_FUNCTION constexpr allocation_mdspan_type allocation_mdspan() const
Provide a modifiable view of the data.
KOKKOS_FUNCTION constexpr ChunkCommon(allocation_mdspan_type allocation_mdspan, SupportType const &domain) noexcept
Constructs a new ChunkCommon from scratch.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon(ChunkCommon const &other)=default
Constructs a new ChunkCommon by copy, yields a new view to the same data.
static KOKKOS_FUNCTION constexpr bool is_always_unique() noexcept
KOKKOS_FUNCTION constexpr DiscreteDomain< QueryDDims... > domain() const noexcept
Provide access to the domain on which this chunk is defined.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon & operator=(ChunkCommon const &other)=default
Copy-assigns a new value to this ChunkCommon, yields a new view to the same data.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon(ChunkCommon &&other) noexcept=default
Constructs a new ChunkCommon by move.
KOKKOS_DEFAULTED_FUNCTION constexpr ChunkCommon & operator=(ChunkCommon &&other) noexcept=default
Move-assigns a new value to this ChunkCommon.
KOKKOS_FUNCTION constexpr size_type extent() const noexcept
KOKKOS_FUNCTION constexpr size_type stride() const
KOKKOS_FUNCTION constexpr bool is_exhaustive() const noexcept
KOKKOS_FUNCTION constexpr mapping_type mapping() const noexcept
static KOKKOS_FUNCTION constexpr int rank() noexcept
KOKKOS_FUNCTION constexpr bool is_unique() const noexcept
KOKKOS_FUNCTION constexpr accessor_type accessor() const
KOKKOS_FUNCTION constexpr SupportType::discrete_vector_type extents() const noexcept
KOKKOS_FUNCTION constexpr size_type size() const noexcept
SupportType m_domain
The mesh on which this chunk is defined.
static KOKKOS_FUNCTION constexpr int rank_dynamic() noexcept
static KOKKOS_FUNCTION constexpr size_type static_extent(std::size_t r) noexcept
static KOKKOS_FUNCTION constexpr bool is_always_exhaustive() noexcept
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec) const
Slice out some dimensions.
Definition chunk.hpp:170
auto operator[](DiscreteVector< QueryDDims... > const &slice_spec) const
Slice out some dimensions.
Definition chunk.hpp:156
char const * label() const
Returns the label of the Chunk.
Definition chunk.hpp:265
const_allocation_mdspan_type allocation_mdspan() const
Provide a mdspan on the memory allocation.
Definition chunk.hpp:289
ElementType const * data_handle() const
Access to the underlying allocation pointer.
Definition chunk.hpp:273
auto allocation_kokkos_view()
Provide an unmanaged Kokkos::View on the memory allocation.
Definition chunk.hpp:305
auto operator[](DiscreteDomain< QueryDDims... > const &odomain)
Slice out some dimensions.
Definition chunk.hpp:192
auto allocation_kokkos_view() const
Provide an unmanaged Kokkos::View on the memory allocation.
Definition chunk.hpp:321
Chunk(SupportType const &domain, Allocator allocator=Allocator())
Construct a Chunk on a domain with uninitialized values.
Definition chunk.hpp:106
Chunk & operator=(Chunk const &other)=delete
Deleted: use deepcopy instead.
allocation_mdspan_type allocation_mdspan()
Provide a mdspan on the memory allocation.
Definition chunk.hpp:297
ElementType * data_handle()
Access to the underlying allocation pointer.
Definition chunk.hpp:281
view_type span_cview() const
Definition chunk.hpp:334
Chunk(Chunk const &other)=delete
Deleted: use deepcopy instead.
~Chunk() noexcept
Definition chunk.hpp:126
Chunk(std::string const &label, SupportType const &domain, Allocator allocator=Allocator())
Construct a labeled Chunk on a domain with uninitialized values.
Definition chunk.hpp:95
element_type & operator()(DElems const &... delems) noexcept
Element access using a list of DiscreteElement.
Definition chunk.hpp:235
Chunk()=default
Empty Chunk.
Chunk & operator=(Chunk &&other) noexcept
Move-assigns a new value to this field.
Definition chunk.hpp:140
Chunk(Chunk &&other) noexcept
Constructs a new Chunk by move.
Definition chunk.hpp:117
friend class Chunk
Definition chunk.hpp:83
span_type span_view()
Definition chunk.hpp:344
view_type span_view() const
Definition chunk.hpp:339
auto operator[](DiscreteDomain< QueryDDims... > const &odomain) const
Slice out some dimensions.
Definition chunk.hpp:184
element_type const & operator()(DElems const &... delems) const noexcept
Element access using a list of DiscreteElement.
Definition chunk.hpp:203
auto operator[](DiscreteVector< QueryDDims... > const &slice_spec)
Slice out some dimensions.
Definition chunk.hpp:163
auto operator[](DiscreteElement< QueryDDims... > const &slice_spec)
Slice out some dimensions.
Definition chunk.hpp:177
static KOKKOS_FUNCTION constexpr discrete_vector_type extents() noexcept
KOKKOS_DEFAULTED_FUNCTION ~DiscreteDomain()=default
KOKKOS_FUNCTION constexpr DiscreteDomain(discrete_element_type const &element_begin, discrete_vector_type const &size)
Construct a DiscreteDomain starting from element_begin with size points.
KOKKOS_FUNCTION constexpr DiscreteDomain(DDoms const &...)
Construct a DiscreteDomain by copies and merge of domains.
KOKKOS_FUNCTION constexpr DiscreteDomain take_last(discrete_vector_type) const
KOKKOS_FUNCTION constexpr bool operator==(DiscreteDomain const &) const
KOKKOS_FUNCTION constexpr DiscreteDomain remove(discrete_vector_type, discrete_vector_type) const
static KOKKOS_FUNCTION constexpr std::size_t rank()
KOKKOS_FUNCTION constexpr DiscreteDomain remove_last(discrete_vector_type) const
KOKKOS_DEFAULTED_FUNCTION DiscreteDomain(DiscreteDomain const &x)=default
static KOKKOS_FUNCTION bool contains() noexcept
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteDomain()=default
KOKKOS_DEFAULTED_FUNCTION DiscreteDomain & operator=(DiscreteDomain &&x)=default
KOKKOS_FUNCTION constexpr operator bool()
static KOKKOS_FUNCTION constexpr std::size_t size()
static KOKKOS_FUNCTION constexpr bool empty() noexcept
static KOKKOS_FUNCTION bool contains(DiscreteElement<>) noexcept
static KOKKOS_FUNCTION constexpr discrete_element_type back() noexcept
KOKKOS_FUNCTION constexpr DiscreteDomain restrict_with(DiscreteDomain< ODDims... > const &) const
static KOKKOS_FUNCTION DiscreteVector distance_from_front(DiscreteElement<>) noexcept
KOKKOS_FUNCTION constexpr DiscreteDomain remove_first(discrete_vector_type) const
static KOKKOS_FUNCTION constexpr discrete_element_type front() noexcept
KOKKOS_DEFAULTED_FUNCTION DiscreteDomain & operator=(DiscreteDomain const &x)=default
KOKKOS_FUNCTION constexpr DiscreteDomain take_first(discrete_vector_type) const
KOKKOS_FUNCTION constexpr DiscreteElement operator()(DiscreteVector<> const &) const noexcept
KOKKOS_DEFAULTED_FUNCTION DiscreteDomain(DiscreteDomain &&x)=default
static KOKKOS_FUNCTION DiscreteVector distance_from_front() noexcept
KOKKOS_DEFAULTED_FUNCTION DiscreteDomain(DiscreteDomain const &x)=default
KOKKOS_DEFAULTED_FUNCTION DiscreteDomain(DiscreteDomain &&x)=default
KOKKOS_FUNCTION constexpr DiscreteDomain remove_last(discrete_vector_type n) const
KOKKOS_FUNCTION bool contains(DElems const &... delems) const noexcept
KOKKOS_FUNCTION constexpr DiscreteDomain take_last(discrete_vector_type n) const
KOKKOS_FUNCTION constexpr DiscreteDomain(DDoms const &... domains)
Construct a DiscreteDomain by copies and merge of domains.
KOKKOS_FUNCTION constexpr decltype(auto) operator[](std::size_t n)
KOKKOS_FUNCTION constexpr DiscreteDomain remove(discrete_vector_type n1, discrete_vector_type n2) const
KOKKOS_FUNCTION constexpr decltype(auto) operator[](std::size_t n) const
KOKKOS_FUNCTION constexpr auto restrict_with(DiscreteDomain< ODDims... > const &odomain) const
static KOKKOS_FUNCTION constexpr std::size_t rank()
KOKKOS_FUNCTION constexpr bool empty() const noexcept
KOKKOS_FUNCTION constexpr std::size_t size() const
friend class DiscreteDomain
KOKKOS_FUNCTION constexpr discrete_vector_type extents() const noexcept
KOKKOS_FUNCTION constexpr DiscreteVector< QueryDDim > extent() const noexcept
KOKKOS_FUNCTION constexpr discrete_element_type front() const noexcept
KOKKOS_DEFAULTED_FUNCTION DiscreteDomain & operator=(DiscreteDomain &&x)=default
KOKKOS_FUNCTION constexpr DiscreteDomain remove_first(discrete_vector_type n) const
KOKKOS_FUNCTION constexpr DiscreteDomain take_first(discrete_vector_type n) const
KOKKOS_DEFAULTED_FUNCTION ~DiscreteDomain()=default
KOKKOS_FUNCTION DiscreteVector< DDims... > distance_from_front(DElems const &... delems) const noexcept
KOKKOS_FUNCTION constexpr discrete_element_type back() const noexcept
KOKKOS_DEFAULTED_FUNCTION DiscreteDomain & operator=(DiscreteDomain const &x)=default
KOKKOS_FUNCTION constexpr DiscreteElement< DDims... > operator()(DiscreteVector< DDims... > const &dvect) const noexcept
KOKKOS_FUNCTION constexpr bool operator==(DiscreteDomain< ODDims... > const &other) const
KOKKOS_DEFAULTED_FUNCTION DiscreteDomain()=default
KOKKOS_FUNCTION constexpr operator bool()
KOKKOS_FUNCTION constexpr DiscreteDomain(discrete_element_type const &element_begin, discrete_vector_type const &size)
Construct a DiscreteDomain starting from element_begin with size points.
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteElement()=default
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector()=default
#define DDC_BUILD_DEPRECATED_CODE
Definition config.hpp:7
#define DDC_BUILD_DOUBLE_PRECISION
Definition config.hpp:15
The top-level namespace of DDC.
constexpr bool is_borrowed_chunk_v
KOKKOS_FUNCTION auto get_domain(ChunkType const &chunk) noexcept
Access the domain (or subdomain) of a view.
constexpr bool enable_chunk< Chunk< ElementType, SupportType, Allocator > >
Definition chunk.hpp:30
KOKKOS_FUNCTION constexpr DiscreteElement< QueryDDims... > back(DiscreteDomain< DDims... > const &domain) noexcept
constexpr bool enable_borrowed_chunk
constexpr bool enable_chunk
constexpr bool is_chunk_v
KOKKOS_FUNCTION constexpr auto remove_dims_of(DiscreteDomain< DDimsA... > const &DDom_a, DiscreteDomain< DDimsB... > const &) noexcept
KOKKOS_FUNCTION constexpr auto remove_dims_of(DiscreteDomain< DDimsA... > const &DDom_a) noexcept
Remove the dimensions DDimsB from DDom_a.
KOKKOS_FUNCTION constexpr DiscreteElement< QueryDDims... > front(DiscreteDomain< DDims... > const &domain) noexcept
constexpr bool is_writable_chunk_v
KOKKOS_FUNCTION constexpr DiscreteVector< QueryDDims... > extents(DiscreteDomain< DDims... > const &domain) noexcept
Chunk(SupportType const &, Allocator) -> Chunk< typename Allocator::value_type, SupportType, Allocator >
KOKKOS_FUNCTION constexpr auto replace_dim_of(DiscreteDomain< DDimsA... > const &DDom_a, DiscreteDomain< DDimsB... > const &DDom_b) noexcept
Chunk(std::string const &, SupportType const &, Allocator) -> Chunk< typename Allocator::value_type, SupportType, Allocator >
constexpr bool is_discrete_domain_v
KOKKOS_FUNCTION constexpr DiscreteDomain< QueryDDims... > select(DiscreteDomain< DDims... > const &domain)
A compile-time sequence of types.
Definition type_seq.hpp:30
KOKKOS_FUNCTION constexpr DiscreteDomainIterator operator--(int)
friend KOKKOS_FUNCTION constexpr bool operator<=(DiscreteDomainIterator const &xx, DiscreteDomainIterator const &yy)
KOKKOS_FUNCTION constexpr DiscreteElement< DDim > operator*() const noexcept
friend KOKKOS_FUNCTION constexpr bool operator>=(DiscreteDomainIterator const &xx, DiscreteDomainIterator const &yy)
KOKKOS_DEFAULTED_FUNCTION DiscreteDomainIterator()=default
KOKKOS_FUNCTION constexpr DiscreteElement< DDim > operator[](difference_type n) const
KOKKOS_FUNCTION constexpr DiscreteDomainIterator & operator--()
KOKKOS_FUNCTION constexpr DiscreteDomainIterator operator++(int)
friend KOKKOS_FUNCTION constexpr DiscreteDomainIterator operator-(DiscreteDomainIterator i, difference_type n)
friend KOKKOS_FUNCTION constexpr DiscreteDomainIterator operator+(DiscreteDomainIterator i, difference_type n)
friend KOKKOS_FUNCTION constexpr bool operator>(DiscreteDomainIterator const &xx, DiscreteDomainIterator const &yy)
friend KOKKOS_FUNCTION constexpr bool operator!=(DiscreteDomainIterator const &xx, DiscreteDomainIterator const &yy)
friend KOKKOS_FUNCTION constexpr bool operator<(DiscreteDomainIterator const &xx, DiscreteDomainIterator const &yy)
KOKKOS_FUNCTION constexpr DiscreteDomainIterator & operator+=(difference_type n)
KOKKOS_FUNCTION constexpr DiscreteDomainIterator & operator++()
friend KOKKOS_FUNCTION constexpr DiscreteDomainIterator operator+(difference_type n, DiscreteDomainIterator i)
friend KOKKOS_FUNCTION constexpr bool operator==(DiscreteDomainIterator const &xx, DiscreteDomainIterator const &yy)
KOKKOS_FUNCTION constexpr DiscreteDomainIterator(DiscreteElement< DDim > value)
KOKKOS_FUNCTION constexpr DiscreteDomainIterator & operator-=(difference_type n)
friend KOKKOS_FUNCTION constexpr difference_type operator-(DiscreteDomainIterator const &xx, DiscreteDomainIterator const &yy)