13#include <Kokkos_Core.hpp>
14#include <Kokkos_StdAlgorithms.hpp>
16#include "ddc/detail/kokkos.hpp"
17#include "ddc/detail/tagged_vector.hpp"
18#include "ddc/detail/type_seq.hpp"
19#include "ddc/discrete_element.hpp"
20#include "ddc/discrete_vector.hpp"
25struct StorageDiscreteDomainIterator;
27template <
class... DDims>
35template <
class... Tags>
46template <
class... Tags>
49 using type = TypeSeq<Tags...>;
52template <
class T,
class U>
55template <
class... DDims,
class... ODDims>
61template <
class InputIt1,
class InputIt2>
62KOKKOS_FUNCTION
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
64 for (; first1 != last1; ++first1, ++first2) {
65 if (!(*first1 == *first2)) {
75 class T =
typename std::iterator_traits<ForwardIt>::value_type,
77KOKKOS_FUNCTION ForwardIt lower_bound(ForwardIt first, ForwardIt last,
const T& value, Compare comp)
80 typename std::iterator_traits<ForwardIt>::difference_type count = last - first;
83 typename std::iterator_traits<ForwardIt>::difference_type
const step = count / 2;
86 if (comp(*it, value)) {
99 class T =
typename std::iterator_traits<ForwardIt>::value_type,
101KOKKOS_FUNCTION
bool binary_search(ForwardIt first, ForwardIt last,
const T& value, Compare comp)
103 first = ::
ddc::detail::lower_bound(first, last, value, comp);
104 return (!(first == last) && !(comp(value, *first)));
110 KOKKOS_FUNCTION DiscreteElementType
111 operator()(DiscreteElement<DDim>
const& delem)
const noexcept
119template <
class... DDims>
126 using discrete_element_type = DiscreteElement<DDims...>;
131 detail::TaggedVector<Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace>, DDims...> m_views;
136 return sizeof...(DDims);
144 class = std::enable_if_t<(is_storage_discrete_domain_v<DDoms> && ...)>>
146 : m_views(domains.m_views...)
151
152
154 Kokkos::View<DiscreteElement<DDims>*, Kokkos::SharedSpace>
const&... views)
156 ((m_views[type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>]
157 = Kokkos::View<DiscreteElementType*, Kokkos::SharedSpace>(views.label(), views.size())),
159 Kokkos::DefaultExecutionSpace
const execution_space;
160 ((Kokkos::Experimental::transform(
161 "StorageDiscreteDomainCtor",
164 m_views[type_seq_rank_v<DDims, detail::TypeSeq<DDims...>>],
165 detail::GetUidFn<DDims>())),
167 execution_space.fence(
"StorageDiscreteDomainCtor");
181 template <
class... ODims>
184 if (
empty() && other.empty()) {
187 if (m_views.size() != other.m_views.size()) {
190 for (std::size_t i = 0; i < m_views.size(); ++i) {
191 if (m_views[i].size() != other.m_views[i].size()) {
195 equal(m_views[i].data(),
196 m_views[i].data() + m_views[i].size(),
197 other.m_views[i].data())) {
204#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
206 template <
class...
ODims>
209 return !(*
this ==
other);
215 return (1UL * ... * get<DDims>(m_views).size());
220 return discrete_vector_type(get<DDims>(m_views).size()...);
228 template <
class QueryDDim>
236 return discrete_element_type(get<DDims>(m_views)(0)...);
241 return discrete_element_type(get<DDims>(m_views)(get<DDims>(m_views).size() - 1)...);
265 discrete_vector_type n1,
266 discrete_vector_type n2)
const
274 return m_views(get<DDims>(dvect)...);
277 template <
class... DElems>
281 sizeof...(DDims) == (0 + ... + DElems::size()),
282 "Invalid number of dimensions");
283 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
284 return (detail::binary_search(
285 get<DDims>(m_views).data(),
286 get<DDims>(m_views).data() + get<DDims>(m_views).size(),
287 uid<DDims>(take<DDims>(delems...)),
292 template <
class... DElems>
294 DElems
const&... delems)
const noexcept
297 sizeof...(DDims) == (0 + ... + DElems::size()),
298 "Invalid number of dimensions");
299 static_assert((is_discrete_element_v<DElems> && ...),
"Expected DiscreteElements");
300 assert(contains(delems...));
302 (detail::lower_bound(
303 get<DDims>(m_views).data(),
304 get<DDims>(m_views).data() + get<DDims>(m_views).size(),
305 uid<DDims>(take<DDims>(delems...)),
307 - get<DDims>(m_views).data())...);
321 std::size_t N =
sizeof...(DDims),
322 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
325 return Kokkos::Experimental::begin(get<DDim0>(m_views));
329 std::size_t N =
sizeof...(DDims),
330 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
333 return Kokkos::Experimental::end(get<DDim0>(m_views));
337 std::size_t N =
sizeof...(DDims),
338 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
341 return Kokkos::Experimental::cbegin(get<DDim0>(m_views));
345 std::size_t N =
sizeof...(DDims),
346 class DDim0 = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
349 return Kokkos::Experimental::cend(get<DDim0>(m_views));
353 std::size_t N =
sizeof...(DDims),
354 class = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
361 std::size_t N =
sizeof...(DDims),
362 class = std::enable_if_t<N == 1, std::tuple_element_t<0, std::tuple<DDims...>>>>
376 using discrete_element_type = DiscreteElement<>;
388 template <
class... ODDims>
411#if !defined(__cpp_impl_three_way_comparison) || __cpp_impl_three_way_comparison < 201902L
415 return !(*
this ==
other);
507template <
class... QueryDDims,
class... DDims>
512 DiscreteElement<QueryDDims...>(domain.front()),
513 DiscreteElement<QueryDDims...>(domain.extents()));
519struct ConvertTypeSeqToStorageDiscreteDomain;
521template <
class... DDims>
522struct ConvertTypeSeqToStorageDiscreteDomain<detail::TypeSeq<DDims...>>
528using convert_type_seq_to_storage_discrete_domain_t =
529 typename ConvertTypeSeqToStorageDiscreteDomain<T>::type;
534template <
class... DDimsA,
class... DDimsB>
539 using TagSeqA = detail::TypeSeq<DDimsA...>;
540 using TagSeqB = detail::TypeSeq<DDimsB...>;
542 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
543 return detail::convert_type_seq_to_storage_discrete_domain_t<type_seq_r>(DDom_a);
549template <
class... DDimsB,
class... DDimsA>
553 using TagSeqA = detail::TypeSeq<DDimsA...>;
554 using TagSeqB = detail::TypeSeq<DDimsB...>;
556 using type_seq_r = type_seq_remove_t<TagSeqA, TagSeqB>;
557 return detail::convert_type_seq_to_storage_discrete_domain_t<type_seq_r>(DDom_a);
563template <
typename DDim1,
typename DDim2,
typename DDimA,
typename... DDimsB>
564KOKKOS_FUNCTION
constexpr std::conditional_t<
565 std::is_same_v<DDimA, DDim1>,
572 if constexpr (std::is_same_v<DDimA, DDim1>) {
582template <
typename DDim1,
typename DDim2,
typename... DDimsA,
typename... DDimsB>
588 using TagSeqA = detail::TypeSeq<DDimsA...>;
589 using TagSeqB = detail::TypeSeq<DDim1>;
590 using TagSeqC = detail::TypeSeq<DDim2>;
592 using type_seq_r =
ddc::type_seq_replace_t<TagSeqA, TagSeqB, TagSeqC>;
593 return ddc::detail::convert_type_seq_to_storage_discrete_domain_t<type_seq_r>(
594 detail::replace_dim_of_1d<
601template <
class... QueryDDims,
class... DDims>
608template <
class... QueryDDims,
class... DDims>
615template <
class... QueryDDims,
class... DDims>
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteElement()=default
KOKKOS_FUNCTION constexpr bool operator!=(DiscreteVector< OTags... > const &rhs) const noexcept
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteVector()=default
static KOKKOS_FUNCTION constexpr discrete_element_type back() noexcept
KOKKOS_DEFAULTED_FUNCTION StorageDiscreteDomain & operator=(StorageDiscreteDomain const &x)=default
static KOKKOS_FUNCTION constexpr bool empty() noexcept
static KOKKOS_FUNCTION DiscreteVector distance_from_front(DiscreteElement<>) noexcept
KOKKOS_FUNCTION constexpr StorageDiscreteDomain remove(discrete_vector_type n1, discrete_vector_type n2) const
static KOKKOS_FUNCTION bool contains() noexcept
KOKKOS_FUNCTION constexpr StorageDiscreteDomain take_last(discrete_vector_type n) const
KOKKOS_FUNCTION constexpr StorageDiscreteDomain take_first(discrete_vector_type n) const
static KOKKOS_FUNCTION constexpr discrete_element_type front() noexcept
KOKKOS_FUNCTION constexpr DiscreteElement operator()(DiscreteVector<> const &) const noexcept
KOKKOS_FUNCTION constexpr operator bool()
KOKKOS_DEFAULTED_FUNCTION ~StorageDiscreteDomain()=default
KOKKOS_DEFAULTED_FUNCTION StorageDiscreteDomain & operator=(StorageDiscreteDomain &&x)=default
KOKKOS_FUNCTION constexpr bool operator==(StorageDiscreteDomain const &other) const
KOKKOS_DEFAULTED_FUNCTION StorageDiscreteDomain(StorageDiscreteDomain const &x)=default
static KOKKOS_FUNCTION constexpr discrete_vector_type extents() noexcept
static KOKKOS_FUNCTION bool contains(DiscreteElement<>) noexcept
static KOKKOS_FUNCTION constexpr std::size_t rank()
KOKKOS_DEFAULTED_FUNCTION constexpr StorageDiscreteDomain()=default
KOKKOS_FUNCTION constexpr StorageDiscreteDomain remove_first(discrete_vector_type n) const
static KOKKOS_FUNCTION DiscreteVector distance_from_front() noexcept
static KOKKOS_FUNCTION constexpr std::size_t size()
KOKKOS_FUNCTION constexpr StorageDiscreteDomain(StorageDiscreteDomain< ODDims... > const &domain)
KOKKOS_FUNCTION constexpr StorageDiscreteDomain remove_last(discrete_vector_type n) const
KOKKOS_DEFAULTED_FUNCTION StorageDiscreteDomain(StorageDiscreteDomain &&x)=default
KOKKOS_FUNCTION constexpr std::size_t size() const
KOKKOS_FUNCTION auto begin() const
KOKKOS_FUNCTION constexpr StorageDiscreteDomain(DDoms const &... domains)
Construct a StorageDiscreteDomain by copies and merge of domains.
KOKKOS_FUNCTION auto cend() const
KOKKOS_FUNCTION constexpr discrete_element_type front() const noexcept
KOKKOS_FUNCTION auto cbegin() const
KOKKOS_FUNCTION bool contains(DElems const &... delems) const noexcept
KOKKOS_DEFAULTED_FUNCTION StorageDiscreteDomain(StorageDiscreteDomain const &x)=default
KOKKOS_FUNCTION DiscreteVector< DDims... > distance_from_front(DElems const &... delems) const noexcept
KOKKOS_FUNCTION constexpr StorageDiscreteDomain take_last(discrete_vector_type n) const
KOKKOS_FUNCTION constexpr StorageDiscreteDomain take_first(discrete_vector_type n) const
friend class StorageDiscreteDomain
KOKKOS_FUNCTION constexpr discrete_element_type back() const noexcept
KOKKOS_FUNCTION constexpr StorageDiscreteDomain remove_first(discrete_vector_type n) const
KOKKOS_FUNCTION constexpr bool empty() const noexcept
KOKKOS_FUNCTION auto end() const
KOKKOS_DEFAULTED_FUNCTION StorageDiscreteDomain & operator=(StorageDiscreteDomain &&x)=default
static KOKKOS_FUNCTION constexpr std::size_t rank()
KOKKOS_DEFAULTED_FUNCTION ~StorageDiscreteDomain()=default
KOKKOS_FUNCTION constexpr operator bool()
KOKKOS_FUNCTION constexpr DiscreteElement< DDims... > operator()(DiscreteVector< DDims... > const &dvect) const noexcept
KOKKOS_FUNCTION constexpr bool operator==(StorageDiscreteDomain< ODims... > const &other) const
KOKKOS_FUNCTION constexpr DiscreteVector< QueryDDim > extent() const noexcept
KOKKOS_FUNCTION constexpr StorageDiscreteDomain remove_last(discrete_vector_type n) const
KOKKOS_FUNCTION constexpr discrete_vector_type extents() const noexcept
KOKKOS_FUNCTION constexpr decltype(auto) operator[](std::size_t n) const
KOKKOS_DEFAULTED_FUNCTION StorageDiscreteDomain(StorageDiscreteDomain &&x)=default
KOKKOS_FUNCTION constexpr auto discrete_elements() const noexcept
KOKKOS_FUNCTION constexpr decltype(auto) operator[](std::size_t n)
KOKKOS_DEFAULTED_FUNCTION StorageDiscreteDomain & operator=(StorageDiscreteDomain const &x)=default
KOKKOS_FUNCTION constexpr StorageDiscreteDomain remove(discrete_vector_type n1, discrete_vector_type n2) const
StorageDiscreteDomain(Kokkos::View< DiscreteElement< DDims > *, Kokkos::SharedSpace > const &... views)
Construct a StorageDiscreteDomain with Kokkos::View explicitly listing the discrete elements.
KOKKOS_DEFAULTED_FUNCTION StorageDiscreteDomain()=default
The top-level namespace of DDC.
KOKKOS_FUNCTION constexpr auto remove_dims_of(StorageDiscreteDomain< DDimsA... > const &DDom_a) noexcept
Remove the dimensions DDimsB from DDom_a.
KOKKOS_FUNCTION constexpr DiscreteElement< QueryDDims... > front(StorageDiscreteDomain< DDims... > const &domain) noexcept
constexpr bool is_storage_discrete_domain_v
KOKKOS_FUNCTION constexpr DiscreteElement< QueryDDims... > back(StorageDiscreteDomain< DDims... > const &domain) noexcept
KOKKOS_FUNCTION constexpr DiscreteVector< QueryDDims... > extents(StorageDiscreteDomain< DDims... > const &domain) noexcept
KOKKOS_FUNCTION constexpr auto replace_dim_of(StorageDiscreteDomain< DDimsA... > const &DDom_a, StorageDiscreteDomain< DDimsB... > const &DDom_b) noexcept
KOKKOS_FUNCTION constexpr auto remove_dims_of(StorageDiscreteDomain< DDimsA... > const &DDom_a, StorageDiscreteDomain< DDimsB... > const &DDom_b) noexcept
KOKKOS_FUNCTION constexpr StorageDiscreteDomain< QueryDDims... > select(StorageDiscreteDomain< DDims... > const &domain)